Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 27 Dec 2008 14:58:06 +0200
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
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:  <87zlihixlt.fsf@kobe.laptop>
In-Reply-To: <20081227094012.GA39306@thought.org> (Gary Kline's message of "Sat, 27 Dec 2008 01:40:13 -0800")
References:  <20081227011335.GA29354@thought.org> <87ocyy2you.fsf@kobe.laptop> <20081227015634.GB29639@thought.org> <8763l61gbd.fsf@kobe.laptop> <20081227094012.GA39306@thought.org>

next in thread | previous in thread | raw e-mail | index | archive | help
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 ">"

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 <stdio.h>
    #include <unistd.h>

    /*-
     * \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 :)




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?87zlihixlt.fsf>