10 #include <sys/socket.h>
11 #include <netinet/in.h>
21 int wrapfile::openmem(
char *buff,
int size) {
29 int wrapfile::opendisk(
char *fn,
int flags,
int perms)
31 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
32 fd = open64(fn, flags, perms);
34 fd = open(fn, flags, perms);
45 int wrapfile::openfd(
int fd)
52 int wrapfile::read(
void *buff,
int sz)
57 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
58 LOG(DBG,
"read: wfpos=%lld wsize=%lld sz=%d",wfpos,wsize,sz);
60 LOG(DBG,
"read: wfpos=%d wsize=%d sz=%d",wfpos,wsize,sz);
63 if((wfpos + sz) > wsize) {
66 memcpy(buff, wbuff + wfpos, sz);
71 return ::read(fd, buff, sz);
80 int wrapfile::write(
void *buff,
int sz)
85 if((wfpos + sz) > wsize) {
88 memcpy(this->wbuff + wfpos, buff, sz);
94 return ::write(fd, buff, sz);
100 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
101 long long int wrapfile::lseek(
long long int offset,
int whence)
103 int wrapfile::lseek(
int offset,
int whence)
125 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
126 return ::lseek64(fd, offset, whence);
128 return ::lseek(fd, offset,whence);
135 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
136 int wrapfile::fstat(
struct stat64 *stat)
138 int wrapfile::fstat(
struct stat *stat)
149 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
150 return ::fstat64(fd, stat);
152 return ::fstat(fd, stat);
158 int wrapfile::close()
189 fs_index::~fs_index()
196 void fs_index::hexdump(
char *buff,
int sz)
198 for(
int i=0;i<sz;i+=16) {
203 printf(
"0x%08x: ",i);
205 for(
int j=0;j<16;j++) {
206 if(j==8) printf(
" ");
207 int x = buff[i+j] & 0x00ff;
214 for(
int j=0;j<16;j++) {
216 if(j == 8) printf(
" ");
219 if(isprint(buff[i+j]))
220 printf(
"%c",buff[i+j]);
229 int fs_index::mountmem(
char *buffer,
int sz,
int flags)
232 wfile.openmem(buffer, sz);
240 int fs_index::mount(
int fd)
251 int fs_index::mount(
char *filename,
int flags,
int perms)
254 wfile.opendisk(filename,flags,perms);
256 if(wfile.fd < 0)
return wfile.fd;
261 int fs_index::mount(
int ip,
int port)
269 fd = socket(AF_INET, SOCK_STREAM, 0) ;
276 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (
char *)&optval,
sizeof(optval)) ;
277 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (
char *)&optval,
sizeof(optval)) ;
279 memset((
char *)&addr,0,
sizeof(addr)) ;
280 addr.sin_family = AF_INET ;
281 addr.sin_port = htons(port) ;
282 addr.sin_addr.s_addr = htonl(ip) ;
285 ret = connect(fd, (sockaddr *)&addr,
sizeof(addr)) ;
291 optval = 64*1024*1024 ;
293 ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (
char *)&optval,
sizeof(optval)) ;
300 socklen_t new_val_len =
sizeof(new_val) ;
301 ret = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &new_val, &new_val_len) ;
303 LOG(NOTE,
"mount: IP 0x%08X:%d claims %d buffer bytes, reports %d (%d)",ip,port,optval,new_val,ret) ;
311 int fs_index::initmount()
314 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
326 if(oflags & O_WRONLY) {
329 LOG(TERR,
"stat.st_size %d",stat.st_size) ;
331 if(stat.st_size > 0) {
332 if(!(oflags & O_APPEND)) {
334 LOG(ERR,
"no APPEND") ;
339 ret = writeFsHeader();
341 LOG(ERR,
"writeFsHeader %d",ret) ;
351 LOG(ERR,
"_create() %d",ret) ;
359 void fs_index::getFullPath(
char *fullpath,
const char *name)
362 strcpy(fullpath,name);
365 strcpy(fullpath,cwd);
366 int n = strlen(fullpath)-1;
367 if(fullpath[n] !=
'/') strcat(fullpath,
"/");
368 strcat(fullpath,name);
376 if(strcmp(name, next->name) == 0)
return next;
383 int fs_index::cd(
char *name)
386 getFullPath(fullpath,name);
389 if(oflags & O_WRONLY) {
390 strcpy(cwd, fullpath);
395 if(!index_created)
return -1;
398 strcpy(tokes,fullpath);
400 char *token = strtok_r(tokes,
"/", &_strtok_static_);
404 fs_inode *newn = find_child(node, token);
407 token = strtok_r(NULL,
"/", &_strtok_static_);
410 if(!node->fchild)
return -1;
413 strcpy(cwd,fullpath);
418 void fs_index::free_inode(
fs_inode *inode)
421 if(inode->fchild) free_inode(inode->fchild);
423 if(inode->next) free_inode(inode->next);
430 void fs_index::umount()
442 ent = &_readdirent_static_;
445 if(!index_created)
return NULL;
448 getFullPath(fullname,dir);
449 strcpy(ent->full_name, fullname);
452 char *name = strtok_r(fullname,
"/", &_strtok_static_);
455 node = find_child(node, name);
456 if(!node)
return NULL;
458 name = strtok_r(NULL,
"/", &_strtok_static_);
462 strcpy(ent->d_name, node->name);
465 ent->offset = node->offset;
466 if(node->fchild) ent->has_child = 1;
467 else ent->has_child = 0;
471 ent->swap = node->swap;
476 fs_dir *fs_index::opendir(
const char *dir)
478 if(!index_created)
return NULL;
483 getFullPath(fullpath, dir);
484 if(fullpath[strlen(fullpath)-1] !=
'/') strcat(fullpath,
"/");
485 strcpy(fullpathc, fullpath);
491 char *name = strtok_r(fullpath,
"/", &_strtok_static_);
497 node = find_child(node,name);
498 if(!node)
return NULL;
500 name = strtok_r(NULL,
"/", &_strtok_static_);
504 if(!ret)
return NULL;
506 strcpy(ret->full_name, fullpathc);
508 ret->currchild = NULL;
510 LOG(DBG,
"returning dir 0x%x",ret);
514 void fs_index::closedir(
fs_dir *dir)
516 if(!index_created)
return;
523 if(!index_created)
return NULL;
528 node = dir->inode->fchild;
532 if(!node)
return NULL;
534 dir->currchild = node;
537 ent = &_readdirent_static_;
540 strcpy(ent->d_name, node->name);
542 strcpy(ent->full_name, dir->full_name);
543 strcat(ent->full_name, node->name);
546 ent->offset = node->offset;
547 if(node->fchild) ent->has_child = 1;
548 else ent->has_child = 0;
550 ent->swap = node->swap;
555 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
556 fs_inode *fs_index::alloc_inode(
const char *name,
long long int off,
int sz,
int overhead)
558 fs_inode *fs_index::alloc_inode(
const char *name,
int off,
int sz,
int overhead)
572 n->overhead = overhead;
574 n->name = (
char *)malloc(strlen(name)+1);
580 strcpy(n->name,name);
585 int fs_index::fileSize(
const char *fn)
587 if(!index_created)
return -1;
598 int fs_index::read(
const char *fn,
char *buff,
int maxsize)
600 if(!index_created)
return -1;
609 LOG(NOTE,
"%s has no data...",fn);
613 if(entry->sz > maxsize) {
617 int ret = wfile.lseek(entry->offset, SEEK_SET);
618 if(ret != entry->offset) {
619 LOG(ERR,
"Invalid seek %d vs %d\n",ret,entry->offset);
623 ret = wfile.read(buff, entry->sz);
624 if(ret != entry->sz) {
625 LOG(ERR,
"Error reading file %d vs %d\n", ret, entry->sz);
631 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
632 long long int fs_index::mountsz()
634 int fs_index::mountsz()
638 if(oflags & O_WRONLY) {
639 if(wfile.type == WRAP_MEM) {
644 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
659 while((entry = readdir(dir))) {
660 if(filelist->n >= filelist->max)
return 0;
662 if(!entry->has_child) {
663 strcpy(filelist->filename[filelist->n], entry->full_name);
667 if(recurse & entry->has_child) {
668 fs_dir *ndir = opendir(entry->full_name);
669 ret = mem_ls(filelist, recurse, ndir);
671 if(ret == 0)
return 0;