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 memset(&geo, 0, sizeof geo);
57 l = 0;
58 u64 = 0;
59 #ifdef BLKGETSIZE64
60 if(ioctl(fd, BLKGETSIZE64, &u64) >= 0)
61 return u64;
62 #endif
63 if(ioctl(fd, BLKGETSIZE, &l) >= 0)
64 return l*512;
65 if(ioctl(fd, HDIO_GETGEO, &geo) >= 0)
66 return (vlong)geo.heads*geo.sectors*geo.cylinders*512;
67 return 0;
68 }
69 #define _HAVEDISKSIZE
70 #endif
72 #if !defined(__linux__) && !defined(__sun__)
73 #define _HAVESTGEN
74 #endif
76 /*
77 * Caching the last group and passwd looked up is
78 * a significant win (stupidly enough) on most systems.
79 * It's not safe for threaded programs, but neither is using
80 * getpwnam in the first place, so I'm not too worried.
81 */
82 int
83 _p9dir(struct stat *lst, struct stat *st, char *name, Dir *d, char **str, char *estr)
84 {
85 char *s;
86 char tmp[20];
87 static struct group *g;
88 static struct passwd *p;
89 static int gid, uid;
90 int sz, fd;
92 fd = -1;
93 USED(fd);
94 sz = 0;
95 if(d)
96 memset(d, 0, sizeof *d);
98 /* name */
99 s = strrchr(name, '/');
100 if(s)
101 s++;
102 if(!s || !*s)
103 s = name;
104 if(*s == '/')
105 s++;
106 if(*s == 0)
107 s = "/";
108 if(d){
109 if(*str + strlen(s)+1 > estr)
110 d->name = "oops";
111 else{
112 strcpy(*str, s);
113 d->name = *str;
114 *str += strlen(*str)+1;
117 sz += strlen(s)+1;
119 /* user */
120 if(p && st->st_uid == uid && p->pw_uid == uid)
122 else{
123 p = getpwuid(st->st_uid);
124 uid = st->st_uid;
126 if(p == nil){
127 snprint(tmp, sizeof tmp, "%d", (int)st->st_uid);
128 s = tmp;
129 }else
130 s = p->pw_name;
131 sz += strlen(s)+1;
132 if(d){
133 if(*str+strlen(s)+1 > estr)
134 d->uid = "oops";
135 else{
136 strcpy(*str, s);
137 d->uid = *str;
138 *str += strlen(*str)+1;
142 /* group */
143 if(g && st->st_gid == gid && g->gr_gid == gid)
145 else{
146 g = getgrgid(st->st_gid);
147 gid = st->st_gid;
149 if(g == nil){
150 snprint(tmp, sizeof tmp, "%d", (int)st->st_gid);
151 s = tmp;
152 }else
153 s = g->gr_name;
154 sz += strlen(s)+1;
155 if(d){
156 if(*str + strlen(s)+1 > estr)
157 d->gid = "oops";
158 else{
159 strcpy(*str, s);
160 d->gid = *str;
161 *str += strlen(*str)+1;
165 if(d){
166 d->type = 'M';
168 d->muid = "";
169 d->qid.path = ((uvlong)st->st_dev<<32) | st->st_ino;
170 #ifdef _HAVESTGEN
171 d->qid.vers = st->st_gen;
172 #endif
173 d->mode = st->st_mode&0777;
174 d->atime = st->st_atime;
175 d->mtime = st->st_mtime;
176 d->length = st->st_size;
178 if(S_ISDIR(st->st_mode)){
179 d->length = 0;
180 d->mode |= DMDIR;
181 d->qid.type = QTDIR;
183 if(S_ISLNK(lst->st_mode)) /* yes, lst not st */
184 d->mode |= DMSYMLINK;
185 if(S_ISFIFO(st->st_mode))
186 d->mode |= DMNAMEDPIPE;
187 if(S_ISSOCK(st->st_mode))
188 d->mode |= DMSOCKET;
189 if(S_ISBLK(st->st_mode)){
190 d->mode |= DMDEVICE;
191 d->qid.path = ('b'<<16)|st->st_rdev;
193 if(S_ISCHR(st->st_mode)){
194 d->mode |= DMDEVICE;
195 d->qid.path = ('c'<<16)|st->st_rdev;
197 /* fetch real size for disks */
198 #ifdef _HAVEDISKSIZE
199 if(S_ISBLK(st->st_mode) && (fd = open(name, O_RDONLY)) >= 0){
200 d->length = disksize(fd, major(st->st_dev));
201 close(fd);
203 #endif
204 #ifdef _HAVEDISKLABEL
205 if(isdisk(st)){
206 int fd, n;
207 struct disklabel lab;
209 if((fd = open(name, O_RDONLY)) < 0)
210 goto nosize;
211 if(ioctl(fd, DIOCGDINFO, &lab) < 0)
212 goto nosize;
213 n = minor(st->st_rdev)&7;
214 if(n >= lab.d_npartitions)
215 goto nosize;
217 d->length = (vlong)(lab.d_partitions[n].p_size) * lab.d_secsize;
219 nosize:
220 if(fd >= 0)
221 close(fd);
223 #endif
226 return sz;