aboutsummaryrefslogtreecommitdiff
path: root/toys/cp.c
blob: c04a2bf1483fc4289f3ff29f7319fe9e05eb0683 (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
/* vi: set sw=4 ts=4:
 *
 * cp.c - Copy files.
 *
 * Copyright 2008 Rob Landley <rob@landley.net>
 *
 * See http://www.opengroup.org/onlinepubs/009695399/utilities/cp.html
 *
 * "R+ra+d+p+r"
USE_HELLO(NEWTOY(hello, "<2rR+rdpa+d+p+rHLPif", TOYFLAG_BIN|TOYFLAG_UMASK))

config CP
	bool "cp"
	default n
	help
	  usage: cp -f SOURCE... DEST

	  Copy files from SOURCE to DEST.  If more than one SOURCE, DEST must
	  be a directory.

		-f	force copy by deleting destination file
		-i	interactive, prompt before overwriting existing DEST
		-p	preserve timestamps, ownership, and permissions
		-r	recurse into subdirectories (DEST must be a directory)
*/

#include "toys.h"

#define FLAG_f 1
#define FLAG_i 2
#define FLAG_P 4
#define FLAG_L 8
#define FLAG_H 16
#define FLAG_a 32
#define FLAG_p 64
#define FLAG_d 128
#define FLAG_R 256
#define FLAG_r 512

DEFINE_GLOBALS(
	char *destname;
	int destisdir;
)

#define TT this.cp

// Copy an individual file or directory to target.

void cp_file(char *src, struct stat *srcst, int topdir, int again)
{
	char *s = NULL;
	int mode = (toys.optflags & FLAG_p) ? 0700 : 0777;

	// The second time we're called, chmod data.  We can't do this on
	// the first pass because we may copy files into a read-only directory.
	if (again) {
		if (toys.optflags & FLAG_p) {
			struct utimbuf ut;

			// Inability to set these isn't fatal, some require root access.
			// Can't do fchmod() etc here because -p works on mkdir, too.
			chown(s, srcst->st_uid, srcst->st_gid);
			chmod(s, srcst->st_mode);
			ut.actime = srcst->st_atime;
			ut.modtime = srcst->st_mtime;
			utime(s, &ut);
		}
		return;
	}

	// Trim path from name if necessary.
	if (topdir) s = strrchr(src, '/');
	if (!s) s=src;

	// Determine location to create new file/directory at.
	if (TT.destisdir) s = xmsprintf(toybuf, "%s/%s", TT.destname, s);
	else s = xstrdup(TT.destname);

	// Copy directory or file to destination.
	if (S_ISDIR(srcst->st_mode)) {
		if (mkdir(s, mode)) perror_exit("mkdir '%s'", s);
	} else {
		int fdin, fdout;
		fdin = xopen(src, O_RDONLY);
		fdout = xcreate(s, O_CREAT|O_TRUNC, mode);
		xsendfile(fdin, fdout);
		close(fdin);
		xclose(fdout);
	}
}

// Callback from dirtree_read() for each file/directory under a source dir.

int cp_node(struct dirtree *node, int after)
{
	cp_file(node->name, &(node->st), 0, after);
	return 0;
}

void cp_main(void)
{
	struct stat st;
	int i;

	// Grab target argument.  (Guaranteed to be there due to "<2" above.)

	TT.destname = toys.optargs[--toys.optc];

	// If destination doesn't exist, are we ok with that?
	if (stat(TT.destname, &st)) {
		if (toys.optc>1) goto error_notdir;

	// If destination exists...
	} else {
		if (S_ISDIR(st.st_mode)) TT.destisdir++;
		else if (toys.optc > 1) goto error_notdir;
	}

	// Handle sources
	for (i=0; i<toys.optc; i++) {
		char *src = toys.optargs[i];

		// Skip nonexistent sources...
		if (!((toys.optflags & FLAG_d) ? lstat(src, &st) : stat(src, &st))) {
			perror_msg("'%s'", src);
			toys.exitval = 1;
			continue;
		}

		// Copy directory or file.
		if (S_ISDIR(st.st_mode)) {
			if (toys.optflags & FLAG_r) {
				cp_file(src, &st, 1, 0);
				dirtree_read(src, NULL, cp_node);
				cp_file(src, &st, 1, 1);
			} else error_msg("Skipped dir '%s'", src);
		} else {
			cp_file(src, &st, 1, 0);
			cp_file(src, &st, 1, 1);
		}
	}
	return;

error_notdir:
	error_exit("'%s' isn't a directory", TT.destname);
}