copy-file: Use 'quote' module consistently.
[gnulib.git] / lib / copy-file.c
1 /* Copying of files.
2    Copyright (C) 2001-2003, 2006-2007, 2009-2012 Free Software Foundation, Inc.
3    Written by Bruno Haible <haible@clisp.cons.org>, 2001.
4
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.
9
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.
14
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/>.  */
17
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "copy-file.h"
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #if HAVE_UTIME || HAVE_UTIMES
32 # if HAVE_UTIME_H
33 #  include <utime.h>
34 # else
35 #  include <sys/utime.h>
36 # endif
37 #endif
38
39 #include "error.h"
40 #include "safe-read.h"
41 #include "full-write.h"
42 #include "acl.h"
43 #include "binary-io.h"
44 #include "quote.h"
45 #include "gettext.h"
46 #include "xalloc.h"
47
48 #define _(str) gettext (str)
49
50 /* The results of open() in this file are not used with fchdir,
51    therefore save some unnecessary work in fchdir.c.  */
52 #undef open
53 #undef close
54
55 enum { IO_SIZE = 32 * 1024 };
56
57 void
58 copy_file_preserving (const char *src_filename, const char *dest_filename)
59 {
60   int src_fd;
61   struct stat statbuf;
62   int mode;
63   int dest_fd;
64   char *buf = xmalloc (IO_SIZE);
65
66   src_fd = open (src_filename, O_RDONLY | O_BINARY);
67   if (src_fd < 0 || fstat (src_fd, &statbuf) < 0)
68     error (EXIT_FAILURE, errno, _("error while opening %s for reading"),
69            quote (src_filename));
70
71   mode = statbuf.st_mode & 07777;
72
73   dest_fd = open (dest_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
74   if (dest_fd < 0)
75     error (EXIT_FAILURE, errno, _("cannot open backup file %s for writing"),
76            quote (dest_filename));
77
78   /* Copy the file contents.  */
79   for (;;)
80     {
81       size_t n_read = safe_read (src_fd, buf, IO_SIZE);
82       if (n_read == SAFE_READ_ERROR)
83         error (EXIT_FAILURE, errno, _("error reading %s"),
84                quote (src_filename));
85       if (n_read == 0)
86         break;
87
88       if (full_write (dest_fd, buf, n_read) < n_read)
89         error (EXIT_FAILURE, errno, _("error writing %s"),
90                quote (est_filename));
91     }
92
93   free (buf);
94
95 #if !USE_ACL
96   if (close (dest_fd) < 0)
97     error (EXIT_FAILURE, errno, _("error writing %s"), quote (dest_filename));
98   if (close (src_fd) < 0)
99     error (EXIT_FAILURE, errno, _("error after reading %s"),
100            quote (src_filename));
101 #endif
102
103   /* Preserve the access and modification times.  */
104 #if HAVE_UTIME
105   {
106     struct utimbuf ut;
107
108     ut.actime = statbuf.st_atime;
109     ut.modtime = statbuf.st_mtime;
110     utime (dest_filename, &ut);
111   }
112 #elif HAVE_UTIMES
113   {
114     struct timeval ut[2];
115
116     ut[0].tv_sec = statbuf.st_atime; ut[0].tv_usec = 0;
117     ut[1].tv_sec = statbuf.st_mtime; ut[1].tv_usec = 0;
118     utimes (dest_filename, &ut);
119   }
120 #endif
121
122 #if HAVE_CHOWN
123   /* Preserve the owner and group.  */
124   chown (dest_filename, statbuf.st_uid, statbuf.st_gid);
125 #endif
126
127   /* Preserve the access permissions.  */
128 #if USE_ACL
129   switch (qcopy_acl (src_filename, src_fd, dest_filename, dest_fd, mode))
130     {
131     case -2:
132       error (EXIT_FAILURE, errno, "%s", quote (src_filename));
133     case -1:
134       error (EXIT_FAILURE, errno, _("preserving permissions for %s"),
135              quote (dest_filename));
136     }
137 #else
138   chmod (dest_filename, mode);
139 #endif
140
141 #if USE_ACL
142   if (close (dest_fd) < 0)
143     error (EXIT_FAILURE, errno, _("error writing %s"), quote (dest_filename));
144   if (close (src_fd) < 0)
145     error (EXIT_FAILURE, errno, _("error after reading %s"),
146            quote (src_filename));
147 #endif
148 }