Blob


1 #include <u.h>
2 #include <libc.h>
4 #include <sys/stat.h>
5 #include <sys/disklabel.h>
6 #include <dirent.h>
7 #include <pwd.h>
8 #include <grp.h>
10 int
11 _p9dir(struct stat *st, char *name, Dir *d, char **str, char *estr)
12 {
13 char *s;
14 char tmp[20];
15 struct group *g;
16 struct pwd *p;
17 int sz;
19 sz = 0;
21 /* name */
22 s = strrchr(name, '/');
23 if(s && s[1])
24 s++;
25 else
26 s = "/";
27 if(d){
28 if(*str + strlen(s)+1 > estr)
29 d->name = "oops";
30 else{
31 strcpy(*str, s);
32 d->name = *str;
33 *str += strlen(*str)+1;
34 }
35 }
36 sz += strlen(s)+1;
38 /* user */
39 p = getpwuid(st->st_uid);
40 if(p == nil){
41 snprint(tmp, sizeof tmp, "%d", (int)st->st_uid);
42 s = tmp;
43 }else
44 s = p->pw_name;
45 sz += strlen(s)+1;
46 if(d){
47 if(*str+strlen(s)+1 > estr)
48 d->uid = "oops";
49 else{
50 strcpy(*str, s);
51 d->uid = *str;
52 *str += strlen(*str)+1;
53 }
54 }
56 /* group */
57 g = getgrgid(st->st_gid);
58 if(g == nil){
59 snprint(tmp, sizeof tmp, "%d", (int)st->st_gid);
60 s = tmp;
61 }else
62 s = g->gr_name;
63 sz += strlen(s)+1;
64 if(d){
65 if(*str + strlen(s)+1 > estr){
66 d->gid = "oops";
67 else{
68 strcpy(*str, s);
69 d->gid = *str;
70 *str += strlen(*str)+1;
71 }
72 }
74 if(d){
75 d->muid = "";
76 d->qid.path = ((uvlong)st->st_dev<<32) | st->st_ino;
77 d->qid.vers = st->st_gen;
78 d->mode = st->st_mode&0777;
79 if(S_ISDIR(st->st_mode)){
80 d->mode |= DMDIR;
81 d->qid.type = QTDIR;
82 }
83 d->atime = st->st_atime;
84 d->mtime = st->st_mtime;
85 d->length = st->st_size;
87 /* fetch real size for disks */
88 if(S_ISCHR(st->st_mode)){
89 int fd, n;
90 struct disklabel lab;
92 if((fd = open(name, O_RDONLY)) < 0)
93 goto nosize;
94 if(ioctl(fd, DIOCGDINFO, &lab) < 0)
95 goto nosize;
96 n = minor(st->st_rdev)&0xFFFF;
97 if(n >= lab.d_npartitions)
98 goto nosize;
99 d->length = (vlong)lab.d_npartitions[n].p_size * lab.d_secsize;
100 nosize:
101 if(fd >= 0)
102 close(fd);
106 return sz;
109 Dir*
110 _dirfstat(char *name, int fd)
112 Dir *d;
113 int size;