14

I have an int file descriptor that was opened earlier (via open) and I need to remove that file.

Do I really have to first get the file name and call remove? (e.g. via using the technique in Getting Filename from file descriptor in C)

Or is there some other (linux specific OK) way of doing it solely based on the file descriptor?

I have searched and the best I could find is the above answer.

Community
  • 1
  • 1
Eran Medan
  • 44,555
  • 61
  • 184
  • 276
  • 1
    No, that's it. There's no special way. – nos Apr 23 '14 at 21:15
  • 2
    No there is not. Have you considered the possibility that there might be more than one directory entry linking to the same inode (:=file) ? – wildplasser Apr 23 '14 at 22:17
  • @wildplasser no I haven't :/ and I'm taking Advanced Operating Systems at GA tech, I guess I need to take OS 101 again :) – Eran Medan Apr 24 '14 at 01:08

5 Answers5

8

You can use /proc to see which path an open fd is linked to, and realpath get the full path of the symlink.

# ls -l /proc/8701/fd
total 0
lr-x------ 1 root root 64 Apr 23 22:44 0 -> /dev/null
lrwx------ 1 root root 64 Apr 23 22:44 1 -> /dev/null
lrwx------ 1 root root 64 Apr 23 22:44 2 -> /dev/null
lrwx------ 1 root root 64 Apr 23 23:19 20 -> socket:[16204]
lrwx------ 1 root root 64 Apr 23 23:19 21 -> socket:[16205]
lrwx------ 1 root root 64 Apr 23 22:44 3 -> socket:[18743]
l-wx------ 1 root root 64 Apr 23 22:44 4 -> /var/lib/dhcp/dhclient-7a30dd46-5058-47aa-b71e-ff77cfbe4194-wlan0.lease
lrwx------ 1 root root 64 Apr 23 22:44 5 -> socket:[16872]
lrwx------ 1 root root 64 Apr 23 22:44 6 -> socket:[18747]
vz0
  • 32,345
  • 7
  • 44
  • 77
4

I don't know of any function that can remove a file based on a file descriptor, but any such function would first have to get the path anyway, and then call unlink.

A file descriptor on Linux is an association between a process and a directory entry. The directory entry is a link between a path (file name) and an inode. There can be many file descriptors associated with a directory entry, and many directory entries associated with an inode.

When you unlink a file, you are removing a link between the directory entry and the inode. If that is the last link, the file is finally removed from disk (i.e. the inode is returned to the free list, and the blocks used by the inode are also freed).

Patrick
  • 2,243
  • 2
  • 23
  • 32
  • I actually think a file descriptor is an association between a process and an inode, not related to one particular directory entry (in fact, there might not be any directory entries left, you can keep an FD to an inode around after unlinking the related path). – Matthijs Kooijman Apr 04 '18 at 15:57
3

Depending on your use-case, if the file content is unwanted (ie. too large or could be harmful) you can use the fd to shrink the file to 0 bytes.

ftruncate(fd, 0);
Critikullx
  • 65
  • 5
1

To the best of my knowledge, there's only remove and unlink, both of which require a path rather than a fd. This makes sense though; An fd is essentially just a pointer to read/write/close etc. A fd doesn't necessarily refer to a file on the file system, so having "delete" for file descriptors wouldn't make much sense.

Cubic
  • 14,902
  • 5
  • 47
  • 92
0

Is this a temporary file that's created by your program? If so you might want to consider mkstemp(): http://pubs.opengroup.org/onlinepubs/009695399/functions/mkstemp.html. If you're ok with getting a FILE * consider tmpfile() as well: http://pubs.opengroup.org/onlinepubs/009695399/functions/tmpfile.html

In this case you don't need to worry about deleting the file. As long as you properly close() the file, the OS will take care of deleting it properly (might not happen immediately).

R.D.
  • 2,471
  • 2
  • 15
  • 21
  • *the OS will take care of deleting it properly* -- this is not true, you still have to unlink the file. – Grisha Levit Jun 28 '21 at 21:37
  • 1
    (If `mkstemp` is used to create a file on a file system that happens to be tmpfs or in a directory subject to `tmpwatch` cleanup, etc., then, yes, the file will eventually be deleted, but that's the case no matter what method was used to create it) – Grisha Levit Jun 28 '21 at 21:39