2 Copyright (C) 2001-2003, 2006-2007, 2009-2012 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "copy-file.h"
31 #if HAVE_UTIME || HAVE_UTIMES
35 # include <sys/utime.h>
40 #include "safe-read.h"
41 #include "full-write.h"
43 #include "binary-io.h"
48 #define _(str) gettext (str)
50 /* The results of open() in this file are not used with fchdir,
51 therefore save some unnecessary work in fchdir.c. */
55 enum { IO_SIZE = 32 * 1024 };
58 qcopy_file_preserving (const char *src_filename, const char *dest_filename)
65 char *buf = xmalloc (IO_SIZE);
67 src_fd = open (src_filename, O_RDONLY | O_BINARY);
70 err = GL_COPY_ERR_OPEN_READ;
73 if (fstat (src_fd, &statbuf) < 0)
75 err = GL_COPY_ERR_OPEN_READ;
79 mode = statbuf.st_mode & 07777;
81 dest_fd = open (dest_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
84 err = GL_COPY_ERR_OPEN_BACKUP_WRITE;
88 /* Copy the file contents. */
91 size_t n_read = safe_read (src_fd, buf, IO_SIZE);
92 if (n_read == SAFE_READ_ERROR)
94 err = GL_COPY_ERR_READ;
100 if (full_write (dest_fd, buf, n_read) < n_read)
102 err = GL_COPY_ERR_WRITE;
110 if (close (dest_fd) < 0)
112 err = GL_COPY_ERR_WRITE;
115 if (close (src_fd) < 0)
117 err = GL_COPY_ERR_AFTER_READ;
122 /* Preserve the access and modification times. */
127 ut.actime = statbuf.st_atime;
128 ut.modtime = statbuf.st_mtime;
129 utime (dest_filename, &ut);
133 struct timeval ut[2];
135 ut[0].tv_sec = statbuf.st_atime; ut[0].tv_usec = 0;
136 ut[1].tv_sec = statbuf.st_mtime; ut[1].tv_usec = 0;
137 utimes (dest_filename, &ut);
142 /* Preserve the owner and group. */
143 chown (dest_filename, statbuf.st_uid, statbuf.st_gid);
146 /* Preserve the access permissions. */
148 switch (qcopy_acl (src_filename, src_fd, dest_filename, dest_fd, mode))
151 err = GL_COPY_ERR_GET_ACL;
154 err = GL_COPY_ERR_SET_ACL;
158 chmod (dest_filename, mode);
162 if (close (dest_fd) < 0)
164 err = GL_COPY_ERR_WRITE;
167 if (close (src_fd) < 0)
169 err = GL_COPY_ERR_AFTER_READ;
185 copy_file_preserving (const char *src_filename, const char *dest_filename)
187 switch (qcopy_file_preserving (src_filename, dest_filename))
192 case GL_COPY_ERR_OPEN_READ:
193 error (EXIT_FAILURE, errno, _("error while opening %s for reading"),
194 quote (src_filename));
196 case GL_COPY_ERR_OPEN_BACKUP_WRITE:
197 error (EXIT_FAILURE, errno, _("cannot open backup file %s for writing"),
198 quote (dest_filename));
200 case GL_COPY_ERR_READ:
201 error (EXIT_FAILURE, errno, _("error reading %s"),
202 quote (src_filename));
204 case GL_COPY_ERR_WRITE:
205 error (EXIT_FAILURE, errno, _("error writing %s"),
206 quote (dest_filename));
208 case GL_COPY_ERR_AFTER_READ:
209 error (EXIT_FAILURE, errno, _("error after reading %s"),
210 quote (src_filename));
212 case GL_COPY_ERR_GET_ACL:
213 error (EXIT_FAILURE, errno, "%s", quote (src_filename));
215 case GL_COPY_ERR_SET_ACL:
216 error (EXIT_FAILURE, errno, _("preserving permissions for %s"),
217 quote (dest_filename));