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