searchfs.c 18 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <auth.h>
  12. #include <fcall.h>
  13. /*
  14. * caveat:
  15. * this stuff is only meant to work for ascii databases
  16. */
  17. typedef struct Fid Fid;
  18. typedef struct Fs Fs;
  19. typedef struct Quick Quick;
  20. typedef struct Match Match;
  21. typedef struct Search Search;
  22. enum
  23. {
  24. OPERM = 0x3, /* mask of all permission types in open mode */
  25. Nfidhash = 32,
  26. /*
  27. * qids
  28. */
  29. Qroot = 1,
  30. Qsearch = 2,
  31. Qstats = 3,
  32. };
  33. /*
  34. * boyer-moore quick string matching
  35. */
  36. struct Quick
  37. {
  38. char *pat;
  39. char *up; /* match string for upper case of pat */
  40. int len; /* of pat (and up) -1; used for fast search */
  41. uint8_t jump[256]; /* jump index table */
  42. int miss; /* amount to jump if we falsely match the last char */
  43. };
  44. extern void quickmk(Quick*, char*, int);
  45. extern void quickfree(Quick*);
  46. extern char* quicksearch(Quick*, char*, char*);
  47. /*
  48. * exact matching of a search string
  49. */
  50. struct Match
  51. {
  52. Match *next;
  53. char *pat; /* null-terminated search string */
  54. char *up; /* upper case of pat */
  55. int len; /* length of both pat and up */
  56. int (*op)(Match*, char*, char*); /* method for this partiticular search */
  57. };
  58. struct Search
  59. {
  60. Quick quick; /* quick match */
  61. Match *match; /* exact matches */
  62. int skip; /* number of matches to skip */
  63. };
  64. extern char* searchsearch(Search*, char*, char*, int*);
  65. extern Search* searchparse(char*, char*);
  66. extern void searchfree(Search*);
  67. struct Fid
  68. {
  69. Lock;
  70. Fid *next;
  71. Fid **last;
  72. uint fid;
  73. int ref; /* number of fcalls using the fid */
  74. int attached; /* fid has beed attached or cloned and not clunked */
  75. int open;
  76. Qid qid;
  77. Search *search; /* search patterns */
  78. char *where; /* current location in the database */
  79. int n; /* number of bytes left in found item */
  80. };
  81. int dostat(int, uint8_t*, int);
  82. void* emalloc(uint);
  83. void fatal(char*, ...);
  84. Match* mkmatch(Match*,
  85. int(*)(Match*, char*, char*),
  86. char*);
  87. Match* mkstrmatch(Match*, char*);
  88. char* nextsearch(char*, char*, char**, char**);
  89. int strlook(Match*, char*, char*);
  90. char* strndup(char*, int);
  91. int tolower(int);
  92. int toupper(int);
  93. char* urlunesc(char*, char*);
  94. void usage(void);
  95. struct Fs
  96. {
  97. Lock; /* for fids */
  98. Fid *hash[Nfidhash];
  99. unsigned char statbuf[1024]; /* plenty big enough */
  100. };
  101. extern void fsrun(Fs*, int);
  102. extern Fid* getfid(Fs*, uint);
  103. extern Fid* mkfid(Fs*, uint);
  104. extern void putfid(Fs*, Fid*);
  105. extern char* fsversion(Fs*, Fcall*);
  106. extern char* fsauth(Fs*, Fcall*);
  107. extern char* fsattach(Fs*, Fcall*);
  108. extern char* fswalk(Fs*, Fcall*);
  109. extern char* fsopen(Fs*, Fcall*);
  110. extern char* fscreate(Fs*, Fcall*);
  111. extern char* fsread(Fs*, Fcall*);
  112. extern char* fswrite(Fs*, Fcall*);
  113. extern char* fsclunk(Fs*, Fcall*);
  114. extern char* fsremove(Fs*, Fcall*);
  115. extern char* fsstat(Fs*, Fcall*);
  116. extern char* fswstat(Fs*, Fcall*);
  117. char *(*fcalls[])(Fs*, Fcall*) =
  118. {
  119. [Tversion] fsversion,
  120. [Tattach] fsattach,
  121. [Tauth] fsauth,
  122. [Twalk] fswalk,
  123. [Topen] fsopen,
  124. [Tcreate] fscreate,
  125. [Tread] fsread,
  126. [Twrite] fswrite,
  127. [Tclunk] fsclunk,
  128. [Tremove] fsremove,
  129. [Tstat] fsstat,
  130. [Twstat] fswstat
  131. };
  132. char Eperm[] = "permission denied";
  133. char Enotdir[] = "not a directory";
  134. char Enotexist[] = "file does not exist";
  135. char Eisopen[] = "file already open for I/O";
  136. char Einuse[] = "fid is already in use";
  137. char Enofid[] = "no such fid";
  138. char Enotopen[] = "file is not open";
  139. char Ebadsearch[] = "bad search string";
  140. Fs fs;
  141. char *database;
  142. char *edatabase;
  143. int messagesize = 8192+IOHDRSZ;
  144. void
  145. main(int argc, char **argv)
  146. {
  147. Dir *d;
  148. char buf[12], *mnt, *srv;
  149. int fd, p[2], n;
  150. mnt = "/tmp";
  151. srv = nil;
  152. ARGBEGIN{
  153. case 's':
  154. srv = ARGF();
  155. mnt = nil;
  156. break;
  157. case 'm':
  158. mnt = ARGF();
  159. break;
  160. }ARGEND
  161. fmtinstall('F', fcallfmt);
  162. if(argc != 1)
  163. usage();
  164. d = nil;
  165. fd = open(argv[0], OREAD);
  166. if(fd < 0 || (d=dirfstat(fd)) == nil)
  167. fatal("can't open %s: %r", argv[0]);
  168. n = d->length;
  169. free(d);
  170. if(n == 0)
  171. fatal("zero length database %s", argv[0]);
  172. database = emalloc(n);
  173. if(read(fd, database, n) != n)
  174. fatal("can't read %s: %r", argv[0]);
  175. close(fd);
  176. edatabase = database + n;
  177. if(pipe(p) < 0)
  178. fatal("pipe failed");
  179. switch(rfork(RFPROC|RFMEM|RFNOTEG|RFNAMEG)){
  180. case 0:
  181. fsrun(&fs, p[0]);
  182. exits(nil);
  183. case -1:
  184. fatal("fork failed");
  185. }
  186. if(mnt == nil){
  187. if(srv == nil)
  188. usage();
  189. fd = create(srv, OWRITE, 0666);
  190. if(fd < 0){
  191. remove(srv);
  192. fd = create(srv, OWRITE, 0666);
  193. if(fd < 0){
  194. close(p[1]);
  195. fatal("create of %s failed", srv);
  196. }
  197. }
  198. sprint(buf, "%d", p[1]);
  199. if(write(fd, buf, strlen(buf)) < 0){
  200. close(p[1]);
  201. fatal("writing %s", srv);
  202. }
  203. close(p[1]);
  204. exits(nil);
  205. }
  206. if(mount(p[1], -1, mnt, MREPL, "", 'M') < 0){
  207. close(p[1]);
  208. fatal("mount failed");
  209. }
  210. close(p[1]);
  211. exits(nil);
  212. }
  213. /*
  214. * execute the search
  215. * do a quick match,
  216. * isolate the line in which the occured,
  217. * and try all of the exact matches
  218. */
  219. char*
  220. searchsearch(Search *search, char *where, char *end, int *np)
  221. {
  222. Match *m;
  223. char *s, *e;
  224. *np = 0;
  225. if(search == nil || where == nil)
  226. return nil;
  227. for(;;){
  228. s = quicksearch(&search->quick, where, end);
  229. if(s == nil)
  230. return nil;
  231. e = memchr(s, '\n', end - s);
  232. if(e == nil)
  233. e = end;
  234. else
  235. e++;
  236. while(s > where && s[-1] != '\n')
  237. s--;
  238. for(m = search->match; m != nil; m = m->next){
  239. if((*m->op)(m, s, e) == 0)
  240. break;
  241. }
  242. if(m == nil){
  243. if(search->skip > 0)
  244. search->skip--;
  245. else{
  246. *np = e - s;
  247. return s;
  248. }
  249. }
  250. where = e;
  251. }
  252. }
  253. /*
  254. * parse a search string of the form
  255. * tag=val&tag1=val1...
  256. */
  257. Search*
  258. searchparse(char *search, char *esearch)
  259. {
  260. Search *s;
  261. Match *m, *next, **last;
  262. char *tag, *val, *p;
  263. int ok;
  264. s = emalloc(sizeof *s);
  265. s->match = nil;
  266. /*
  267. * acording to the http spec,
  268. * repeated search queries are ingored.
  269. * the last search given is performed on the original object
  270. */
  271. while((p = memchr(s, '?', esearch - search)) != nil){
  272. search = p + 1;
  273. }
  274. while(search < esearch){
  275. search = nextsearch(search, esearch, &tag, &val);
  276. if(tag == nil)
  277. continue;
  278. ok = 0;
  279. if(strcmp(tag, "skip") == 0){
  280. s->skip = strtoul(val, &p, 10);
  281. if(*p == 0)
  282. ok = 1;
  283. }else if(strcmp(tag, "search") == 0){
  284. s->match = mkstrmatch(s->match, val);
  285. ok = 1;
  286. }
  287. free(tag);
  288. free(val);
  289. if(!ok){
  290. searchfree(s);
  291. return nil;
  292. }
  293. }
  294. if(s->match == nil){
  295. free(s);
  296. return nil;
  297. }
  298. /*
  299. * order the matches by probability of occurance
  300. * first cut is just by length
  301. */
  302. for(ok = 0; !ok; ){
  303. ok = 1;
  304. last = &s->match;
  305. for(m = *last; m && m->next; m = *last){
  306. if(m->next->len > m->len){
  307. next = m->next;
  308. m->next = next->next;
  309. next->next = m;
  310. *last = next;
  311. ok = 0;
  312. }
  313. last = &m->next;
  314. }
  315. }
  316. /*
  317. * convert the best search into a fast lookup
  318. */
  319. m = s->match;
  320. s->match = m->next;
  321. quickmk(&s->quick, m->pat, 1);
  322. free(m->pat);
  323. free(m->up);
  324. free(m);
  325. return s;
  326. }
  327. void
  328. searchfree(Search *s)
  329. {
  330. Match *m, *next;
  331. if(s == nil)
  332. return;
  333. for(m = s->match; m; m = next){
  334. next = m->next;
  335. free(m->pat);
  336. free(m->up);
  337. free(m);
  338. }
  339. quickfree(&s->quick);
  340. free(s);
  341. }
  342. char*
  343. nextsearch(char *search, char *esearch, char **tagp, char **valp)
  344. {
  345. char *tag, *val;
  346. *tagp = nil;
  347. *valp = nil;
  348. for(tag = search; search < esearch && *search != '='; search++)
  349. ;
  350. if(search == esearch)
  351. return search;
  352. tag = urlunesc(tag, search);
  353. search++;
  354. for(val = search; search < esearch && *search != '&'; search++)
  355. ;
  356. val = urlunesc(val, search);
  357. if(search != esearch)
  358. search++;
  359. *tagp = tag;
  360. *valp = val;
  361. return search;
  362. }
  363. Match*
  364. mkstrmatch(Match *m, char *pat)
  365. {
  366. char *s;
  367. for(s = pat; *s; s++){
  368. if(*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'){
  369. *s = 0;
  370. m = mkmatch(m, strlook, pat);
  371. pat = s + 1;
  372. }else
  373. *s = tolower(*s);
  374. }
  375. return mkmatch(m, strlook, pat);
  376. }
  377. Match*
  378. mkmatch(Match *next, int (*op)(Match*, char*, char*), char *pat)
  379. {
  380. Match *m;
  381. char *p;
  382. int n;
  383. n = strlen(pat);
  384. if(n == 0)
  385. return next;
  386. m = emalloc(sizeof *m);
  387. m->op = op;
  388. m->len = n;
  389. m->pat = strdup(pat);
  390. m->up = strdup(pat);
  391. for(p = m->up; *p; p++)
  392. *p = toupper(*p);
  393. for(p = m->pat; *p; p++)
  394. *p = tolower(*p);
  395. m->next = next;
  396. return m;
  397. }
  398. int
  399. strlook(Match *m, char *str, char *e)
  400. {
  401. char *pat, *up, *s;
  402. int c, pc, fc, fuc, n;
  403. n = m->len;
  404. fc = m->pat[0];
  405. fuc = m->up[0];
  406. for(; str + n <= e; str++){
  407. c = *str;
  408. if(c != fc && c != fuc)
  409. continue;
  410. s = str + 1;
  411. up = m->up + 1;
  412. for(pat = m->pat + 1; pc = *pat; pat++){
  413. c = *s;
  414. if(c != pc && c != *up)
  415. break;
  416. up++;
  417. s++;
  418. }
  419. if(pc == 0)
  420. return 1;
  421. }
  422. return 0;
  423. }
  424. /*
  425. * boyer-moore style pattern matching
  426. * implements an exact match for ascii
  427. * however, if mulitbyte upper-case and lower-case
  428. * characters differ in length or in more than one byte,
  429. * it only implements an approximate match
  430. */
  431. void
  432. quickmk(Quick *q, char *spat, int ignorecase)
  433. {
  434. char *pat, *up;
  435. uint8_t *j;
  436. int ep, ea, cp, ca, i, c, n;
  437. /*
  438. * allocate the machine
  439. */
  440. n = strlen(spat);
  441. if(n == 0){
  442. q->pat = nil;
  443. q->up = nil;
  444. q->len = -1;
  445. return;
  446. }
  447. pat = emalloc(2* n + 2);
  448. q->pat = pat;
  449. up = pat;
  450. if(ignorecase)
  451. up = pat + n + 1;
  452. q->up = up;
  453. while(c = *spat++){
  454. if(ignorecase){
  455. *up++ = toupper(c);
  456. c = tolower(c);
  457. }
  458. *pat++ = c;
  459. }
  460. pat = q->pat;
  461. up = q->up;
  462. pat[n] = up[n] = '\0';
  463. /*
  464. * make the skipping table
  465. */
  466. if(n > 255)
  467. n = 255;
  468. j = q->jump;
  469. memset(j, n, 256);
  470. n--;
  471. q->len = n;
  472. for(i = 0; i <= n; i++){
  473. j[(uint8_t)pat[i]] = n - i;
  474. j[(uint8_t)up[i]] = n - i;
  475. }
  476. /*
  477. * find the minimum safe amount to skip
  478. * if we match the last char but not the whole pat
  479. */
  480. ep = pat[n];
  481. ea = up[n];
  482. for(i = n - 1; i >= 0; i--){
  483. cp = pat[i];
  484. ca = up[i];
  485. if(cp == ep || cp == ea || ca == ep || ca == ea)
  486. break;
  487. }
  488. q->miss = n - i;
  489. }
  490. void
  491. quickfree(Quick *q)
  492. {
  493. if(q->pat != nil)
  494. free(q->pat);
  495. q->pat = nil;
  496. }
  497. char *
  498. quicksearch(Quick *q, char *s, char *e)
  499. {
  500. char *pat, *up, *m, *ee;
  501. uint8_t *j;
  502. int len, n, c, mc;
  503. len = q->len;
  504. if(len < 0)
  505. return s;
  506. j = q->jump;
  507. pat = q->pat;
  508. up = q->up;
  509. s += len;
  510. ee = e - (len * 4 + 4);
  511. while(s < e){
  512. /*
  513. * look for a match on the last char
  514. */
  515. while(s < ee && (n = j[(uint8_t)*s])){
  516. s += n;
  517. s += j[(uint8_t)*s];
  518. s += j[(uint8_t)*s];
  519. s += j[(uint8_t)*s];
  520. }
  521. if(s >= e)
  522. return nil;
  523. while(n = j[(uint8_t)*s]){
  524. s += n;
  525. if(s >= e)
  526. return nil;
  527. }
  528. /*
  529. * does the string match?
  530. */
  531. m = s - len;
  532. for(n = 0; c = pat[n]; n++){
  533. mc = *m++;
  534. if(c != mc && mc != up[n])
  535. break;
  536. }
  537. if(!c)
  538. return s - len;
  539. s += q->miss;
  540. }
  541. return nil;
  542. }
  543. void
  544. fsrun(Fs *fs, int fd)
  545. {
  546. Fcall rpc;
  547. char *err;
  548. uint8_t *buf;
  549. int n;
  550. buf = emalloc(messagesize);
  551. for(;;){
  552. /*
  553. * reading from a pipe or a network device
  554. * will give an error after a few eof reads
  555. * however, we cannot tell the difference
  556. * between a zero-length read and an interrupt
  557. * on the processes writing to us,
  558. * so we wait for the error
  559. */
  560. n = read9pmsg(fd, buf, messagesize);
  561. if(n == 0)
  562. continue;
  563. if(n < 0)
  564. fatal("mount read");
  565. rpc.data = (char*)buf + IOHDRSZ;
  566. if(convM2S(buf, n, &rpc) == 0)
  567. continue;
  568. // fprint(2, "recv: %F\n", &rpc);
  569. /*
  570. * flushes are way too hard.
  571. * a reply to the original message seems to work
  572. */
  573. if(rpc.type == Tflush)
  574. continue;
  575. else if(rpc.type >= Tmax || !fcalls[rpc.type])
  576. err = "bad fcall type";
  577. else
  578. err = (*fcalls[rpc.type])(fs, &rpc);
  579. if(err){
  580. rpc.type = Rerror;
  581. rpc.ename = err;
  582. }else
  583. rpc.type++;
  584. n = convS2M(&rpc, buf, messagesize);
  585. // fprint(2, "send: %F\n", &rpc);
  586. if(write(fd, buf, n) != n)
  587. fatal("mount write");
  588. }
  589. }
  590. Fid*
  591. mkfid(Fs *fs, uint fid)
  592. {
  593. Fid *f;
  594. int h;
  595. h = fid % Nfidhash;
  596. for(f = fs->hash[h]; f; f = f->next){
  597. if(f->fid == fid)
  598. return nil;
  599. }
  600. f = emalloc(sizeof *f);
  601. f->next = fs->hash[h];
  602. if(f->next != nil)
  603. f->next->last = &f->next;
  604. f->last = &fs->hash[h];
  605. fs->hash[h] = f;
  606. f->fid = fid;
  607. f->ref = 1;
  608. f->attached = 1;
  609. f->open = 0;
  610. return f;
  611. }
  612. Fid*
  613. getfid(Fs *fs, uint fid)
  614. {
  615. Fid *f;
  616. int h;
  617. h = fid % Nfidhash;
  618. for(f = fs->hash[h]; f; f = f->next){
  619. if(f->fid == fid){
  620. if(f->attached == 0)
  621. break;
  622. f->ref++;
  623. return f;
  624. }
  625. }
  626. return nil;
  627. }
  628. void
  629. putfid(Fs *fs, Fid *f)
  630. {
  631. f->ref--;
  632. if(f->ref == 0 && f->attached == 0){
  633. *f->last = f->next;
  634. if(f->next != nil)
  635. f->next->last = f->last;
  636. if(f->search != nil)
  637. searchfree(f->search);
  638. free(f);
  639. }
  640. }
  641. char*
  642. fsversion(Fs *fs, Fcall *rpc)
  643. {
  644. if(rpc->msize < 256)
  645. return "version: message size too small";
  646. if(rpc->msize > messagesize)
  647. rpc->msize = messagesize;
  648. messagesize = rpc->msize;
  649. if(strncmp(rpc->version, "9P2000", 6) != 0)
  650. return "unrecognized 9P version";
  651. rpc->version = "9P2000";
  652. return nil;
  653. }
  654. char*
  655. fsauth(Fs *fs, Fcall *f)
  656. {
  657. return "searchfs: authentication not required";
  658. }
  659. char*
  660. fsattach(Fs *fs, Fcall *rpc)
  661. {
  662. Fid *f;
  663. f = mkfid(fs, rpc->fid);
  664. if(f == nil)
  665. return Einuse;
  666. f->open = 0;
  667. f->qid.type = QTDIR;
  668. f->qid.path = Qroot;
  669. f->qid.vers = 0;
  670. rpc->qid = f->qid;
  671. putfid(fs, f);
  672. return nil;
  673. }
  674. char*
  675. fswalk(Fs *fs, Fcall *rpc)
  676. {
  677. Fid *f, *nf;
  678. int nqid, nwname, type;
  679. char *err, *name;
  680. uint32_t path;
  681. f = getfid(fs, rpc->fid);
  682. if(f == nil)
  683. return Enofid;
  684. nf = nil;
  685. if(rpc->fid != rpc->newfid){
  686. nf = mkfid(fs, rpc->newfid);
  687. if(nf == nil){
  688. putfid(fs, f);
  689. return Einuse;
  690. }
  691. nf->qid = f->qid;
  692. putfid(fs, f);
  693. f = nf; /* walk f */
  694. }
  695. err = nil;
  696. path = f->qid.path;
  697. nwname = rpc->nwname;
  698. for(nqid=0; nqid<nwname; nqid++){
  699. if(path != Qroot){
  700. err = Enotdir;
  701. break;
  702. }
  703. name = rpc->wname[nqid];
  704. if(strcmp(name, "search") == 0){
  705. type = QTFILE;
  706. path = Qsearch;
  707. }else if(strcmp(name, "stats") == 0){
  708. type = QTFILE;
  709. path = Qstats;
  710. }else if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0){
  711. type = QTDIR;
  712. path = path;
  713. }else{
  714. err = Enotexist;
  715. break;
  716. }
  717. rpc->wqid[nqid] = (Qid){path, 0, type};
  718. }
  719. if(nwname > 0){
  720. if(nf != nil && nqid < nwname)
  721. nf->attached = 0;
  722. if(nqid == nwname)
  723. f->qid = rpc->wqid[nqid-1];
  724. }
  725. putfid(fs, f);
  726. rpc->nwqid = nqid;
  727. f->open = 0;
  728. return err;
  729. }
  730. char *
  731. fsopen(Fs *fs, Fcall *rpc)
  732. {
  733. Fid *f;
  734. int mode;
  735. f = getfid(fs, rpc->fid);
  736. if(f == nil)
  737. return Enofid;
  738. if(f->open){
  739. putfid(fs, f);
  740. return Eisopen;
  741. }
  742. mode = rpc->mode & OPERM;
  743. if(mode == OEXEC
  744. || f->qid.path == Qroot && (mode == OWRITE || mode == ORDWR)){
  745. putfid(fs, f);
  746. return Eperm;
  747. }
  748. f->open = 1;
  749. f->where = nil;
  750. f->n = 0;
  751. f->search = nil;
  752. rpc->qid = f->qid;
  753. rpc->iounit = messagesize-IOHDRSZ;
  754. putfid(fs, f);
  755. return nil;
  756. }
  757. char *
  758. fscreate(Fs *fs, Fcall *f)
  759. {
  760. return Eperm;
  761. }
  762. char*
  763. fsread(Fs *fs, Fcall *rpc)
  764. {
  765. Fid *f;
  766. int n, off, count, len;
  767. f = getfid(fs, rpc->fid);
  768. if(f == nil)
  769. return Enofid;
  770. if(!f->open){
  771. putfid(fs, f);
  772. return Enotopen;
  773. }
  774. count = rpc->count;
  775. off = rpc->offset;
  776. rpc->count = 0;
  777. if(f->qid.path == Qroot){
  778. if(off > 0)
  779. rpc->count = 0;
  780. else
  781. rpc->count = dostat(Qsearch, (uint8_t*)rpc->data,
  782. count);
  783. putfid(fs, f);
  784. if(off == 0 && rpc->count <= BIT16SZ)
  785. return "directory read count too small";
  786. return nil;
  787. }
  788. if(f->qid.path == Qstats){
  789. len = 0;
  790. }else{
  791. for(len = 0; len < count; len += n){
  792. if(f->where == nil || f->search == nil)
  793. break;
  794. if(f->n == 0)
  795. f->where = searchsearch(f->search, f->where, edatabase, &f->n);
  796. n = f->n;
  797. if(n != 0){
  798. if(n > count-len)
  799. n = count-len;
  800. memmove(rpc->data+len, f->where, n);
  801. f->where += n;
  802. f->n -= n;
  803. }
  804. }
  805. }
  806. putfid(fs, f);
  807. rpc->count = len;
  808. return nil;
  809. }
  810. char*
  811. fswrite(Fs *fs, Fcall *rpc)
  812. {
  813. Fid *f;
  814. f = getfid(fs, rpc->fid);
  815. if(f == nil)
  816. return Enofid;
  817. if(!f->open || f->qid.path != Qsearch){
  818. putfid(fs, f);
  819. return Enotopen;
  820. }
  821. if(f->search != nil)
  822. searchfree(f->search);
  823. f->search = searchparse(rpc->data, rpc->data + rpc->count);
  824. if(f->search == nil){
  825. putfid(fs, f);
  826. return Ebadsearch;
  827. }
  828. f->where = database;
  829. f->n = 0;
  830. putfid(fs, f);
  831. return nil;
  832. }
  833. char *
  834. fsclunk(Fs *fs, Fcall *rpc)
  835. {
  836. Fid *f;
  837. f = getfid(fs, rpc->fid);
  838. if(f != nil){
  839. f->attached = 0;
  840. putfid(fs, f);
  841. }
  842. return nil;
  843. }
  844. char *
  845. fsremove(Fs *fs, Fcall *f)
  846. {
  847. return Eperm;
  848. }
  849. char *
  850. fsstat(Fs *fs, Fcall *rpc)
  851. {
  852. Fid *f;
  853. f = getfid(fs, rpc->fid);
  854. if(f == nil)
  855. return Enofid;
  856. rpc->stat = fs->statbuf;
  857. rpc->nstat = dostat(f->qid.path, rpc->stat, sizeof fs->statbuf);
  858. putfid(fs, f);
  859. if(rpc->nstat <= BIT16SZ)
  860. return "stat count too small";
  861. return nil;
  862. }
  863. char *
  864. fswstat(Fs *fs, Fcall *f)
  865. {
  866. return Eperm;
  867. }
  868. int
  869. dostat(int path, uint8_t *buf, int nbuf)
  870. {
  871. Dir d;
  872. switch(path){
  873. case Qroot:
  874. d.name = ".";
  875. d.mode = DMDIR|0555;
  876. d.qid.type = QTDIR;
  877. break;
  878. case Qsearch:
  879. d.name = "search";
  880. d.mode = 0666;
  881. d.qid.type = QTFILE;
  882. break;
  883. case Qstats:
  884. d.name = "stats";
  885. d.mode = 0666;
  886. d.qid.type = QTFILE;
  887. break;
  888. }
  889. d.qid.path = path;
  890. d.qid.vers = 0;
  891. d.length = 0;
  892. d.uid = d.gid = d.muid = "none";
  893. d.atime = d.mtime = time(nil);
  894. return convD2M(&d, buf, nbuf);
  895. }
  896. char *
  897. urlunesc(char *s, char *e)
  898. {
  899. char *t, *v;
  900. int c, n;
  901. v = emalloc((e - s) + 1);
  902. for(t = v; s < e; s++){
  903. c = *s;
  904. if(c == '%'){
  905. if(s + 2 >= e)
  906. break;
  907. n = s[1];
  908. if(n >= '0' && n <= '9')
  909. n = n - '0';
  910. else if(n >= 'A' && n <= 'F')
  911. n = n - 'A' + 10;
  912. else if(n >= 'a' && n <= 'f')
  913. n = n - 'a' + 10;
  914. else
  915. break;
  916. c = n;
  917. n = s[2];
  918. if(n >= '0' && n <= '9')
  919. n = n - '0';
  920. else if(n >= 'A' && n <= 'F')
  921. n = n - 'A' + 10;
  922. else if(n >= 'a' && n <= 'f')
  923. n = n - 'a' + 10;
  924. else
  925. break;
  926. s += 2;
  927. c = c * 16 + n;
  928. }
  929. *t++ = c;
  930. }
  931. *t = 0;
  932. return v;
  933. }
  934. int
  935. toupper(int c)
  936. {
  937. if(c >= 'a' && c <= 'z')
  938. c += 'A' - 'a';
  939. return c;
  940. }
  941. int
  942. tolower(int c)
  943. {
  944. if(c >= 'A' && c <= 'Z')
  945. c += 'a' - 'A';
  946. return c;
  947. }
  948. void
  949. fatal(char *fmt, ...)
  950. {
  951. va_list arg;
  952. char buf[1024];
  953. write(2, "searchfs: ", 8);
  954. va_start(arg, fmt);
  955. vseprint(buf, buf+1024, fmt, arg);
  956. va_end(arg);
  957. write(2, buf, strlen(buf));
  958. write(2, "\n", 1);
  959. exits(fmt);
  960. }
  961. void *
  962. emalloc(uint n)
  963. {
  964. void *p;
  965. p = malloc(n);
  966. if(p == nil)
  967. fatal("out of memory");
  968. memset(p, 0, n);
  969. return p;
  970. }
  971. void
  972. usage(void)
  973. {
  974. fprint(2, "usage: searchfs [-m mountpoint] [-s srvfile] database\n");
  975. exits("usage");
  976. }