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