-
Notifications
You must be signed in to change notification settings - Fork 3
/
fops.c
144 lines (132 loc) · 2.18 KB
/
fops.c
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
#include "a.h"
int
mkdir(char *wd, char *name)
{
char *p;
int fd;
p = abspath(wd, name);
if(access(p, 0) >= 0){
werrstr("directory already exists");
free(p);
return -1;
}
fd = create(p, OREAD, DMDIR|0755);
if(fd < 0){
free(p);
return -1;
}
free(p);
close(fd);
return 0;
}
int
rmdir(char *path)
{
Dir *dirs;
int i, fd, ndirs;
fd = open(path, OREAD);
if(fd < 0)
return -1;
ndirs = dirreadall(fd, &dirs);
close(fd);
if(ndirs < 0)
return -1;
for(i = 0; i < ndirs; i++){
if(rm(path, dirs[i]) < 0){
free(dirs);
return -1;
}
}
free(dirs);
if(remove(path) < 0)
return -1;
return 0;
}
int
rm(char *path, Dir d)
{
char buf[1024] = {0};
int rc;
snprint(buf, sizeof buf, "%s/%s", path, d.name);
if(d.qid.type&QTDIR){
rc = rmdir(buf);
}else{
rc = remove(buf);
}
return rc;
}
static int
fcopy(char *ipath, char *in, char *opath, char *out, int mode)
{
int rc, ifd, ofd, r, w;
char buf[8192];
snprint(buf, sizeof buf, "%s/%s", ipath, in);
ifd = open(buf, OREAD);
if(ifd < 0)
return -1;
snprint(buf, sizeof buf, "%s/%s", opath, out);
ofd = create(buf, OWRITE, mode);
if(ofd < 0){
close(ifd);
return -1;
}
rc = 0;
for(;;){
r = read(ifd, buf, sizeof buf);
if(r < 0){
rc = -1;
break;
}else if(r == 0)
break;
w = write(ofd, buf, r);
if(w != r){
rc = -1;
break;
}
}
close(ifd);
close(ofd);
return rc;
}
static int
dircp(char *ipath, Dir d, char *opath, char *oname)
{
char outp[1024], inp[1024];
Dir *dirs;
int i, n, fd;
snprint(outp, sizeof outp, "%s/%s", opath, oname ? oname : d.name);
if(access(outp, 0) < 0){
fd = create(outp, OREAD, DMDIR|0755);
if(fd < 0)
return -1;
close(fd);
}
snprint(inp, sizeof inp, "%s/%s", ipath, d.name);
fd = open(inp, OREAD);
if(fd < 0)
return -1;
n = dirreadall(fd, &dirs);
if(n < 0){
close(fd);
return -1;
}
close(fd);
for(i = 0; i < n; i++){
if(cp(inp, dirs[i], outp, dirs[i].name) < 0){
free(dirs);
return -1;
}
}
free(dirs);
return 0;
}
int
cp(char *ipath, Dir d, char *opath, char *oname)
{
int rc;
if(d.qid.type&QTDIR){
rc = dircp(ipath, d, opath, oname);
}else
rc = fcopy(ipath, d.name, opath, oname ? oname : d.name, d.mode);
return rc;
}