Date: Sat, 27 Dec 2008 23:58:35 +0200 From: Giorgos Keramidas <keramida@freebsd.org> To: Gary Kline <kline@thought.org> Cc: FreeBSD Mailing List <freebsd-questions@freebsd.org> Subject: Re: how can i be certain that a file has copied exactly? Message-ID: <87k59lgu0k.fsf@kobe.laptop> In-Reply-To: <20081227213551.GA75428@thought.org> (Gary Kline's message of "Sat, 27 Dec 2008 13:35:51 -0800") References: <20081227011335.GA29354@thought.org> <87ocyy2you.fsf@kobe.laptop> <20081227015634.GB29639@thought.org> <8763l61gbd.fsf@kobe.laptop> <20081227094012.GA39306@thought.org> <87zlihixlt.fsf@kobe.laptop> <20081227213551.GA75428@thought.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, 27 Dec 2008 13:35:51 -0800, Gary Kline <kline@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@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().
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?87k59lgu0k.fsf>