aboutsummaryrefslogtreecommitdiffstats
path: root/tests/zfs-tests/cmd/cp_files.c
blob: 9af64a112631de2425a975a67ded3eb9b614dfde (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>

int
main(int argc, char *argv[])
{
	int tfd;
	DIR *sdir;
	struct dirent *dirent;

	if (argc != 3) {
		fprintf(stderr, "Usage: %s SRC DST\n", argv[0]);
		exit(1);
	}

	sdir = opendir(argv[1]);
	if (sdir == NULL) {
		fprintf(stderr, "Failed to open %s: %s\n",
		    argv[1], strerror(errno));
		exit(2);
	}

	tfd = open(argv[2], O_DIRECTORY);
	if (tfd < 0) {
		fprintf(stderr, "Failed to open %s: %s\n",
		    argv[2], strerror(errno));
		closedir(sdir);
		exit(3);
	}

	while ((dirent = readdir(sdir)) != NULL) {
		if (dirent->d_name[0] == '.' &&
		    (dirent->d_name[1] == '.' || dirent->d_name[1] == '\0'))
			continue;

		int fd = openat(tfd, dirent->d_name, O_CREAT|O_WRONLY, 0666);
		if (fd < 0) {
			fprintf(stderr, "Failed to create %s/%s: %s\n",
			    argv[2], dirent->d_name, strerror(errno));
			closedir(sdir);
			close(tfd);
			exit(4);
		}
		close(fd);
	}

	closedir(sdir);
	close(tfd);

	return (0);
}