xref: /illumos-gate/usr/src/lib/libc/port/gen/truncate.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * ftruncate() and truncate() set a file to a specified
327c478bd9Sstevel@tonic-gate  * length using fcntl(F_FREESP) system call. If the file
337c478bd9Sstevel@tonic-gate  * was previously longer than length, the bytes past the
347c478bd9Sstevel@tonic-gate  * length will no longer be accessible. If it was shorter,
357c478bd9Sstevel@tonic-gate  * bytes not written will be zero filled.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/feature_tests.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
41*7257d1b4Sraf #pragma weak _ftruncate64 = ftruncate64
42*7257d1b4Sraf #pragma weak _truncate64 = truncate64
43*7257d1b4Sraf #define	ftruncate	ftruncate64
44*7257d1b4Sraf #define	truncate	truncate64
457c478bd9Sstevel@tonic-gate #endif /* !_LP64 && _FILE_OFFSET_BITS == 64 */
467c478bd9Sstevel@tonic-gate 
47*7257d1b4Sraf #include "lint.h"
487c478bd9Sstevel@tonic-gate #include <unistd.h>
497c478bd9Sstevel@tonic-gate #include <stdio.h>
507c478bd9Sstevel@tonic-gate #include <fcntl.h>
51a574db85Sraf #include <pthread.h>
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate int
ftruncate(int fildes,off_t len)55*7257d1b4Sraf ftruncate(int fildes, off_t len)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	struct flock lck;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	lck.l_whence = 0;	/* offset l_start from beginning of file */
607c478bd9Sstevel@tonic-gate 	lck.l_start = len;
617c478bd9Sstevel@tonic-gate 	lck.l_type = F_WRLCK;	/* setting a write lock */
627c478bd9Sstevel@tonic-gate 	lck.l_len = (off_t)0;	/* until the end of the file address space */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (fcntl(fildes, F_FREESP, &lck) == -1) {
657c478bd9Sstevel@tonic-gate 		return (-1);
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 	return (0);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int
truncate(const char * path,off_t len)71*7257d1b4Sraf truncate(const char *path, off_t len)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 
74a574db85Sraf 	int rval = 0;
75a574db85Sraf 	int cancel_state;
767c478bd9Sstevel@tonic-gate 	int fd;
777c478bd9Sstevel@tonic-gate 
78a574db85Sraf 	/*
79a574db85Sraf 	 * truncate() is not a cancellation point,
80a574db85Sraf 	 * even though it calls open() and close().
81a574db85Sraf 	 */
82a574db85Sraf 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
83a574db85Sraf 	if ((fd = open(path, O_WRONLY)) == -1 || ftruncate(fd, len) == -1)
84a574db85Sraf 		rval = -1;
85a574db85Sraf 	if (fd >= 0)
867c478bd9Sstevel@tonic-gate 		(void) close(fd);
87a574db85Sraf 	(void) pthread_setcancelstate(cancel_state, NULL);
88a574db85Sraf 	return (rval);
897c478bd9Sstevel@tonic-gate }
90