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
fdwill 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 exceeded255characters, or an entire path name exceeded1023characters. 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 thechflags(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]β thelengthargument was greater than the maximum file size. you aimed too high, babe.[EINVAL]β thelengthargument was less than0. 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]β thepathargument points outside the process's allocated address space.
ftruncate() system call succeeds unlessβ¦
[EBADF]β thefdargument is not a valid descriptor.[EINVAL]β thefdargument references a file descriptor that is not a regular file or shared memory object.[EINVAL]β thefddescriptor 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
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms