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 97529508 2007-05-15 devnull #if defined(__FreeBSD__)
11 97529508 2007-05-15 devnull #include <sys/disk.h>
12 64bcfff3 2003-11-25 devnull #include <sys/disklabel.h>
13 fe8c925e 2005-07-21 devnull #include <sys/ioctl.h>
16 69bdb78d 2007-05-11 devnull #if defined(__OpenBSD__)
17 97529508 2007-05-15 devnull #include <sys/disklabel.h>
18 97529508 2007-05-15 devnull #include <sys/ioctl.h>
19 97529508 2007-05-15 devnull #define _HAVEDISKLABEL
20 669250d1 2003-12-03 devnull static int diskdev[] = {
21 669250d1 2003-12-03 devnull 151, /* aacd */
22 669250d1 2003-12-03 devnull 116, /* ad */
23 669250d1 2003-12-03 devnull 157, /* ar */
24 669250d1 2003-12-03 devnull 118, /* afd */
25 669250d1 2003-12-03 devnull 133, /* amrd */
26 669250d1 2003-12-03 devnull 13, /* da */
27 669250d1 2003-12-03 devnull 102, /* fla */
28 669250d1 2003-12-03 devnull 109, /* idad */
29 669250d1 2003-12-03 devnull 95, /* md */
30 669250d1 2003-12-03 devnull 131, /* mlxd */
31 669250d1 2003-12-03 devnull 168, /* pst */
32 669250d1 2003-12-03 devnull 147, /* twed */
33 669250d1 2003-12-03 devnull 43, /* vn */
34 669250d1 2003-12-03 devnull 3, /* wd */
35 669250d1 2003-12-03 devnull 87, /* wfd */
36 a98755e0 2006-01-07 devnull 4, /* da on FreeBSD 5 */
38 669250d1 2003-12-03 devnull static int
39 669250d1 2003-12-03 devnull isdisk(struct stat *st)
41 669250d1 2003-12-03 devnull int i, dev;
43 669250d1 2003-12-03 devnull if(!S_ISCHR(st->st_mode))
44 669250d1 2003-12-03 devnull return 0;
45 669250d1 2003-12-03 devnull dev = major(st->st_rdev);
46 669250d1 2003-12-03 devnull for(i=0; i<nelem(diskdev); i++)
47 669250d1 2003-12-03 devnull if(diskdev[i] == dev)
48 669250d1 2003-12-03 devnull return 1;
49 669250d1 2003-12-03 devnull return 0;
53 69bdb78d 2007-05-11 devnull #if defined(__FreeBSD__) /* maybe OpenBSD too? */
54 69bdb78d 2007-05-11 devnull char *diskdev[] = {
72 69bdb78d 2007-05-11 devnull static int
73 69bdb78d 2007-05-11 devnull isdisk(struct stat *st)
75 69bdb78d 2007-05-11 devnull char *name;
76 43f16cfa 2007-05-11 devnull int i, len;
78 69bdb78d 2007-05-11 devnull if(!S_ISCHR(st->st_mode))
79 69bdb78d 2007-05-11 devnull return 0;
80 69bdb78d 2007-05-11 devnull name = devname(st->st_rdev, S_IFCHR);
81 43f16cfa 2007-05-11 devnull for(i=0; i<nelem(diskdev); i++){
82 43f16cfa 2007-05-11 devnull len = strlen(diskdev[i]);
83 43f16cfa 2007-05-11 devnull if(strncmp(diskdev[i], name, len) == 0 && isdigit((uchar)name[len]))
84 69bdb78d 2007-05-11 devnull return 1;
86 69bdb78d 2007-05-11 devnull return 0;
91 7317e77d 2004-04-25 devnull #if defined(__linux__)
92 7317e77d 2004-04-25 devnull #include <linux/hdreg.h>
93 7317e77d 2004-04-25 devnull #include <linux/fs.h>
94 7317e77d 2004-04-25 devnull #include <sys/ioctl.h>
95 a3e9d893 2005-08-10 devnull #undef major
96 a3e9d893 2005-08-10 devnull #define major(dev) ((int)(((dev) >> 8) & 0xff))
97 7317e77d 2004-04-25 devnull static vlong
98 7317e77d 2004-04-25 devnull disksize(int fd, int dev)
100 7317e77d 2004-04-25 devnull u64int u64;
102 7317e77d 2004-04-25 devnull struct hd_geometry geo;
104 ceecb313 2004-06-09 devnull memset(&geo, 0, sizeof geo);
106 ceecb313 2004-06-09 devnull u64 = 0;
107 76e6aca8 2004-05-15 devnull #ifdef BLKGETSIZE64
108 7317e77d 2004-04-25 devnull if(ioctl(fd, BLKGETSIZE64, &u64) >= 0)
109 7317e77d 2004-04-25 devnull return u64;
111 7317e77d 2004-04-25 devnull if(ioctl(fd, BLKGETSIZE, &l) >= 0)
112 3d484b0d 2005-12-29 devnull return l*512;
113 7317e77d 2004-04-25 devnull if(ioctl(fd, HDIO_GETGEO, &geo) >= 0)
114 7317e77d 2004-04-25 devnull return (vlong)geo.heads*geo.sectors*geo.cylinders*512;
115 7317e77d 2004-04-25 devnull return 0;
117 7317e77d 2004-04-25 devnull #define _HAVEDISKSIZE
120 64bcfff3 2003-11-25 devnull #if !defined(__linux__) && !defined(__sun__)
121 64bcfff3 2003-11-25 devnull #define _HAVESTGEN
124 63fcc2bc 2006-06-12 devnull int _p9usepwlibrary = 1;
126 6f6553df 2004-04-08 devnull * Caching the last group and passwd looked up is
127 6f6553df 2004-04-08 devnull * a significant win (stupidly enough) on most systems.
128 6f6553df 2004-04-08 devnull * It's not safe for threaded programs, but neither is using
129 6f6553df 2004-04-08 devnull * getpwnam in the first place, so I'm not too worried.
132 0a229052 2005-02-08 devnull _p9dir(struct stat *lst, struct stat *st, char *name, Dir *d, char **str, char *estr)
134 fd04aace 2003-11-23 devnull char *s;
135 fd04aace 2003-11-23 devnull char tmp[20];
136 6f6553df 2004-04-08 devnull static struct group *g;
137 6f6553df 2004-04-08 devnull static struct passwd *p;
138 6f6553df 2004-04-08 devnull static int gid, uid;
139 7317e77d 2004-04-25 devnull int sz, fd;
141 631fe87f 2004-05-14 devnull fd = -1;
142 631fe87f 2004-05-14 devnull USED(fd);
145 fd04aace 2003-11-23 devnull memset(d, 0, sizeof *d);
147 fd04aace 2003-11-23 devnull /* name */
148 fd04aace 2003-11-23 devnull s = strrchr(name, '/');
151 fd04aace 2003-11-23 devnull if(!s || !*s)
152 fd04aace 2003-11-23 devnull s = name;
153 fd04aace 2003-11-23 devnull if(*s == '/')
155 fd04aace 2003-11-23 devnull if(*s == 0)
156 fd04aace 2003-11-23 devnull s = "/";
158 fd04aace 2003-11-23 devnull if(*str + strlen(s)+1 > estr)
159 fd04aace 2003-11-23 devnull d->name = "oops";
161 fd04aace 2003-11-23 devnull strcpy(*str, s);
162 fd04aace 2003-11-23 devnull d->name = *str;
163 fd04aace 2003-11-23 devnull *str += strlen(*str)+1;
166 fd04aace 2003-11-23 devnull sz += strlen(s)+1;
168 fd04aace 2003-11-23 devnull /* user */
169 6f6553df 2004-04-08 devnull if(p && st->st_uid == uid && p->pw_uid == uid)
171 63fcc2bc 2006-06-12 devnull else if(_p9usepwlibrary){
172 6f6553df 2004-04-08 devnull p = getpwuid(st->st_uid);
173 6f6553df 2004-04-08 devnull uid = st->st_uid;
175 63fcc2bc 2006-06-12 devnull if(p == nil || st->st_uid != uid || p->pw_uid != uid){
176 fd04aace 2003-11-23 devnull snprint(tmp, sizeof tmp, "%d", (int)st->st_uid);
177 fd04aace 2003-11-23 devnull s = tmp;
179 fd04aace 2003-11-23 devnull s = p->pw_name;
180 fd04aace 2003-11-23 devnull sz += strlen(s)+1;
182 fd04aace 2003-11-23 devnull if(*str+strlen(s)+1 > estr)
183 fd04aace 2003-11-23 devnull d->uid = "oops";
185 fd04aace 2003-11-23 devnull strcpy(*str, s);
186 fd04aace 2003-11-23 devnull d->uid = *str;
187 fd04aace 2003-11-23 devnull *str += strlen(*str)+1;
191 fd04aace 2003-11-23 devnull /* group */
192 6f6553df 2004-04-08 devnull if(g && st->st_gid == gid && g->gr_gid == gid)
194 63fcc2bc 2006-06-12 devnull else if(_p9usepwlibrary){
195 6f6553df 2004-04-08 devnull g = getgrgid(st->st_gid);
196 6f6553df 2004-04-08 devnull gid = st->st_gid;
198 63fcc2bc 2006-06-12 devnull if(g == nil || st->st_gid != gid || g->gr_gid != gid){
199 fd04aace 2003-11-23 devnull snprint(tmp, sizeof tmp, "%d", (int)st->st_gid);
200 fd04aace 2003-11-23 devnull s = tmp;
202 fd04aace 2003-11-23 devnull s = g->gr_name;
203 fd04aace 2003-11-23 devnull sz += strlen(s)+1;
205 fd04aace 2003-11-23 devnull if(*str + strlen(s)+1 > estr)
206 fd04aace 2003-11-23 devnull d->gid = "oops";
208 fd04aace 2003-11-23 devnull strcpy(*str, s);
209 fd04aace 2003-11-23 devnull d->gid = *str;
210 fd04aace 2003-11-23 devnull *str += strlen(*str)+1;
215 fd04aace 2003-11-23 devnull d->type = 'M';
217 fd04aace 2003-11-23 devnull d->muid = "";
218 fd04aace 2003-11-23 devnull d->qid.path = ((uvlong)st->st_dev<<32) | st->st_ino;
219 1c253ceb 2003-11-23 devnull #ifdef _HAVESTGEN
220 fd04aace 2003-11-23 devnull d->qid.vers = st->st_gen;
222 3d484b0d 2005-12-29 devnull if(d->qid.vers == 0)
223 3d484b0d 2005-12-29 devnull d->qid.vers = st->st_mtime + st->st_ctime;
224 fd04aace 2003-11-23 devnull d->mode = st->st_mode&0777;
225 fd04aace 2003-11-23 devnull d->atime = st->st_atime;
226 fd04aace 2003-11-23 devnull d->mtime = st->st_mtime;
227 fd04aace 2003-11-23 devnull d->length = st->st_size;
229 fd04aace 2003-11-23 devnull if(S_ISDIR(st->st_mode)){
230 fd04aace 2003-11-23 devnull d->length = 0;
231 fd04aace 2003-11-23 devnull d->mode |= DMDIR;
232 fd04aace 2003-11-23 devnull d->qid.type = QTDIR;
234 98660df2 2005-02-21 devnull if(S_ISLNK(lst->st_mode)) /* yes, lst not st */
235 2acd6fa6 2005-02-08 devnull d->mode |= DMSYMLINK;
236 2acd6fa6 2005-02-08 devnull if(S_ISFIFO(st->st_mode))
237 2acd6fa6 2005-02-08 devnull d->mode |= DMNAMEDPIPE;
238 2acd6fa6 2005-02-08 devnull if(S_ISSOCK(st->st_mode))
239 2acd6fa6 2005-02-08 devnull d->mode |= DMSOCKET;
240 2acd6fa6 2005-02-08 devnull if(S_ISBLK(st->st_mode)){
241 2acd6fa6 2005-02-08 devnull d->mode |= DMDEVICE;
242 2acd6fa6 2005-02-08 devnull d->qid.path = ('b'<<16)|st->st_rdev;
244 2acd6fa6 2005-02-08 devnull if(S_ISCHR(st->st_mode)){
245 2acd6fa6 2005-02-08 devnull d->mode |= DMDEVICE;
246 2acd6fa6 2005-02-08 devnull d->qid.path = ('c'<<16)|st->st_rdev;
248 fd04aace 2003-11-23 devnull /* fetch real size for disks */
249 7317e77d 2004-04-25 devnull #ifdef _HAVEDISKSIZE
250 3d72637f 2004-04-26 devnull if(S_ISBLK(st->st_mode) && (fd = open(name, O_RDONLY)) >= 0){
251 7317e77d 2004-04-25 devnull d->length = disksize(fd, major(st->st_dev));
252 7317e77d 2004-04-25 devnull close(fd);
255 83ab7d88 2007-11-27 rsc #if defined(DIOCGMEDIASIZE)
256 97529508 2007-05-15 devnull if(isdisk(st)){
258 97529508 2007-05-15 devnull off_t mediasize;
260 97529508 2007-05-15 devnull if((fd = open(name, O_RDONLY)) >= 0){
261 97529508 2007-05-15 devnull if(ioctl(fd, DIOCGMEDIASIZE, &mediasize) >= 0)
262 97529508 2007-05-15 devnull d->length = mediasize;
263 97529508 2007-05-15 devnull close(fd);
266 97529508 2007-05-15 devnull #elif defined(_HAVEDISKLABEL)
267 669250d1 2003-12-03 devnull if(isdisk(st)){
268 fd04aace 2003-11-23 devnull int fd, n;
269 fd04aace 2003-11-23 devnull struct disklabel lab;
271 fd04aace 2003-11-23 devnull if((fd = open(name, O_RDONLY)) < 0)
272 fd04aace 2003-11-23 devnull goto nosize;
273 fd04aace 2003-11-23 devnull if(ioctl(fd, DIOCGDINFO, &lab) < 0)
274 fd04aace 2003-11-23 devnull goto nosize;
275 fd04aace 2003-11-23 devnull n = minor(st->st_rdev)&7;
276 fd04aace 2003-11-23 devnull if(n >= lab.d_npartitions)
277 fd04aace 2003-11-23 devnull goto nosize;
279 fd04aace 2003-11-23 devnull d->length = (vlong)(lab.d_partitions[n].p_size) * lab.d_secsize;
282 fd04aace 2003-11-23 devnull if(fd >= 0)
283 fd04aace 2003-11-23 devnull close(fd);
288 fd04aace 2003-11-23 devnull return sz;