11 #include <SUNRT/clock.h>
12 #include "sfs_index.h"
20 char *striptodir(
char *str)
28 if(*(str+1) !=
'\0') lslash = str;
40 int fs_cd(
int argc,
char *argv[])
42 if(argc !=2)
return -1;
44 if(idx->cd(argv[1]) < 0) {
45 printf(
"%s is invalid directory\n",argv[1]);
49 printf(
"pwd set to %s\n", idx->cwd);
56 printf(
"%s\n", idx->cwd);
61 int fs_mount(
int argc,
char *argv[])
68 double t = record_time();
70 printf(
"mount <daqfilename>\n");
74 const char *pwd=getenv(
"PWD");
77 if(argv[1][0] ==
'/') {
81 if(pwd == NULL) pwd =
"/";
82 sprintf(fn,
"%s/%s",pwd,argv[1]);
85 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
86 struct stat64 filestat;
87 if(stat64(fn,&filestat) < 0)
90 if(stat(fn,&filestat) < 0)
93 printf(
"Error reading file %s: %s\n",fn,strerror(errno));
97 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
98 idx_fd = open64(fn, O_RDONLY);
100 idx_fd = open(fn, O_RDONLY);
104 printf(
"Error reading file %s\n",fn);
105 strcpy(g_filename,
"NONE");
114 idx->mount(fn, O_RDONLY);
117 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
118 printf(
"Mounted file %s: %lld bytes in %5.2f sec\n",fn,(
long long int)filestat.st_size,t);
120 printf(
"Mounted file %s: %d bytes in %5.2f sec\n",fn,filestat.st_size,t);
127 strcpy(g_filename, fn);
131 int getDirSize(
fs_dir *dir) {
136 while((entry = idx->readdir(dir, &storage))) {
140 if(entry->has_child) {
141 fs_dir *ndir = idx->opendir(entry->full_name);
142 sz += getDirSize(ndir);
154 if(entry->has_child) {
155 fs_dir *ndir = idx->opendir(entry->full_name);
156 sz += getDirSize(ndir);
163 int ls_dir(
const char *tabs,
int recurse,
fs_dir *dir)
167 if(recurse > 0) recurse--;
171 while((entry = idx->readdir(dir, &storage))) {
173 int sz = getEntrySize(entry);
174 printf(
"%s [%7d bytes] %s%c\n",
175 tabs, sz, entry->full_name,entry->has_child?
'/':
' ');
177 if(recurse && entry->has_child) {
178 fs_dir *ndir = idx->opendir(entry->full_name);
179 ls_dir(tabs, recurse, ndir);
186 int fs_ls(
int argc,
char *argv[])
191 if(memcmp(argv[1],
"-r", 2) == 0) {
193 if(strlen(argv[1]) > 2) {
194 recurse = atoi(&argv[1][2]);
201 fs_dir *dir = idx->opendir(idx->cwd);
203 printf(
"Error opening directory %s\n",idx->cwd);
207 ls_dir(
"", recurse, dir);
214 int fs_cat(
int argc,
char *argv[])
217 printf(
"cat <file> <optional output file>\n");
223 if(strcmp(argv[0],
"cat") == 0) type = 0;
224 else if (strcmp(argv[0],
"od") == 0) type = 8;
227 int sz = idx->fileSize(argv[1]);
228 if(sz < 0)
return -1;
230 char *buff = (
char *)malloc(sz);
232 printf(
"Error allocating %d bytes\n",sz);
236 int ret = idx->read(argv[1], buff, sz);
238 printf(
"Error reading %d bytes\n",sz);
241 if(strcmp(argv[0],
"save") == 0) {
243 printf(
"Need a filename\n");
247 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
248 int fd = open64(argv[2], O_WRONLY | O_CREAT,0777);
250 int fd = open(argv[2], O_WRONLY | O_CREAT, 0777);
255 printf(
"error opening file %s\n",argv[2]);
258 int ret = write(fd,buff,sz);
260 printf(
"Error writing to file %s (only wrote %d of %d bytes\n",
271 write(STDOUT_FILENO, buff, sz);
272 write(STDOUT_FILENO,
"\n",
sizeof(
"\n"));
275 else if (type == 8) {
279 fs_index::hexdump(buff, sz);
284 for(
int i=0;i<sz;i++) {
285 if(isprint(buff[i])) {
307 printf(
"fs <cmd> <args>\n");
309 printf(
"\tcd <dir>\n");
312 printf(
"\tmount <file>\n");
315 printf(
"\tstrings <fn>\n");
316 printf(
"\tod <fn>\n");
317 printf(
"\tsave <fn> <ofn>\n");
318 printf(
"\tsize <dir>\n");
324 int docmd(
int argc,
char *argv[])
326 if((strcmp(argv[0],
"mount") == 0)) {
327 fs_mount(argc, argv);
332 printf(
"No mounted file: mount a file first\n");
336 else if(strcmp(argv[0],
"pwd") == 0) {
340 else if(strcmp(argv[0],
"cd") == 0) {
344 else if(strcmp(argv[0],
"ls") == 0) {
348 else if(strcmp(argv[0],
"dump") == 0) {
352 else if(strcmp(argv[0],
"od") == 0) {
356 else if(strcmp(argv[0],
"save") == 0) {
360 else if(strcmp(argv[0],
"strings") == 0) {
364 else if(strcmp(argv[0],
"size") == 0) {
366 ((
sfs_index *)idx)->getDirSize(argv[1], &x);
367 printf(
"x->size = %lld x->dataSize = %lld\n",x.size, x.dataSize);
371 printf(
"\nInvalid command: %s\n\n",argv[0]);
378 int main(
int argc,
char *argv[])
386 strcpy(g_filename,
"none");
392 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
394 int ret = stat64(argv[1], &sstat);
397 int ret = stat(argv[1], &sstat);
401 printf(
"No file %s\n",argv[1]);
406 av[0] = (
char *)
"mount";
414 int ret = idx->mountSingleDir(argv[1]);
416 printf(
"Error reading %s\n", argv[1]);
421 if(strcmp(argv[2],
"ls") != 0) {
422 fs_dir *d = idx->opendir(
"/");
425 fs_dirent *e = idx->readdir(d, &storage);
426 if(e) idx->cd(e->full_name);
431 docmd(argc-2, &argv[2]);
432 ret = idx->mountNextDir();
441 char *_strtok_static_;
449 strcpy(pwd, idx->pwd());
452 printf(
"%s:%s > ", g_filename, pwd);
458 av[0] = strtok_r(buff,
" ", &_strtok_static_);
459 if(av[0] == NULL)
continue;
461 for(ac = 1; ac < 10; ac++) {
462 av[ac] = strtok_r(NULL,
" ", &_strtok_static_);
463 if(av[ac] == NULL)
break;
466 if(strcmp(av[0],
"quit") == 0)
break;