From owner-freebsd-questions@FreeBSD.ORG Sat Dec 27 21:58:51 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 92F851065673 for ; Sat, 27 Dec 2008 21:58:51 +0000 (UTC) (envelope-from keramida@freebsd.org) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id 9C2228FC1E for ; Sat, 27 Dec 2008 21:58:50 +0000 (UTC) (envelope-from keramida@freebsd.org) Received: from kobe.laptop (adsl57-25.kln.forthnet.gr [77.49.184.25]) (authenticated bits=128) by igloo.linux.gr (8.14.3/8.14.3/Debian-5) with ESMTP id mBRLwa8k026967 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Sat, 27 Dec 2008 23:58:42 +0200 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.3/8.14.3) with ESMTP id mBRLwa5M037784; Sat, 27 Dec 2008 23:58:36 +0200 (EET) (envelope-from keramida@freebsd.org) Received: (from keramida@localhost) by kobe.laptop (8.14.3/8.14.3/Submit) id mBRLwZUL037783; Sat, 27 Dec 2008 23:58:35 +0200 (EET) (envelope-from keramida@freebsd.org) From: Giorgos Keramidas To: Gary Kline 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> Date: Sat, 27 Dec 2008 23:58:35 +0200 In-Reply-To: <20081227213551.GA75428@thought.org> (Gary Kline's message of "Sat, 27 Dec 2008 13:35:51 -0800") Message-ID: <87k59lgu0k.fsf@kobe.laptop> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-MailScanner-ID: mBRLwa8k026967 X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-4.302, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.10, BAYES_00 -2.60) X-Hellug-MailScanner-From: keramida@freebsd.org X-Spam-Status: No Cc: FreeBSD Mailing List Subject: Re: how can i be certain that a file has copied exactly? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 27 Dec 2008 21:58:51 -0000 On Sat, 27 Dec 2008 13:35:51 -0800, Gary Kline 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 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: >> > >> > > > >> > 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().