Compare commits

...

7 commits

Author SHA1 Message Date
Ori Bernstein
d1b72a27b7 walk: error on mutually exclusive flags 2024-12-12 20:35:58 +00:00
Ori Bernstein
b8797a1cc6 walk: fix skipped files, simplify seen() 2024-12-12 20:17:51 +00:00
Ori Bernstein
e59b7e28cd git: refactor capability parsing
this should be a nop, but set us up better
for handling and exposing capabilities in
git/serve.
2024-12-10 23:15:22 +00:00
Ori Bernstein
e58df8173f walk: show siblings with the same qid
the code for skipping directory loops in
walk was taken from du, but the desired
behavior for the two programs is differnt;

while du wants to count the total size used,
and double-counting directories would be a
mistake, walk wants to enumerate all finite
paths in a namespace.
2024-12-10 23:14:33 +00:00
Ori Bernstein
ace81cb1ae mkfiles: check for test/mkfile when recursing
Some programs have test directories without a test
mkfile; this causes spurious errors. Discriminate
more carefully.
2024-12-10 23:11:44 +00:00
Ori Bernstein
bf398d28c3 cpiofs: add newc support for cpiofs (thanks rminnich)
Linux initrds, uroot file systems, and
others generate newc cpio images; add
support for them.
2024-12-10 23:07:26 +00:00
Ori Bernstein
15f66c69d8 tar: use IOUNIT to compute Dblock 2024-12-10 23:05:38 +00:00
9 changed files with 212 additions and 147 deletions

View file

@ -134,33 +134,6 @@ branchmatch(char *br, char *pat)
return strcmp(br, name) == 0;
}
char *
matchcap(char *s, char *cap, int full)
{
if(strncmp(s, cap, strlen(cap)) == 0)
if(!full || strlen(s) == strlen(cap))
return s + strlen(cap);
return nil;
}
void
handlecaps(char *caps)
{
char *p, *n, *c, *r;
for(p = caps; p != nil; p = n){
n = strchr(p, ' ');
if(n != nil)
*n++ = 0;
if((c = matchcap(p, "symref=", 0)) != nil){
if((r = strchr(c, ':')) != nil){
*r++ = '\0';
print("symref %s %s\n", c, r);
}
}
}
}
void
fail(char *pack, char *idx, char *msg, ...)
{
@ -202,6 +175,7 @@ fetchpack(Conn *c)
int nref, refsz, first, nsent;
int i, l, n, req, pfd;
vlong packsz;
Capset cs;
Objset hadobj;
Object *o;
Objq haveq;
@ -220,8 +194,11 @@ fetchpack(Conn *c)
if(n == 0)
break;
if(first && n > strlen(buf))
handlecaps(buf + strlen(buf) + 1);
if(first && n > strlen(buf)){
parsecaps(buf + strlen(buf) + 1, &cs);
if(cs.symfrom[0] != 0)
print("symref %s %s\n", cs.symfrom, cs.symto);
}
first = 0;
getfields(buf, sp, nelem(sp), 1, " \t\n\r");

View file

@ -82,6 +82,14 @@ struct Hash {
uchar h[20];
};
struct Capset {
char symfrom[256];
char symto[256];
int sideband;
int sideband64k;
int report;
};
struct Conn {
int type;
int rfd;
@ -330,6 +338,7 @@ int gitconnect(Conn *, char *, char *);
int readphase(Conn *);
int writephase(Conn *);
void closeconn(Conn *);
void parsecaps(char *, Capset *);
/* queues */
void qinit(Objq*);

View file

@ -17,6 +17,42 @@ enum {
Nbranch = 32,
};
char *
matchcap(char *s, char *cap, int full)
{
if(strncmp(s, cap, strlen(cap)) == 0)
if(!full || strlen(s) == strlen(cap))
return s + strlen(cap);
return nil;
}
void
parsecaps(char *caps, Capset *cs)
{
char *p, *n, *c, *t;
memset(cs, 0, sizeof(*cs));
for(p = caps; p != nil; p = n){
n = strchr(p, ' ');
if(n != nil)
*n++ = 0;
if(matchcap(p, "report-status", 1) != nil)
cs->report = 1;
else if(matchcap(p, "side-band", 1) != nil)
cs->sideband = 1;
else if(matchcap(p, "side-band-64k", 1) != nil)
cs->sideband64k = 1;
else if((c = matchcap(p, "symref=", 0)) != nil){
if((t = strchr(c, ':')) == nil)
continue;
*t++ = '\0';
snprint(cs->symfrom, sizeof(cs->symfrom), c);
snprint(cs->symto, sizeof(cs->symto), t);
}
}
}
void
tracepkt(int v, char *pfx, char *b, int n)
{
@ -158,7 +194,7 @@ parseuri(char *uri, char *proto, char *host, char *port, char *path, char *repo)
snprint(port, Nport, "80");
else if(strncmp(proto, "hjgit", 5) == 0)
snprint(port, Nport, "17021");
else if(strncmp(proto, "gits", 4) == 0)
else if(strncmp(proto, "gits", 5) == 0)
snprint(port, Nport, "9419");
else
hasport = 0;

View file

@ -3,15 +3,8 @@
#include "git.h"
typedef struct Capset Capset;
typedef struct Map Map;
struct Capset {
int sideband;
int sideband64k;
int report;
};
struct Map {
char *ref;
Hash ours;
@ -89,33 +82,6 @@ readours(Hash **tailp, char ***refp)
return nu;
}
char *
matchcap(char *s, char *cap, int full)
{
if(strncmp(s, cap, strlen(cap)) == 0)
if(!full || strlen(s) == strlen(cap))
return s + strlen(cap);
return nil;
}
void
parsecaps(char *caps, Capset *cs)
{
char *p, *n;
for(p = caps; p != nil; p = n){
n = strchr(p, ' ');
if(n != nil)
*n++ = 0;
if(matchcap(p, "report-status", 1) != nil)
cs->report = 1;
if(matchcap(p, "side-band", 1) != nil)
cs->sideband = 1;
if(matchcap(p, "side-band-64k", 1) != nil)
cs->sideband64k = 1;
}
}
int
sendpack(Conn *c)
{

View file

@ -82,7 +82,7 @@ clean:V:
man:V: $MANFILES
test:VQ: $PROGS
if(test -d ./test)
if(test -f ./test/mkfile)
cd test && mk $MKFLAGS test
if not
status=()

View file

@ -63,7 +63,7 @@ $MAN/%: %.man
man:V: $MAN/$TARG
test:QV: $O.out $TESTDEP
if(test -d ./test){
if(test -f ./test/mkfile){
pwd
@{cd ./test && mk $MKFLAGS test}
}

View file

@ -114,12 +114,37 @@ rdasc(int n)
for(x = 0; n > 0; n--) {
if((y = egetc() - '0') & ~7)
sysfatal("not octal");
sysfatal("rdasc:%#c not octal", y);
x = x<<3 | y;
}
return x;
}
static vlong
rdascx(int n)
{
vlong x;
int y;
for(x = 0; n > 0; n--) {
y = egetc();
if ((y >= '0') && (y <= '9')){
x = x << 4 | (y - '0');
continue;
}
if ((y >= 'a') && (y <= 'f')){
x = x << 4 | 10 + (y - 'a');
continue;
}
if ((y >= 'A') && (y <= 'F')){
x = x << 4 | 10 + (y - 'A');
continue;
}
sysfatal("rdascx:%#x:not hex", y);
}
return x;
}
/* sysvr3 and sysvr4 skip records with names longer than 256. sysiii,
sysvr1, and sysvr2 overrun their 256 byte buffer */
static void
@ -157,10 +182,70 @@ rdsysiii(Fileinf *f)
f->name = buf;
}
/*
struct cpio_newc_header {
char c_magic[6];
char c_ino[8];
char c_mode[8];
char c_uid[8];
char c_gid[8];
char c_nlink[8];
char c_mtime[8];
char c_filesize[8];
char c_devmajor[8];
char c_devminor[8];
char c_rdevmajor[8];
char c_rdevminor[8];
char c_namesize[8];
char c_check[8];
};
*/
static void
rdnewc(Fileinf *f)
{
int namesz, n;
static char buf[256];
rdascx(8); /* ino */
f->mode = rdascx(8);
f->uid = rdascx(8);
f->gid = rdascx(8);
rdascx(8); /* nlink */
f->mdate = rdascx(8);
f->size = rdascx(8);
rdascx(8); //devmajor
rdascx(8); //devminor
rdascx(8); //rdevmajor
rdascx(8); //rdevminor
namesz = rdascx(8);
rdascx(8); // checksum
/* namesz includes the trailing nul */
if(namesz == 0)
sysfatal("name too small");
if(namesz > sizeof (buf))
sysfatal("name too big");
if((n = Bread(tape, buf, namesz)) < 0)
sysfatal("read error: %r");
if(n < namesz)
sysfatal("unexpected eof");
if(buf[n-1] != '\0')
sysfatal("no nul after file name");
if((n = strlen(buf)) != namesz-1)
sysfatal("mismatched name length: saw %d; expected %d", n, namesz-1);
f->name = buf;
if((Bseek(tape, 0, 1) & 3)) {
int skip = 4-(Bseek(tape, 0, 1) & 3);
for(int i = 0; i < skip; i++)
egetc();
}
}
static HdrReader *
rdmagic(void)
{
uchar buf[6];
uchar buf[8];
buf[0] = egetc();
buf[1] = egetc();
@ -171,10 +256,17 @@ rdmagic(void)
buf[3] = egetc();
buf[4] = egetc();
buf[5] = egetc();
buf[6] = 0;
if(memcmp(buf, "070707", 6) == 0)
return rdsysiii;
sysfatal("Out of phase--get MERT help");
if(memcmp(buf, "070701", 6) == 0)
return rdnewc;
for(int i = 0; i < 6; i++)
print("%#x,", buf[i]);
sysfatal("Out of phase(%s)--get MERT help", buf);
}
void
@ -187,7 +279,6 @@ populate(char *name)
record headers */
if((tape = Bopen(name, OREAD)) == nil)
sysfatal("Can't open argument file");
extern void (*_sysfatal)(char *, va_list);
_sysfatal = addrfatal;
@ -224,10 +315,16 @@ populate(char *name)
poppath(f, 1);
Bseek(tape, f.size, 1);
/* skip padding */
if(rdhdr == rdpwb11 && (Bseek(tape, 0, 1) & 1))
if(((rdhdr == rdpwb11)||(rdhdr == rdnewc)) && (Bseek(tape, 0, 1) & 1))
egetc();
/* sleazy alignment hack. Who needs a for loop? */
if(rdhdr == rdnewc && (Bseek(tape, 0, 1) & 2)) {
egetc();
egetc();
}
}
}

View file

@ -56,8 +56,7 @@ enum {
Binsize = 0x80, /* flag in size[0], from gnu: positive binary size */
Binnegsz = 0xff, /* flag in size[0]: negative binary size */
Nblock = 40, /* maximum blocksize */
Dblock = 20, /* default blocksize */
Dblock = IOUNIT/Tblock, /* blocksize */
Debug = 0,
};

View file

@ -4,15 +4,21 @@
#include <String.h>
#include <fcall.h>
typedef struct Seen Seen;
struct Seen {
Seen *up;
Dir *d;
};
int Cflag = 0;
int uflag = 0;
String *stfmt;
/* should turn these flags into a mask */
int dflag = 1;
int fflag = 1;
int tflag = 0;
int xflag = 0;
int printdirs = 1;
int printfiles = 1;
int temponly = 0;
int execonly = 0;
long maxdepth = ~(1<<31);
long mindepth = 0;
@ -21,8 +27,6 @@ Dir *dotdir = nil;
Biobuf *bout;
int seen(Dir*);
void
warn(char *fmt, ...)
{
@ -40,6 +44,17 @@ warn(char *fmt, ...)
fprint(2, "%s\n", buf);
}
int
seen(Seen *s, Dir *d)
{
for(; s != nil; s = s->up)
if(d->qid.path == s->d->qid.path
&& d->qid.type == s->d->qid.type
&& d->dev == s->d->dev)
return 1;
return 0;
}
void
dofile(char *path, Dir *f, int pathonly)
{
@ -47,8 +62,8 @@ dofile(char *path, Dir *f, int pathonly)
if(
(f == dotdir)
|| (tflag && ! (f->qid.type & QTTMP))
|| (xflag && ! (f->mode & DMEXEC))
|| (temponly && ! (f->qid.type & QTTMP))
|| (execonly && ! (f->mode & DMEXEC))
)
return;
@ -91,19 +106,21 @@ dofile(char *path, Dir *f, int pathonly)
}
void
walk(char *path, Dir *cf, long depth)
walk(char *path, Dir *cf, Seen *up, long depth)
{
String *file;
Dir *dirs, *f, *fe;
Seen s;
long n, fseen;
int fd;
long n;
if(cf == nil){
warn("path: %s: %r", path);
return;
}
if(depth >= maxdepth)
fseen = seen(up, cf);
if(depth >= maxdepth || fseen)
goto nodescend;
if((fd = open(path, OREAD)) < 0){
@ -111,32 +128,31 @@ walk(char *path, Dir *cf, long depth)
return;
}
s.d = cf;
s.up = up;
while((n = dirread(fd, &dirs)) > 0){
fe = dirs+n;
for(f = dirs; f < fe; f++){
if(seen(f))
continue;
if(! (f->qid.type & QTDIR)){
if(fflag && depth >= mindepth)
if(!(f->qid.type & QTDIR)){
if(printfiles && depth >= mindepth)
dofile(path, f, 0);
} else if(strcmp(f->name, ".") == 0 || strcmp(f->name, "..") == 0){
}else if(strcmp(f->name, ".") == 0 || strcmp(f->name, "..") == 0){
warn(". or .. named file: %s/%s", path, f->name);
} else{
}else{
if(depth+1 > maxdepth){
dofile(path, f, 0);
continue;
} else if(path == dotpath){
}else if(path == dotpath){
if((file = s_new()) == nil)
sysfatal("s_new: %r");
} else{
}else{
if((file = s_copy(path)) == nil)
sysfatal("s_copy: %r");
if(s_len(file) != 1 || *s_to_c(file) != '/')
s_putc(file, '/');
}
s_append(file, f->name);
walk(s_to_c(file), f, depth+1);
walk(s_to_c(file), f, &s, depth+1);
s_free(file);
}
}
@ -148,7 +164,7 @@ walk(char *path, Dir *cf, long depth)
nodescend:
depth--;
if(dflag && depth >= mindepth)
if(printdirs && (depth >= mindepth || fseen))
dofile(path, cf, 0);
}
@ -221,7 +237,6 @@ usage(void)
functions, but since they are a no-op and libString needs
a rework, I left them in - BurnZeZ
*/
void
main(int argc, char **argv)
{
@ -233,10 +248,10 @@ main(int argc, char **argv)
case 'C': Cflag++; break; /* undocumented; do not cleanname() the args */
case 'u': uflag++; break; /* unbuffered output */
case 'd': dflag++; fflag = 0; break; /* only dirs */
case 'f': fflag++; dflag = 0; break; /* only non-dirs */
case 't': tflag++; break; /* only tmp files */
case 'x': xflag++; break; /* only executable permission */
case 'd': printfiles = 0; break; /* only dirs */
case 'f': printdirs = 0; break; /* only non-dirs */
case 't': temponly = 1; break; /* only tmp files */
case 'x': execonly = 1; break; /* only executable permission */
case 'n': elimdepth(EARGF(usage())); break;
case 'e':
@ -251,6 +266,9 @@ main(int argc, char **argv)
usage();
}ARGEND;
if(!printfiles && !printdirs)
sysfatal("mutually exclusive flags: -f and -d");
fmtinstall('M', dirmodefmt);
if((bout = Bfdopen(1, OWRITE)) == nil)
@ -266,8 +284,8 @@ main(int argc, char **argv)
maxdepth++;
if(argc == 0){
dotdir = dirstat(".");
walk(dotpath, dotdir, 1);
} else for(i=0; i<argc; i++){
walk(dotpath, dotdir, nil, 1);
}else for(i=0; i<argc; i++){
if(strncmp(argv[i], "#/", 2) == 0)
slashslash(argv[i]+2);
else{
@ -275,54 +293,17 @@ main(int argc, char **argv)
cleanname(argv[i]);
slashslash(argv[i]);
}
if((d = dirstat(argv[i])) != nil && ! (d->qid.type & QTDIR)){
if(fflag && !seen(d) && mindepth < 1)
if((d = dirstat(argv[i])) == nil)
continue;
if(d->qid.type & QTDIR){
walk(argv[i], d, nil, 1);
}else{
if(printfiles && mindepth < 1)
dofile(argv[i], d, 1);
} else
walk(argv[i], d, 1);
}
free(d);
}
Bterm(bout);
exits(nil);
}
/* below pilfered from /sys/src/cmd/du.c
* NOTE: I did not check for bugs */
#define NCACHE 256 /* must be power of two */
typedef struct
{
Dir* cache;
int n;
int max;
} Cache;
Cache cache[NCACHE];
int
seen(Dir *dir)
{
Dir *dp;
int i;
Cache *c;
c = &cache[dir->qid.path&(NCACHE-1)];
dp = c->cache;
for(i=0; i<c->n; i++, dp++)
if(dir->qid.path == dp->qid.path &&
dir->type == dp->type &&
dir->dev == dp->dev)
return 1;
if(c->n == c->max){
if (c->max == 0)
c->max = 8;
else
c->max += c->max/2;
c->cache = realloc(c->cache, c->max*sizeof(Dir));
if(c->cache == nil)
sysfatal("realloc: %r");
}
c->cache[c->n++] = *dir;
return 0;
}