TRUNCATE(2)

πŸ’… TRUNCATE β€” the truncate, ftruncate situation

okay bestie, so this is truncate(2) and ftruncate(2) from section 2 of the manual β€” the system calls, not the command line thing, don't get it twisted 🫢


✨ NAME ✨

truncate, ftruncate β€” truncate or extend a file to a specified length

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)

🌸 SYNOPSIS 🌸

#include <unistd.h>

int
truncate(const char *path, off_t length);

int
ftruncate(int fd, off_t length);

πŸ¦‹ DESCRIPTION πŸ¦‹

So the truncate() system call takes the file named by path, or the one referenced by fd, and resizes it to length bytes. That's it. That's the whole vibe.

If the file was bigger than that size, the extra data is just gone. Lost. Not coming back. πŸ’€

If the file was smaller than that size, it gets extended β€” as if you'd written bytes with the value zero. Padding era, but make it zeroes.

The ftruncate() system call does the same thing but for the file or shared memory object backing the file descriptor fd, resizing it to length bytes. Two important receipts here:

  • The file descriptor must be a valid file descriptor that's open for writing. Non-negotiable, bestie.
  • The file position pointer associated with fd will not be modified. It stays exactly where it was. It's unbothered. ✨

πŸ”₯ RETURN VALUES πŸ”₯

When everything goes right, you get 0 back. Slay. πŸŽ€

When it doesn't, you get -1, and the global variable errno gets set to tell you what went wrong.

Special little quirk tho: if the file you're trying to modify is not a directory and not a regular file, the truncate() call has no effect and still returns the value 0. Yeah. It just says nothing happened and calls that a win. Iconic behaviour, honestly. πŸ™ˆ


😭 ERRORS 😭

truncate() system call succeeds unless…

  • [ENOTDIR] β€” a component of the path prefix is not a directory.
  • [ENAMETOOLONG] β€” a component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters. That's a lot of characters, bestie, what are you doing.
  • [ENOENT] β€” the named file does not exist. it's giving nothing. it's giving void. πŸ’€
  • [EACCES] β€” search permission is denied for a component of the path prefix.
  • [EACCES] β€” the named file is not writable by the user.
  • [ELOOP] β€” too many symbolic links were encountered in translating the pathname. the symlinks were linking to each other and it got out of hand πŸŒ€
  • [EPERM] β€” the named file has its immutable or append-only flag set. peep the chflags(2) manual page for more info on that one.
  • [EISDIR] β€” the named file is a directory. nope. not that kind of file, hun.
  • [EROFS] β€” the named file lives on a read-only file system. frozen. no edits. 🧊
  • [ETXTBSY] β€” the file is a pure procedure (shared text) file that is currently being executed. busy girl, do not disturb.
  • [EFBIG] β€” the length argument was greater than the maximum file size. you aimed too high, babe.
  • [EINVAL] β€” the length argument was less than 0. negative length is not a length, it's a crisis.
  • [EIO] β€” an I/O error occurred updating the inode.
  • [EINTEGRITY] β€” corrupted data was detected while reading from the file system. 😬
  • [EFAULT] β€” the path argument points outside the process's allocated address space.

ftruncate() system call succeeds unless…

  • [EBADF] β€” the fd argument is not a valid descriptor.
  • [EINVAL] β€” the fd argument references a file descriptor that is not a regular file or shared memory object.
  • [EINVAL] β€” the fd descriptor is not open for writing.

πŸ‘€ SEE ALSO πŸ‘€

chflags(2), open(2), shm_open(2)


πŸ“œ HISTORY πŸ“œ

The truncate() and ftruncate() system calls appeared in 4.2BSD. old money, lowkey. πŸ’œ

πŸ› BUGS πŸ›

These calls should be generalized to allow ranges of bytes in a file to be discarded. like, wouldn't that be nice.
Use of truncate() to extend a file is not portable. so if you're out here extending files with it, just know you're living dangerously, bestie. ⚠️

that's the whole page, no notes πŸ’…

Debian ✦ March 30, 2020 ✦ TRUNCATE(2) β€” ok bye, go resize responsibly πŸ¦‹βœ¨

πŸ“¦ where this page comes from