views:

298

answers:

5

I'm writing a simple program that is used to synchronize files to an FTP. I want to be able to check if the local version of a file is different from the remote version, so I can tell if the file(s) need to be transfered. I could check the file size, but that's not 100% reliable because obviously it's possible for two files to be the same size but contain different data. The date/time the files were modified is also not reliable as the user's computer date could be set wrong.

Is there some other way to tell if a local file and a file on an FTP are identical?

A: 

Use a checksum. You generate the md5 (or sha1, sha2 etc) hash of both files, and if the files are identical, then the hashes will be identical.

James Deville
How does this work with no download?
John Saunders
Well, it works if you have control of the ftp, and can generate checksums whenever a file is uploaded... that's technically still a download, I suppose, but it's not a download of an arbitrary-sized file.
neminem
+3  A: 

There isn't a generic way. If the ftp site includes a checksum file, you can download that (which will be a lot quicker since a checksum is quite small) and then see if the checksums match. But of course, this relies on the owner of the ftp site creating a checksum file and keeping it up to date.

Other then that, you are S.O.L.

R Samuel Klatchko
+2  A: 

If the server is plain-old FTP, you can't do any better than checking the size and timestamps.

FTP has no mechanism for giving you the hashes/checksums of files, so you would need to do something like keeping a special "listing file" that has all the file names and hashes, or doing a separate request via HTTP, or some other protocol.

Ideally, you should not be using FTP anyway, it's really an obsolete protocol. If you have control of the system, you could use rsync or something like it.

Tim Sylvester
A: 

IETF tried to achieve this by adding new FTP commands such as MD5 and MMD5.

http://www.faqs.org/rfcs/ftp-rfcs.html

However, no all FTP vendors support them. So you must have a check on the targeting FTP server you application will work against to see if it supports MD5/MMD5. If not, you can pick up the workarounds mentioned above.

Lex Li
A: 

Couldn't you use a FileSystemWatcher and just have the client remember what changed? http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

Chris