FTRUNCATE(3POSIX)

πŸ’– FTRUNCATE πŸ’– (that's the 3POSIX one, from the POSIX Programmer's Manual, bestie)

okay so real quick before we dive in 🫢

πŸ“’ PROLOG πŸ“’

so this page is part of the POSIX Programmer's Manual, okay? and the Linux version of this interface may be different β€” like, if you're on Linux, go check the actual Linux manual page for how Linux behaves. or honestly it might not even be implemented on Linux at all. no cap, just go look. πŸ‘€


✨ NAME ✨

ftruncate β€” truncate a file to a specified length πŸ’…


🌸 SYNOPSIS 🌸

#include <unistd.h>

int ftruncate(int fildes, off_t length);

πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the tea. if fildes isn't a valid file descriptor that's open for writing, then ftruncate() shall fail. periodt. it's giving "write access only". βœ‹

if fildes refers to a regular file, ftruncate() shall cause the size of the file to be truncated to length. and like β€” if the file used to be bigger than length, that extra data shall no longer be available to reads on the file. gone. poof. πŸ«₯

but if the file was previously smaller than that size? then ftruncate() shall increase the size of the file. and if the file size gets increased, that extended area shall appear as if it were zero-filled. literally zeroes. ✨

also β€” super important β€” the value of the seek pointer shall not be modified by a call to ftruncate(). she stays exactly where she was. πŸ’…

upon successful completion, if fildes refers to a regular file, ftruncate() shall mark for update the last data modification and last file status change timestamps of the file, and the S_ISUID and S_ISGID bits of the file mode may be cleared. if the ftruncate() function is unsuccessful, the file is unaffected β€” no harm no foul. πŸŽ€

now. if the request would cause the file size to exceed the soft file size limit for the process, the request shall fail and the implementation shall generate the SIGXFSZ signal for the thread. yeah, she's dramatic like that. 😭

if fildes refers to a directory, ftruncate() shall fail. obviously. not today. πŸ™…

if fildes refers to any other file type β€” except a shared memory object β€” the result is unspecified. so like, don't. πŸ’€

if fildes refers to a shared memory object, ftruncate() shall set the size of the shared memory object to length. slay. πŸ‘‘

if the effect of ftruncate() is to decrease the size of a memory mapped file or a shared memory object, and whole pages beyond the new end were previously mapped, then the whole pages beyond the new end shall be discarded. bye bye pages 🧚

and references to those discarded pages shall result in the generation of a SIGBUS signal. ⚠️ so like, don't touch them fr fr.

if the effect of ftruncate() is to increase the size of a memory object, it is unspecified whether the contents of any mapped pages between the old end-of-file and the new are flushed to the underlying object. we just don't know. vibe check inconclusive. 🌈


🌟 RETURN VALUE 🌟

upon successful completion, ftruncate() shall return 0. otherwise, -1 shall be returned and errno set to indicate the error. that's the whole deal. 🫢


πŸ’œ ERRORS πŸ’œ

the ftruncate() function shall fail if:

  • EINTR β€” a signal was caught during execution. πŸ™ˆ
  • EINVAL β€” the length argument was less than 0. negative length is not the vibe.
  • EFBIG or EINVAL β€” the length argument was greater than the maximum file size. 🚫
  • EFBIG β€” the file is a regular file and length is greater than the offset maximum established in the open file description associated with fildes.
  • EIO β€” an I/O error occurred while reading from or writing to a file system. 😭
  • EBADF or EINVAL β€” the fildes argument is not a file descriptor open for writing.
the following sections are informative. so like, optional reading, but we love a little extra credit. πŸ“š

πŸŽ€ EXAMPLES πŸŽ€

none. literally nothing here. bestie said no examples today. πŸ™ˆ


πŸ“ APPLICATION USAGE πŸ“

none. also nothing. we're keeping it mysterious. πŸ’…


🌈 RATIONALE 🌈

none. the reasoning is between her and the standards committee. ✨


⭐ FUTURE DIRECTIONS ⭐

none. the future is unwritten, lowkey. πŸ¦‹


πŸ‘‘ SEE ALSO πŸ‘‘

  • open()
  • truncate()

the Base Definitions volume of POSIX.1‐2017, <unistd.h> πŸ’–


portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. πŸ’™

in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. like, she's the final boss, no arguing. βš–οΈ

the original Standard can be obtained online at http://www.opengroup.org/unix/online.html . πŸ”—

any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . πŸ›


and that's the whole page bestie!! this icon was brought to you by IEEE/The Open Group, 2017 β€” yeah, 2017, she's been iconic for a minute πŸ‘‘πŸ’– mwah, go truncate something responsibly ✨