A variant of the question Getting Filename from file descriptor in C. This is 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 as where it lives, of course)? I'm looking for something similar to rename(2) or link(2) but that would accept a file descriptor as input instead of a file name.
The problem with rename(2) and link(2) is that even though you can attempt to go from the file descritor to the file name, this might fail. I'm thinking more precisely about the case where the opened file descriptor refers to a file that was already unlinked --- in this case, the file has no more name. It seems that there is no way to prevent the file from being deleted when we close() the file descriptor. But am I wrong? Can we, with the Posix or even Linux API, give it a name again?
Update: we can actually see the content of a deleted file on Linux in /proc/<pid>/fd/<fd>
, even though it looks like a broken symbolic link. We can't use link(2) or ln(1) to rematerialize such a file, though, because it thinks we're trying to do a cross-device link.