1 478ee963 2003-11-23 devnull #include <u.h>
2 478ee963 2003-11-23 devnull #define NOPLAN9DEFINES
3 478ee963 2003-11-23 devnull #include <libc.h>
4 478ee963 2003-11-23 devnull #include <sys/types.h>
5 fd04aace 2003-11-23 devnull #include <sys/stat.h>
6 fd04aace 2003-11-23 devnull #include <dirent.h>
7 fd04aace 2003-11-23 devnull #include <pwd.h>
8 fd04aace 2003-11-23 devnull #include <grp.h>
10 fe8c925e 2005-07-21 devnull #if defined(__FreeBSD__) || defined(__OpenBSD__)
11 64bcfff3 2003-11-25 devnull #include <sys/disklabel.h>
12 fe8c925e 2005-07-21 devnull #include <sys/ioctl.h>
13 69bdb78d 2007-05-11 devnull #define _HAVEDISKLABEL
16 69bdb78d 2007-05-11 devnull #if defined(__OpenBSD__)
17 669250d1 2003-12-03 devnull static int diskdev[] = {
18 669250d1 2003-12-03 devnull 151, /* aacd */
19 669250d1 2003-12-03 devnull 116, /* ad */
20 669250d1 2003-12-03 devnull 157, /* ar */
21 669250d1 2003-12-03 devnull 118, /* afd */
22 669250d1 2003-12-03 devnull 133, /* amrd */
23 669250d1 2003-12-03 devnull 13, /* da */
24 669250d1 2003-12-03 devnull 102, /* fla */
25 669250d1 2003-12-03 devnull 109, /* idad */
26 669250d1 2003-12-03 devnull 95, /* md */
27 669250d1 2003-12-03 devnull 131, /* mlxd */
28 669250d1 2003-12-03 devnull 168, /* pst */
29 669250d1 2003-12-03 devnull 147, /* twed */
30 669250d1 2003-12-03 devnull 43, /* vn */
31 669250d1 2003-12-03 devnull 3, /* wd */
32 669250d1 2003-12-03 devnull 87, /* wfd */
33 a98755e0 2006-01-07 devnull 4, /* da on FreeBSD 5 */
35 669250d1 2003-12-03 devnull static int
36 669250d1 2003-12-03 devnull isdisk(struct stat *st)
38 669250d1 2003-12-03 devnull int i, dev;
40 669250d1 2003-12-03 devnull if(!S_ISCHR(st->st_mode))
41 669250d1 2003-12-03 devnull return 0;
42 669250d1 2003-12-03 devnull dev = major(st->st_rdev);
43 669250d1 2003-12-03 devnull for(i=0; i<nelem(diskdev); i++)
44 669250d1 2003-12-03 devnull if(diskdev[i] == dev)
45 669250d1 2003-12-03 devnull return 1;
46 669250d1 2003-12-03 devnull return 0;
50 69bdb78d 2007-05-11 devnull #if defined(__FreeBSD__) /* maybe OpenBSD too? */
51 69bdb78d 2007-05-11 devnull char *diskdev[] = {
69 69bdb78d 2007-05-11 devnull static int
70 69bdb78d 2007-05-11 devnull isdisk(struct stat *st)
72 69bdb78d 2007-05-11 devnull char *name;
73 43f16cfa 2007-05-11 devnull int i, len;
75 69bdb78d 2007-05-11 devnull if(!S_ISCHR(st->st_mode))
76 69bdb78d 2007-05-11 devnull return 0;
77 69bdb78d 2007-05-11 devnull name = devname(st->st_rdev, S_IFCHR);
78 43f16cfa 2007-05-11 devnull for(i=0; i<nelem(diskdev); i++){
79 43f16cfa 2007-05-11 devnull len = strlen(diskdev[i]);
80 43f16cfa 2007-05-11 devnull if(strncmp(diskdev[i], name, len) == 0 && isdigit((uchar)name[len]))
81 69bdb78d 2007-05-11 devnull return 1;
83 69bdb78d 2007-05-11 devnull return 0;
88 7317e77d 2004-04-25 devnull #if defined(__linux__)
89 7317e77d 2004-04-25 devnull #include <linux/hdreg.h>
90 7317e77d 2004-04-25 devnull #include <linux/fs.h>
91 7317e77d 2004-04-25 devnull #include <sys/ioctl.h>
92 a3e9d893 2005-08-10 devnull #undef major
93 a3e9d893 2005-08-10 devnull #define major(dev) ((int)(((dev) >> 8) & 0xff))
94 7317e77d 2004-04-25 devnull static vlong
95 7317e77d 2004-04-25 devnull disksize(int fd, int dev)
97 7317e77d 2004-04-25 devnull u64int u64;
99 7317e77d 2004-04-25 devnull struct hd_geometry geo;
101 ceecb313 2004-06-09 devnull memset(&geo, 0, sizeof geo);
103 ceecb313 2004-06-09 devnull u64 = 0;
104 76e6aca8 2004-05-15 devnull #ifdef BLKGETSIZE64
105 7317e77d 2004-04-25 devnull if(ioctl(fd, BLKGETSIZE64, &u64) >= 0)
106 7317e77d 2004-04-25 devnull return u64;
108 7317e77d 2004-04-25 devnull if(ioctl(fd, BLKGETSIZE, &l) >= 0)
109 3d484b0d 2005-12-29 devnull return l*512;
110 7317e77d 2004-04-25 devnull if(ioctl(fd, HDIO_GETGEO, &geo) >= 0)
111 7317e77d 2004-04-25 devnull return (vlong)geo.heads*geo.sectors*geo.cylinders*512;
112 7317e77d 2004-04-25 devnull return 0;
114 7317e77d 2004-04-25 devnull #define _HAVEDISKSIZE
117 64bcfff3 2003-11-25 devnull #if !defined(__linux__) && !defined(__sun__)
118 64bcfff3 2003-11-25 devnull #define _HAVESTGEN
121 63fcc2bc 2006-06-12 devnull int _p9usepwlibrary = 1;
123 6f6553df 2004-04-08 devnull * Caching the last group and passwd looked up is
124 6f6553df 2004-04-08 devnull * a significant win (stupidly enough) on most systems.
125 6f6553df 2004-04-08 devnull * It's not safe for threaded programs, but neither is using
126 6f6553df 2004-04-08 devnull * getpwnam in the first place, so I'm not too worried.
129 0a229052 2005-02-08 devnull _p9dir(struct stat *lst, struct stat *st, char *name, Dir *d, char **str, char *estr)
131 fd04aace 2003-11-23 devnull char *s;
132 fd04aace 2003-11-23 devnull char tmp[20];
133 6f6553df 2004-04-08 devnull static struct group *g;
134 6f6553df 2004-04-08 devnull static struct passwd *p;
135 6f6553df 2004-04-08 devnull static int gid, uid;
136 7317e77d 2004-04-25 devnull int sz, fd;
138 631fe87f 2004-05-14 devnull fd = -1;
139 631fe87f 2004-05-14 devnull USED(fd);
142 fd04aace 2003-11-23 devnull memset(d, 0, sizeof *d);
144 fd04aace 2003-11-23 devnull /* name */
145 fd04aace 2003-11-23 devnull s = strrchr(name, '/');
148 fd04aace 2003-11-23 devnull if(!s || !*s)
149 fd04aace 2003-11-23 devnull s = name;
150 fd04aace 2003-11-23 devnull if(*s == '/')
152 fd04aace 2003-11-23 devnull if(*s == 0)
153 fd04aace 2003-11-23 devnull s = "/";
155 fd04aace 2003-11-23 devnull if(*str + strlen(s)+1 > estr)
156 fd04aace 2003-11-23 devnull d->name = "oops";
158 fd04aace 2003-11-23 devnull strcpy(*str, s);
159 fd04aace 2003-11-23 devnull d->name = *str;
160 fd04aace 2003-11-23 devnull *str += strlen(*str)+1;
163 fd04aace 2003-11-23 devnull sz += strlen(s)+1;
165 fd04aace 2003-11-23 devnull /* user */
166 6f6553df 2004-04-08 devnull if(p && st->st_uid == uid && p->pw_uid == uid)
168 63fcc2bc 2006-06-12 devnull else if(_p9usepwlibrary){
169 6f6553df 2004-04-08 devnull p = getpwuid(st->st_uid);
170 6f6553df 2004-04-08 devnull uid = st->st_uid;
172 63fcc2bc 2006-06-12 devnull if(p == nil || st->st_uid != uid || p->pw_uid != uid){
173 fd04aace 2003-11-23 devnull snprint(tmp, sizeof tmp, "%d", (int)st->st_uid);
174 fd04aace 2003-11-23 devnull s = tmp;
176 fd04aace 2003-11-23 devnull s = p->pw_name;
177 fd04aace 2003-11-23 devnull sz += strlen(s)+1;
179 fd04aace 2003-11-23 devnull if(*str+strlen(s)+1 > estr)
180 fd04aace 2003-11-23 devnull d->uid = "oops";
182 fd04aace 2003-11-23 devnull strcpy(*str, s);
183 fd04aace 2003-11-23 devnull d->uid = *str;
184 fd04aace 2003-11-23 devnull *str += strlen(*str)+1;
188 fd04aace 2003-11-23 devnull /* group */
189 6f6553df 2004-04-08 devnull if(g && st->st_gid == gid && g->gr_gid == gid)
191 63fcc2bc 2006-06-12 devnull else if(_p9usepwlibrary){
192 6f6553df 2004-04-08 devnull g = getgrgid(st->st_gid);
193 6f6553df 2004-04-08 devnull gid = st->st_gid;
195 63fcc2bc 2006-06-12 devnull if(g == nil || st->st_gid != gid || g->gr_gid != gid){
196 fd04aace 2003-11-23 devnull snprint(tmp, sizeof tmp, "%d", (int)st->st_gid);
197 fd04aace 2003-11-23 devnull s = tmp;
199 fd04aace 2003-11-23 devnull s = g->gr_name;
200 fd04aace 2003-11-23 devnull sz += strlen(s)+1;
202 fd04aace 2003-11-23 devnull if(*str + strlen(s)+1 > estr)
203 fd04aace 2003-11-23 devnull d->gid = "oops";
205 fd04aace 2003-11-23 devnull strcpy(*str, s);
206 fd04aace 2003-11-23 devnull d->gid = *str;
207 fd04aace 2003-11-23 devnull *str += strlen(*str)+1;
212 fd04aace 2003-11-23 devnull d->type = 'M';
214 fd04aace 2003-11-23 devnull d->muid = "";
215 fd04aace 2003-11-23 devnull d->qid.path = ((uvlong)st->st_dev<<32) | st->st_ino;
216 1c253ceb 2003-11-23 devnull #ifdef _HAVESTGEN
217 fd04aace 2003-11-23 devnull d->qid.vers = st->st_gen;
219 3d484b0d 2005-12-29 devnull if(d->qid.vers == 0)
220 3d484b0d 2005-12-29 devnull d->qid.vers = st->st_mtime + st->st_ctime;
221 fd04aace 2003-11-23 devnull d->mode = st->st_mode&0777;
222 fd04aace 2003-11-23 devnull d->atime = st->st_atime;
223 fd04aace 2003-11-23 devnull d->mtime = st->st_mtime;
224 fd04aace 2003-11-23 devnull d->length = st->st_size;
226 fd04aace 2003-11-23 devnull if(S_ISDIR(st->st_mode)){
227 fd04aace 2003-11-23 devnull d->length = 0;
228 fd04aace 2003-11-23 devnull d->mode |= DMDIR;
229 fd04aace 2003-11-23 devnull d->qid.type = QTDIR;
231 98660df2 2005-02-21 devnull if(S_ISLNK(lst->st_mode)) /* yes, lst not st */
232 2acd6fa6 2005-02-08 devnull d->mode |= DMSYMLINK;
233 2acd6fa6 2005-02-08 devnull if(S_ISFIFO(st->st_mode))
234 2acd6fa6 2005-02-08 devnull d->mode |= DMNAMEDPIPE;
235 2acd6fa6 2005-02-08 devnull if(S_ISSOCK(st->st_mode))
236 2acd6fa6 2005-02-08 devnull d->mode |= DMSOCKET;
237 2acd6fa6 2005-02-08 devnull if(S_ISBLK(st->st_mode)){
238 2acd6fa6 2005-02-08 devnull d->mode |= DMDEVICE;
239 2acd6fa6 2005-02-08 devnull d->qid.path = ('b'<<16)|st->st_rdev;
241 2acd6fa6 2005-02-08 devnull if(S_ISCHR(st->st_mode)){
242 2acd6fa6 2005-02-08 devnull d->mode |= DMDEVICE;
243 2acd6fa6 2005-02-08 devnull d->qid.path = ('c'<<16)|st->st_rdev;
245 fd04aace 2003-11-23 devnull /* fetch real size for disks */
246 7317e77d 2004-04-25 devnull #ifdef _HAVEDISKSIZE
247 3d72637f 2004-04-26 devnull if(S_ISBLK(st->st_mode) && (fd = open(name, O_RDONLY)) >= 0){
248 7317e77d 2004-04-25 devnull d->length = disksize(fd, major(st->st_dev));
249 7317e77d 2004-04-25 devnull close(fd);
252 1c253ceb 2003-11-23 devnull #ifdef _HAVEDISKLABEL
253 669250d1 2003-12-03 devnull if(isdisk(st)){
254 fd04aace 2003-11-23 devnull int fd, n;
255 fd04aace 2003-11-23 devnull struct disklabel lab;
257 fd04aace 2003-11-23 devnull if((fd = open(name, O_RDONLY)) < 0)
258 fd04aace 2003-11-23 devnull goto nosize;
259 fd04aace 2003-11-23 devnull if(ioctl(fd, DIOCGDINFO, &lab) < 0)
260 fd04aace 2003-11-23 devnull goto nosize;
261 fd04aace 2003-11-23 devnull n = minor(st->st_rdev)&7;
262 fd04aace 2003-11-23 devnull if(n >= lab.d_npartitions)
263 fd04aace 2003-11-23 devnull goto nosize;
265 fd04aace 2003-11-23 devnull d->length = (vlong)(lab.d_partitions[n].p_size) * lab.d_secsize;
268 fd04aace 2003-11-23 devnull if(fd >= 0)
269 fd04aace 2003-11-23 devnull close(fd);
274 fd04aace 2003-11-23 devnull return sz;