• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Find if file has been modifed using C

DarkKnight

Golden Member
I want my application to be able detect when a file has been modified.

My initial idea is to store the file's timestamp in a variable, and when I reopen the file, check to see if the new timestamp is different. The problem I'm running into is that I dont know how to get a file's timestamp using C. Does anyone know how to do this?

Also, if you know of a better way of finding out whether a file has been modified, please let me know.

 
Originally posted by: DarkKnight
I want my application to be able detect when a file has been modified.

My initial idea is to store the file's timestamp in a variable, and when I reopen the file, check to see if the new timestamp is different. The problem I'm running into is that I dont know how to get a file's timestamp using C. Does anyone know how to do this?

Also, if you know of a better way of finding out whether a file has been modified, please let me know.
It depends on the OS that your application is running on.

 
probably just as reliable would be to hash the file. MD5 although no longer considered a secure hash function still has some uses here. It is definitely more processor intensive than checking stat but it is a lot more accurate in that it would be much much harder to modify the file and trick the detection algorithm.
 
man 3 fstat
man 2 stat
etc.

that'll get you the ctime, mtime, atime.

Though the mtime could be changed by programs that do not modify the file, or it could be left alone by programs that do modify the file if they've been coded to do that.

To really tell if the file has been modified in content, yeah, you should read the file, calculate the hash, and compare that, it's a slow process. SHA1 hashes can be easily calculated... I'm sure there are built in hash libraries in the openssl devel package or something like libtom or a bunch of others.

If you want an *efficient* way to potentially tell, check out inotify/gamin/fam (file alteration monitor) et. al. which are kernel / daemon level filesystem hooks that notify interested parties when an event such as a modification has taken place on a file that is being 'watched'.
http://en.wikipedia.org/wiki/Inotify
http://en.wikipedia.org/wiki/File_alteration_monitor

of course nothing is preventing the file from being modified when those services aren't watching it.... so... I guess it depends on what works for your needs.

 
Back
Top