2003-11-23 18:12:54 +00:00
|
|
|
#include <u.h>
|
2003-11-25 01:40:27 +00:00
|
|
|
#define NOPLAN9DEFINES
|
2003-11-23 18:12:54 +00:00
|
|
|
#include <libc.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
extern int _p9dir(struct stat*, char*, Dir*, char**, char*);
|
|
|
|
|
2003-11-25 02:11:11 +00:00
|
|
|
#if defined(__linux__)
|
2003-11-24 00:43:41 +00:00
|
|
|
static int
|
2003-11-25 02:11:11 +00:00
|
|
|
mygetdents(int fd, struct dirent *buf, int n)
|
|
|
|
{
|
|
|
|
off_t off;
|
2003-12-09 06:37:26 +00:00
|
|
|
int nn;
|
2003-11-25 02:11:11 +00:00
|
|
|
|
2004-01-09 20:07:49 +00:00
|
|
|
/* This doesn't match the man page, but it works in Debian with a 2.2 kernel */
|
2003-11-25 02:11:11 +00:00
|
|
|
off = p9seek(fd, 0, 1);
|
|
|
|
nn = getdirentries(fd, (void*)buf, n, &off);
|
|
|
|
return nn;
|
|
|
|
}
|
|
|
|
#elif defined(__APPLE__) || defined(__FreeBSD__)
|
|
|
|
static int
|
|
|
|
mygetdents(int fd, struct dirent *buf, int n)
|
2003-11-24 00:43:41 +00:00
|
|
|
{
|
2003-11-24 22:39:06 +00:00
|
|
|
long off;
|
2003-12-03 22:50:48 +00:00
|
|
|
return getdirentries(fd, (void*)buf, n, &off);
|
2003-11-24 00:43:41 +00:00
|
|
|
}
|
2003-11-25 02:11:11 +00:00
|
|
|
#elif defined(__sun__)
|
|
|
|
static int
|
|
|
|
mygetdents(int fd, struct dirent *buf, int n)
|
|
|
|
{
|
|
|
|
return getdents(fd, (void*)buf, n);
|
|
|
|
}
|
2003-12-03 22:50:48 +00:00
|
|
|
#endif
|
2003-11-24 00:43:41 +00:00
|
|
|
|
2003-11-23 18:12:54 +00:00
|
|
|
static int
|
|
|
|
countde(char *p, int n)
|
|
|
|
{
|
|
|
|
char *e;
|
|
|
|
int m;
|
|
|
|
struct dirent *de;
|
|
|
|
|
|
|
|
e = p+n;
|
|
|
|
m = 0;
|
|
|
|
while(p < e){
|
|
|
|
de = (struct dirent*)p;
|
|
|
|
if(de->d_reclen <= 4+2+2+1 || p+de->d_reclen > e)
|
|
|
|
break;
|
2003-11-23 19:49:17 +00:00
|
|
|
if(de->d_name[0]=='.' && de->d_name[1]==0)
|
|
|
|
de->d_name[0] = 0;
|
|
|
|
else if(de->d_name[0]=='.' && de->d_name[1]=='.' && de->d_name[2]==0)
|
|
|
|
de->d_name[0] = 0;
|
2003-12-04 00:17:10 +00:00
|
|
|
m++;
|
2003-11-23 18:12:54 +00:00
|
|
|
p += de->d_reclen;
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dirpackage(int fd, char *buf, int n, Dir **dp)
|
|
|
|
{
|
|
|
|
int oldwd;
|
|
|
|
char *p, *str, *estr;
|
|
|
|
int i, nstr, m;
|
|
|
|
struct dirent *de;
|
|
|
|
struct stat st;
|
|
|
|
Dir *d;
|
|
|
|
|
|
|
|
n = countde(buf, n);
|
|
|
|
if(n <= 0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
if((oldwd = open(".", O_RDONLY)) < 0)
|
|
|
|
return -1;
|
|
|
|
if(fchdir(fd) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
nstr = 0;
|
2003-12-04 00:17:10 +00:00
|
|
|
|
2003-11-23 18:12:54 +00:00
|
|
|
for(i=0; i<n; i++){
|
|
|
|
de = (struct dirent*)p;
|
2003-12-04 00:17:10 +00:00
|
|
|
if(de->d_name[0] == 0)
|
|
|
|
/* nothing */ {}
|
|
|
|
else if(stat(de->d_name, &st) < 0)
|
2003-11-23 19:49:17 +00:00
|
|
|
de->d_name[0] = 0;
|
2003-11-23 18:12:54 +00:00
|
|
|
else
|
|
|
|
nstr += _p9dir(&st, de->d_name, nil, nil, nil);
|
|
|
|
p += de->d_reclen;
|
|
|
|
}
|
|
|
|
|
|
|
|
d = malloc(sizeof(Dir)*n+nstr);
|
|
|
|
if(d == nil){
|
|
|
|
fchdir(oldwd);
|
|
|
|
close(oldwd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
str = (char*)&d[n];
|
|
|
|
estr = str+nstr;
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
m = 0;
|
|
|
|
for(i=0; i<n; i++){
|
|
|
|
de = (struct dirent*)p;
|
2003-11-23 19:49:17 +00:00
|
|
|
if(de->d_name[0] != 0 && stat(de->d_name, &st) >= 0)
|
2003-11-23 18:12:54 +00:00
|
|
|
_p9dir(&st, de->d_name, &d[m++], &str, estr);
|
|
|
|
p += de->d_reclen;
|
|
|
|
}
|
|
|
|
|
|
|
|
fchdir(oldwd);
|
|
|
|
close(oldwd);
|
|
|
|
*dp = d;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
dirread(int fd, Dir **dp)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
struct stat st;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
*dp = 0;
|
|
|
|
|
|
|
|
if(fstat(fd, &st) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(st.st_blksize < 8192)
|
|
|
|
st.st_blksize = 8192;
|
|
|
|
|
|
|
|
buf = malloc(st.st_blksize);
|
|
|
|
if(buf == nil)
|
|
|
|
return -1;
|
|
|
|
|
2003-11-24 22:39:06 +00:00
|
|
|
n = mygetdents(fd, (void*)buf, st.st_blksize);
|
2003-11-23 18:12:54 +00:00
|
|
|
if(n < 0){
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
n = dirpackage(fd, buf, n, dp);
|
|
|
|
free(buf);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long
|
|
|
|
dirreadall(int fd, Dir **d)
|
|
|
|
{
|
|
|
|
uchar *buf, *nbuf;
|
|
|
|
long n, ts;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if(fstat(fd, &st) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(st.st_blksize < 8192)
|
|
|
|
st.st_blksize = 8192;
|
|
|
|
|
|
|
|
buf = nil;
|
|
|
|
ts = 0;
|
|
|
|
for(;;){
|
|
|
|
nbuf = realloc(buf, ts+st.st_blksize);
|
|
|
|
if(nbuf == nil){
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf = nbuf;
|
2003-11-24 22:39:06 +00:00
|
|
|
n = mygetdents(fd, (void*)(buf+ts), st.st_blksize);
|
2003-11-23 18:12:54 +00:00
|
|
|
if(n <= 0)
|
|
|
|
break;
|
|
|
|
ts += n;
|
|
|
|
}
|
|
|
|
if(ts >= 0)
|
|
|
|
ts = dirpackage(fd, buf, ts, d);
|
|
|
|
free(buf);
|
|
|
|
if(ts == 0 && n < 0)
|
|
|
|
return -1;
|
|
|
|
return ts;
|
|
|
|
}
|