Use a consistent style for including <config.h>.
[gnulib.git] / lib / copy-file.c
1 /* Copying of files.
2    Copyright (C) 2001-2003 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 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 /* Specification.  */
25 #include "copy-file.h"
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stddef.h>
30 #include <sys/stat.h>
31
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35
36 #if HAVE_UTIME || HAVE_UTIMES
37 # if HAVE_UTIME_H
38 #  include <utime.h>
39 # else
40 #  include <sys/utime.h>
41 # endif
42 #endif
43
44 #include "error.h"
45 #include "safe-read.h"
46 #include "full-write.h"
47 #include "binary-io.h"
48 #include "exit.h"
49 #include "gettext.h"
50
51 #define _(str) gettext (str)
52
53 void
54 copy_file_preserving (const char *src_filename, const char *dest_filename)
55 {
56   int src_fd;
57   struct stat statbuf;
58   int mode;
59   int dest_fd;
60   char buf[4096];
61   const size_t buf_size = sizeof (buf);
62
63   src_fd = open (src_filename, O_RDONLY | O_BINARY);
64   if (src_fd < 0 || fstat (src_fd, &statbuf) < 0)
65     error (EXIT_FAILURE, errno, _("error while opening \"%s\" for reading"),
66            src_filename);
67
68   mode = statbuf.st_mode & 07777;
69
70   dest_fd = open (dest_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
71   if (dest_fd < 0)
72     error (EXIT_FAILURE, errno, _("cannot open backup file \"%s\" for writing"),
73            dest_filename);
74
75   /* Copy the file contents.  */
76   for (;;)
77     {
78       size_t n_read = safe_read (src_fd, buf, buf_size);
79       if (n_read == SAFE_READ_ERROR)
80         error (EXIT_FAILURE, errno, _("error reading \"%s\""), src_filename);
81       if (n_read == 0)
82         break;
83
84       if (full_write (dest_fd, buf, n_read) < n_read)
85         error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
86     }
87
88   if (close (dest_fd) < 0)
89     error (EXIT_FAILURE, errno, _("error writing \"%s\""), dest_filename);
90   if (close (src_fd) < 0)
91     error (EXIT_FAILURE, errno, _("error after reading \"%s\""), src_filename);
92
93   /* Preserve the access and modification times.  */
94 #if HAVE_UTIME
95   {
96     struct utimbuf ut;
97
98     ut.actime = statbuf.st_atime;
99     ut.modtime = statbuf.st_mtime;
100     utime (dest_filename, &ut);
101   }
102 #elif HAVE_UTIMES
103   {
104     struct timeval ut[2];
105
106     ut[0].tv_sec = statbuf.st_atime; ut[0].tv_usec = 0;
107     ut[1].tv_sec = statbuf.st_mtime; ut[1].tv_usec = 0;
108     utimes (dest_filename, &ut);
109   }
110 #endif
111
112 #if HAVE_CHOWN
113   /* Preserve the owner and group.  */
114   chown (dest_filename, statbuf.st_uid, statbuf.st_gid);
115 #endif
116
117   /* Preserve the access permissions.  */
118   chmod (dest_filename, mode);
119 }