summaryrefslogtreecommitdiffabout
path: root/src/diskio.c
blob: a86997eace6ed0f3372fcbbd5cb928faf67f92a9 (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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2017, 2019
   Sergey Poznyakoff

   Wydawca 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 of the License, or (at your
   option) any later version.

   Wydawca 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 wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"

/* Return true if ARG is NULL or is a sub-directory of DIR */
int
sub_dir_p(const char *arg, const char *dir)
{
	int dlen;

	if (!arg)
		return 0;

	dlen = strlen(dir);

	return strlen(arg) > dlen
		&& memcmp(dir, arg, dlen) == 0 && arg[dlen] == '/';
}

/* Concatenate BASE directory name, a single directory separator (/) and
   NAME. Return in PBASELEN the length of BASE, not counting eventual trailing
   slash. */
char *
concat_dir(const char *base, const char *name, size_t * pbaselen)
{
	size_t len = strlen(base);
	size_t size;
	char *dir;

	while (len > 0 && base[len - 1] == '/')
		len--;

	size = len + 1 + strlen(name);
	dir = grecs_malloc(size + 1);
	memcpy(dir, base, len);
	dir[len++] = '/';
	strcpy(dir + len, name);

	if (pbaselen)
		*pbaselen = len;
	return dir;
}

/* Create the directory DIR, eventually creating all intermediate directories
   starting from DIR + BASELEN. */
int
create_hierarchy(char const *dir, size_t baselen)
{
	int rc;
	struct stat st;
	char *p;

	if (stat(dir, &st) == 0) {
		if (!S_ISDIR(st.st_mode)) {
			wy_log(LOG_ERR, _("component %s is not a directory"),
			       dir);
			return 1;
		}
		return 0;
	} else if (errno != ENOENT) {
		wy_log(LOG_ERR, _("cannot stat file %s: %s"), dir,
		       strerror(errno));
		return 1;
	}

	p = strrchr(dir, '/');
	if (p) {
		if (p - dir + 1 < baselen) {
			wy_log(LOG_ERR, _("base directory %s does not exist"),
			       dir);
			return 1;
		}
		*p = 0;
		rc = create_hierarchy(dir, baselen);
	} else {
		/* Current directory always exists */
		rc = 0;
	}

	if (rc == 0) {
		if (p)
			*p = '/';
		if (mkdir(dir, MKDIR_PERMISSIONS)) {
			wy_log(LOG_ERR, _("cannot create directory %s: %s"),
			       dir, strerror(errno));
			rc = 1;
		}
	}
	return rc;
}

/* Create a directory BASE/NAME (with eventual intermediate directories in
   NAME). 
   Do nothing if wy_dry_run is set. */
char *
create_directory(const char *base, const char *name)
{
	size_t baselen;
	char *dir = concat_dir(base, name, &baselen);

	if (!wy_dry_run) {
		if (create_hierarchy(dir, baselen)) {
			free(dir);
			dir = NULL;
		}
	}
	return dir;
}

/* Copy FILE to DST_FILE. */
int
copy_file(const char *file, const char *dst_file)
{
	int in_fd, out_fd;
	struct stat st;
	int rc;
	char *buf;
	size_t bufsize;
	size_t fsize;

	in_fd = open(file, O_RDONLY);

	if (in_fd == -1) {
		wy_log(LOG_ERR,
		       _("cannot open source file %s for reading: %s"),
		       file, strerror(errno));
		return 1;
	}

	if (fstat(in_fd, &st)) {
		wy_log(LOG_ERR,
		       _("cannot stat source file %s: %s"),
		       file, strerror(errno));
		close(in_fd);
		return 1;
	}

	out_fd = creat(dst_file, CREAT_PERMISSIONS);
	if (out_fd == -1) {
		wy_log(LOG_ERR,
		       _("cannot create destination file %s: %s"),
		       dst_file, strerror(errno));
		close(in_fd);
		return 1;
	}

	buf = NULL;
	fsize = st.st_size;

	for (bufsize = fsize; bufsize > 0 && (buf = malloc(bufsize)) == NULL;
	     bufsize /= 2) ;
	if (bufsize == 0)
		grecs_alloc_die();

	rc = 0;
	while (fsize > 0) {
		size_t rest;
		size_t rdbytes;

		rest = fsize > bufsize ? bufsize : fsize;
		rdbytes = read(in_fd, buf, rest);
		if (rdbytes == -1) {
			wy_log(LOG_ERR, _("unexpected error reading %s: %s"),
			       file, strerror(errno));
			rc = 1;
			break;
		}
		rest = write(out_fd, buf, rdbytes);
		if (rest == -1) {
			wy_log(LOG_ERR,
			       _("unexpected error writing to %s: %s"),
			       dst_file, strerror(errno));
			rc = 1;
			break;
		} else if (rest != rdbytes) {
			wy_log(LOG_ERR, _("short write on %s"), dst_file);
			rc = 1;
		}
		fsize -= rdbytes;
	}
	free(buf);

	close(in_fd);
	close(out_fd);
	if (rc)
		unlink(dst_file);
	return rc;
}

/* Move FILE to DST_FILE. If they reside on different devices, use copy_file
   + unlink. */
int
do_move_file(const char *file, const char *dst_file)
{
	int rc = 0;

	if (rename(file, dst_file)) {
		if (errno == EXDEV) {
			if (copy_file(file, dst_file)) {
				wy_log(LOG_CRIT, _("cannot copy %s to %s: %s"),
				       file, dst_file, strerror(errno));
				rc = 1;
			} else if (unlink(file)) {
				wy_log(LOG_ERR, _("cannot unlink %s: %s"),
				       file, strerror(errno));
			}
		} else {
			wy_log(LOG_CRIT, _("cannot move %s to %s: %s"),
			       file, dst_file, strerror(errno));
			rc = 1;
		}
	}
	return rc;
}

/* Append the FILE to the tar ARCHIVE.
   Do nothing if wy_dry_run is set. */
int
tar_append_file(const char *archive, const char *file)
{
	const char *argv[6];

	wy_debug(1, (_("tarring %s to %s"), file, archive));
	if (wy_dry_run) {
		UPDATE_STATS(WY_STAT_ARCHIVES);
		return 0;
	}

	argv[0] = tar_command_name;
	argv[1] = "-f";
	argv[2] = archive;
	argv[3] = "-r";
	argv[4] = file;
	argv[5] = NULL;

	switch (wydawca_exec(6, argv, NULL)) {
	case exec_success:
		UPDATE_STATS(WY_STAT_ARCHIVES);
		return 0;

	case exec_fail:
	case exec_error:
		wy_log(LOG_ERR, _("cannot archive %s"), file);
		break;
	}

	return 1;
}

/* Backup a file to a directory.
   Arguments:
     DST_FILE        - File nam to back up.
     DST_DIR         - Directory part of DST_FILE.
     FILE            - File part of DST_FILE; can contain subdirs.
     ARCHIVE         - Archive descriptor.
     RELDIR          - Directory part of FILE

   Do nothing if wy_dry_run is set.    */
int
backup_file(const char *dst_file, const char *dst_dir, const char *file,
	    const struct archive_descr *archive, const char *reldir)
{
	int rc = 0;
	char *adir;
	char *file_name;

	if (archive->name[0] == '/')
		adir = create_directory(archive->name, reldir);
	else
		adir = create_directory(dst_dir, archive->name);
	if (!adir)
		return 1;

	file_name = concat_dir(adir, file, NULL);
	if (access(file_name, F_OK) == 0) {
		if (archive->backup_type == no_backups) {
			wy_debug(1, (_("removing previous archive "
				       "file `%s'"),
				     file_name));
			if (!wy_dry_run && unlink(file_name)) {
				wy_log(LOG_ERR,
				       _("cannot unlink previous archive "
					 "file `%s': %s"),
				       file_name, strerror(errno));
				free(file_name);
				free(adir);
				return 1;
			}
		} else {
			char *archive_file_name =
			    find_backup_file_name(file_name,
						  archive->backup_type);
			wy_debug(1, (_("backing up previous archive "
				       "file `%s' to `%s'"),
				     file_name, archive_file_name));
			if (!wy_dry_run) {
				rc = do_move_file(file_name,
						  archive_file_name);
				if (rc) {
					wy_log(LOG_ERR,
				   _("backing `%s' up as `%s' failed: %s"),
					       file_name, archive_file_name,
					       strerror(errno));
					free(archive_file_name);
					free(file_name);
					free(adir);
					return 1;
				}
			}
			free(archive_file_name);
		}
	}

	wy_debug(1, (_("archiving `%s' to `%s'"), dst_file, file_name));
	if (!wy_dry_run) {
		rc = do_move_file(dst_file, file_name);
		if (rc)
			wy_log(LOG_ERR, _("archiving `%s' as `%s' failed: %s"),
			       dst_file, file_name, strerror(errno));
	}
	free(file_name);
	free(adir);
	return rc;
}

/* Select the appropriate backup type and backup a file. See backup_file
   for the argument description. */
int
do_archive_file(const char *dst_file, const char *dst_dir, const char *file,
		const struct archive_descr *archive, const char *reldir)
{
	switch (archive->type) {
	case archive_none:
		break;

	case archive_directory:
		if (backup_file(dst_file, dst_dir, file, archive, reldir))
			return 1;
		UPDATE_STATS(WY_STAT_ARCHIVES);
		break;

	case archive_tar:
		if (tar_append_file(archive->name, dst_file))
			return 1;
		UPDATE_STATS(WY_STAT_ARCHIVES);
		break;
	}

	if (!wy_dry_run && unlink(dst_file) && errno != ENOENT) {
		wy_log(LOG_ERR, _("canot unlink file `%s': %s"),
		       dst_file, strerror(errno));
		return 1;
	}
	return 0;
}

static int
replace_allowed_p(struct wy_triplet *trp)
{
	const char *val;

	if (trp->version < 102)
		return 1;

	if (directive_get_value(trp, "replace", &val))
		return 1;
	return strcmp(val, "true") == 0;
}

const char *
dir_get_path(struct spool *sp)
{
	return sp->source_dir;
}

/* Move the part FILE_ID of the triplet TRP between the directories in
   TRP->SPOOL. TRP->RELATIVE_DIR gives relative directory (i.e. the
   directory part of the file name) for backup purposes.

   Do nothing if wy_dry_run is set. */
int
dir_move_file(struct wy_triplet *trp, enum file_type file_id)
{
	char *dst_file;
	int rc = 0;
	const struct spool *spool = trp->spool;
	char *dst_dir = create_directory(spool->dest_dir, trp->relative_dir);

	if (!dst_dir)
		return 1;
	dst_file = concat_dir(dst_dir, trp->file[file_id].name, NULL);

	wy_debug(1, (_("installing %s to %s"),
		     trp->file[file_id].name, dst_dir));

	if (access(dst_file, F_OK) == 0) {
		if (replace_allowed_p(trp))
			rc = do_archive_file(dst_file, dst_dir,
					     trp->file[file_id].name,
					     &spool->archive,
					     trp->relative_dir);
		else {
			wy_log(LOG_ERR,
			       _("refusing to upload %s because it already "
				 "exists and replace is not allowed"),
			       trp->file[file_id].name);
			free(dst_file);
			free(dst_dir);
			return 1;
		}
	}

	if (!wy_dry_run && rc == 0)
		rc = do_move_file(trp->file[file_id].name, dst_file);

	free(dst_file);
	free(dst_dir);
	if (rc == 0)
		UPDATE_STATS(WY_STAT_UPLOADS);
	return rc;
}

/* Archive the file FILE_NAME, located in DPAIR->dest_dir, and remove the
   file. Get user IDs from the triplet TRP.
   Do nothing if wy_dry_run is set.
*/
int
archive_single_file(struct wy_triplet *trp, const char *file_name,
		    int noentok)
{
	char *dst_file;
	int rc = 0;
	const struct spool *spool = trp->spool;
	char *dst_dir = create_directory(spool->dest_dir, trp->relative_dir);

	if (!dst_dir)
		return 1;

	dst_file = safe_file_name(concat_dir(dst_dir, file_name, NULL));
	if (!sub_dir_p(dst_file, spool->dest_dir)) {
		wy_log(LOG_ERR,
		       _("file to be archived `%s' does not lie under `%s'"),
		       dst_file, spool->dest_dir);
		free(dst_file);
		free(dst_dir);
		return 1;
	}

	if (access(dst_file, F_OK) == 0) {
		wy_debug(1, (_("archiving file `%s'"), dst_file));
		rc = do_archive_file(dst_file, dst_dir, file_name,
				     &spool->archive, trp->relative_dir);
	} else if (errno == ENOENT) {
		if (!noentok)
			wy_log(LOG_NOTICE,
			       _("nothing to archive: "
				 "file `%s' does not exist"),
			       dst_file);
	} else {
		wy_log(LOG_ERR, _("canot access file `%s': %s"),
		       dst_file, strerror(errno));
		rc = 1;
	}

	free(dst_file);
	free(dst_dir);
	return rc;
}

static char *
make_signame(const char *file_name)
{
	size_t len;

	if (((len = strlen(file_name)) > SUF_SIG_LEN
	     && memcmp(file_name + len - SUF_SIG_LEN, SUF_SIG, SUF_SIG_LEN))) {
		char *signame = grecs_malloc(len + SUF_SIG_LEN + 1);
		strcpy(signame, file_name);
		return strcat(signame, SUF_SIG);
	}
	return NULL;
}

/* Archive the file FILE_NAME, located in DPAIR->dest_dir, and remove the
   file. Get user IDs from the triplet TRP. Unless FILE_NAME ends in
   ".sig", do the same with FILE_NAME.sig, if such a file exists.
   Do nothing if wy_dry_run is set.
*/
int
dir_archive_file(struct wy_triplet *trp, const char *file_name)
{
	int rc;
	char *signame;

	rc = archive_single_file(trp, file_name, 0);
	if (rc == 0 && archive_signatures
	    && (signame = make_signame(file_name))) {
		rc = archive_single_file(trp, signame, 1);
		free(signame);
	}
	return rc;
}

/* Create a symbolic link from WANTED_SRC to WANTED_DST in the subdirectory
   TRP->relative_dir of SPOOL->dest_dir. 

   Do nothing if wy_dry_run is set. */
int
dir_symlink_file(struct wy_triplet *trp,
		 const char *wanted_src, const char *wanted_dst)
{
	int rc = 0;
	const struct spool *spool = trp->spool;
	char *dst_dir = create_directory(spool->dest_dir, trp->relative_dir);
	char *src, *dst;

	if (!dst_dir)
		return 1;

	src = safe_file_name_alloc(wanted_src);
	if (!src || src[0] == '/') {
		wy_log(LOG_ERR,
		       _("symlink source `%s' does not lie under `%s'"),
		       wanted_src, spool->dest_dir);
		free(src);
		return 1;
	}

	dst = safe_file_name_alloc(wanted_dst);
	if (!dst || dst[0] == '/') {
		wy_log(LOG_ERR,
		       _("symlink destination `%s' does not lie under `%s'"),
		       wanted_dst, spool->dest_dir);
		free(src);
		free(dst);
		return 1;
	}

	wy_debug(1, (_("symlinking %s to %s in directory %s"),
		     src, dst, dst_dir));

	if (!wy_dry_run) {
		char *p = strrchr(dst, '/');
		if (p > dst) {
			char *dir;

			*p = 0;
			dir = create_directory(spool->dest_dir, dst);
			if (!dir)
				rc = 1;
			else
				free(dir);
			*p = '/';
		}

		if (rc == 0) {
			if (push_dir(dst_dir))
				wy_log(LOG_ERR, _("cannot change to %s: %s"),
				       dst_dir, strerror(errno));
			else {
				struct stat st;

				if (lstat(dst, &st) == 0) {
					if (!S_ISLNK(st.st_mode)) {
						wy_log(LOG_ERR,
			       _("file %s exists and is not a symbolic link"),
						       dst);
						rc = 1;
					} else if (unlink(dst)) {
						wy_log(LOG_ERR,
			       _("cannot unlink %s: %s"),
						       dst, strerror(errno));
						rc = 1;
					}
				} else if (errno != ENOENT) {
					wy_log(LOG_ERR,
					       _("cannot stat file %s: %s"),
					       dst, strerror(errno));
					rc = 1;
				}

				if (rc == 0) {
					rc = symlink(src, dst);
					if (rc)
						wy_log(LOG_ERR,
			  _("symlinking %s to %s in directory %s failed: %s"),
						       src, dst, dst_dir,
						       strerror(errno));
				}
				if (pop_dir()) {
					wy_log(LOG_EMERG,
			       _("cannot restore current directory: %s"),
					       strerror(errno));
					exit(EX_SOFTWARE);
				}
			}
		}
	}

	free(src);
	free(dst);
	free(dst_dir);
	if (rc == 0)
		UPDATE_STATS(WY_STAT_SYMLINKS);
	return rc;
}

/* Auxiliary function for rmsymlink_file (see below) */
static int
do_rmsymlink_file(const char *dst_file, int noentok)
{
	struct stat st;

	wy_debug(1, (_("removing symbolic link %s"), dst_file));

	if (stat(dst_file, &st)) {
		if (errno == ENOENT) {
			if (!noentok)
				wy_log(LOG_NOTICE,
				       _("symlink `%s' does not exist"),
				       dst_file);
			return 0;
		}
		if (!S_ISLNK(st.st_mode)) {
			wy_log(LOG_ERR,
			       _("refusing to unlink %s: is not a symlink"),
			       dst_file);
			return 1;
		}
	}
	if (!wy_dry_run && unlink(dst_file)) {
		wy_log(LOG_ERR, _("cannot unlink %s: %s"), dst_file,
		       strerror(errno));
		return 1;
	}
	UPDATE_STATS(WY_STAT_RMSYMLINKS);
	return 0;
}

/* Remove the symbolic link TRP->spool->dest_dir/TRP->relative_dir/FILE_NAME

   Do nothing if wy_dry_run is set. */
int
dir_rmsymlink_file(struct wy_triplet *trp, const char *file_name)
{
	char *dst_file;
	int rc = 0;
	char *signame;
	const struct spool *spool = trp->spool;
	char *dst_dir = create_directory(spool->dest_dir, trp->relative_dir);

	if (!dst_dir)
		return 1;

	dst_file = safe_file_name(concat_dir(dst_dir, file_name, NULL));
	if (!sub_dir_p(dst_file, spool->dest_dir)) {
		wy_log(LOG_ERR,
		       _("refusing to remove a symlink `%s' that is not "
			 "located under `%s'"), dst_file, spool->dest_dir);
		free(dst_file);
		free(dst_dir);
		return 1;
	}

	rc = do_rmsymlink_file(dst_file, 0);
	if (rc == 0 && archive_signatures && (signame = make_signame(dst_file))) {
		rc = do_rmsymlink_file(signame, 1);
		free(signame);
	}

	free(dst_file);
	free(dst_dir);
	return rc;
}

int
dir_test_url(wy_url_t url, grecs_locus_t * locus)
{
	int rc;
	const char *dest_dir;

	dest_dir = wy_url_path(url);
	if (!dest_dir) {
		grecs_error(locus, 0,
			    _("cannot extract directory part from URL"));
		return -1;
	}
	if (test_dir(dest_dir, &rc) && rc != ENOENT) {
		if (rc)
			grecs_error(locus, rc, _("cannot access %s"),
				    dest_dir);
		else
			grecs_error(locus, 0, _("%s is not a directory"),
				    dest_dir);
		return 1;
	}
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.