Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding File Truncation

Quoting from Advanced Programming in the UNIX Environnement (page 505), Section 13.6:

We need to truncate the file, because the previous instance of the daemon might have had a process ID larger than ours, with a larger string length. For examples , if the previous instance of the daemon was process ID 12345, and the new instance is process ID 9999, when we write the process ID to the file, we will be left with 99995 in the file. Truncating the file prevents data from the previous daemon appearing as if it applies to the current daemon.

This comment was made on this function:

already_running(void)
{
    int fd;
    char buf[16];
    fd = open(LOCKFILE, O_RDWR|O_CREAT, LOCKMODE);
    if (fd < 0) {
        syslog(LOG_ERR, "can't open %s: %s", LOCKFILE, strerror(errno));
        exit(1);
    }
    if (lockfile(fd) < 0) {
        if (errno == EACCES || errno == EAGAIN) {
            close(fd);
            return(1);
        }
        syslog(LOG_ERR, "can't lock %s: %s", LOCKFILE, strerror(errno));
        exit(1);
    }
    ftruncate(fd, 0);
    sprintf(buf, "%ld", (long)getpid()); 
    write(fd, buf, strlen(buf)+1);
    return 0;
}

I don't understand how is this behavior possible, and how file truncation prevent that behavior from happening. Could somebody explain this?

Thanks for answering!

like image 940
aldeb Avatar asked Sep 22 '15 13:09

aldeb


People also ask

What happens when we truncate file?

It is used to extend or reduce the file size. Truncating a file is much quicker and simpler without modifying the permissions and ownership of the file. The truncated size depends on the original size of the file; the extra data will be lost if the file size is greater than the specified size.

What is truncation example?

Truncation allows you to search the "root" form of a word with all its different endings by adding a symbol to the end of a word. Example: typing in bank* will retrieve results with these words: bank, banks, banking, bankers, bankruptcy The most common truncation symbol is the asterisk * but databases vary.

What does truncating a file mean Python?

Python File truncate() Method The truncate() method resizes the file to the given number of bytes. If the size is not specified, the current position will be used.

What does it mean to truncate file in C?

The truncate function changes the size of filename to length . If length is shorter than the previous length, data at the end will be lost. The file must be writable by the user to perform this operation. If length is longer, holes will be added to the end.


2 Answers

In the above example, the file is initially 5 bytes long. When you open it for writing, and write the string "9999" to it without truncating, it will just overwrite the first 4 bytes, and leave the 5th byte in place. Hence the file will read "99995". Truncating sets the file length to 0, effectively erasing the previous content.

like image 89
Hellmar Becker Avatar answered Oct 19 '22 11:10

Hellmar Becker


Hellmar has already provided the answer to your question -- but in the interest of shortening the code (who doesn't like code golf?), you could simplify the open call to:

already_running(void)
{
    int fd;
    char buf[16];
    fd = open(LOCKFILE, O_RDWR|O_CREAT|O_TRUNC, LOCKMODE);
    ...

The addition of O_TRUNC to the flags will cause the file to be truncated. http://linux.die.net/man/2/open

If the file already exists and is a regular file and the open mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0.

like image 33
user590028 Avatar answered Oct 19 '22 11:10

user590028