Use spaces for indentation, not tabs.
[gnulib.git] / lib / copy-file.c
1 /* Copying of files.
2    Copyright (C) 2001-2003, 2006-2007 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 "gettext.h"
45 #include "xalloc.h"
46
47 #define _(str) gettext (str)
48
49 /* The results of open() in this file are not used with fchdir,
50    therefore save some unnecessary work in fchdir.c.  */
51 #undef open
52 #undef close
53
54 enum { IO_SIZE = 32 * 1024 };
55
56 void
57 copy_file_preserving (const char *src_filename, const char *dest_filename)
58 {
59   int src_fd;
60   struct stat statbuf;
61   int mode;
62   int dest_fd;
63   char *buf = xmalloc (IO_SIZE);
64
65   src_fd = open (src_filename, O_RDONLY | O_BINARY);
66   if (src_fd < 0 || fstat (src_fd, &statbuf) < 0)
67     error (EXIT_FAILURE, errno, _("error while opening \"%s\" for reading"),
68            src_filename);
69
70   mode = statbuf.st_mode & 07777;
71
72   dest_fd = open (dest_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
73   if (dest_fd < 0)
74     error (EXIT_FAILURE, errno, _("cannot open backup file \"%s\" for writing"),
75            dest_filename);
76
77   /* Copy the file contents.  */
78   for (;;)
79     {
80       size_t n_read = safe_read (src_fd, buf, IO_SIZE);
81       if (n_read == SAFE_READ_ERROR)
82         error (EXIT_FAILURE, errno, _("error reading \"%s\""), src_filename);
83       if (n_read == 0)
84         break;
85
86       if (full_write (dest_fd, buf, n_read) < n_read)
87         error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
88     }
89
90   free (buf);
91
92 #if !USE_ACL
93   if (close (dest_fd) < 0)
94     error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
95   if (close (src_fd) < 0)
96     error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename);
97 #endif
98
99   /* Preserve the access and modification times.  */
100 #if HAVE_UTIME
101   {
102     struct utimbuf ut;
103
104     ut.actime = statbuf.st_atime;
105     ut.modtime = statbuf.st_mtime;
106     utime (dest_filename, &ut);
107   }
108 #elif HAVE_UTIMES
109   {
110     struct timeval ut[2];
111
112     ut[0].tv_sec = statbuf.st_atime; ut[0].tv_usec = 0;
113     ut[1].tv_sec = statbuf.st_mtime; ut[1].tv_usec = 0;
114     utimes (dest_filename, &ut);
115   }
116 #endif
117
118 #if HAVE_CHOWN
119   /* Preserve the owner and group.  */
120   chown (dest_filename, statbuf.st_uid, statbuf.st_gid);
121 #endif
122
123   /* Preserve the access permissions.  */
124 #if USE_ACL
125   if (copy_acl (src_filename, src_fd, dest_filename, dest_fd, mode))
126     exit (EXIT_FAILURE);
127 #else
128   chmod (dest_filename, mode);
129 #endif
130
131 #if USE_ACL
132   if (close (dest_fd) < 0)
133     error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
134   if (close (src_fd) < 0)
135     error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename);
136 #endif
137 }