From owner-freebsd-questions@FreeBSD.ORG Sat Dec 27 12:58:24 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 B0B221065673 for ; Sat, 27 Dec 2008 12:58:24 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id 2424C8FC2E for ; Sat, 27 Dec 2008 12:58:23 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) 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 mBRCw7ti023493 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Sat, 27 Dec 2008 14:58:13 +0200 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.3/8.14.3) with ESMTP id mBRCw7aD031988; Sat, 27 Dec 2008 14:58:07 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.3/8.14.3/Submit) id mBRCw6g4031987; Sat, 27 Dec 2008 14:58:06 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) 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> Date: Sat, 27 Dec 2008 14:58:06 +0200 In-Reply-To: <20081227094012.GA39306@thought.org> (Gary Kline's message of "Sat, 27 Dec 2008 01:40:13 -0800") Message-ID: <87zlihixlt.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: mBRCw7ti023493 X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-3.867, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.53, BAYES_00 -2.60) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr 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 12:58:24 -0000 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 ">" There should be no problem when you copy a file using read() and write() with _any_ buffer size. Even a simple routine that reads byte by byte and copies the file should work (a bit more of error checking is needed wherever I have used a (void) cast but you get the idea): #include #include /*- * \brief copy a file, using stdio byte-level read and writes * * Copy the `fromname' file to the `toname' file, using only fgetc() * and fputc() operations from stdio. The internals of stdio are * free, of course, to use larger read() and write() buffers but all * this should be "hidden" from the code of copyfile(). * * \param fromname The name of the source file to copy. * \param toname The name of the destination file where * `fromname' will be copied to. * * \return Upon successful completion 0 is * returned. Otherwise, EOF is returned * and the global variable errno is set to * indicate the error. */ int copyfile(const char *fromname, const char *toname) { FILE *ifp; FILE *ofp; int ch; if ((ifp = fopen(fromname, "rb")) == NULL) return -1; if ((ofp = fopen(toname, "wb")) == NULL) { (void)fclose(ifp); return -1; } while ((ch = fgetc(ifp)) != EOF) { if (fputc(ch, ofp) == EOF) break; } if (ferror(ifp) != 0 || ferror(ofp) != 0) { (void)unlink(toname); (void)fclose(ofp); (void)fclose(ifp); return -1; } if (fclose(ofp) == EOF) { (void)fclose(ifp); return -1; } if (fclose(ifp) == EOF) { return -1; } return 0; } So if you are seeing copy errors when you change the buffer size, you are doing something odd with your buffers. We would have to see the actual code if you want more detailed help about possible buffer handling bugs :)