1 be36ff68 2004-04-29 devnull #define _GNU_SOURCE /* for Linux O_DIRECT */
2 b3994ec5 2003-12-11 devnull #include <u.h>
3 8cb7308f 2020-05-18 rsc #include <dirent.h>
4 8cb7308f 2020-05-18 rsc #include <errno.h>
5 e7504005 2004-06-11 devnull #include <sys/file.h>
6 58fdc083 2020-05-18 rsc #include <sys/stat.h>
7 8cb7308f 2020-05-18 rsc #define NOPLAN9DEFINES
8 8cb7308f 2020-05-18 rsc #include <libc.h>
10 8cb7308f 2020-05-18 rsc static struct {
17 8cb7308f 2020-05-18 rsc dirput(int fd, DIR *d)
23 8cb7308f 2020-05-18 rsc werrstr("invalid fd");
26 8cb7308f 2020-05-18 rsc lock(&dirs.lk);
27 8cb7308f 2020-05-18 rsc if(fd >= dirs.nd) {
28 8cb7308f 2020-05-18 rsc nd = dirs.nd*2;
31 8cb7308f 2020-05-18 rsc dp = realloc(dirs.d, nd*sizeof dirs.d[0]);
32 8cb7308f 2020-05-18 rsc if(dp == nil) {
33 8cb7308f 2020-05-18 rsc werrstr("out of memory");
34 8cb7308f 2020-05-18 rsc unlock(&dirs.lk);
37 8cb7308f 2020-05-18 rsc for(i=dirs.nd; i<nd; i++)
40 8cb7308f 2020-05-18 rsc dirs.nd = nd;
42 8cb7308f 2020-05-18 rsc dirs.d[fd] = d;
43 8cb7308f 2020-05-18 rsc unlock(&dirs.lk);
48 8cb7308f 2020-05-18 rsc dirget(int fd)
52 8cb7308f 2020-05-18 rsc lock(&dirs.lk);
54 8cb7308f 2020-05-18 rsc if(0 <= fd && fd < dirs.nd)
55 8cb7308f 2020-05-18 rsc d = dirs.d[fd];
56 8cb7308f 2020-05-18 rsc unlock(&dirs.lk);
61 8cb7308f 2020-05-18 rsc dirdel(int fd)
65 8cb7308f 2020-05-18 rsc lock(&dirs.lk);
67 8cb7308f 2020-05-18 rsc if(0 <= fd && fd < dirs.nd) {
68 8cb7308f 2020-05-18 rsc d = dirs.d[fd];
69 8cb7308f 2020-05-18 rsc dirs.d[fd] = nil;
71 8cb7308f 2020-05-18 rsc unlock(&dirs.lk);
76 58fdc083 2020-05-18 rsc p9create(char *path, int mode, ulong perm)
78 58fdc083 2020-05-18 rsc int fd, cexec, umode, rclose, lock, rdwr;
79 58fdc083 2020-05-18 rsc struct flock fl;
81 58fdc083 2020-05-18 rsc rdwr = mode&3;
82 58fdc083 2020-05-18 rsc lock = mode&OLOCK;
83 58fdc083 2020-05-18 rsc cexec = mode&OCEXEC;
84 58fdc083 2020-05-18 rsc rclose = mode&ORCLOSE;
85 58fdc083 2020-05-18 rsc mode &= ~(ORCLOSE|OCEXEC|OLOCK);
87 58fdc083 2020-05-18 rsc /* XXX should get mode mask right? */
89 58fdc083 2020-05-18 rsc if(perm&DMDIR){
90 58fdc083 2020-05-18 rsc if(mode != OREAD){
91 58fdc083 2020-05-18 rsc werrstr("bad mode in directory create");
94 58fdc083 2020-05-18 rsc if(mkdir(path, perm&0777) < 0)
96 58fdc083 2020-05-18 rsc fd = open(path, O_RDONLY);
98 58fdc083 2020-05-18 rsc umode = (mode&3)|O_CREAT|O_TRUNC;
99 58fdc083 2020-05-18 rsc mode &= ~(3|OTRUNC);
100 58fdc083 2020-05-18 rsc if(mode&ODIRECT){
101 58fdc083 2020-05-18 rsc umode |= O_DIRECT;
102 58fdc083 2020-05-18 rsc mode &= ~ODIRECT;
104 58fdc083 2020-05-18 rsc if(mode&OEXCL){
105 58fdc083 2020-05-18 rsc umode |= O_EXCL;
106 58fdc083 2020-05-18 rsc mode &= ~OEXCL;
108 58fdc083 2020-05-18 rsc if(mode&OAPPEND){
109 58fdc083 2020-05-18 rsc umode |= O_APPEND;
110 58fdc083 2020-05-18 rsc mode &= ~OAPPEND;
113 58fdc083 2020-05-18 rsc werrstr("unsupported mode in create");
116 58fdc083 2020-05-18 rsc fd = open(path, umode, perm);
119 58fdc083 2020-05-18 rsc if(fd >= 0){
121 58fdc083 2020-05-18 rsc fl.l_type = (rdwr==OREAD) ? F_RDLCK : F_WRLCK;
122 58fdc083 2020-05-18 rsc fl.l_whence = SEEK_SET;
123 58fdc083 2020-05-18 rsc fl.l_start = 0;
124 58fdc083 2020-05-18 rsc fl.l_len = 0;
125 58fdc083 2020-05-18 rsc if(fcntl(fd, F_SETLK, &fl) < 0){
127 58fdc083 2020-05-18 rsc werrstr("lock: %r");
132 58fdc083 2020-05-18 rsc fcntl(fd, F_SETFL, FD_CLOEXEC);
134 58fdc083 2020-05-18 rsc remove(path);
140 8ad51794 2004-03-25 devnull p9open(char *name, int mode)
142 b3994ec5 2003-12-11 devnull int cexec, rclose;
143 d9e16d25 2004-06-11 devnull int fd, umode, lock, rdwr;
144 c71af67a 2005-01-07 devnull struct flock fl;
145 8cb7308f 2020-05-18 rsc struct stat st;
148 d9e16d25 2004-06-11 devnull rdwr = mode&3;
149 d9e16d25 2004-06-11 devnull umode = rdwr;
150 b3994ec5 2003-12-11 devnull cexec = mode&OCEXEC;
151 b3994ec5 2003-12-11 devnull rclose = mode&ORCLOSE;
152 e7504005 2004-06-11 devnull lock = mode&OLOCK;
153 e7504005 2004-06-11 devnull mode &= ~(3|OCEXEC|ORCLOSE|OLOCK);
154 b3994ec5 2003-12-11 devnull if(mode&OTRUNC){
155 b3994ec5 2003-12-11 devnull umode |= O_TRUNC;
156 b3994ec5 2003-12-11 devnull mode ^= OTRUNC;
158 be36ff68 2004-04-29 devnull if(mode&ODIRECT){
159 be36ff68 2004-04-29 devnull umode |= O_DIRECT;
160 be36ff68 2004-04-29 devnull mode ^= ODIRECT;
162 a19ff5b2 2005-01-07 devnull if(mode&ONONBLOCK){
163 a19ff5b2 2005-01-07 devnull umode |= O_NONBLOCK;
164 a19ff5b2 2005-01-07 devnull mode ^= ONONBLOCK;
166 b589fce2 2005-02-11 devnull if(mode&OAPPEND){
167 b589fce2 2005-02-11 devnull umode |= O_APPEND;
168 b589fce2 2005-02-11 devnull mode ^= OAPPEND;
170 b3994ec5 2003-12-11 devnull if(mode){
171 debcecb5 2004-05-14 devnull werrstr("mode 0x%x not supported", mode);
172 b3994ec5 2003-12-11 devnull return -1;
174 b3994ec5 2003-12-11 devnull fd = open(name, umode);
175 b3994ec5 2003-12-11 devnull if(fd >= 0){
176 e7504005 2004-06-11 devnull if(lock){
177 c71af67a 2005-01-07 devnull fl.l_type = (rdwr==OREAD) ? F_RDLCK : F_WRLCK;
178 c71af67a 2005-01-07 devnull fl.l_whence = SEEK_SET;
179 c71af67a 2005-01-07 devnull fl.l_start = 0;
180 c71af67a 2005-01-07 devnull fl.l_len = 0;
181 c71af67a 2005-01-07 devnull if(fcntl(fd, F_SETLK, &fl) < 0){
182 e7504005 2004-06-11 devnull close(fd);
183 915734a7 2005-03-18 devnull werrstr("lock: %r");
184 e7504005 2004-06-11 devnull return -1;
187 b3994ec5 2003-12-11 devnull if(cexec)
188 b3994ec5 2003-12-11 devnull fcntl(fd, F_SETFL, FD_CLOEXEC);
189 8cb7308f 2020-05-18 rsc if(fstat(fd, &st) >= 0 && S_ISDIR(st.st_mode)) {
190 8cb7308f 2020-05-18 rsc d = fdopendir(fd);
191 8cb7308f 2020-05-18 rsc if(d == nil) {
195 8cb7308f 2020-05-18 rsc if(dirput(fd, d) < 0) {
196 8cb7308f 2020-05-18 rsc closedir(d);
200 b3994ec5 2003-12-11 devnull if(rclose)
201 b3994ec5 2003-12-11 devnull remove(name);
203 b3994ec5 2003-12-11 devnull return fd;
207 16f60479 2020-05-18 rsc p9seek(int fd, vlong offset, int whence)
211 8cb7308f 2020-05-18 rsc if((d = dirget(fd)) != nil) {
212 8cb7308f 2020-05-18 rsc if(whence == 1 && offset == 0)
213 8cb7308f 2020-05-18 rsc return telldir(d);
214 8cb7308f 2020-05-18 rsc if(whence == 0) {
215 8cb7308f 2020-05-18 rsc seekdir(d, offset);
218 8cb7308f 2020-05-18 rsc werrstr("bad seek in directory");
222 16f60479 2020-05-18 rsc return lseek(fd, offset, whence);
226 6fd4e901 2020-05-18 rsc p9close(int fd)
230 8cb7308f 2020-05-18 rsc if((d = dirdel(fd)) != nil)
231 8cb7308f 2020-05-18 rsc return closedir(d);
232 6fd4e901 2020-05-18 rsc return close(fd);
235 8cb7308f 2020-05-18 rsc typedef struct DirBuild DirBuild;
236 8cb7308f 2020-05-18 rsc struct DirBuild {
244 58fdc083 2020-05-18 rsc extern int _p9dir(struct stat*, struct stat*, char*, Dir*, char**, char*);
247 8cb7308f 2020-05-18 rsc dirbuild1(DirBuild *b, struct stat *lst, struct stat *st, char *name)
249 8cb7308f 2020-05-18 rsc int i, nstr;
251 8cb7308f 2020-05-18 rsc int md, mstr;
252 8cb7308f 2020-05-18 rsc char *lo, *hi, *newlo;
254 8cb7308f 2020-05-18 rsc nstr = _p9dir(lst, st, name, nil, nil, nil);
255 8cb7308f 2020-05-18 rsc if(b->md-b->nd < 1 || b->estr-b->str < nstr) {
256 8cb7308f 2020-05-18 rsc // expand either d space or str space or both.
258 8cb7308f 2020-05-18 rsc if(b->md-b->nd < 1) {
263 8cb7308f 2020-05-18 rsc mstr = b->estr-(char*)&b->d[b->md];
264 8cb7308f 2020-05-18 rsc if(b->estr-b->str < nstr) {
265 8cb7308f 2020-05-18 rsc mstr += nstr;
266 8cb7308f 2020-05-18 rsc mstr += mstr/2;
268 8cb7308f 2020-05-18 rsc if(mstr < 512)
270 8cb7308f 2020-05-18 rsc d = realloc(b->d, md*sizeof d[0] + mstr);
271 8cb7308f 2020-05-18 rsc if(d == nil)
273 8cb7308f 2020-05-18 rsc // move strings and update pointers in Dirs
274 8cb7308f 2020-05-18 rsc lo = (char*)&b->d[b->md];
275 8cb7308f 2020-05-18 rsc newlo = (char*)&d[md];
276 8cb7308f 2020-05-18 rsc hi = b->str;
277 8cb7308f 2020-05-18 rsc memmove(newlo, lo+((char*)d-(char*)b->d), hi-lo);
278 8cb7308f 2020-05-18 rsc for(i=0; i<b->nd; i++) {
279 8cb7308f 2020-05-18 rsc if(lo <= d[i].name && d[i].name < hi)
280 8cb7308f 2020-05-18 rsc d[i].name += newlo - lo;
281 8cb7308f 2020-05-18 rsc if(lo <= d[i].uid && d[i].uid < hi)
282 8cb7308f 2020-05-18 rsc d[i].uid += newlo - lo;
283 8cb7308f 2020-05-18 rsc if(lo <= d[i].gid && d[i].gid < hi)
284 8cb7308f 2020-05-18 rsc d[i].gid += newlo - lo;
285 8cb7308f 2020-05-18 rsc if(lo <= d[i].muid && d[i].muid < hi)
286 8cb7308f 2020-05-18 rsc d[i].muid += newlo - lo;
290 8cb7308f 2020-05-18 rsc b->str += newlo - lo;
291 8cb7308f 2020-05-18 rsc b->estr = newlo + mstr;
293 8cb7308f 2020-05-18 rsc _p9dir(lst, st, name, &b->d[b->nd], &b->str, b->estr);
299 8cb7308f 2020-05-18 rsc dirreadmax(int fd, Dir **dp, int max)
304 58fdc083 2020-05-18 rsc struct dirent *de;
305 58fdc083 2020-05-18 rsc struct stat st, lst;
307 8cb7308f 2020-05-18 rsc if((dir = dirget(fd)) == nil) {
308 8cb7308f 2020-05-18 rsc werrstr("not a directory");
312 8cb7308f 2020-05-18 rsc memset(&b, 0, sizeof b);
313 8cb7308f 2020-05-18 rsc for(i=0; max == -1 || i<max; i++) { // max = not too many, not too few
315 8cb7308f 2020-05-18 rsc de = readdir(dir);
316 8cb7308f 2020-05-18 rsc if(de == nil) {
317 8cb7308f 2020-05-18 rsc if(b.nd == 0 && errno != 0)
321 c53ad837 2020-05-18 rsc // Note: not all systems have d_namlen. Assume NUL-terminated.
322 8cb7308f 2020-05-18 rsc if(de->d_name[0]=='.' && de->d_name[1]==0)
324 8cb7308f 2020-05-18 rsc if(de->d_name[0]=='.' && de->d_name[1]=='.' && de->d_name[2]==0)
326 8cb7308f 2020-05-18 rsc if(fstatat(fd, de->d_name, &lst, AT_SYMLINK_NOFOLLOW) < 0)
329 8cb7308f 2020-05-18 rsc if(S_ISLNK(lst.st_mode))
330 8cb7308f 2020-05-18 rsc fstatat(fd, de->d_name, &st, 0);
331 8cb7308f 2020-05-18 rsc dirbuild1(&b, &lst, &st, de->d_name);
334 8cb7308f 2020-05-18 rsc return b.nd;
338 58fdc083 2020-05-18 rsc dirread(int fd, Dir **dp)
340 8cb7308f 2020-05-18 rsc return dirreadmax(fd, dp, 10);
344 8cb7308f 2020-05-18 rsc dirreadall(int fd, Dir **dp)
346 8cb7308f 2020-05-18 rsc return dirreadmax(fd, dp, -1);