00001 #include <stdio.h>
00002 #include <sys/types.h>
00003 #include <sys/stat.h>
00004 #include <fcntl.h>
00005 #include <errno.h>
00006 #include <string.h>
00007 #include <stdlib.h>
00008 #include <unistd.h>
00009 #include <time.h>
00010 #include <ctype.h>
00011 #include <SUNRT/clock.h>
00012 #include "sfs_index.h"
00013 #include "get_line.h"
00014 #include <rtsLog.h>
00015
00016 char g_filename[100];
00017 sfs_index *idx;
00018 int idx_fd;
00019
00020 char *striptodir(char *str)
00021 {
00022 char *ostr = str;
00023 char *lslash = str-1;
00024
00025 while(*str != '\0') {
00026 if(*str == '/') {
00027
00028 if(*(str+1) != '\0') lslash = str;
00029 }
00030 str++;
00031 }
00032
00033 lslash++;
00034 *lslash = '\0';
00035 return ostr;
00036 }
00037
00038
00039
00040 int fs_cd(int argc, char *argv[])
00041 {
00042 if(argc !=2) return -1;
00043
00044 if(idx->cd(argv[1]) < 0) {
00045 printf("%s is invalid directory\n",argv[1]);
00046 }
00047
00048
00049 printf("pwd set to %s\n", idx->cwd);
00050
00051 return 0;
00052 }
00053
00054 int fs_pwd()
00055 {
00056 printf("%s\n", idx->cwd);
00057 return 0;
00058 }
00059
00060
00061 int fs_mount(int argc, char *argv[])
00062 {
00063 if(idx) {
00064 idx->umount();
00065 delete idx;
00066 }
00067
00068 double t = record_time();
00069 if(argc < 2) {
00070 printf("mount <daqfilename>\n");
00071 return -1;
00072 }
00073
00074 char *pwd=getenv("PWD");
00075 char fn[255];
00076
00077 if(argv[1][0] == '/') {
00078 strcpy(fn,argv[1]);
00079 }
00080 else {
00081 if(pwd == NULL) pwd = "/";
00082 sprintf(fn,"%s/%s",pwd,argv[1]);
00083 }
00084
00085 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
00086 struct stat64 filestat;
00087 if(stat64(fn,&filestat) < 0)
00088 #else
00089 struct stat filestat;
00090 if(stat(fn,&filestat) < 0)
00091 #endif
00092 {
00093 printf("Error reading file %s: %s\n",fn,strerror(errno));
00094 return 1;
00095 }
00096
00097 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
00098 idx_fd = open64(fn, O_RDONLY);
00099 #else
00100 idx_fd = open(fn, O_RDONLY);
00101 #endif
00102
00103 if(idx_fd < 0) {
00104 printf("Error reading file %s\n",fn);
00105 strcpy(g_filename, "NONE");
00106
00107
00108
00109 return -1;
00110 }
00111
00112 idx = new sfs_index;
00113
00114 idx->mount(fn, O_RDONLY);
00115 t = record_time();
00116
00117 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
00118 printf("Mounted file %s: %lld bytes in %5.2f sec\n",fn,filestat.st_size,t);
00119 #else
00120 printf("Mounted file %s: %d bytes in %5.2f sec\n",fn,filestat.st_size,t);
00121 #endif
00122
00123
00124
00125
00126
00127 strcpy(g_filename, fn);
00128 return 0;
00129 }
00130
00131 int ls_dir(char *tabs, int recurse, fs_dir *dir)
00132 {
00133
00134
00135
00136 fs_dirent *entry;
00137 while((entry = idx->readdir(dir))) {
00138 printf("%s [%7d bytes] %s%c\n",
00139 tabs,entry->sz,entry->full_name,entry->has_child?'/':' ');
00140
00141 if(recurse & entry->has_child) {
00142 fs_dir *ndir = idx->opendir(entry->full_name);
00143 ls_dir(tabs, recurse, ndir);
00144 idx->closedir(ndir);
00145 }
00146 }
00147 return 0;
00148 }
00149
00150 int fs_ls(int argc, char *argv[])
00151 {
00152 int recurse = 0;
00153
00154 if(argc > 1) {
00155 if(strcmp(argv[1], "-r") == 0) {
00156 recurse = 1;
00157 }
00158 }
00159
00160
00161 fs_dir *dir = idx->opendir(idx->cwd);
00162 if(!dir) {
00163 printf("Error opening directory %s\n",idx->cwd);
00164 return -1;
00165 }
00166
00167 ls_dir("", recurse, dir);
00168
00169 idx->closedir(dir);
00170
00171 return 0;
00172 }
00173
00174 int fs_cat(int argc, char *argv[])
00175 {
00176 if(argc < 2) {
00177 printf("cat <file> <optional output file>\n");
00178 return -1;
00179 }
00180
00181
00182 int type;
00183 if(strcmp(argv[0], "cat") == 0) type = 0;
00184 else if (strcmp(argv[0], "od") == 0) type = 8;
00185 else type = 1;
00186
00187 int sz = idx->fileSize(argv[1]);
00188 if(sz < 0) return -1;
00189
00190 char *buff = (char *)malloc(sz);
00191 if(!buff) {
00192 printf("Error allocating %d bytes\n",sz);
00193 return -1;
00194 }
00195
00196 int ret = idx->read(argv[1], buff, sz);
00197 if(ret != sz) {
00198 printf("Error reading %d bytes\n",sz);
00199 }
00200
00201 if(strcmp(argv[0], "save") == 0) {
00202 if(argc < 3) {
00203 printf("Need a filename\n");
00204 free(buff);
00205 return -1;
00206 }
00207 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
00208 int fd = open64(argv[2], O_WRONLY | O_CREAT,0777);
00209 #else
00210 int fd = open(argv[2], O_WRONLY | O_CREAT, 0777);
00211 #endif
00212
00213 if(fd < 0) {
00214 free(buff);
00215 printf("error opening file %s\n",argv[2]);
00216 return -1;
00217 }
00218 int ret = write(fd,buff,sz);
00219 if(ret < sz) {
00220 printf("Error writing to file %s (only wrote %d of %d bytes\n",
00221 argv[2], ret, sz);
00222 free(buff);
00223 return -1;
00224 }
00225 close(fd);
00226 free(buff);
00227 return 0;
00228 }
00229
00230 if(type == 0) {
00231 write(STDOUT_FILENO, buff, sz);
00232 write(STDOUT_FILENO, "\n", sizeof("\n"));
00233
00234 }
00235 else if (type == 8) {
00236
00237
00238
00239 fs_index::hexdump(buff, sz);
00240 printf("\n");
00241 }
00242 else {
00243 int instr = 0;
00244 for(int i=0;i<sz;i++) {
00245 if(isprint(buff[i])) {
00246 instr = 1;
00247 putchar(buff[i]);
00248 }
00249 else {
00250 if(instr == 1) {
00251 printf("\n");
00252 instr = 0;
00253 }
00254 }
00255 }
00256 printf("\n");
00257 }
00258
00259 free(buff);
00260 return 0;
00261 }
00262
00263
00264
00265 int help()
00266 {
00267 printf("fs <cmd> <args>\n");
00268
00269 printf("\tcd <dir>\n");
00270 printf("\tdump\n");
00271 printf("\tpwd\n");
00272 printf("\tmount <file>\n");
00273 printf("\tls\n");
00274
00275 printf("\tstrings <fn>\n");
00276 printf("\tod <fn>\n");
00277 printf("\tsave <fn> <ofn>\n");
00278 printf("\tsize <dir>\n");
00279 printf("\n\n");
00280 return 1;
00281 }
00282
00283
00284 int docmd(int argc, char *argv[])
00285 {
00286 if((strcmp(argv[0], "mount") == 0)) {
00287 fs_mount(argc, argv);
00288 return 0;
00289 }
00290
00291 if(!idx) {
00292 printf("No mounted file: mount a file first\n");
00293 help();
00294 return 0;
00295 }
00296 else if(strcmp(argv[0], "pwd") == 0) {
00297 fs_pwd();
00298 return 0;
00299 }
00300 else if(strcmp(argv[0], "cd") == 0) {
00301 fs_cd(argc, argv);
00302 return 0;
00303 }
00304 else if(strcmp(argv[0], "ls") == 0) {
00305 fs_ls(argc, argv);
00306 return 0;
00307 }
00308 else if(strcmp(argv[0], "dump") == 0) {
00309 idx->dump(idx_fd);
00310 return 0;
00311 }
00312 else if(strcmp(argv[0], "od") == 0) {
00313 fs_cat(argc, argv);
00314 return 0;
00315 }
00316 else if(strcmp(argv[0], "save") == 0) {
00317 fs_cat(argc, argv);
00318 return 0;
00319 }
00320 else if(strcmp(argv[0], "strings") == 0) {
00321 fs_cat(argc, argv);
00322 return 0;
00323 }
00324 else if(strcmp(argv[0], "size") == 0) {
00325 SfsDirsize x;
00326 ((sfs_index *)idx)->getDirSize(argv[1], &x);
00327 printf("x->size = %lld x->dataSize = %lld\n",x.size, x.dataSize);
00328 return 0;
00329 }
00330 else {
00331 printf("\nInvalid command: %s\n\n",argv[0]);
00332 help();
00333 return 0;
00334 }
00335 }
00336
00337
00338 int main(int argc, char *argv[])
00339 {
00340
00341 char *av[10];
00342 int ac = 0;
00343
00344 rtsLogOutput(2);
00345 rtsLogLevel(WARN);
00346 strcpy(g_filename, "none");
00347
00348 idx = NULL;
00349
00350 if(argc > 1) {
00351
00352 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
00353 struct stat64 sstat;
00354 int ret = stat64(argv[1], &sstat);
00355 #else
00356 struct stat sstat;
00357 int ret = stat(argv[1], &sstat);
00358 #endif
00359
00360 if(ret != 0) {
00361 printf("No file %s\n",argv[1]);
00362 return -1;
00363 }
00364
00365 if(argc == 2) {
00366 av[0] = "mount";
00367 av[1] = argv[1];
00368 ac = 2;
00369
00370 docmd(ac, av);
00371 }
00372 else {
00373 idx = new sfs_index();
00374 int ret = idx->mountSingleDir(argv[1]);
00375 if(ret < 0) {
00376 printf("Error reading %s\n", argv[1]);
00377 return -1;
00378 }
00379 do {
00380
00381 if(strcmp(argv[2],"ls") != 0) {
00382 fs_dir *d = idx->opendir("/");
00383 if(d) {
00384 fs_dirent *e = idx->readdir(d);
00385 if(e) idx->cd(e->full_name);
00386 idx->closedir(d);
00387 }
00388 }
00389
00390 docmd(argc-2, &argv[2]);
00391 ret = idx->mountNextDir();
00392 } while(ret > 0);
00393
00394
00395 return 0;
00396 }
00397 }
00398
00399 for(;;) {
00400 char buff[256];
00401 char pwd[100];
00402
00403 if(!idx) {
00404 strcpy(pwd, "none");
00405 }
00406 else {
00407 strcpy(pwd, idx->pwd());
00408 }
00409
00410 printf("%s:%s > ", g_filename, pwd);
00411
00412 fflush(stdout);
00413
00414 get_line(buff);
00415
00416 av[0] = strtok(buff, " ");
00417 if(av[0] == NULL) continue;
00418
00419 for(ac = 1; ac < 10; ac++) {
00420 av[ac] = strtok(NULL, " ");
00421 if(av[ac] == NULL) break;
00422 }
00423
00424 if(strcmp(av[0], "quit") == 0) break;
00425
00426 docmd(ac, av);
00427
00428 }
00429
00430 return 0;
00431 }
00432
00433
00434