Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #17429

Re: copying files

From Nobody <nobody@nowhere.com>
Subject Re: copying files
Date 2012-02-16 10:58 +0000
Message-Id <pan.2012.02.16.10.58.41.838000@nowhere.com>
Newsgroups comp.lang.c
References <f62ccd5a-1cf4-4585-b8ca-c515a53d8a73@w9g2000vbv.googlegroups.com>
Organization Zen Internet

Show all headers | View raw


On Thu, 16 Feb 2012 01:03:12 -0800, Hans Vlems wrote:

> I'm maintaing large numbers of Adobe Reader files (.pdf). One of my
> programs, written in C (gcc 4.4.4), must make a copies between
> different filesystems of these pdf files.
> There is AFAIK no library function that does this, which leaves me two
> options:
> 1- use the console interface, i.e. build a command string and pass
> this to system().

Avoid system() unless executing a "canned" command supplied by the user.
If you need to spawn a child process with specific arguments, use fork()
and exec*() rather than attempting to construct a shell command.

> 2- open the file, copy the contents and close the target

First, you need to decide what you mean by "copy". Part of the reason that
there isn't a library function is that there isn't a single obvious
definition of what it means to copy a file. Two plausible choices are:

1. open() the destination, write the contents of the source to it, close()
it.

2. open() a temporary file in the same directory as the destination,
write the contents of the source to it, close() it, rename() it over the
original.

The two alternatives have many differences, including (but not limited to):

1. If there are multiple hard links to the destination, #1 will leave all
links intact, and all will refer to the modified file. #2 will break one
specific hard link, causing the filename to point to a new file; the
others will still refer to the original file.

2. If the destination file is open in some other process, #1 will cause
the process to immediately see the new contents, while #2 will only affect
processes which open() the file after the rename() has occurred.

3. #1 requires that you have write permission on the destination file if
it exists, or write permission on the directory if it doesn't. #2 requires
that you have write permission on the directory (the file's permissions
don't matter); if the destination exists and the directory has the sticky
bit set, you must own the file (or be root).

4. #1 won't affect the owner, group or permissions of an existing file. #2
will create a new file with your uid, primary gid and umask.

5. If the destination exists and is a device (block or character) or FIFO,
#1 will open() it and write to it. #2 will replace it with a file.

6. If the destination exists and is a symlink, #1 will open() it (i.e.
open its target) and write to it. #2 will replace it with a file.

Note that the Unix "cp" command is similar to option #1, except that if
the file exists but open()ing it fails and the "-f" flag is used, it
attempts to unlink() it then, if that succeeds, proceeds as if the file
didn't exist.

> I'd rather avoid option 1 because system runs out of control of my
> program.
> My question is what read and write functions are best suited to copy
> the (binary) pdf files?
> 'Performance is not the main objective, but I want to be sure that the
> copy finished succesfully and accurately.

For robustness, choose option #2 above. If writing the file fails,
remove() the temporary file rather than rename()ing it. The original file
will be left intact.

Performance-wise, mmap()ing the source and write()ing directly from the
mmap()d region eliminates a copy. mmap()ing both source and destination
and memcpy()ing may or may nor provide any additional benefit.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 01:03 -0800
  Re: copying files Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-16 01:23 -0800
    Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 01:31 -0800
      Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:14 -0800
        Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:43 -0800
          Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:59 -0800
            Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 15:06 -0800
    Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:13 -0800
      Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:56 -0800
        Re: copying files Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-16 10:11 -0800
          Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 15:11 -0800
            Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 15:21 -0800
              Re: copying files Philip Lantz <prl@canterey.us> - 2012-02-17 09:53 -0800
                Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-18 07:22 -0800
            Re: copying files Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-02-17 03:36 -0800
              Re: copying files Willem <willem@toad.stack.nl> - 2012-02-17 22:04 +0000
                Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-18 07:28 -0800
                Re: copying files Willem <willem@toad.stack.nl> - 2012-02-18 21:28 +0000
              Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-18 07:19 -0800
        Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 15:02 -0800
  Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 01:34 -0800
    Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:27 -0800
      Re: copying files Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-16 17:11 +0000
        Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:57 -0800
      Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:54 -0800
    Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 06:41 -0800
  Re: copying files JohnF <john@please.see.sig.for.email.com> - 2012-02-16 09:49 +0000
    Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 03:10 -0800
      Re: copying files JohnF <john@please.see.sig.for.email.com> - 2012-02-16 11:52 +0000
        Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 06:56 -0800
          Re: copying files David Thompson <dave.thompson2@verizon.net> - 2012-02-24 04:43 -0500
        Re: copying files Ike Naar <ike@iceland.freeshell.org> - 2012-02-16 16:19 +0000
          Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:50 -0800
        Re: copying files David Thompson <dave.thompson2@verizon.net> - 2012-02-24 04:43 -0500
  Re: copying files Nobody <nobody@nowhere.com> - 2012-02-16 10:58 +0000
    Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:41 -0800
      Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 14:59 -0800
      Re: copying files Nobody <nobody@nowhere.com> - 2012-02-17 06:25 +0000
  Re: copying files Kleuske <kleuske@nowhere.net> - 2012-02-16 11:15 +0000
    Re: copying files Hans Vlems <hvlems@freenet.de> - 2012-02-16 06:45 -0800
    Re: copying files Keith Thompson <kst-u@mib.org> - 2012-02-16 09:49 -0800

csiph-web