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

瘦欲@ 提交于 2020-01-01 04:59:06

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/16169149/is-it-possible-to-recreate-a-file-from-an-opened-file-descriptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!