Variant of the question Getting the file name from the file descriptor in C. It's about Linux.
If I have a file descriptor that refers to a regular file, can I “save” the file descriptor by giving it a new file name (somewhere on the same device where it lives, of course)? I was looking for something similar to renaming (2) or link (2), but which would take the file descriptor as an input name instead of a file name.
The problem with renaming (2) and link (2) is that even if you can try to switch from the descritor file to the file name, it may fail. I think more precisely about the case when an open file descriptor refers to a file that has already been disconnected - in this case, the file no longer has a name. There seems to be no way to prevent file deletion when closing () the file descriptor. But am I wrong? Can we with Posix or even the Linux API give it a name again?
Update: we can actually see the contents of the remote file on Linux in /proc/<pid>/fd/<fd> , although it looks like a broken symbolic link. We cannot use link (2) or ln (1) to remake such a file, though, because he thinks we are trying to link to several devices.
posix rename
Armin rigo
source share