387 lines
8.1 KiB
HTML
387 lines
8.1 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of TRUNCATE</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>TRUNCATE</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
truncate, ftruncate - truncate a file to a specified length
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>></B>
|
|
|
|
<BR>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<B>int truncate(const char *</B><I>path</I><B>, off_t </B><I>length</I><B>);</B>
|
|
|
|
<BR>
|
|
|
|
<B>int ftruncate(int </B><I>fd</I><B>, off_t </B><I>length</I><B>);</B>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
|
|
<P>
|
|
|
|
<B>truncate</B>():
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
_XOPEN_SOURCE >= 500
|
|
|
|
<BR>
|
|
|
|
<BR> || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
|
|
<BR> || /* Glibc versions <= 2.19: */ _BSD_SOURCE
|
|
</DL>
|
|
|
|
<P>
|
|
|
|
<B>ftruncate</B>():
|
|
|
|
<DL COMPACT><DT id="2"><DD>
|
|
_XOPEN_SOURCE >= 500
|
|
|
|
<BR> || /* Since glibc 2.3.5: */ _POSIX_C_SOURCE >= 200112L
|
|
<BR> || /* Glibc versions <= 2.19: */ _BSD_SOURCE
|
|
</DL>
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>truncate</B>()
|
|
|
|
and
|
|
<B>ftruncate</B>()
|
|
|
|
functions cause the regular file named by
|
|
<I>path</I>
|
|
|
|
or referenced by
|
|
<I>fd</I>
|
|
|
|
to be truncated to a size of precisely
|
|
<I>length</I>
|
|
|
|
bytes.
|
|
<P>
|
|
|
|
If the file previously was larger than this size, the extra data is lost.
|
|
If the file previously was shorter, it is extended, and
|
|
the extended part reads as null bytes ('\0').
|
|
<P>
|
|
|
|
The file offset is not changed.
|
|
<P>
|
|
|
|
If the size changed, then the st_ctime and st_mtime fields
|
|
(respectively, time of last status change and
|
|
time of last modification; see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+inode">inode</A></B>(7))
|
|
|
|
for the file are updated,
|
|
and the set-user-ID and set-group-ID mode bits may be cleared.
|
|
<P>
|
|
|
|
With
|
|
<B>ftruncate</B>(),
|
|
|
|
the file must be open for writing; with
|
|
<B>truncate</B>(),
|
|
|
|
the file must be writable.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, zero is returned.
|
|
On error, -1 is returned, and
|
|
<I>errno</I>
|
|
|
|
is set appropriately.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
For
|
|
<B>truncate</B>():
|
|
|
|
<DL COMPACT>
|
|
<DT id="3"><B>EACCES</B>
|
|
|
|
<DD>
|
|
Search permission is denied for a component of the path prefix,
|
|
or the named file is not writable by the user.
|
|
(See also
|
|
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7).)
|
|
|
|
<DT id="4"><B>EFAULT</B>
|
|
|
|
<DD>
|
|
The argument
|
|
<I>path</I>
|
|
|
|
points outside the process's allocated address space.
|
|
<DT id="5"><B>EFBIG</B>
|
|
|
|
<DD>
|
|
The argument
|
|
<I>length</I>
|
|
|
|
is larger than the maximum file size. (XSI)
|
|
<DT id="6"><B>EINTR</B>
|
|
|
|
<DD>
|
|
While blocked waiting to complete,
|
|
the call was interrupted by a signal handler; see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7).
|
|
|
|
<DT id="7"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
The argument
|
|
<I>length</I>
|
|
|
|
is negative or larger than the maximum file size.
|
|
<DT id="8"><B>EIO</B>
|
|
|
|
<DD>
|
|
An I/O error occurred updating the inode.
|
|
<DT id="9"><B>EISDIR</B>
|
|
|
|
<DD>
|
|
The named file is a directory.
|
|
<DT id="10"><B>ELOOP</B>
|
|
|
|
<DD>
|
|
Too many symbolic links were encountered in translating the pathname.
|
|
<DT id="11"><B>ENAMETOOLONG</B>
|
|
|
|
<DD>
|
|
A component of a pathname exceeded 255 characters,
|
|
or an entire pathname exceeded 1023 characters.
|
|
<DT id="12"><B>ENOENT</B>
|
|
|
|
<DD>
|
|
The named file does not exist.
|
|
<DT id="13"><B>ENOTDIR</B>
|
|
|
|
<DD>
|
|
A component of the path prefix is not a directory.
|
|
<DT id="14"><B>EPERM</B>
|
|
|
|
<DD>
|
|
|
|
|
|
The underlying filesystem does not support extending
|
|
a file beyond its current size.
|
|
<DT id="15"><B>EPERM</B>
|
|
|
|
<DD>
|
|
The operation was prevented by a file seal; see
|
|
<B><A HREF="/cgi-bin/man/man2html?2+fcntl">fcntl</A></B>(2).
|
|
|
|
<DT id="16"><B>EROFS</B>
|
|
|
|
<DD>
|
|
The named file resides on a read-only filesystem.
|
|
<DT id="17"><B>ETXTBSY</B>
|
|
|
|
<DD>
|
|
The file is an executable file that is being executed.
|
|
</DL>
|
|
<P>
|
|
|
|
For
|
|
<B>ftruncate</B>()
|
|
|
|
the same errors apply, but instead of things that can be wrong with
|
|
<I>path</I>,
|
|
|
|
we now have things that can be wrong with the file descriptor,
|
|
<I>fd</I>:
|
|
|
|
<DL COMPACT>
|
|
<DT id="18"><B>EBADF</B>
|
|
|
|
<DD>
|
|
<I>fd</I>
|
|
|
|
is not a valid file descriptor.
|
|
<DT id="19"><B>EBADF</B> or <B>EINVAL</B>
|
|
|
|
<DD>
|
|
<I>fd</I>
|
|
|
|
is not open for writing.
|
|
<DT id="20"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
<I>fd</I>
|
|
|
|
does not reference a regular file or a POSIX shared memory object.
|
|
<DT id="21"><B>EINVAL</B> or <B>EBADF</B>
|
|
|
|
<DD>
|
|
The file descriptor
|
|
<I>fd</I>
|
|
|
|
is not open for writing.
|
|
POSIX permits, and portable applications should handle,
|
|
either error for this case.
|
|
(Linux produces
|
|
<B>EINVAL</B>.)
|
|
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008,
|
|
4.4BSD, SVr4 (these calls first appeared in 4.2BSD).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAH"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
<B>ftruncate</B>()
|
|
|
|
can also be used to set the size of a POSIX shared memory object; see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+shm_open">shm_open</A></B>(7).
|
|
|
|
<P>
|
|
|
|
The details in DESCRIPTION are for XSI-compliant systems.
|
|
For non-XSI-compliant systems, the POSIX standard allows
|
|
two behaviors for
|
|
<B>ftruncate</B>()
|
|
|
|
when
|
|
<I>length</I>
|
|
|
|
exceeds the file length
|
|
(note that
|
|
<B>truncate</B>()
|
|
|
|
is not specified at all in such an environment):
|
|
either returning an error, or extending the file.
|
|
Like most UNIX implementations, Linux follows the XSI requirement
|
|
when dealing with native filesystems.
|
|
However, some nonnative filesystems do not permit
|
|
<B>truncate</B>()
|
|
|
|
and
|
|
<B>ftruncate</B>()
|
|
|
|
to be used to extend a file beyond its current length:
|
|
a notable example on Linux is VFAT.
|
|
|
|
<P>
|
|
|
|
The original Linux
|
|
<B>truncate</B>()
|
|
|
|
and
|
|
<B>ftruncate</B>()
|
|
|
|
system calls were not designed to handle large file offsets.
|
|
Consequently, Linux 2.4 added
|
|
<B>truncate64</B>()
|
|
|
|
and
|
|
<B>ftruncate64</B>()
|
|
|
|
system calls that handle large files.
|
|
However, these details can be ignored by applications using glibc, whose
|
|
wrapper functions transparently employ the more recent system calls
|
|
where they are available.
|
|
<P>
|
|
|
|
On some 32-bit architectures,
|
|
the calling signature for these system calls differ,
|
|
for the reasons described in
|
|
<B><A HREF="/cgi-bin/man/man2html?2+syscall">syscall</A></B>(2).
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>BUGS</H2>
|
|
|
|
A header file bug in glibc 2.12 meant that the minimum value of
|
|
|
|
<B>_POSIX_C_SOURCE</B>
|
|
|
|
required to expose the declaration of
|
|
<B>ftruncate</B>()
|
|
|
|
was 200809L instead of 200112L.
|
|
This has been fixed in later glibc versions.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+truncate">truncate</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7)
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="22"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="23"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="24"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="25"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="26"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="27"><A HREF="#lbAG">CONFORMING TO</A><DD>
|
|
<DT id="28"><A HREF="#lbAH">NOTES</A><DD>
|
|
<DT id="29"><A HREF="#lbAI">BUGS</A><DD>
|
|
<DT id="30"><A HREF="#lbAJ">SEE ALSO</A><DD>
|
|
<DT id="31"><A HREF="#lbAK">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:35 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|