Preserve ACLs while copying.
[gnulib.git] / lib / copy-file.c
1 /* Copying of files.
2    Copyright (C) 2001-2003, 2006 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 2, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include "copy-file.h"
24
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stddef.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 "exit.h"
45 #include "gettext.h"
46
47 #define _(str) gettext (str)
48
49 void
50 copy_file_preserving (const char *src_filename, const char *dest_filename)
51 {
52   int src_fd;
53   struct stat statbuf;
54   int mode;
55   int dest_fd;
56   char buf[4096];
57   const size_t buf_size = sizeof (buf);
58
59   src_fd = open (src_filename, O_RDONLY | O_BINARY);
60   if (src_fd < 0 || fstat (src_fd, &statbuf) < 0)
61     error (EXIT_FAILURE, errno, _("error while opening \"%s\" for reading"),
62            src_filename);
63
64   mode = statbuf.st_mode & 07777;
65
66   dest_fd = open (dest_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
67   if (dest_fd < 0)
68     error (EXIT_FAILURE, errno, _("cannot open backup file \"%s\" for writing"),
69            dest_filename);
70
71   /* Copy the file contents.  */
72   for (;;)
73     {
74       size_t n_read = safe_read (src_fd, buf, buf_size);
75       if (n_read == SAFE_READ_ERROR)
76         error (EXIT_FAILURE, errno, _("error reading \"%s\""), src_filename);
77       if (n_read == 0)
78         break;
79
80       if (full_write (dest_fd, buf, n_read) < n_read)
81         error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
82     }
83
84 #if !USE_ACL
85   if (close (dest_fd) < 0)
86     error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
87   if (close (src_fd) < 0)
88     error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename);
89 #endif
90
91   /* Preserve the access and modification times.  */
92 #if HAVE_UTIME
93   {
94     struct utimbuf ut;
95
96     ut.actime = statbuf.st_atime;
97     ut.modtime = statbuf.st_mtime;
98     utime (dest_filename, &ut);
99   }
100 #elif HAVE_UTIMES
101   {
102     struct timeval ut[2];
103
104     ut[0].tv_sec = statbuf.st_atime; ut[0].tv_usec = 0;
105     ut[1].tv_sec = statbuf.st_mtime; ut[1].tv_usec = 0;
106     utimes (dest_filename, &ut);
107   }
108 #endif
109
110 #if HAVE_CHOWN
111   /* Preserve the owner and group.  */
112   chown (dest_filename, statbuf.st_uid, statbuf.st_gid);
113 #endif
114
115   /* Preserve the access permissions.  */
116 #if USE_ACL
117   if (copy_acl (src_filename, src_fd, dest_filename, dest_fd, mode))
118     exit (EXIT_FAILURE);
119 #else
120   chmod (dest_filename, mode);
121 #endif
122
123 #if USE_ACL
124   if (close (dest_fd) < 0)
125     error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
126   if (close (src_fd) < 0)
127     error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename);
128 #endif
129 }