9

Now, this question may seem weird, and it probably is, but to give some context, I've been reading this to learn about i-nodes in which the author gives an interesting example:

{
  FILE *fp;

  fp = fopen("some.hidden.file","w");
  unlink("some.hidden.file"); /* deletes the filename part */

  /* some.hidden.file no longer has a filename and is truly hidden */
  fprintf(fp,"This data won't be found\n"); /* access the data part */
  /*etc*/
  fclose(fp); /* finally release the data part */
}

This allows to create a "hidden" temporary file.

My question here being: is there any way to recreate a filename that points to the inode held opened by fp after the call to unlink()?

Disclaimer: I do not intend to do this in real code; I'm merely (re)learning about i-nodes and wonder if this is possible.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ereOn
  • 53,676
  • 39
  • 161
  • 238
  • 1
    AFAIK, there is no way to give a name to an open file descriptor. – Jonathan Leffler Apr 23 '13 at 12:21
  • From the [manpage](http://linux.die.net/man/2/unlink): "If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed. " – Bart Friederichs Apr 23 '13 at 12:36
  • @BartFriederichs: Sure, that is what was explained in the link I gave. But it doesn't really answer my question, which is more: "Is there a way to recreate a file pointing to the currently opened "i-node" before it gets destroyed ?" – ereOn Apr 23 '13 at 12:39
  • The opened "file" will still show under /proc/"pid"/fd/ , I'm unsure of you can create a (sym)link from that though. – nos Apr 23 '13 at 12:49

1 Answers1

2

I'm afraid it is not possible because the link system call demands a valid file name (which means, an existing link) rather than an UNIX file descriptor. There is no flink function in the Single UNIX Specification.

Medinoc
  • 6,577
  • 20
  • 42
  • Yep, but there could be some other, low-level function that deals with i-nodes. I just don't know where to look ;) – ereOn Apr 23 '13 at 12:44
  • You would be very OS-specific, I'd say, I quick Googling got me here: http://www.win.tue.nl/~aeb/linux/lk/lk-8.html (frist hit, you might want to look a little further; I googled 'kernel inode handling'). – Bart Friederichs Apr 23 '13 at 12:53