c linux check if file is updated/changed/modified?
Solution 1
Look at the man page for stat(2)
. Get the st_mtime
member of the struct stat
structure, 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.
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.
Solution 2
You have to use 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.
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.
So even if the st_mtime is the same, you can't be sure that the file hasn't changed. Therefore you have to assume that it has, and there's no point in deluding yourself by doing the test.
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.
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't changed, even hashing (although that approaches confidence).
Don't waste your time with stat(), it's a fool's errand.
Solution 3
The canonical way is to check the mtime of the file via stat(2)
.
Comments
-
Aditya P almost 2 years
How to check in c , linux if a file has been updated/changed .
I want to check a file for update before opening the file and performing extraction/ i/o operations from it.
-
Jeegar Patel about 12 yearsif stat gets fail and dont return 0 then does it mean file is modified..??
-
another.anon.coward about 12 years@Mr.32: I think you are confused with the message. What it means is that the error has occured in
file_is_modified
function (that is the name of the function if you see). Maybeperror(" [file_is_modified] stat");
would not have made you have this doubt -
Jeegar Patel about 12 years@another.anon.coward yea you are right and now my doubt is also clear :) thanks
-
Aditya P about 12 yearsso basically i would have to track the times of the files at start?
-
Anthony about 12 years@AdityaGameProgrammer Yes, although if you check out another.anon.coward's suggestion of
inotify
means that you can monitor file system operations. It's a bit more complex though. -
Aditya P about 12 yearsi guess its simpler to track st_mtime of files.
-
Anthony about 12 yearsYep, if you're willing to handle the overhead of tracking them. Another upside is that you can persist them, so you don't have to leave a process running.
-
Lucian Wischik over 5 yearsRogerborg's comment below points out that stat will inherently give you incorrect answers in certain situations...