how can i be certain that a file has copied exactly?

Lowell Gilbert freebsd-questions-local at be-well.ilk.org
Sat Dec 27 02:01:51 UTC 2008


Gary Kline <kline at thought.org> writes:

> On Sat, Dec 27, 2008 at 03:29:05AM +0200, Giorgos Keramidas wrote:
>> On Fri, 26 Dec 2008 17:13:39 -0800, Gary Kline <kline at thought.org> wrote:
>> > is there a way i can be sure that my little C program has copied a
>> > dos/win file named, say, foo.htm\;7 to simply foo.htm?
>> >
>> > my program uses fopen/fgets/fputs to copy the markup files.  of the
>> > several i have copied, no problem.  unless i hack cmp or diff, i have
>> > to avoid the shell.
>> >
>> > any ideas? in other words, does anybody have a prefab cmp(oldfile,
>> > newfile) fn?
>> 
>> You don't need a prefab `cmp' function, because the base system already
>> includes tools that can help:
>> 
>>   (a) The `cmp' utility:
>> 
>>             cmp file1 file2 ; echo $?
>> 
>>   (b) Checksum tools like `md5', `sha1' and `sha256':
>> 
>>             md5 file1 file2
>> 
>>             sha1 file1 file2
>> 
>>             sha256 file1 file2
>> 
>>       You can then compare the file checksums.  If both the md5 and
>>       sha256 checksums are identical, then the files are the same[1].
>> 
>>       [1] There is a possibility of ``checksum collisions'', especially
>>           with md5 (see [2] for more details).  But if you use two or
>>           more checksum types and none of them show differences, the
>>           odds of a collision are small enough for most practical
>>           purposes.
>> 
>>       [2] http://en.wikipedia.org/wiki/Md5#Vulnerability
>
>
> 		the problem is that there are several thousands of these files
> 		with dos names and an embedded '\;'7 in the file names.  the
> 		shell gets in the way.  i have tried 
>
> 		sprintf(cmdbuf, "/usr/bin/cmp %s %s", orig, new);
> 		system(cmdbuf);
>
> 		chokes on the embedded bytes.  
>
> 		i'm thinking of using
>
> 		find . -name "*" -print -exec {} \;
>
> 		and let me program select out the file suffix.  i unlink the
> 		screwy dos-ish filename.  that's why i want to be sure the
> 		copied/renamed files are right.

Your problem there is just normal file globbing.  If you quote the names
just like you would on the command line (modulo escaping for C's special
characters), it should work fine.

-- 
Lowell Gilbert, embedded/networking software engineer, Boston area
		http://be-well.ilk.org/~lowell/


More information about the freebsd-questions mailing list