plan9port/src/lib9/_p9dir.c

216 lines
3.7 KiB
C
Raw Normal View History

2003-11-23 20:04:53 +00:00
#include <u.h>
#define NOPLAN9DEFINES
#include <libc.h>
#include <sys/types.h>
2003-11-23 18:12:54 +00:00
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
2003-11-25 02:11:11 +00:00
#if defined(__FreeBSD__)
#include <sys/disklabel.h>
static int diskdev[] = {
151, /* aacd */
116, /* ad */
157, /* ar */
118, /* afd */
133, /* amrd */
13, /* da */
102, /* fla */
109, /* idad */
95, /* md */
131, /* mlxd */
168, /* pst */
147, /* twed */
43, /* vn */
3, /* wd */
87, /* wfd */
};
static int
isdisk(struct stat *st)
{
int i, dev;
if(!S_ISCHR(st->st_mode))
return 0;
dev = major(st->st_rdev);
for(i=0; i<nelem(diskdev); i++)
if(diskdev[i] == dev)
return 1;
return 0;
}
2003-11-25 02:11:11 +00:00
#define _HAVEDISKLABEL
#endif
#if defined(__linux__)
#include <linux/hdreg.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
static vlong
disksize(int fd, int dev)
{
u64int u64;
long l;
struct hd_geometry geo;
2004-06-09 14:13:49 +00:00
memset(&geo, 0, sizeof geo);
l = 0;
u64 = 0;
2004-05-15 23:16:38 +00:00
#ifdef BLKGETSIZE64
if(ioctl(fd, BLKGETSIZE64, &u64) >= 0)
return u64;
2004-05-15 23:16:38 +00:00
#endif
if(ioctl(fd, BLKGETSIZE, &l) >= 0)
return l*512;
if(ioctl(fd, HDIO_GETGEO, &geo) >= 0)
return (vlong)geo.heads*geo.sectors*geo.cylinders*512;
return 0;
}
#define _HAVEDISKSIZE
#endif
2003-11-25 02:11:11 +00:00
#if !defined(__linux__) && !defined(__sun__)
#define _HAVESTGEN
#endif
/*
* Caching the last group and passwd looked up is
* a significant win (stupidly enough) on most systems.
* It's not safe for threaded programs, but neither is using
* getpwnam in the first place, so I'm not too worried.
*/
2003-11-23 18:12:54 +00:00
int
_p9dir(struct stat *st, char *name, Dir *d, char **str, char *estr)
{
char *s;
char tmp[20];
static struct group *g;
static struct passwd *p;
static int gid, uid;
int sz, fd;
2003-11-23 18:12:54 +00:00
2004-05-14 20:08:02 +00:00
fd = -1;
USED(fd);
2003-11-23 18:12:54 +00:00
sz = 0;
if(d)
memset(d, 0, sizeof *d);
/* name */
s = strrchr(name, '/');
if(s)
s++;
if(!s || !*s)
s = name;
if(*s == '/')
s++;
if(*s == 0)
s = "/";
if(d){
if(*str + strlen(s)+1 > estr)
d->name = "oops";
else{
strcpy(*str, s);
d->name = *str;
*str += strlen(*str)+1;
}
}
sz += strlen(s)+1;
/* user */
if(p && st->st_uid == uid && p->pw_uid == uid)
;
else{
p = getpwuid(st->st_uid);
uid = st->st_uid;
}
2003-11-23 18:12:54 +00:00
if(p == nil){
snprint(tmp, sizeof tmp, "%d", (int)st->st_uid);
s = tmp;
}else
s = p->pw_name;
sz += strlen(s)+1;
if(d){
if(*str+strlen(s)+1 > estr)
d->uid = "oops";
else{
strcpy(*str, s);
d->uid = *str;
*str += strlen(*str)+1;
}
}
/* group */
if(g && st->st_gid == gid && g->gr_gid == gid)
;
else{
g = getgrgid(st->st_gid);
gid = st->st_gid;
}
2003-11-23 18:12:54 +00:00
if(g == nil){
snprint(tmp, sizeof tmp, "%d", (int)st->st_gid);
s = tmp;
}else
s = g->gr_name;
sz += strlen(s)+1;
if(d){
if(*str + strlen(s)+1 > estr)
d->gid = "oops";
else{
strcpy(*str, s);
d->gid = *str;
*str += strlen(*str)+1;
}
}
if(d){
d->type = 'M';
d->muid = "";
d->qid.path = ((uvlong)st->st_dev<<32) | st->st_ino;
2003-11-23 19:49:17 +00:00
#ifdef _HAVESTGEN
2003-11-23 18:12:54 +00:00
d->qid.vers = st->st_gen;
2003-11-23 19:49:17 +00:00
#endif
2003-11-23 18:12:54 +00:00
d->mode = st->st_mode&0777;
d->atime = st->st_atime;
d->mtime = st->st_mtime;
d->length = st->st_size;
if(S_ISDIR(st->st_mode)){
d->length = 0;
d->mode |= DMDIR;
d->qid.type = QTDIR;
}
/* fetch real size for disks */
#ifdef _HAVEDISKSIZE
2004-04-26 19:28:28 +00:00
if(S_ISBLK(st->st_mode) && (fd = open(name, O_RDONLY)) >= 0){
d->length = disksize(fd, major(st->st_dev));
close(fd);
}
#endif
2003-11-23 19:49:17 +00:00
#ifdef _HAVEDISKLABEL
if(isdisk(st)){
2003-11-23 18:12:54 +00:00
int fd, n;
struct disklabel lab;
if((fd = open(name, O_RDONLY)) < 0)
goto nosize;
if(ioctl(fd, DIOCGDINFO, &lab) < 0)
goto nosize;
n = minor(st->st_rdev)&7;
if(n >= lab.d_npartitions)
goto nosize;
d->length = (vlong)(lab.d_partitions[n].p_size) * lab.d_secsize;
nosize:
if(fd >= 0)
close(fd);
}
2003-11-23 19:49:17 +00:00
#endif
2003-11-23 18:12:54 +00:00
}
return sz;
}