From owner-freebsd-questions@FreeBSD.ORG Sat Dec 27 21:36:00 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 29A331065673 for ; Sat, 27 Dec 2008 21:36:00 +0000 (UTC) (envelope-from kline@thought.org) Received: from aristotle.thought.org (aristotle.thought.org [209.180.213.210]) by mx1.freebsd.org (Postfix) with ESMTP id E71798FC18 for ; Sat, 27 Dec 2008 21:35:59 +0000 (UTC) (envelope-from kline@thought.org) Received: from thought.org (tao.thought.org [10.47.0.250]) (authenticated bits=0) by aristotle.thought.org (8.14.2/8.14.2) with ESMTP id mBRLaPSA071984; Sat, 27 Dec 2008 13:36:26 -0800 (PST) (envelope-from kline@thought.org) Received: by thought.org (nbSMTP-1.00) for uid 1002 kline@thought.org; Sat, 27 Dec 2008 13:35:51 -0800 (PST) Date: Sat, 27 Dec 2008 13:35:51 -0800 From: Gary Kline To: Giorgos Keramidas Message-ID: <20081227213551.GA75428@thought.org> 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> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87zlihixlt.fsf@kobe.laptop> User-Agent: Mutt/1.4.2.3i X-Organization: Thought Unlimited. Public service Unix since 1986. X-Of_Interest: With 22 years of service to the Unix community. X-Spam-Status: No, score=-4.4 required=3.6 tests=ALL_TRUSTED,BAYES_00 autolearn=ham version=3.2.3 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on aristotle.thought.org 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:36:00 -0000 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! gary > > 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 :) I'll grep -r thru /usr/src to see if any utility actually does use my shortcut! ...Somehow I doubt it. gary > > _______________________________________________ > freebsd-questions@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-questions > To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org" -- Gary Kline kline@thought.org http://www.thought.org Public Service Unix http://jottings.thought.org http://transfinite.thought.org The 2.17a release of Jottings: http://jottings.thought.org/index.php