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(__APPLE__)
11 #define _HAVESTGEN
12 #include <sys/disk.h>
13 static vlong
14 disksize(int fd, struct stat *st)
15 {
16 u64int bc;
17 u32int bs;
19 bs = 0;
20 bc = 0;
21 ioctl(fd, DKIOCGETBLOCKSIZE, &bs);
22 ioctl(fd, DKIOCGETBLOCKCOUNT, &bc);
23 if(bs >0 && bc > 0)
24 return bc*bs;
25 return 0;
26 }
28 #elif defined(__FreeBSD__)
29 #define _HAVESTGEN
30 #include <sys/disk.h>
31 #include <sys/disklabel.h>
32 #include <sys/ioctl.h>
33 static vlong
34 disksize(int fd, struct stat *st)
35 {
36 off_t mediasize;
38 if(ioctl(fd, DIOCGMEDIASIZE, &mediasize) >= 0)
39 return mediasize;
40 return 0;
41 }
43 #elif defined(__OpenBSD__)
44 #define _HAVESTGEN
45 #include <sys/disklabel.h>
46 #include <sys/ioctl.h>
47 static vlong
48 disksize(int fd, struct stat *st)
49 {
50 struct disklabel lab;
51 int n;
53 if(!S_ISCHR(st->st_mode))
54 return 0;
55 if(ioctl(fd, DIOCGDINFO, &lab) < 0)
56 return 0;
57 n = minor(st->st_rdev)&7;
58 if(n >= lab.d_npartitions)
59 return 0;
60 return (vlong)lab.d_partitions[n].p_size * lab.d_secsize;
61 }
63 #elif defined(__linux__)
64 #include <linux/hdreg.h>
65 #include <linux/fs.h>
66 #include <sys/ioctl.h>
67 #undef major
68 #define major(dev) ((int)(((dev) >> 8) & 0xff))
69 static vlong
70 disksize(int fd, struct stat *st)
71 {
72 u64int u64;
73 long l;
74 struct hd_geometry geo;
76 memset(&geo, 0, sizeof geo);
77 l = 0;
78 u64 = 0;
79 #ifdef BLKGETSIZE64
80 if(ioctl(fd, BLKGETSIZE64, &u64) >= 0)
81 return u64;
82 #endif
83 if(ioctl(fd, BLKGETSIZE, &l) >= 0)
84 return l*512;
85 if(ioctl(fd, HDIO_GETGEO, &geo) >= 0)
86 return (vlong)geo.heads*geo.sectors*geo.cylinders*512;
87 return 0;
88 }
90 #else
91 static vlong
92 disksize(int fd, struct stat *st)
93 {
94 return 0;
95 }
96 #endif
98 int _p9usepwlibrary = 1;
99 /*
100 * Caching the last group and passwd looked up is
101 * a significant win (stupidly enough) on most systems.
102 * It's not safe for threaded programs, but neither is using
103 * getpwnam in the first place, so I'm not too worried.
104 */
105 int
106 _p9dir(struct stat *lst, struct stat *st, char *name, Dir *d, char **str, char *estr)
108 char *s;
109 char tmp[20];
110 static struct group *g;
111 static struct passwd *p;
112 static int gid, uid;
113 int sz, fd;
115 fd = -1;
116 USED(fd);
117 sz = 0;
118 if(d)
119 memset(d, 0, sizeof *d);
121 /* name */
122 s = strrchr(name, '/');
123 if(s)
124 s++;
125 if(!s || !*s)
126 s = name;
127 if(*s == '/')
128 s++;
129 if(*s == 0)
130 s = "/";
131 if(d){
132 if(*str + strlen(s)+1 > estr)
133 d->name = "oops";
134 else{
135 strcpy(*str, s);
136 d->name = *str;
137 *str += strlen(*str)+1;
140 sz += strlen(s)+1;
142 /* user */
143 if(p && st->st_uid == uid && p->pw_uid == uid)
145 else if(_p9usepwlibrary){
146 p = getpwuid(st->st_uid);
147 uid = st->st_uid;
149 if(p == nil || st->st_uid != uid || p->pw_uid != uid){
150 snprint(tmp, sizeof tmp, "%d", (int)st->st_uid);
151 s = tmp;
152 }else
153 s = p->pw_name;
154 sz += strlen(s)+1;
155 if(d){
156 if(*str+strlen(s)+1 > estr)
157 d->uid = "oops";
158 else{
159 strcpy(*str, s);
160 d->uid = *str;
161 *str += strlen(*str)+1;
165 /* group */
166 if(g && st->st_gid == gid && g->gr_gid == gid)
168 else if(_p9usepwlibrary){
169 g = getgrgid(st->st_gid);
170 gid = st->st_gid;
172 if(g == nil || st->st_gid != gid || g->gr_gid != gid){
173 snprint(tmp, sizeof tmp, "%d", (int)st->st_gid);
174 s = tmp;
175 }else
176 s = g->gr_name;
177 sz += strlen(s)+1;
178 if(d){
179 if(*str + strlen(s)+1 > estr)
180 d->gid = "oops";
181 else{
182 strcpy(*str, s);
183 d->gid = *str;
184 *str += strlen(*str)+1;
188 if(d){
189 d->type = 'M';
191 d->muid = "";
192 d->qid.path = ((uvlong)st->st_dev<<32) | st->st_ino;
193 #ifdef _HAVESTGEN
194 d->qid.vers = st->st_gen;
195 #endif
196 if(d->qid.vers == 0)
197 d->qid.vers = st->st_mtime + st->st_ctime;
198 d->mode = st->st_mode&0777;
199 d->atime = st->st_atime;
200 d->mtime = st->st_mtime;
201 d->length = st->st_size;
203 if(S_ISLNK(lst->st_mode)){ /* yes, lst not st */
204 d->mode |= DMSYMLINK;
205 d->length = lst->st_size;
207 else if(S_ISDIR(st->st_mode)){
208 d->length = 0;
209 d->mode |= DMDIR;
210 d->qid.type = QTDIR;
212 else if(S_ISFIFO(st->st_mode))
213 d->mode |= DMNAMEDPIPE;
214 else if(S_ISSOCK(st->st_mode))
215 d->mode |= DMSOCKET;
216 else if(S_ISBLK(st->st_mode)){
217 d->mode |= DMDEVICE;
218 d->qid.path = ('b'<<16)|st->st_rdev;
220 else if(S_ISCHR(st->st_mode)){
221 d->mode |= DMDEVICE;
222 d->qid.path = ('c'<<16)|st->st_rdev;
224 /* fetch real size for disks */
225 if(S_ISBLK(lst->st_mode) || S_ISCHR(lst->st_mode)){
226 if((fd = open(name, O_RDONLY)) >= 0){
227 d->length = disksize(fd, st);
228 close(fd);
233 return sz;