views:

24

answers:

2

I want to make a procedure that does one way syncrhonization of a folder between a server and a client. I was thinking to use ModifiedDate as a criterio, provided that only the date of the server files will be used. Procedure will not use Modified dates of files on the client at all. It will read dates from the server and compare them with dates read from the server last time the procedure run. Do you think this is safe? Is there any possibility that Modified Date will not be changed when a file is edited or it will be changed without touching the contents of the file (eg. from some strange antivirus programs)?

+1  A: 

Don't count on the modification date of a file.

Strange programs (antiviruses and such) are not the problem more than the fact that you simply can't count on the client and server clocks to be synchronized.

Why not do a straightforward diff or hash calculation? You can't get a better comparison than that.

Taking performance considerations into effect, you can use the following heuristic:

  1. If date hasn't changed then file is obviously the same
  2. If date has changed, file contents might have changed, and might have not (for example: it has been touched). In this case in order to get a definitive answer you must examine the file somehow.

Bottom line: modification date can always give you a true negative (file not changed), but might sometimes yield a false positive - and this case you must verify.

Yuval A
Really huge files might slow things down, perhaps? I wonder if a checksum solution would also suffer from the same drawback…
htw
@Yuval Please read my question carefully. I'm saying that I intend to compare server dates only with the server dates used during that last syncrhonization. Client dates will not be used.@htw Both straightfoward diff and checksums (requires reading file completely to calculate) is not fast enough for what I want to do.So the question is still the same: Is there any possibility that modified date can change without actually modifying the file? Or is there any possibility that modified date does change when modifying the file?
papadi
@papadi - updated my answer
Yuval A
Good point! I could indeed calculate a hash only if date changes.
papadi
A: 

You didn't mention what OS you're on, but on UNIX platforms the modification time can be set by client code to any value it wants (see the utimes() API or touch command). Therefore you shouldn't rely on modification times to tell you whether a file has changed or not. I imagine Windows is somewhat similar.

Graham Lee
Yes, forgot to mention that I'm talking about Windows. On Windows it is also possible to change modification date programmaticaly.But my quesiton is not technical in this sense. I know for sure that it is possible. The question is if anybody has seen this happening. Since the files will be copied to temporary place and will be used only by my application, I don't expect changes in the modification dates in purpose. The only thing that concerns my are strange applications, like antivirus and if they can cause problems to me.
papadi