static inline uint8_t dcache_namehash_get(dircache *d,uint8_t nleng,const uint8_t *name,uint32_t *inode,uint8_t attr[35]) { uint32_t hash,disp,hashmask; const uint8_t *ptr; if (d->namehashtab==NULL) { dcache_makenamehash(d); } hashmask = d->hashsize-1; hash = dcache_hash(name,nleng); disp = ((hash*0x53B23891)&hashmask)|1; while ((ptr=d->namehashtab[hash&hashmask])) { if (*ptr==nleng && memcmp(ptr+1,name,nleng)==0) { ptr+=1+nleng; *inode = get32bit(&ptr); if (*ptr) { // are attributes valid ? memcpy(attr,ptr,35); return 1; } else { return 0; } } hash+=disp; } return 0; }
void dcache_makenamehash(dircache *d) { const uint8_t *ptr,*eptr; uint8_t enleng; uint32_t hash,disp; uint32_t hashmask; if (d->hashsize==0) { dcache_calchashsize(d); } hashmask = d->hashsize-1; d->namehashtab = malloc(sizeof(uint8_t*)*d->hashsize); memset(d->namehashtab,0,sizeof(uint8_t*)*d->hashsize); ptr = d->dbuff; eptr = d->dbuff+d->dsize; while (ptr<eptr) { enleng = *ptr; if (ptr+enleng+40<=eptr) { hash = dcache_hash(ptr+1,enleng); disp = ((hash*0x53B23891)&hashmask)|1; while (d->namehashtab[hash&hashmask]) { hash+=disp; } d->namehashtab[hash&hashmask]=ptr; } ptr+=enleng+40; } }
static inline void dcache_namehash_invalidate(dircache *d,uint8_t nleng,const uint8_t *name) { uint32_t hash,disp,hashmask; const uint8_t *ptr; if (d->namehashtab==NULL) { dcache_makenamehash(d); } hashmask = d->hashsize-1; hash = dcache_hash(name,nleng); disp = ((hash*0x53B23891)&hashmask)|1; while ((ptr=d->namehashtab[hash&hashmask])) { if (*ptr==nleng && memcmp(ptr+1,name,nleng)==0) { ptr+=1+nleng; memset((uint8_t*)ptr,0,sizeof(uint32_t)+35); return; } hash+=disp; } }
int lfs_genflock (char *filename) { int len; len = strlen (filename); char *ptr = malloc (14); memset (ptr, 0, 14); memset (lfs_n.instance.fname, 0, 20); sprintf (ptr, "/tmp/rfs%d.lock", dcache_hash (filename, len) % 1024); memcpy (lfs_n.instance.fname, ptr, strlen (ptr)); if (access (lfs_n.instance.fname, F_OK) == 0) { printf ("RFS filesystem instance is already running\n"); exit (0); } lfs_n.instance.fd = open (lfs_n.instance.fname, O_RDWR | O_CREAT, 0600); if (lfs_n.instance.fd < 0) { perror ("create pidfile failed"); } return LFS_OK; }