Linux - mkstemp64 如何创建隐藏文件,我如何在文件系统中看到它

Linux - How does mkstemp64 create a hidden file, and how can I see it in the file system

背景:

在 CentOS 7 上 x86_64。我正在使用 applydeltarpm,并且在从增量创建新的 RPM 时 运行 磁盘 space 不足。我看到 / 磁盘 space 在应用过程中使用率增加到 100%,但无法使用 ls -l /tmp 或 [=15= 在卷上找到 working/temp 文件].

我更改了 applydeltarpm 源代码以使用 /var/tmp 而不是 /tmp,并重建了 RPM。现在 apply 适用于修改后的 applydeltarpm,因为 /var/tmp 有更多的磁盘 space。但是我仍然找不到用 mkstemp64.

创建的临时文件

问题:

mkstemp64 创建的临时文件似乎是 "non-existent",但对于创建者来说仍然作为文件描述符存在,并且在 [=12= 时使用相当多的磁盘 space ] 创建一个大的 RPM(在慢速磁盘上应用 1 小时)。 mkstemp64 文档说创建了一个实际文件。源代码显示模板文件名为/tmp/deltarpmpageXXXXXX。但是不存在具有该模板名称的文件。

如何在系统上创建这个临时文件而不用通常的目录列表 lsfind 找到。以及如何在系统中找到这些 "non-existent" 文件?

(我很好奇,因为我也在监控系统安全)

参考文献:

https://github.com/rpm-software-management/deltarpm/blob/master/applydeltarpm.c

  # line 198

  if (pagefd < 0)
    {
      char tmpname[80];
      sprintf(tmpname, "/tmp/deltarpmpageXXXXXX");
#ifdef DELTARPM_64BIT
      pagefd = mkstemp64(tmpname);
#else
      pagefd = mkstemp(tmpname);
#endif
      if (pagefd < 0)
        {
          fprintf(stderr, "could not create page area\n");
          exit(1);
        }
      unlink(tmpname);
    }

https://www.mkssoftware.com/docs/man3/mkstemp.3.asp

The mktemp() function returns a unique file name based on the template parameter. At the time it is generated, no file in the current directory has that name. No file is actually created, so it is possible that another application could create a file with this name.

The mkstemp() function is similar to mktemp() in that it create a unique file name based on template; however, mkstemp() actually creates the file and returns its file descriptor. The name of the created file is stored in template.

The mkstemp64() function is identical to the mkstemp() function except that the file is opened with the O_LARGEFILE flag set.

如果不再需要通过文件系统访问临时文件,通常的做法是在创建后立即取消link临时文件。如果进程崩溃或稍后忘记取消link,这可以避免临时文件悬空。

unlink() 不会删除文件,它只会从文件系统中删除文件的 link。文件系统中文件的每个 link 都会使文件的 link 计数增加一(同一个文件可以有多个 link)。此外,调用 open()mmap() 打开文件的每个进程都会增加文件计数,直到它关闭描述符 - 然后 link 计数减少。只要至少有一个 link 文件就存在。当link 计数为零时,文件实际上被删除了。

mkstemp() 还在后台调用 open() 打开临时文件和 return 它的描述符。

为了查看已打开但文件系统中不再存在的文件,您可以使用 lsof 并搜索文件名后有“(deleted)”的行.

lsof | grep '(deleted)'

这些文件使用的(磁盘)space将被释放,当它们附加到的进程完成或自行关闭文件描述符时。