summaryrefslogtreecommitdiff
path: root/libmailutils/base/copyfile.c
blob: 246e2c8a13200cbd96ba1e151f1c0a8b3e2bfd4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2016-2021 Free Software Foundation, Inc.

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Mailutils is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.h>
#include <mailutils/stream.h>
#include <mailutils/util.h>
#include <mailutils/diag.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/nls.h>

static int copy_regular_file (const char *srcpath, const char *dstpath,
			      int flags, struct stat *st);
static int copy_symlink (const char *srcpath, const char *dstpath);
static int copy_dir (const char *srcpath, const char *dstpath, int flags);

/* Copy SRCPATH to DSTPATH.  SRCPATH can be any kind of file.  If it is
   a directory, its content will be copied recursively.

   FLAGS:

   MU_COPY_OVERWRITE      Overwrite destination file, if it exists.
   MU_COPY_MODE           Preserve file mode
   MU_COPY_OWNER          Preserve file ownership 
   MU_COPY_DEREF          Dereference symbolic links: operate on files they
                          refer to.
*/
int
mu_copy_file (const char *srcpath, const char *dstpath, int flags)
{
  int rc = 0;
  struct stat st;

  if (((flags & MU_COPY_DEREF) ? stat : lstat) (srcpath, &st))
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("can't stat file %s: %s"),
		 srcpath, mu_strerror (errno)));
      return errno;
    }

  if (access (dstpath, F_OK) == 0)
    {
      if (flags & MU_COPY_OVERWRITE)
	{
	  rc = mu_remove_file (dstpath);
	  if (rc)
	    {
	      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
			(_("can't remove destination %s: %s"),
			 dstpath, mu_strerror (rc)));
	      return rc;
	    }
	}
      else
	return EEXIST;
    }
  
  switch (st.st_mode & S_IFMT)
    {
    case S_IFREG:
      return copy_regular_file (srcpath, dstpath, flags, &st);

    case S_IFLNK:
      return copy_symlink (srcpath, dstpath);

    case S_IFDIR:
      return copy_dir (srcpath, dstpath, flags);

    case S_IFBLK:
    case S_IFCHR:
      if (mknod (dstpath, st.st_mode & 0777, st.st_dev))
	{
	  rc = errno;
	  mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		    (_("%s: cannot create node: %s"),
		     dstpath,
		     mu_strerror (rc)));
	}
      break;
      
    case S_IFIFO:
      if (mkfifo (dstpath, st.st_mode & 0777))
	{
	  rc = errno;
	  mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		    (_("%s: cannot create node: %s"),
		     dstpath,
		     mu_strerror (rc)));
	}
      break;

    default:
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("%s: don't know how to copy file of that type"),
		 srcpath));
      return ENOTSUP;
    }

  return rc;
}
      
static int
copy_regular_file (const char *srcpath, const char *dstpath, int flags,
		   struct stat *st)
{
  int rc;
  mu_stream_t src, dst;
  mode_t mask, mode;

  rc = mu_file_stream_create (&src, srcpath, MU_STREAM_READ);
  if (rc)
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("cannot open source file %s: %s"),
		 srcpath, mu_strerror (rc)));
      return rc;
    }

  mask = umask (077);
  mode = ((flags & MU_COPY_MODE) ? st->st_mode : (0666 & ~mask)) & 0777;

  rc = mu_file_stream_create (&dst, dstpath, MU_STREAM_CREAT|MU_STREAM_WRITE);
  umask (mask);
  if (rc)
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("cannot open destination file %s: %s"),
		 dstpath, mu_strerror (rc)));
      mu_stream_destroy (&src);
      return rc;
    }

  rc = mu_stream_copy (dst, src, 0, NULL);
  if (rc)
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("failed to copy %s to %s: %s"),
		 srcpath, dstpath, mu_strerror (rc)));
    }
  else 
    {
      mu_transport_t trans[2];

      rc = mu_stream_ioctl (dst, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET, trans);
      if (rc == 0)
	{	    
	  if (fchmod ((int) (intptr_t) trans[0], mode))
	    {
	      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
			(_("%s: cannot chmod: %s"),
			 dstpath, mu_strerror (errno)));
	      rc = MU_ERR_RESTORE_META;
	    }
	  else if (flags & MU_COPY_OWNER)
	    {
	      uid_t uid;
	      gid_t gid;

	      if (getuid () == 0)
		{
		  uid = st->st_uid;
		  gid = st->st_gid;
		}
	      else if (getuid () == st->st_uid)
		{
		  uid = -1;
		  gid = st->st_gid;
		}
	      else
		{
		  uid = -1;
		  gid = -1;
		}

	      if (gid != -1)
		{
		  if (fchown ((int) (intptr_t) trans[0], uid, gid))
		    {
		      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
				(_("%s: cannot chown to %lu.%lu: %s"),
				 dstpath,
				 (unsigned long) uid,
				 (unsigned long) gid,
				 mu_strerror (errno)));
		      rc = MU_ERR_RESTORE_META;
		    }
		}
	    }
	}
      else
	{
	  mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		    (_("can't change file mode and ownership after copying %s to %s;"
		       " cannot get file handle: %s"),
		     srcpath, dstpath,
		     mu_strerror (rc)));
	}      
    }
  
  mu_stream_destroy (&src);
  mu_stream_destroy (&dst);
  
  return rc;
}

static int
copy_symlink (const char *srcpath, const char *dstpath)
{
  int rc;
  char *buf = NULL;
  size_t size = 0;
  
  rc = mu_readlink (srcpath, &buf, &size, NULL);
  if (rc)
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("%s: cannot read link: %s"),
		 srcpath, mu_strerror (rc)));
      return rc;
    }

  if (symlink (buf, dstpath))
    {
      rc = errno;
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("%s: can't link %s to %s: %s"),
		 srcpath, buf, dstpath, mu_strerror (rc)));
    }
  free (buf);
  return rc;
}
  
static int
copy_dir (const char *srcpath, const char *dstpath, int flags)
{
  DIR *dirp;
  struct dirent *dp;
  struct stat st;
  int rc;
  mode_t mode, mask;
  
  if (stat (srcpath, &st))
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("can't stat file %s: %s"),
		 srcpath, mu_strerror (errno)));
      return errno;
    }

  mask = umask (077);
  mode = ((flags & MU_COPY_MODE) ? st.st_mode : (0777 & ~mask)) & 0777;
  
  rc = mkdir (dstpath, 0700);
  umask (mask);
	  
  if (rc)
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("can't create directory %s: %s"),
		 dstpath, mu_strerror (errno)));
      return errno;
    }
  
  dirp = opendir (srcpath);
  if (dirp == NULL)
    {
      rc = errno;
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		("cannot open directory %s: %s",
		 srcpath, mu_strerror (errno)));
      return rc;
    }

  while ((dp = readdir (dirp)))
    {
      char const *ename = dp->d_name;
      char *src, *dst;
      
      if (ename[ename[0] != '.' ? 0 : ename[1] != '.' ? 1 : 2] == 0)
	continue;

      src = mu_make_file_name (srcpath, ename);
      dst = mu_make_file_name (dstpath, ename);
      rc = mu_copy_file (src, dst, flags);
      free (dst);
      free (src);

      if (rc)
	break;
    }
  closedir (dirp);

  if (chmod (dstpath, mode))
    {
      rc = errno;
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("%s: cannot chmod: %s"),
		 dstpath, mu_strerror (rc)));
    }
  else if (flags & MU_COPY_OWNER)
    {
      uid_t uid;
      gid_t gid;
	      
      if (getuid () == 0)
	{
	  uid = st.st_uid;
	  gid = st.st_gid;
	}
      else if (getuid () == st.st_uid)
	{
	  uid = -1;
	  gid = st.st_gid;
	}
      else
	{
	  uid = -1;
	  gid = -1;
	}
      
      if (gid != -1)
	{
	  if (chown (dstpath, uid, gid))
	    {
	      rc = errno;
	      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
			(_("%s: cannot chown to %lu.%lu: %s"),
			 dstpath,
			 (unsigned long) uid,
			 (unsigned long) gid,
			 mu_strerror (rc)));
	    }
	}
    }
  return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.