Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to recreate a file from an opened file descriptor?

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.

like image 766
ereOn Avatar asked Apr 23 '13 12:04

ereOn


1 Answers

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.

like image 62
Medinoc Avatar answered Sep 18 '22 12:09

Medinoc