17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5a574db85Sraf * Common Development and Distribution License (the "License").
6a574db85Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21a574db85Sraf
227c478bd9Sstevel@tonic-gate /*
23a574db85Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
30*7257d1b4Sraf #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * ftruncate() and truncate() set a file to a specified
347c478bd9Sstevel@tonic-gate * length using fcntl(F_FREESP) system call. If the file
357c478bd9Sstevel@tonic-gate * was previously longer than length, the bytes past the
367c478bd9Sstevel@tonic-gate * length will no longer be accessible. If it was shorter,
377c478bd9Sstevel@tonic-gate * bytes not written will be zero filled.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <sys/feature_tests.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
43*7257d1b4Sraf #pragma weak _ftruncate64 = ftruncate64
44*7257d1b4Sraf #pragma weak _truncate64 = truncate64
45*7257d1b4Sraf #define ftruncate ftruncate64
46*7257d1b4Sraf #define truncate truncate64
477c478bd9Sstevel@tonic-gate #endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
487c478bd9Sstevel@tonic-gate
49*7257d1b4Sraf #include "lint.h"
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <stdio.h>
527c478bd9Sstevel@tonic-gate #include <fcntl.h>
53a574db85Sraf #include <pthread.h>
547c478bd9Sstevel@tonic-gate #include <sys/types.h>
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate int
ftruncate(int fildes,off_t len)57*7257d1b4Sraf ftruncate(int fildes, off_t len)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate struct flock lck;
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate lck.l_whence = 0; /* offset l_start from beginning of file */
627c478bd9Sstevel@tonic-gate lck.l_start = len;
637c478bd9Sstevel@tonic-gate lck.l_type = F_WRLCK; /* setting a write lock */
647c478bd9Sstevel@tonic-gate lck.l_len = (off_t)0; /* until the end of the file address space */
657c478bd9Sstevel@tonic-gate
667c478bd9Sstevel@tonic-gate if (fcntl(fildes, F_FREESP, &lck) == -1) {
677c478bd9Sstevel@tonic-gate return (-1);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate return (0);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate int
truncate(const char * path,off_t len)73*7257d1b4Sraf truncate(const char *path, off_t len)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate
76a574db85Sraf int rval = 0;
77a574db85Sraf int cancel_state;
787c478bd9Sstevel@tonic-gate int fd;
797c478bd9Sstevel@tonic-gate
80a574db85Sraf /*
81a574db85Sraf * truncate() is not a cancellation point,
82a574db85Sraf * even though it calls open() and close().
83a574db85Sraf */
84a574db85Sraf (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
85a574db85Sraf if ((fd = open(path, O_WRONLY)) == -1 || ftruncate(fd, len) == -1)
86a574db85Sraf rval = -1;
87a574db85Sraf if (fd >= 0)
887c478bd9Sstevel@tonic-gate (void) close(fd);
89a574db85Sraf (void) pthread_setcancelstate(cancel_state, NULL);
90a574db85Sraf return (rval);
917c478bd9Sstevel@tonic-gate }
92