00001 #include "define.h"
00002
00003 #define OUT_BUF 20
00004
00005 int main(int argc, char* const* argv)
00006 {
00007 pst_file pstfile;
00008 pst_index_ll *ptr;
00009 char *outdir = NULL, *file = NULL, *outname = NULL;
00010 char *buf = NULL;
00011 int c;
00012 FILE *fp;
00013
00014 while ((c = getopt(argc, argv, "o:")) != -1) {
00015 switch (c) {
00016 case 'o':
00017 outdir = optarg;
00018 break;
00019 default:
00020 printf("Unknown switch %c\n", c);
00021 }
00022 }
00023 if (optind < argc) {
00024 file = argv[optind];
00025 } else {
00026 printf("Usage: dumpblocks [options] pstfile\n");
00027 printf("\tcopies the datablocks from the pst file into separate files\n");
00028 printf("Options: \n");
00029 printf("\t-o target\tSpecify the output directory\n");
00030 exit(1);
00031 }
00032 DEBUG_INIT("dumpblocks.log");
00033 DEBUG_REGISTER_CLOSE();
00034 DEBUG_ENT("main");
00035
00036 printf("Opening file %s\n", file);
00037 if (pst_open(&pstfile, file)) {
00038 printf("Failed to open file %s\n", file);
00039 exit(1);
00040 }
00041
00042 printf("Reading Indexes\n");
00043 if (pst_load_index(&pstfile)) {
00044 printf("Failed to load indexes in file %s\n", argv[1]);
00045 exit(1);
00046 }
00047
00048 if (outdir != NULL)
00049 if (chdir(outdir)) {
00050 printf("Failed to change into directory %s\n", outdir);
00051 exit(1);
00052 }
00053
00054 ptr = pstfile.i_head;
00055 outname = (char *) pst_malloc(OUT_BUF);
00056 printf("Saving blocks\n");
00057 while (ptr != NULL) {
00058 size_t c;
00059 c = pst_ff_getIDblock_dec(&pstfile, ptr->i_id, &buf);
00060 if (c) {
00061 snprintf(outname, OUT_BUF, "%#"PRIx64, ptr->i_id);
00062 if ((fp = fopen(outname, "wb")) == NULL) {
00063 printf("Failed to open file %s\n", outname);
00064 continue;
00065 }
00066 pst_fwrite(buf, 1, c, fp);
00067 fclose(fp);
00068 } else {
00069 printf("Failed to read block i_id %#"PRIx64"\n", ptr->i_id);
00070 }
00071 ptr = ptr->next;
00072 }
00073 pst_close(&pstfile);
00074 DEBUG_RET();
00075 return 0;
00076 }