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