gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:17:19 26 4
gpt4 key购买 nike

背景:

在 CentOS 7 x86_64 上。我正在使用 applydeltarpm,并且在从 delta 创建新的 RPM 时磁盘空间不足。我看到 / 磁盘空间使用量在应用过程中增加到 100%,但无法使用 ls -l/tmp找到/tmp -mmin -1 -type f

我更改了 applydeltarpm 源代码以使用 /var/tmp 而不是 /tmp,并重建了 RPM。现在 apply 使用修改后的 applydeltarpm,因为 /var/tmp 有更多的磁盘空间。但是我仍然找不到用 mkstemp64 创建的临时文件。

问题:

mkstemp64 创建的临时文件似乎“不存在”,但对于创建者来说仍然作为文件描述符存在,并且在 applydeltarpm 时占用了大量磁盘空间> 创建一个大的 RPM(在慢速磁盘上应用 1 小时)。 mkstemp64 文档说创建了一个实际文件。源代码显示模板文件名为/tmp/deltarpmpageXXXXXX。但是不存在具有该模板名称的文件。

这个临时文件如何能够在系统上创建而不用通常的目录列表 lsfind 找到。如何在系统中找到这些“不存在”的文件?

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

引用资料:

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.

最佳答案

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

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

mkstemp() 还在幕后调用 open() 以打开临时文件并返回其描述符。

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

lsof | grep '(deleted)'

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

关于Linux - mkstemp64 如何创建隐藏文件,我如何在文件系统中看到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45760956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com