Ricardo Lucca
Ricardo Lucca

Reputation: 168

auto delete file on linux

I am trying to do a file be deleted when a program ends. I remember that before I could put the unlink() before the first close() and I don't need reopen the file.

What I expect: The file is erased after the program ends.

What is happening: The file is erased when the call to unlink happens the file is erased.

My sample program:

int main()
{
    int fd = open(argv[1], O_CREAT);
    int x = 1;
    write(fd, "1234\n", 5);
    close(fd);

    fd = open(argv[1], 0);
    unlink(argv[1]);

    while (x <= 3)
    {
      int k;
      scanf(" %d", &k);

      x++;
    }

    close(fd);
    return 0;
}

Has a way that I can open() the file, interact with it and on close() delete the file from harddisk? I'm using fedora linux 18.

I need know the name of the file that I did open in this way because it will be used by another application.

Upvotes: 1

Views: 3195

Answers (3)

Nominal Animal
Nominal Animal

Reputation: 39356

Unlinking a file simply detaches the file name from the underlying inode, making it impossible to open the file using that file name afterwards.

If any process has the file still open, they can happily read and write it, as those operations operate on the inode and not the file name. Also, if there are hardlinks (other file names referring to the same inode) left, those other file names can be used to open the file just fine. See e.g. the Wikipedia article on inodes for further details.


Edited to add:

In Linux, you can leverage the /proc pseudofilesystem. If your application (with process ID PID) has file descriptor FD open, with the file name already unlinked, it can still let another application work on it by telling the other application to work on /proc/PID/fd/FD. It is a pseudo-file, meaning it looks like a (non-functioning!) symlink, but it is not -- it's just useful Linux kernel magic: as long as the other application just opens it normally (open()/fopen() etc., no lstat()/readlink() stuff), they will get access as if they were opening a normal file.

As a real-world example, open two terminals, and in one write

bash -c 'exec 3<>foobar ; echo $$ ; rm foobar ; echo "Initial contents" >&3 ; cat >&3'

The first line it outputs is the PID, and FD is 3 here. Anything you type (after pressing Enter) will be appended to a file that was briefly named foobar, but no longer exists. (You can easily verify that.)

In a second terminal, type

cat /proc/PID/fd/3

to see what that file contains.

Upvotes: 2

Grady Player
Grady Player

Reputation: 14549

The File is unlinked, so it won't show up from ls... but the file still exists there is an inode and you could actually re-link it... the file won't be removed from the disk until all file descriptors pointing to it are closed...

you could still read and write to the fd while it is open after it is unlinked...

Upvotes: 0

paulsm4
paulsm4

Reputation: 121669

It sounds like what you really want is tmpfile():

The tmpfile() function opens a unique temporary file in binary read/write (w+b) mode. The file will be automatically deleted when it is closed or the program terminates.

Upvotes: 1

Related Questions