/* * dump any cached information, forget the hash tables, and reopen a single file */ int ndbreopen(Ndb *db) { int fd; Dir *d; /* forget what we know about the open files */ if(db->mtime){ _ndbcacheflush(db); hffree(db); close(Bfildes(&db->b)); Bterm(&db->b); db->mtime = 0; } /* try the open again */ fd = open(db->file, OREAD); if(fd < 0) return -1; d = dirfstat(fd); if(d == nil){ close(fd); return -1; } /* no hashfile for /net/ndb (avoids deadlock in cs) */ if(d->type == 'I') db->nohash = 1; db->qid = d->qid; db->mtime = d->mtime; db->length = d->length; Binits(&db->b, fd, OREAD, db->buf, sizeof(db->buf)); free(d); return 0; }
void main(int argc, char **argv) { Biobuf bout; char *fields[NFLDS], name[2*LEN], *p, *namep; char *uid, *gid; uint32_t mode, mtime; uint64_t bytes; quotefmtinstall(); namep = name; ARGBEGIN{ case 'd': p = ARGF(); if(strlen(p) >= LEN) error("destination fs name too long\n"); strcpy(name, p); namep = name + strlen(name); break; case 'h': hflag = 1; Binit(&bout, 1, OWRITE); break; case 'u': uflag = 1; Tflag = 1; break; case 'T': Tflag = 1; break; case 'v': vflag = 1; break; default: usage(); }ARGEND Binits(&bin, 0, OREAD, binbuf, sizeof binbuf); while(p = Brdline(&bin, '\n')){ p[Blinelen(&bin)-1] = '\0'; strcpy(linebuf, p); p = linebuf; if(strcmp(p, "end of archive") == 0){ Bterm(&bout); fprint(2, "done\n"); exits(0); } if (gettokens(p, fields, NFLDS, " \t") != NFLDS){ warn("too few fields in file header"); continue; } p = unquotestrdup(fields[0]); strcpy(namep, p); free(p); mode = strtoul(fields[1], 0, 8); uid = fields[2]; gid = fields[3]; mtime = strtoul(fields[4], 0, 10); bytes = strtoull(fields[5], 0, 10); if(argc){ if(!selected(namep, argc, argv)){ if(bytes) seekpast(bytes); continue; } mkdirs(name, namep); } if(hflag){ Bprint(&bout, "%q %luo %q %q %lu %llu\n", name, mode, uid, gid, mtime, bytes); if(bytes) seekpast(bytes); continue; } if(mode & DMDIR) mkdir(name, mode, mtime, uid, gid); else extract(name, mode, mtime, uid, gid, bytes); } fprint(2, "premature end of archive\n"); exits("premature end of archive"); }
void searchfor(char *search) { int i, j, n, fd; char *p, *sp; Biobufhdr *b; char *arg[32]; hprint(hout, "<head><title>plan 9 search for %H</title></head>\n", search); hprint(hout, "<body>\n"); hprint(hout, "<p>This is a keyword search through Plan 9 man pages.\n"); hprint(hout, "The search is case insensitive; blanks denote \"boolean and\".\n"); hprint(hout, "<FORM METHOD=\"GET\" ACTION=\"/magic/man2html\">\n"); hprint(hout, "<INPUT NAME=\"pat\" TYPE=\"text\" SIZE=\"60\">\n"); hprint(hout, "<INPUT TYPE=\"submit\" VALUE=\"Submit\">\n"); hprint(hout, "</FORM>\n"); hprint(hout, "<hr><H6>Search for %H</H6>\n", search); n = getfields(search, arg, 32, 1, "+"); for(i = 0; i < n; i++){ for(j = i+1; j < n; j++){ if(strcmp(arg[i], arg[j]) > 0){ sp = arg[j]; arg[j] = arg[i]; arg[i] = sp; } } sp = malloc(strlen(arg[i]) + 2); if(sp != nil){ strcpy(sp+1, arg[i]); sp[0] = ' '; arg[i] = sp; } } /* * search index till line starts alphabetically < first token */ fd = open("/sys/man/searchindex", OREAD); if(fd < 0){ hprint(hout, "<body>error: No Plan 9 search index\n"); hprint(hout, "</body>"); return; } p = malloc(32*1024); if(p == nil){ close(fd); return; } b = ezalloc(sizeof *b); Binits(b, fd, OREAD, (uint8_t*)p, 32*1024); for(;;){ p = Brdline(b, '\n'); if(p == nil) break; p[Blinelen(b)-1] = 0; for(i = 0; i < n; i++){ sp = strstr(p, arg[i]); if(sp == nil) break; p = sp; } if(i < n) continue; sp = strrchr(p, '\t'); if(sp == nil) continue; sp++; hprint(hout, "<p><a href=\"/magic/man2html/%U\">/magic/man2html/%H</a>\n", sp, sp); } hprint(hout, "</body>"); Bterm(b); free(b); free(p); close(fd); }
void main(int argc, char **argv) { Ndbtuple *t, *nt; int n; Dir *d; uint8_t buf[8]; char file[128]; int fd; uint32_t off; uint8_t *p; if(argc != 3){ fprint(2, "usage: mkhash file attribute\n"); exits("usage"); } db = ndbopen(argv[1]); if(db == 0){ fprint(2, "mkhash: can't open %s\n", argv[1]); exits(syserr()); } /* try a bigger than normal buffer */ Binits(&db->b, Bfildes(&db->b), OREAD, nbuf, sizeof(nbuf)); /* count entries to calculate hash size */ n = 0; while(nt = ndbparse(db)){ for(t = nt; t; t = t->entry){ if(strcmp(t->attr, argv[2]) == 0) n++; } ndbfree(nt); } /* allocate an array large enough for worst case */ hlen = 2*n+1; n = hlen*NDBPLEN + hlen*2*NDBPLEN; ht = mallocz(n, 1); if(ht == 0){ fprint(2, "mkhash: not enough memory\n"); exits(syserr()); } for(p = ht; p < &ht[n]; p += NDBPLEN) NDBPUTP(NDBNAP, p); nextchain = hlen*NDBPLEN; /* create the in core hash table */ Bseek(&db->b, 0, 0); off = 0; while(nt = ndbparse(db)){ for(t = nt; t; t = t->entry){ if(strcmp(t->attr, argv[2]) == 0) enter(t->val, off); } ndbfree(nt); off = Boffset(&db->b); } /* create the hash file */ snprint(file, sizeof(file), "%s.%s", argv[1], argv[2]); fd = create(file, ORDWR, 0664); if(fd < 0){ fprint(2, "mkhash: can't create %s\n", file); exits(syserr()); } NDBPUTUL(db->mtime, buf); NDBPUTUL(hlen, buf+NDBULLEN); if(write(fd, buf, NDBHLEN) != NDBHLEN){ fprint(2, "mkhash: writing %s\n", file); exits(syserr()); } if(write(fd, ht, nextchain) != nextchain){ fprint(2, "mkhash: writing %s\n", file); exits(syserr()); } close(fd); /* make sure file didn't change while we were making the hash */ d = dirstat(argv[1]); if(d == nil || d->qid.path != db->qid.path || d->qid.vers != db->qid.vers){ fprint(2, "mkhash: %s changed underfoot\n", argv[1]); remove(file); exits("changed"); } exits(0); }
void main(int argc, char **argv) { char *rargv[10]; int rargc; char *fields[NFLDS], name[2*LEN], *p, *namep; char uid[NAMELEN], gid[NAMELEN]; ulong mode, bytes, mtime; char *file; int i, n, stdin, fd, chatty; char blk[512]; if(argc>1 && strcmp(argv[1], "RAMFS") == 0){ argv[1] = argv[0]; ramfsmain(argc-1, argv+1); exits(nil); } if(argc>1 && strcmp(argv[1], "BUNZIP") == 0){ _unbzip(0, 1); exits(nil); } rfork(RFNOTEG); stdin = 0; file = nil; namep = name; mtpt = "/root"; chatty = 0; ARGBEGIN{ case 'd': chatty = !chatty; break; case 'f': file = ARGF(); break; case 's': stdin++; break; case 'm': mtpt = ARGF(); break; default: usage(); }ARGEND if(argc != 0) usage(); if(file == nil) { fprint(2, "must specify -f file\n"); usage(); } if((fd = open(file, OREAD)) < 0) { fprint(2, "cannot open \"%s\": %r\n", file); exits("open"); } rargv[0] = "ramfs"; rargc = 1; if(stdin) rargv[rargc++] = "-i"; rargv[rargc++] = "-m"; rargv[rargc++] = mtpt; rargv[rargc] = nil; ramfsmain(rargc, rargv); if(1 || strstr(file, "disk")) { /* search for archive on block boundary */ if(chatty) fprint(2, "searching for bz\n"); for(i=0;; i++){ if((n = readn(fd, blk, sizeof blk)) != sizeof blk) sysfatal("read %d gets %d: %r\n", i, n); if(strncmp(blk, "bzfilesystem\n", 13) == 0) break; } if(chatty) fprint(2, "found at %d\n", i); } if(chdir(mtpt) < 0) error("chdir %s: %r", mtpt); fd = unbflz(unbzip(blockread(fd, blk+13, sizeof(blk)-13))); Binits(&bin, fd, OREAD, binbuf, sizeof binbuf); while(p = Brdline(&bin, '\n')){ p[Blinelen(&bin)-1] = '\0'; if(chatty) fprint(2, "%s\n", p); if(strcmp(p, "end of archive") == 0){ _exits(0); } if(getfields(p, fields, NFLDS, 0, " \t") != NFLDS){ warn("too few fields in file header"); continue; } strcpy(namep, fields[0]); mode = strtoul(fields[1], 0, 8); mtime = strtoul(fields[4], 0, 10); bytes = strtoul(fields[5], 0, 10); strncpy(uid, fields[2], NAMELEN); strncpy(gid, fields[3], NAMELEN); if(mode & DMDIR) mkdir(name, mode, mtime, uid, gid); else extract(name, mode, mtime, uid, gid, bytes); } fprint(2, "premature end of archive\n"); exits("premature end of archive"); }