annotate lib/copy-file.c @ 12421:e8d2c6fc33ad

Use spaces for indentation, not tabs.
author Bruno Haible <bruno@clisp.org>
date Thu, 10 Dec 2009 20:28:30 +0100
parents e8fbc5424b99
children b5e42ef33b49
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Copying of files.
8196
5166dec344af exit.h is replaced with stdlib.h.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
2 Copyright (C) 2001-2003, 2006-2007 Free Software Foundation, Inc.
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8196
diff changeset
5 This program is free software: you can redistribute it and/or modify
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8196
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8196
diff changeset
8 (at your option) any later version.
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8196
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6751
diff changeset
19 #include <config.h>
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include "copy-file.h"
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <errno.h>
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <fcntl.h>
4310
17ce7ef2789c Include <stddef.h>, for size_t.
Bruno Haible <bruno@clisp.org>
parents: 4308
diff changeset
26 #include <stddef.h>
8196
5166dec344af exit.h is replaced with stdlib.h.
Bruno Haible <bruno@clisp.org>
parents: 7863
diff changeset
27 #include <stdlib.h>
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <sys/stat.h>
6751
1b0092424a44 Include <unistd.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 6259
diff changeset
29 #include <unistd.h>
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 #if HAVE_UTIME || HAVE_UTIMES
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 # if HAVE_UTIME_H
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 # include <utime.h>
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 # else
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 # include <sys/utime.h>
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 # endif
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #endif
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #include "error.h"
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 #include "safe-read.h"
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #include "full-write.h"
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
42 #include "acl.h"
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 #include "binary-io.h"
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #include "gettext.h"
12198
e8fbc5424b99 digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
Pádraig Brady <P@draigBrady.com>
parents: 9309
diff changeset
45 #include "xalloc.h"
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 #define _(str) gettext (str)
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48
7863
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7742
diff changeset
49 /* The results of open() in this file are not used with fchdir,
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7742
diff changeset
50 therefore save some unnecessary work in fchdir.c. */
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7742
diff changeset
51 #undef open
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7742
diff changeset
52 #undef close
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7742
diff changeset
53
12198
e8fbc5424b99 digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
Pádraig Brady <P@draigBrady.com>
parents: 9309
diff changeset
54 enum { IO_SIZE = 32 * 1024 };
7863
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7742
diff changeset
55
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 void
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 copy_file_preserving (const char *src_filename, const char *dest_filename)
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 {
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 int src_fd;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 struct stat statbuf;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 int mode;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 int dest_fd;
12198
e8fbc5424b99 digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
Pádraig Brady <P@draigBrady.com>
parents: 9309
diff changeset
63 char *buf = xmalloc (IO_SIZE);
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 src_fd = open (src_filename, O_RDONLY | O_BINARY);
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 if (src_fd < 0 || fstat (src_fd, &statbuf) < 0)
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 error (EXIT_FAILURE, errno, _("error while opening \"%s\" for reading"),
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12198
diff changeset
68 src_filename);
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 mode = statbuf.st_mode & 07777;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 dest_fd = open (dest_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 if (dest_fd < 0)
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 error (EXIT_FAILURE, errno, _("cannot open backup file \"%s\" for writing"),
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12198
diff changeset
75 dest_filename);
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 /* Copy the file contents. */
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 for (;;)
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 {
12198
e8fbc5424b99 digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
Pádraig Brady <P@draigBrady.com>
parents: 9309
diff changeset
80 size_t n_read = safe_read (src_fd, buf, IO_SIZE);
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 if (n_read == SAFE_READ_ERROR)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12198
diff changeset
82 error (EXIT_FAILURE, errno, _("error reading \"%s\""), src_filename);
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 if (n_read == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12198
diff changeset
84 break;
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 if (full_write (dest_fd, buf, n_read) < n_read)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12198
diff changeset
87 error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 }
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89
12198
e8fbc5424b99 digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
Pádraig Brady <P@draigBrady.com>
parents: 9309
diff changeset
90 free (buf);
e8fbc5424b99 digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
Pádraig Brady <P@draigBrady.com>
parents: 9309
diff changeset
91
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
92 #if !USE_ACL
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 if (close (dest_fd) < 0)
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 if (close (src_fd) < 0)
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename);
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
97 #endif
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 /* Preserve the access and modification times. */
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 #if HAVE_UTIME
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 {
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 struct utimbuf ut;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 ut.actime = statbuf.st_atime;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 ut.modtime = statbuf.st_mtime;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 utime (dest_filename, &ut);
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 }
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 #elif HAVE_UTIMES
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 {
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 struct timeval ut[2];
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 ut[0].tv_sec = statbuf.st_atime; ut[0].tv_usec = 0;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 ut[1].tv_sec = statbuf.st_mtime; ut[1].tv_usec = 0;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 utimes (dest_filename, &ut);
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 }
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 #endif
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117
4268
4933582b32c1 Portability to Woe32.
Bruno Haible <bruno@clisp.org>
parents: 4266
diff changeset
118 #if HAVE_CHOWN
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 /* Preserve the owner and group. */
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 chown (dest_filename, statbuf.st_uid, statbuf.st_gid);
4268
4933582b32c1 Portability to Woe32.
Bruno Haible <bruno@clisp.org>
parents: 4266
diff changeset
121 #endif
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 /* Preserve the access permissions. */
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
124 #if USE_ACL
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
125 if (copy_acl (src_filename, src_fd, dest_filename, dest_fd, mode))
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
126 exit (EXIT_FAILURE);
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
127 #else
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 chmod (dest_filename, mode);
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
129 #endif
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
130
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
131 #if USE_ACL
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
132 if (close (dest_fd) < 0)
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
133 error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
134 if (close (src_fd) < 0)
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
135 error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename);
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
136 #endif
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 }