maint: update copyright
[gnulib.git] / lib / copy-file.c
1 /* Copying of files.
2    Copyright (C) 2001-2003, 2006-2007, 2009-2014 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 "ignore-value.h"
41 #include "safe-read.h"
42 #include "full-write.h"
43 #include "acl.h"
44 #include "binary-io.h"
45 #include "quote.h"
46 #include "gettext.h"
47 #include "xalloc.h"
48
49 #define _(str) gettext (str)
50
51 /* The results of open() in this file are not used with fchdir,
52    therefore save some unnecessary work in fchdir.c.  */
53 #undef open
54 #undef close
55
56 enum { IO_SIZE = 32 * 1024 };
57
58 int
59 qcopy_file_preserving (const char *src_filename, const char *dest_filename)
60 {
61   int err = 0;
62   int src_fd;
63   struct stat statbuf;
64   int mode;
65   int dest_fd;
66   char *buf = xmalloc (IO_SIZE);
67
68   src_fd = open (src_filename, O_RDONLY | O_BINARY);
69   if (src_fd < 0)
70     {
71       err = GL_COPY_ERR_OPEN_READ;
72       goto error;
73     }
74   if (fstat (src_fd, &statbuf) < 0)
75     {
76       err = GL_COPY_ERR_OPEN_READ;
77       goto error_src;
78     }
79
80   mode = statbuf.st_mode & 07777;
81
82   dest_fd = open (dest_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
83   if (dest_fd < 0)
84     {
85       err = GL_COPY_ERR_OPEN_BACKUP_WRITE;
86       goto error_src;
87     }
88
89   /* Copy the file contents.  */
90   for (;;)
91     {
92       size_t n_read = safe_read (src_fd, buf, IO_SIZE);
93       if (n_read == SAFE_READ_ERROR)
94         {
95           err = GL_COPY_ERR_READ;
96           goto error_src_dest;
97         }
98       if (n_read == 0)
99         break;
100
101       if (full_write (dest_fd, buf, n_read) < n_read)
102         {
103           err = GL_COPY_ERR_WRITE;
104           goto error_src_dest;
105         }
106     }
107
108   free (buf);
109
110 #if !USE_ACL
111   if (close (dest_fd) < 0)
112     {
113       err = GL_COPY_ERR_WRITE;
114       goto error_src;
115     }
116   if (close (src_fd) < 0)
117     {
118       err = GL_COPY_ERR_AFTER_READ;
119       goto error;
120     }
121 #endif
122
123   /* Preserve the access and modification times.  */
124 #if HAVE_UTIME
125   {
126     struct utimbuf ut;
127
128     ut.actime = statbuf.st_atime;
129     ut.modtime = statbuf.st_mtime;
130     utime (dest_filename, &ut);
131   }
132 #elif HAVE_UTIMES
133   {
134     struct timeval ut[2];
135
136     ut[0].tv_sec = statbuf.st_atime; ut[0].tv_usec = 0;
137     ut[1].tv_sec = statbuf.st_mtime; ut[1].tv_usec = 0;
138     utimes (dest_filename, &ut);
139   }
140 #endif
141
142 #if HAVE_CHOWN
143   /* Preserve the owner and group.  */
144   ignore_value (chown (dest_filename, statbuf.st_uid, statbuf.st_gid));
145 #endif
146
147   /* Preserve the access permissions.  */
148 #if USE_ACL
149   switch (qcopy_acl (src_filename, src_fd, dest_filename, dest_fd, mode))
150     {
151     case -2:
152       err = GL_COPY_ERR_GET_ACL;
153       goto error_src_dest;
154     case -1:
155       err = GL_COPY_ERR_SET_ACL;
156       goto error_src_dest;
157     }
158 #else
159   chmod (dest_filename, mode);
160 #endif
161
162 #if USE_ACL
163   if (close (dest_fd) < 0)
164     {
165       err = GL_COPY_ERR_WRITE;
166       goto error_src;
167     }
168   if (close (src_fd) < 0)
169     {
170       err = GL_COPY_ERR_AFTER_READ;
171       goto error;
172     }
173 #endif
174
175   return 0;
176
177  error_src_dest:
178   close (dest_fd);
179  error_src:
180   close (src_fd);
181  error:
182   return err;
183 }
184
185 void
186 copy_file_preserving (const char *src_filename, const char *dest_filename)
187 {
188   switch (qcopy_file_preserving (src_filename, dest_filename))
189     {
190     case 0:
191       return;
192
193     case GL_COPY_ERR_OPEN_READ:
194       error (EXIT_FAILURE, errno, _("error while opening %s for reading"),
195              quote (src_filename));
196
197     case GL_COPY_ERR_OPEN_BACKUP_WRITE:
198       error (EXIT_FAILURE, errno, _("cannot open backup file %s for writing"),
199              quote (dest_filename));
200
201     case GL_COPY_ERR_READ:
202       error (EXIT_FAILURE, errno, _("error reading %s"),
203              quote (src_filename));
204
205     case GL_COPY_ERR_WRITE:
206       error (EXIT_FAILURE, errno, _("error writing %s"),
207              quote (dest_filename));
208
209     case GL_COPY_ERR_AFTER_READ:
210       error (EXIT_FAILURE, errno, _("error after reading %s"),
211              quote (src_filename));
212
213     case GL_COPY_ERR_GET_ACL:
214       error (EXIT_FAILURE, errno, "%s", quote (src_filename));
215
216     case GL_COPY_ERR_SET_ACL:
217       error (EXIT_FAILURE, errno, _("preserving permissions for %s"),
218              quote (dest_filename));
219
220     default:
221       abort ();
222     }
223 }