c linux 检查文件是否更新/更改/修改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9376975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:43:16  来源:igfitidea点击:

c linux check if file is updated/changed/modified?

clinuxfile-io

提问by Aditya P

How to check in c , linux if a file has been updated/changed .

如果文件已更新/更改,如何检查 c、linux。

I want to check a file for update before opening the file and performing extraction/ i/o operations from it.

我想在打开文件并从中执行提取/输入/输出操作之前检查文件的更新。

采纳答案by Anthony

Look at the man pagefor stat(2). Get the st_mtimemember of the struct statstructure, which will tell you the modification time of the file. If the current mtime is later than a prior mtime, the file has been modified.

看看手册页stat(2)。获取结构体的st_mtime成员struct stat,它会告诉你文件的修改时间。如果当前 mtime 晚于前一个 mtime,则文件已被修改。

An example:

一个例子:

int file_is_modified(const char *path, time_t oldMTime) {
    struct stat file_stat;
    int err = stat(path, &file_stat);
    if (err != 0) {
        perror(" [file_is_modified] stat");
        exit(errno);
    }
    return file_stat.st_mtime > oldMTime;
}


Here's an introduction to inotify, if that's what you're looking for.

这是对 的介绍inotify,如果这就是您要找的。

回答by Ignacio Vazquez-Abrams

The canonical way is to check the mtime of the file via stat(2).

规范的方法是通过stat(2).

回答by Rogerborg

You have to use inotify.

你必须使用inotify。

stat() is worse than useless for this purpose. If the st_mtime is different from the last time you checked it, then this tells you that the file has changed, and all is well.

stat() 为此目的比无用更糟糕。如果 st_mtime 与您上次检查的时间不同,则表明文件已更改,并且一切正常。

But what if the st_mtime is the same? There's no guarantee that this means the file wasn't changed within the granularity of the filesystem timestamp. On ext3, for example, the granularity tends to be in multiple milliseconds. You can't rely on the time difference between your checking either, what matters is how quickly the file may have been changed after the last time your process checked it.

但是如果 st_mtime 相同呢?不能保证这意味着文件没有在文件系统时间戳的粒度范围内更改。例如,在 ext3 上,粒度往往以几毫秒为单位。您也不能依赖检查之间的时间差,重要的是在您的进程上次检查文件后文件更改的速度有多快。

So even if the st_mtime is the same, you can't be sure that the file hasn'tchanged. Therefore you have to assume that it has, and there's no point in deluding yourself by doing the test.

因此,即使 st_mtime 相同,您也不能确定文件没有更改。因此,您必须假设它确实存在,并且通过进行测试来欺骗自己是没有意义的。

The same issues applies to st_ino, if you are expecting the file (of that name) to be replaced by a new file in a create-and-replace operation. inode numbers can be re-used, and after a couple of replacements, a file (by name) can be back to its original inode number again.

同样的问题也适用于 st_ino,如果您希望在创建和替换操作中用新文件替换(该名称的)文件。inode 编号可以重复使用,经过几次替换后,文件(按名称)可以再次恢复到其原始 inode 编号。

The same problem applies to file size, or even creating a hash of the file. All that allows you to determine is that the file has changed. None of these methods let you be completely confident that it hasn'tchanged, even hashing (although that approaches confidence).

同样的问题适用于文件大小,甚至创建文件的哈希。所有允许您确定的是文件已更改。这些方法都没有让您完全确信它没有改变,即使是散列(尽管这接近于信心)。

Don't waste your time with stat(), it's a fool's errand.

不要在 stat() 上浪费你的时间,这是一个傻瓜的差事。