简体   繁体   中英

How to delete a file in Linux where all I have is the file descriptor

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 ? (eg 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.

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]

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 (ie the inode is returned to the free list, and the blocks used by the inode are also freed).

根据您的使用情况,如果文件内容不受欢迎(即,太大或可能有害),您可以使用fd将文件缩小为0字节。

ftruncate(fd, 0);

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM