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__) || defined(__OpenBSD__)
11 #include <sys/disklabel.h>
12 #include <sys/ioctl.h>
13 static int diskdev[] = {
14 151, /* aacd */
15 116, /* ad */
16 157, /* ar */
17 118, /* afd */
18 133, /* amrd */
19 13, /* da */
20 102, /* fla */
21 109, /* idad */
22 95, /* md */
23 131, /* mlxd */
24 168, /* pst */
25 147, /* twed */
26 43, /* vn */
27 3, /* wd */
28 87, /* wfd */
29 };
30 static int
31 isdisk(struct stat *st)
32 {
33 int i, dev;
35 if(!S_ISCHR(st->st_mode))
36 return 0;
37 dev = major(st->st_rdev);
38 for(i=0; i<nelem(diskdev); i++)
39 if(diskdev[i] == dev)
40 return 1;
41 return 0;
42 }
43 #define _HAVEDISKLABEL
44 #endif
46 #if defined(__linux__)
47 #include <linux/hdreg.h>
48 #include <linux/fs.h>
49 #include <sys/ioctl.h>
50 #undef major
51 #define major(dev) ((int)(((dev) >> 8) & 0xff))
52 static vlong
53 disksize(int fd, int dev)
54 {
55 u64int u64;
56 long l;
57 struct hd_geometry geo;
59 memset(&geo, 0, sizeof geo);
60 l = 0;
61 u64 = 0;
62 #ifdef BLKGETSIZE64
63 if(ioctl(fd, BLKGETSIZE64, &u64) >= 0)
64 return u64;
65 #endif
66 if(ioctl(fd, BLKGETSIZE, &l) >= 0)
67 return (vlong)l*512;
68 if(ioctl(fd, HDIO_GETGEO, &geo) >= 0)
69 return (vlong)geo.heads*geo.sectors*geo.cylinders*512;
70 return 0;
71 }
72 #define _HAVEDISKSIZE
73 #endif
75 #if !defined(__linux__) && !defined(__sun__)
76 #define _HAVESTGEN
77 #endif
79 /*
80 * Caching the last group and passwd looked up is
81 * a significant win (stupidly enough) on most systems.
82 * It's not safe for threaded programs, but neither is using
83 * getpwnam in the first place, so I'm not too worried.
84 */
85 int
86 _p9dir(struct stat *lst, struct stat *st, char *name, Dir *d, char **str, char *estr)
87 {
88 char *s;
89 char tmp[20];
90 static struct group *g;
91 static struct passwd *p;
92 static int gid, uid;
93 int sz, fd;
95 fd = -1;
96 USED(fd);
97 sz = 0;
98 if(d)
99 memset(d, 0, sizeof *d);
101 /* name */
102 s = strrchr(name, '/');
103 if(s)
104 s++;
105 if(!s || !*s)
106 s = name;
107 if(*s == '/')
108 s++;
109 if(*s == 0)
110 s = "/";
111 if(d){
112 if(*str + strlen(s)+1 > estr)
113 d->name = "oops";
114 else{
115 strcpy(*str, s);
116 d->name = *str;
117 *str += strlen(*str)+1;
120 sz += strlen(s)+1;
122 /* user */
123 if(p && st->st_uid == uid && p->pw_uid == uid)
125 else{
126 p = getpwuid(st->st_uid);
127 uid = st->st_uid;
129 if(p == nil){
130 snprint(tmp, sizeof tmp, "%d", (int)st->st_uid);
131 s = tmp;
132 }else
133 s = p->pw_name;
134 sz += strlen(s)+1;
135 if(d){
136 if(*str+strlen(s)+1 > estr)
137 d->uid = "oops";
138 else{
139 strcpy(*str, s);
140 d->uid = *str;
141 *str += strlen(*str)+1;
145 /* group */
146 if(g && st->st_gid == gid && g->gr_gid == gid)
148 else{
149 g = getgrgid(st->st_gid);
150 gid = st->st_gid;
152 if(g == nil){
153 snprint(tmp, sizeof tmp, "%d", (int)st->st_gid);
154 s = tmp;
155 }else
156 s = g->gr_name;
157 sz += strlen(s)+1;
158 if(d){
159 if(*str + strlen(s)+1 > estr)
160 d->gid = "oops";
161 else{
162 strcpy(*str, s);
163 d->gid = *str;
164 *str += strlen(*str)+1;
168 if(d){
169 d->type = 'M';
171 d->muid = "";
172 d->qid.path = ((uvlong)st->st_dev<<32) | st->st_ino;
173 #ifdef _HAVESTGEN
174 d->qid.vers = st->st_gen;
175 #endif
176 d->mode = st->st_mode&0777;
177 d->atime = st->st_atime;
178 d->mtime = st->st_mtime;
179 d->length = st->st_size;
181 if(S_ISDIR(st->st_mode)){
182 d->length = 0;
183 d->mode |= DMDIR;
184 d->qid.type = QTDIR;
186 if(S_ISLNK(lst->st_mode)) /* yes, lst not st */
187 d->mode |= DMSYMLINK;
188 if(S_ISFIFO(st->st_mode))
189 d->mode |= DMNAMEDPIPE;
190 if(S_ISSOCK(st->st_mode))
191 d->mode |= DMSOCKET;
192 if(S_ISBLK(st->st_mode)){
193 d->mode |= DMDEVICE;
194 d->qid.path = ('b'<<16)|st->st_rdev;
196 if(S_ISCHR(st->st_mode)){
197 d->mode |= DMDEVICE;
198 d->qid.path = ('c'<<16)|st->st_rdev;
200 /* fetch real size for disks */
201 #ifdef _HAVEDISKSIZE
202 if(S_ISBLK(st->st_mode) && (fd = open(name, O_RDONLY)) >= 0){
203 d->length = disksize(fd, major(st->st_dev));
204 close(fd);
206 #endif
207 #ifdef _HAVEDISKLABEL
208 if(isdisk(st)){
209 int fd, n;
210 struct disklabel lab;
212 if((fd = open(name, O_RDONLY)) < 0)
213 goto nosize;
214 if(ioctl(fd, DIOCGDINFO, &lab) < 0)
215 goto nosize;
216 n = minor(st->st_rdev)&7;
217 if(n >= lab.d_npartitions)
218 goto nosize;
220 d->length = (vlong)(lab.d_partitions[n].p_size) * lab.d_secsize;
222 nosize:
223 if(fd >= 0)
224 close(fd);
226 #endif
229 return sz;