dumpblocks.c
Go to the documentation of this file.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 size_t i;
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", NULL);
00033 DEBUG_ENT("main");
00034
00035 printf("Opening file %s\n", file);
00036 if (pst_open(&pstfile, file, NULL)) {
00037 printf("Failed to open file %s\n", file);
00038 exit(1);
00039 }
00040
00041 printf("Reading Indexes\n");
00042 if (pst_load_index(&pstfile)) {
00043 printf("Failed to load indexes in file %s\n", argv[1]);
00044 exit(1);
00045 }
00046
00047 if (outdir != NULL)
00048 if (chdir(outdir)) {
00049 printf("Failed to change into directory %s\n", outdir);
00050 exit(1);
00051 }
00052
00053 outname = (char *) pst_malloc(OUT_BUF);
00054 printf("Saving blocks\n");
00055 for (i = 0; i < pstfile.i_count; i++) {
00056 pst_index_ll *ptr = &pstfile.i_table[i];
00057 size_t c = pst_ff_getIDblock_dec(&pstfile, ptr->i_id, &buf);
00058 if (c) {
00059 snprintf(outname, OUT_BUF, "%#"PRIx64, ptr->i_id);
00060 if ((fp = fopen(outname, "wb")) == NULL) {
00061 printf("Failed to open file %s\n", outname);
00062 continue;
00063 }
00064 pst_fwrite(buf, 1, c, fp);
00065 fclose(fp);
00066 } else {
00067 printf("Failed to read block i_id %#"PRIx64"\n", ptr->i_id);
00068 }
00069 }
00070 pst_close(&pstfile);
00071 DEBUG_RET();
00072 return 0;
00073 }