Blob


1 #include <u.h>
2 #define NOPLAN9DEFINES
3 #include <libc.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <dirent.h>
7 #include <pwd.h>
8 #include <grp.h>
10 #if defined(__FreeBSD__)
11 #include <sys/disklabel.h>
12 static int diskdev[] = {
13 151, /* aacd */
14 116, /* ad */
15 157, /* ar */
16 118, /* afd */
17 133, /* amrd */
18 13, /* da */
19 102, /* fla */
20 109, /* idad */
21 95, /* md */
22 131, /* mlxd */
23 168, /* pst */
24 147, /* twed */
25 43, /* vn */
26 3, /* wd */
27 87, /* wfd */
28 };
29 static int
30 isdisk(struct stat *st)
31 {
32 int i, dev;
34 if(!S_ISCHR(st->st_mode))
35 return 0;
36 dev = major(st->st_rdev);
37 for(i=0; i<nelem(diskdev); i++)
38 if(diskdev[i] == dev)
39 return 1;
40 return 0;
41 }
42 #define _HAVEDISKLABEL
43 #endif
45 #if defined(__linux__)
46 #include <linux/hdreg.h>
47 #include <linux/fs.h>
48 #include <sys/ioctl.h>
49 static vlong
50 disksize(int fd, int dev)
51 {
52 u64int u64;
53 long l;
54 struct hd_geometry geo;
56 if(ioctl(fd, BLKGETSIZE64, &u64) >= 0)
57 return u64;
58 if(ioctl(fd, BLKGETSIZE, &l) >= 0)
59 return l*512;
60 if(ioctl(fd, HDIO_GETGEO, &geo) >= 0)
61 return (vlong)geo.heads*geo.sectors*geo.cylinders*512;
62 return 0;
63 }
64 #define _HAVEDISKSIZE
65 #endif
67 #if !defined(__linux__) && !defined(__sun__)
68 #define _HAVESTGEN
69 #endif
71 /*
72 * Caching the last group and passwd looked up is
73 * a significant win (stupidly enough) on most systems.
74 * It's not safe for threaded programs, but neither is using
75 * getpwnam in the first place, so I'm not too worried.
76 */
77 int
78 _p9dir(struct stat *st, char *name, Dir *d, char **str, char *estr)
79 {
80 char *s;
81 char tmp[20];
82 static struct group *g;
83 static struct passwd *p;
84 static int gid, uid;
85 int sz, fd;
87 fd = -1;
88 USED(fd);
89 sz = 0;
90 if(d)
91 memset(d, 0, sizeof *d);
93 /* name */
94 s = strrchr(name, '/');
95 if(s)
96 s++;
97 if(!s || !*s)
98 s = name;
99 if(*s == '/')
100 s++;
101 if(*s == 0)
102 s = "/";
103 if(d){
104 if(*str + strlen(s)+1 > estr)
105 d->name = "oops";
106 else{
107 strcpy(*str, s);
108 d->name = *str;
109 *str += strlen(*str)+1;
112 sz += strlen(s)+1;
114 /* user */
115 if(p && st->st_uid == uid && p->pw_uid == uid)
117 else{
118 p = getpwuid(st->st_uid);
119 uid = st->st_uid;
121 if(p == nil){
122 snprint(tmp, sizeof tmp, "%d", (int)st->st_uid);
123 s = tmp;
124 }else
125 s = p->pw_name;
126 sz += strlen(s)+1;
127 if(d){
128 if(*str+strlen(s)+1 > estr)
129 d->uid = "oops";
130 else{
131 strcpy(*str, s);
132 d->uid = *str;
133 *str += strlen(*str)+1;
137 /* group */
138 if(g && st->st_gid == gid && g->gr_gid == gid)
140 else{
141 g = getgrgid(st->st_gid);
142 gid = st->st_gid;
144 if(g == nil){
145 snprint(tmp, sizeof tmp, "%d", (int)st->st_gid);
146 s = tmp;
147 }else
148 s = g->gr_name;
149 sz += strlen(s)+1;
150 if(d){
151 if(*str + strlen(s)+1 > estr)
152 d->gid = "oops";
153 else{
154 strcpy(*str, s);
155 d->gid = *str;
156 *str += strlen(*str)+1;
160 if(d){
161 d->type = 'M';
163 d->muid = "";
164 d->qid.path = ((uvlong)st->st_dev<<32) | st->st_ino;
165 #ifdef _HAVESTGEN
166 d->qid.vers = st->st_gen;
167 #endif
168 d->mode = st->st_mode&0777;
169 d->atime = st->st_atime;
170 d->mtime = st->st_mtime;
171 d->length = st->st_size;
173 if(S_ISDIR(st->st_mode)){
174 d->length = 0;
175 d->mode |= DMDIR;
176 d->qid.type = QTDIR;
179 /* fetch real size for disks */
180 #ifdef _HAVEDISKSIZE
181 if(S_ISBLK(st->st_mode) && (fd = open(name, O_RDONLY)) >= 0){
182 d->length = disksize(fd, major(st->st_dev));
183 close(fd);
185 #endif
186 #ifdef _HAVEDISKLABEL
187 if(isdisk(st)){
188 int fd, n;
189 struct disklabel lab;
191 if((fd = open(name, O_RDONLY)) < 0)
192 goto nosize;
193 if(ioctl(fd, DIOCGDINFO, &lab) < 0)
194 goto nosize;
195 n = minor(st->st_rdev)&7;
196 if(n >= lab.d_npartitions)
197 goto nosize;
199 d->length = (vlong)(lab.d_partitions[n].p_size) * lab.d_secsize;
201 nosize:
202 if(fd >= 0)
203 close(fd);
205 #endif
208 return sz;