aboutsummaryrefslogtreecommitdiffstats
path: root/FICS/copyfile.c
blob: 408a0f4c3c00cd0607c642774a42b49f1cbe1098 (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
// SPDX-FileCopyrightText: 2025 Markus Uhlin <maxxe@rpblc.net>
// SPDX-License-Identifier: ISC

#include <sys/types.h>
#include <sys/stat.h>

#include <err.h>
#include <fcntl.h>
#include <sha1.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "copyfile.h"

#define PRINT_CHECKSUMS 0
#define SELF_TEST 0

static int
do_post_checks(const char *p1, const char *p2)
{
	char	 buf[2][41];
	char	*str[2];

	str[0] = &buf[0][0];
	str[1] = &buf[1][0];

	memset(str[0], 0, sizeof(buf[0]));
	memset(str[1], 0, sizeof(buf[1]));

	if (SHA1File(p1, str[0]) != NULL &&
	    SHA1File(p2, str[1]) != NULL) {
#if PRINT_CHECKSUMS
		puts(str[0]);
		puts(str[1]);
#endif

		if (strcmp(str[0], str[1]) != 0) {
			warnx("%s: digest mismatch", __func__);
			return -1;
		}
	} else
		warnx("%s: failed to calculate file digest", __func__);
	return 0;
}

bool
fics_copyfile(const char *p1, const char *p2, const bool post_checks)
{
	char	tmp[2048] = { '\0' };
	int	fd[2];
	ssize_t ret[2];
	ssize_t total_read = 0;
	ssize_t total_written = 0;

	if (p1 == NULL || p2 == NULL ||
	    strcmp(p1, "") == 0 ||
	    strcmp(p2, "") == 0)
		return false;
	if (!is_regular_file(p1)) {
		warnx("%s: not a regular file", __func__);
		return false;
	}
	if ((fd[0] = open(p1, O_RDONLY)) < 0) {
		warn("%s: open(%s, ...)", __func__, p1);
		return false;
	}

	fd[1] = open(p2, (O_RDWR|O_APPEND|O_CREAT|O_TRUNC),
	    (S_IRUSR|S_IWUSR|S_IRGRP));
	if (fd[1] < 0) {
		warn("%s: open(%s, ...)", __func__, p2);
		close(fd[0]);
		return false;
	}

	while ((ret[0] = read(fd[0], tmp, sizeof tmp)) > 0) {
		total_read += ret[0];

		if ((ret[1] = write(fd[1], tmp, ret[0])) != ret[0]) {
			warnx("%s: written mismatch read", __func__);
			break;
		}

		total_written += ret[1];
	}

	if (close(fd[0]) != 0)
		perror("close");
	if (close(fd[1]) != 0)
		perror("close");

	if (total_read != total_written) {
		warnx("%s: total written mismatch total read", __func__);
		return false;
	} else if (post_checks) {
		if (do_post_checks(p1, p2) == -1)
			return false;
	}

	return true;
}

bool
is_regular_file(const char *path)
{
	struct stat sb = { 0 };

	if (path == NULL || strcmp(path, "") == 0)
		return false;
	return (stat(path, &sb) == 0 && S_ISREG(sb.st_mode));
}

#if SELF_TEST
int
main(int argc, char *argv[])
{
	if (argc != 3)
		errx(1, "bogus number of args");
	return (fics_copyfile(argv[1], argv[2], true) ? 0 : 1);
}
#endif