plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

walk.c (1287B)


      1 /* Copyright (C) 2003 Russ Cox, Massachusetts Institute of Technology */
      2 /* See COPYRIGHT */
      3 
      4 #include <u.h>
      5 #include <libc.h>
      6 #include <fcall.h>
      7 #include <9pclient.h>
      8 #include "fsimpl.h"
      9 
     10 CFid*
     11 fswalk(CFid *fid, char *oname)
     12 {
     13 	char *freep, *name;
     14 	int i, nwalk;
     15 	char *p;
     16 	CFid *wfid;
     17 	Fcall tx, rx;
     18 
     19 	freep = nil;
     20 	name = oname;
     21 	if(name){
     22 		freep = malloc(strlen(name)+1);
     23 		if(freep == nil)
     24 			return nil;
     25 		strcpy(freep, name);
     26 		name = freep;
     27 	}
     28 
     29 	if((wfid = _fsgetfid(fid->fs)) == nil){
     30 		free(freep);
     31 		return nil;
     32 	}
     33 
     34 	nwalk = 0;
     35 	do{
     36 		/* collect names */
     37 		for(i=0; name && *name && i < MAXWELEM; ){
     38 			p = name;
     39 			name = strchr(name, '/');
     40 			if(name)
     41 				*name++ = 0;
     42 			if(*p == 0 || (*p == '.' && *(p+1) == 0))
     43 				continue;
     44 			tx.wname[i++] = p;
     45 		}
     46 
     47 		/* do a walk */
     48 		tx.type = Twalk;
     49 		tx.fid = nwalk ? wfid->fid : fid->fid;
     50 		tx.newfid = wfid->fid;
     51 		tx.nwname = i;
     52 		if(_fsrpc(fid->fs, &tx, &rx, 0) < 0){
     53 		Error:
     54 			free(freep);
     55 			if(nwalk)
     56 				fsclose(wfid);
     57 			else
     58 				_fsputfid(wfid);
     59 			return nil;
     60 		}
     61 		if(rx.nwqid != tx.nwname){
     62 			/* XXX lame error */
     63 			werrstr("file '%s' not found", oname);
     64 			goto Error;
     65 		}
     66 		if(rx.nwqid == 0)
     67 			wfid->qid = fid->qid;
     68 		else
     69 			wfid->qid = rx.wqid[rx.nwqid-1];
     70 		nwalk++;
     71 	}while(name && *name);
     72 	return wfid;
     73 }