gpt4 book ai didi

c - 确定 "stat()"返回的有效时间戳精度

转载 作者:太空狗 更新时间:2023-10-29 15:27:30 26 4
gpt4 key购买 nike

我正在尝试确定软件中 struct statst_mtim.tv_nsec 字段对于特定目录/文件系统的有效精度。

有没有一种方法可以确定文件系统的修改时间精度(而不是库的“纳秒”精度或操作系统的目录缓存精度)?

添加:对于一些背景信息,这是用于更新某些文件的工具。基本场景是:“如果第一组文件中的某个文件可能比第二组文件中的任何文件都新,则使用第一组文件更新第二组文件”后跟“如果第二组文件中的某个文件可能比第三组文件中的任何文件都新,则使用第二组文件更新第三组文件”。 p>

我遇到的问题是第一次运行该工具时(修改第一组中的文件后)它会更新第二组文件,然后更新第三组文件(这是正确的行为);但是第二次运行该工具(当没有任何更改时)第二组文件和第三组文件将具有相同的时间戳,因此它必须假设第三组中的文件可能更新并且它会更新第三组文件莫名其妙。

为了解决最初的问题,我在更新第三组文件之前引入了延迟(“nanosleep();”);这样下次运行该工具时,第三组文件会稍微旧一些。这确实避免了不必要的更新。

当然不是那么简单 - 存在任意数量的相互依赖的“文件组”(不仅仅是 3 组)。

这让我想到了我当前的问题 - 对于某些文件系统,时间戳精度低至 2 秒,并且所需的“最坏情况”延迟非常大(例如,它加起来至少有 60 秒的延迟 31 “文件组”)的级别。对于大多数文件系统,时间戳要精确得多,大量浪费的时间可能会消失。当然,该工具旨在“尽可能便携”,我无法真正对时间戳精度做出任何假设(如果我知道文件系统总是 ext4 或其他东西,那将非常容易) .

最佳答案

根据这个OpenGroup Link ,

The resolution of timestamps of files in a file system is implementation-defined, but shall be no coarser than one-second resolution. The three timestamps shall always have values that are supported by the file system. Whenever any of a file's timestamps are to be set to a value V according to the rules of the preceding paragraphs of this section, the implementation shall immediately set the timestamp to the greatest value supported by the file system that is not greater than V.

所以你保证它至少是一秒钟。

此外,根据 stat(2) 上的 Linux 手册页:

Since kernel 2.5.48, the stat structure supports nanosecond resolution for the three file timestamp fields.

关于c - 确定 "stat()"返回的有效时间戳精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6849609/

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