File: mv.c

package info (click to toggle)
klibc 2.0.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,248 kB
  • sloc: ansic: 47,887; asm: 2,394; perl: 758; makefile: 193; sh: 183
file content (267 lines) | stat: -rw-r--r-- 4,943 bytes parent folder | download | duplicates (3)
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
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <linux/limits.h>

#define BUFFER_SIZE 32768

/* copy_file - copy a file, used on different fs mv */
static int copy_file(const char *src, const char *dest, mode_t mode)
{
	char buf[BUFFER_SIZE];
	int sfd, dfd;
	ssize_t len;

	sfd = open(src, O_RDONLY);
	if (sfd < 0)
		return -1;

	dfd = open(dest, O_WRONLY | O_CREAT, mode);
	if (dfd < 0) {
		close(sfd);
		return -1;
	}

	while ((len = read(sfd, buf, sizeof(buf))) > 0) {
		len = write(dfd, buf, len);
		if (len < 0) {
			close(sfd);
			close(dfd);
			return -1;
		}
	}

	close(sfd);
	close(dfd);
	return 0;
}

/* copy - recursively copy directories */
static int copy(char *src, const char *dest)
{
	int len;
	struct stat sb, sf;
	char target[PATH_MAX];
	char *p;

	p = strrchr(src, '/');
	if (p) {
		p++;
		/* trailing slashes case */
		if (strlen(p) == 0) {
			len = strlen(src) - 1;
			p = src;
			while (0 < len && p[len] == '/')
				p[len--] = '\0';
			p = strrchr(p, '/');
			p++;
		}
	} else {
		p = src;
	}

	memset(&sb, 0, sizeof(struct stat));
	/* might not exist yet */
	if (stat(dest, &sb) == 0) {
		if (S_ISDIR(sb.st_mode)) {
			len = snprintf(target, PATH_MAX, "%s/%s", dest, p);
			if (len  >= PATH_MAX)
				return -1;
		} else {
			len = snprintf(target, PATH_MAX, "%s/%s", dest, src);
			if (len  >= PATH_MAX)
				return -1;
		}
	} else {
		len = snprintf(target, PATH_MAX, "%s", dest);
		if (len  >= PATH_MAX)
			return -1;
	}

	if (rename(src, target) == 0) {
		return 0;
	} else {
		if (errno != EXDEV)
			return -1;
	}


	/* cross fs copy */
	memset(&sf, 0, sizeof(struct stat));
	if (stat(src, &sf) < 0)
		return -1;
	if (!S_ISDIR(sf.st_mode)) {
		len = copy_file(src, target, sf.st_mode);
		if (len == 0)
			return 0;
		else
			return -1;
	}

	DIR *dir;
	struct dirent *d;
	char path[PATH_MAX];

	if (mkdir(target, sf.st_mode) < 0)
		return -1;

	dir = opendir(src);
	if (!dir) {
		/* EACCES means we can't read it.
		 * Might be empty and removable. */
		if (errno != EACCES)
			return -1;
	}
	while ((d = readdir(dir))) {

		/* Skip . and .. */
		if (d->d_name[0] == '.' && (d->d_name[1] == '\0'
		    || (d->d_name[1] == '.' && d->d_name[2] == '\0')))
			continue;

		/* skip to long path */
		if (strlen(src) + 1 + strlen(d->d_name) >= PATH_MAX  - 1)
			continue;

		snprintf(path, sizeof path, "%s/%s", src, d->d_name);
		if (len  >= sizeof path)
			return -1;

		memset(&sf, 0, sizeof(struct stat));
		if (stat(path, &sf) < 0) {
			closedir(dir);
			return -1;
		}

		/* recursively copy files and directories */
		if (copy(path, target) < 0)
			return -1;
	}
	closedir(dir);
	return 0;
}

/* nuke - rm a file or directory recursively */
static int nuke(const char *src)
{
	struct stat sb;
	DIR *dir;
	struct dirent *d;
	char path[PATH_MAX];
	int len;

	memset(&sb, 0, sizeof(struct stat));
	/* gone, no work */
	if (stat(src, &sb) < 0)
		return 0;

	if (!S_ISDIR(sb.st_mode)) {
		if (unlink(src) == 0)
			return 0;
		else
			return 1;
	}

	dir = opendir(src);
	if (!dir) {
		/* EACCES means we can't read it.
		 * Might be empty and removable. */
		if (errno != EACCES)
			return -1;
	}
	while ((d = readdir(dir))) {

		/* Skip . and .. */
		if (d->d_name[0] == '.' && (d->d_name[1] == '\0'
		    || (d->d_name[1] == '.' && d->d_name[2] == '\0')))
			continue;

		/* skip to long path */
		if (strlen(src) + 1 + strlen(d->d_name) >= PATH_MAX  - 1)
			continue;

		len = snprintf(path, sizeof path, "%s/%s", src, d->d_name);
		if (len  >= sizeof path)
			return -1;

		memset(&sb, 0, sizeof(struct stat));
		if (stat(path, &sb) < 0) {
			closedir(dir);
			return -1;
		}

		if (nuke(path) < 0)
			return -1;
	}
	closedir(dir);
	rmdir(src);
	return 0;
}

int main(int argc, char *argv[])
{
	int c, f;
	struct stat sb;

	f = 0;
	do {
		c = getopt(argc, argv, "f");
		if (c == EOF)
			break;

		switch (c) {

		case 'f':
			f = 1;
			break;
		case '?':
			fprintf(stderr, "%s: invalid option -%c\n",
				argv[0], optopt);
			return 1;
		}

	} while (1);

	/* not enough arguments */
	if (argc - optind < 2) {
		fprintf(stderr, "Usage: %s [-f] source dest\n", argv[0]);
		return 1;
	}

	/* check on many archs if destination is a directory to mv in */
	memset(&sb, 0, sizeof(struct stat));
	if (stat(argv[argc - 1], &sb) < 0 && argc - optind > 2) {
		if (!(S_ISDIR(sb.st_mode))) {
			fprintf(stderr,
				"multiple targets and %s is not a directory\n",
				argv[argc - 1]);
			return 1;
		}
	}

	/* remove destination */
	if (f)
		nuke(argv[argc - 1]);

	/* the mv action */
	for (c = optind; c < argc - 1; c++)
		if (copy(argv[c], argv[argc - 1]) < 0) {
			perror("Could not copy file");
			return -1;
		}

	/* Only rm after sucessfull rename */
	for (c = optind; c < argc - 1; c++)
		if (nuke(argv[c]) < 0) {
			perror("Could not rm file");
			return -1;
		}
	return 0;
}