annotate lib/copy-file.c @ 17101:99fa3b05f1c8

pipe-filter-gi, pipe-filter-ii: better use of 'inline' * lib/pipe-filter-aux.c: New file. * lib/pipe-filter-aux.h (PIPE_FILTER_AUX_INLINE): New macro. Replace all uses of 'static inline' with it. Use _GL_INLINE_HEADER_BEGIN, _GL_INLINE_HEADER_END. * lib/pipe-filter-gi.c (filter_init, filter_cleanup) (filter_retcode): No real need for inline here. * modules/pipe-filter-gi, modules/pipe-filter-ii: (Files): Add lib/pipe-filter-aux.c. (Depends-on): Add extern-inline. (configure.ac): Do not require AC_C_INLINE. (lib_SOURCES): Add pipe-filter-aux.c.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 29 Aug 2012 22:17:49 -0700
parents d50152d6b5d2
children e542fd46ad6f
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.
16201
8250f2777afc maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents: 14079
diff changeset
2 Copyright (C) 2001-2003, 2006-2007, 2009-2012 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"
16271
a9b070c205b2 copy-file: Refactor.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
44 #include "quote.h"
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 #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
46 #include "xalloc.h"
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 #define _(str) gettext (str)
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49
7863
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7742
diff changeset
50 /* 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
51 therefore save some unnecessary work in fchdir.c. */
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7742
diff changeset
52 #undef open
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7742
diff changeset
53 #undef close
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7742
diff changeset
54
12198
e8fbc5424b99 digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
Pádraig Brady <P@draigBrady.com>
parents: 9309
diff changeset
55 enum { IO_SIZE = 32 * 1024 };
7863
cb2590895141 New module 'fchdir'.
Bruno Haible <bruno@clisp.org>
parents: 7742
diff changeset
56
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
57 int
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
58 qcopy_file_preserving (const char *src_filename, const char *dest_filename)
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 {
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
60 int err = 0;
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 int src_fd;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 struct stat statbuf;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 int mode;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 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
65 char *buf = xmalloc (IO_SIZE);
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 src_fd = open (src_filename, O_RDONLY | O_BINARY);
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
68 if (src_fd < 0)
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
69 {
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
70 err = GL_COPY_ERR_OPEN_READ;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
71 goto error;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
72 }
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
73 if (fstat (src_fd, &statbuf) < 0)
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
74 {
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
75 err = GL_COPY_ERR_OPEN_READ;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
76 goto error_src;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
77 }
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 mode = statbuf.st_mode & 07777;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 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
82 if (dest_fd < 0)
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
83 {
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
84 err = GL_COPY_ERR_OPEN_BACKUP_WRITE;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
85 goto error_src;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
86 }
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 /* Copy the file contents. */
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 for (;;)
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 {
12198
e8fbc5424b99 digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
Pádraig Brady <P@draigBrady.com>
parents: 9309
diff changeset
91 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
92 if (n_read == SAFE_READ_ERROR)
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
93 {
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
94 err = GL_COPY_ERR_READ;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
95 goto error_src_dest;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
96 }
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 if (n_read == 0)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12198
diff changeset
98 break;
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 if (full_write (dest_fd, buf, n_read) < n_read)
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
101 {
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
102 err = GL_COPY_ERR_WRITE;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
103 goto error_src_dest;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
104 }
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 }
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106
12198
e8fbc5424b99 digests, copy-file: increase the IO buffer size from 4KiB to 32KiB
Pádraig Brady <P@draigBrady.com>
parents: 9309
diff changeset
107 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
108
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
109 #if !USE_ACL
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 if (close (dest_fd) < 0)
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
111 {
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
112 err = GL_COPY_ERR_WRITE;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
113 goto error_src;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
114 }
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 if (close (src_fd) < 0)
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
116 {
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
117 err = GL_COPY_ERR_AFTER_READ;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
118 goto error;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
119 }
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
120 #endif
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 /* Preserve the access and modification times. */
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 #if HAVE_UTIME
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 {
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 struct utimbuf ut;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 ut.actime = statbuf.st_atime;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 ut.modtime = statbuf.st_mtime;
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 utime (dest_filename, &ut);
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 }
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 #elif HAVE_UTIMES
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 {
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 struct timeval ut[2];
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135 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
136 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
137 utimes (dest_filename, &ut);
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 }
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 #endif
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140
4268
4933582b32c1 Portability to Woe32.
Bruno Haible <bruno@clisp.org>
parents: 4266
diff changeset
141 #if HAVE_CHOWN
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 /* Preserve the owner and group. */
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 chown (dest_filename, statbuf.st_uid, statbuf.st_gid);
4268
4933582b32c1 Portability to Woe32.
Bruno Haible <bruno@clisp.org>
parents: 4266
diff changeset
144 #endif
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 /* Preserve the access permissions. */
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
147 #if USE_ACL
16271
a9b070c205b2 copy-file: Refactor.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
148 switch (qcopy_acl (src_filename, src_fd, dest_filename, dest_fd, mode))
a9b070c205b2 copy-file: Refactor.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
149 {
a9b070c205b2 copy-file: Refactor.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
150 case -2:
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
151 err = GL_COPY_ERR_GET_ACL;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
152 goto error_src_dest;
16271
a9b070c205b2 copy-file: Refactor.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
153 case -1:
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
154 err = GL_COPY_ERR_SET_ACL;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
155 goto error_src_dest;
16271
a9b070c205b2 copy-file: Refactor.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
156 }
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
157 #else
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 chmod (dest_filename, mode);
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
159 #endif
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
160
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
161 #if USE_ACL
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
162 if (close (dest_fd) < 0)
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
163 {
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
164 err = GL_COPY_ERR_WRITE;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
165 goto error_src;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
166 }
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
167 if (close (src_fd) < 0)
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
168 {
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
169 err = GL_COPY_ERR_AFTER_READ;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
170 goto error;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
171 }
7742
4a26effdff28 Preserve ACLs while copying.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
172 #endif
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
173
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
174 return 0;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
175
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
176 error_src_dest:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
177 close (dest_fd);
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
178 error_src:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
179 close (src_fd);
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
180 error:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
181 return err;
4266
f9ad295c45bb New module copy-file.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 }
16273
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
183
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
184 void
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
185 copy_file_preserving (const char *src_filename, const char *dest_filename)
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
186 {
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
187 switch (qcopy_file_preserving (src_filename, dest_filename))
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
188 {
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
189 case 0:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
190 return;
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
191
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
192 case GL_COPY_ERR_OPEN_READ:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
193 error (EXIT_FAILURE, errno, _("error while opening %s for reading"),
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
194 quote (src_filename));
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
195
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
196 case GL_COPY_ERR_OPEN_BACKUP_WRITE:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
197 error (EXIT_FAILURE, errno, _("cannot open backup file %s for writing"),
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
198 quote (dest_filename));
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
199
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
200 case GL_COPY_ERR_READ:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
201 error (EXIT_FAILURE, errno, _("error reading %s"),
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
202 quote (src_filename));
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
203
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
204 case GL_COPY_ERR_WRITE:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
205 error (EXIT_FAILURE, errno, _("error writing %s"),
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
206 quote (dest_filename));
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
207
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
208 case GL_COPY_ERR_AFTER_READ:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
209 error (EXIT_FAILURE, errno, _("error after reading %s"),
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
210 quote (src_filename));
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
211
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
212 case GL_COPY_ERR_GET_ACL:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
213 error (EXIT_FAILURE, errno, "%s", quote (src_filename));
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
214
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
215 case GL_COPY_ERR_SET_ACL:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
216 error (EXIT_FAILURE, errno, _("preserving permissions for %s"),
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
217 quote (dest_filename));
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
218
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
219 default:
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
220 abort ();
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
221 }
d50152d6b5d2 copy-file: add error-code-returning variant.
Reuben Thomas <rrt@sc3d.org>
parents: 16272
diff changeset
222 }