I have an int
file descriptor that was opened earlier (via open
) and I need to remove that file.
我有一个早期打开的int文件描述符(通过打开),我需要删除该文件。
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)
我真的必须先获取文件名并调用删除吗? (例如,通过使用C中的文件描述符获取文件名中的技术)
Or is there some other (linux specific OK) way of doing it solely based on the file descriptor?
或者是否还有一些其他(linux特定的OK)方式完全基于文件描述符?
I have searched and the best I could find is the above answer.
我搜索过,我能找到的最好的是上面的答案。
5 个解决方案
#1
5
You can use /proc
to see which path an open fd is linked to, and realpath get the full path of the symlink.
您可以使用/ proc查看open fd链接到哪个路径,realpath获取符号链接的完整路径。
# 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]
#2
2
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
.
我不知道任何可以根据文件描述符删除文件的函数,但任何这样的函数首先必须获取路径,然后调用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.
Linux上的文件描述符是进程和目录条目之间的关联。目录条目是路径(文件名)和inode之间的链接。可以有许多与目录条目相关联的文件描述符,以及与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).
取消链接文件时,将删除目录条目和inode之间的链接。如果这是最后一个链接,则最终从磁盘中删除该文件(即,将inode返回到空闲列表,并且还释放inode使用的块)。
#3
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.
据我所知,只有删除和取消链接,这两者都需要路径而不是fd。这虽然有意义; fd本质上只是指向读/写/关闭等的指针.fd不一定是指文件系统上的文件,因此对文件描述符进行“删除”没有多大意义。
#4
1
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.
根据您的使用情况,如果文件内容不受欢迎(即太大或可能有害),您可以使用fd将文件缩小为0字节。
ftruncate(fd, 0);
#5
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
这是一个由您的程序创建的临时文件吗?如果是这样,您可能需要考虑mkstemp():http://pubs.opengroup.org/onlinepubs/009695399/functions/mkstemp.html。如果您对获取文件感到满意*也可以考虑使用tmpfile():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).
在这种情况下,您无需担心删除该文件。只要你正确关闭()文件,操作系统就会正确地删除它(可能不会立即发生)。
#1
5
You can use /proc
to see which path an open fd is linked to, and realpath get the full path of the symlink.
您可以使用/ proc查看open fd链接到哪个路径,realpath获取符号链接的完整路径。
# 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]
#2
2
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
.
我不知道任何可以根据文件描述符删除文件的函数,但任何这样的函数首先必须获取路径,然后调用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.
Linux上的文件描述符是进程和目录条目之间的关联。目录条目是路径(文件名)和inode之间的链接。可以有许多与目录条目相关联的文件描述符,以及与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).
取消链接文件时,将删除目录条目和inode之间的链接。如果这是最后一个链接,则最终从磁盘中删除该文件(即,将inode返回到空闲列表,并且还释放inode使用的块)。
#3
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.
据我所知,只有删除和取消链接,这两者都需要路径而不是fd。这虽然有意义; fd本质上只是指向读/写/关闭等的指针.fd不一定是指文件系统上的文件,因此对文件描述符进行“删除”没有多大意义。
#4
1
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.
根据您的使用情况,如果文件内容不受欢迎(即太大或可能有害),您可以使用fd将文件缩小为0字节。
ftruncate(fd, 0);
#5
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
这是一个由您的程序创建的临时文件吗?如果是这样,您可能需要考虑mkstemp():http://pubs.opengroup.org/onlinepubs/009695399/functions/mkstemp.html。如果您对获取文件感到满意*也可以考虑使用tmpfile():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).
在这种情况下,您无需担心删除该文件。只要你正确关闭()文件,操作系统就会正确地删除它(可能不会立即发生)。