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

Giorgos Keramidas keramida at freebsd.org
Sat Dec 27 21:58:51 UTC 2008


On Sat, 27 Dec 2008 13:35:51 -0800, Gary Kline <kline at thought.org> wrote:
> On Sat, Dec 27, 2008 at 02:58:06PM +0200, Giorgos Keramidas wrote:
>> On Sat, 27 Dec 2008 01:40:13 -0800, Gary Kline <kline at thought.org> wrote:
>> > howdy,
>> >
>> > in a word, YES, /usr/bin/cmp saved the save before i unlinked the
>> > oldfile.  here is the strangeness.  maybe you know, giorgos, or
>> > somebody else on-list.  At first--before i got smart and used your
>> > snprintf to simply /bin/cp and then unlink---yes, or /bin/mv, or
>> > simply rename()--- Before, while i creating via fgets/fputs a new
>> > file, everything went fine until i ran out of buffer space.  i
>> > increased to buf[4096] to buf[65535].  more files were successfully
>> > copied from dos\;5 to .dos/*.htm, actually.  suddenly, cmp caught a
>> > mismatch and the program exited.  a careful diff showed the err a
>> > something like line 3751.  my copy was missing a byte near the EOF:
>> >
>> > </body></html
>> >
>> > minus the closing ">"
>
> Your code copies flawlessly.  I noticed late last night that cmp uses
> the same byte-by-byte cp and IIRC checks each to make certain they
> bytes are identical.  My copyFile() function simply used fopen, fgets,
> and fputs.  I yanked it from a program that copied files from ~/Mail
> where the lines were around 80 bytes rather than in the thousands.
> With few newlines.  The gotcha got me, in other words!  Thanks much
> for the function!

That's good news, because I didn't even compile it.  I just wrote it in
my mailer and hit send.  I'm glad it worked :)

For what it's worth, if you are not handling *text* files, fgets() and
fputs() are probably a bad idea.  They are line oriented, and they
depend on the presence of '\n' characters.  The concept of ``lines'' is,
at best, ill defined for binary files.  So it makes more sense to use
either byte-for-byte copies and rely on stdio to do buffering, or to use
some sort of custom buffer and fread()/fwrite() or plain read()/write().



More information about the freebsd-questions mailing list