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