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 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", 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 ptr = pstfile.i_head;
00054 outname = (char *) pst_malloc(OUT_BUF);
00055 printf("Saving blocks\n");
00056 while (ptr != NULL) {
00057 size_t c;
00058 c = pst_ff_getIDblock_dec(&pstfile, ptr->i_id, &buf);
00059 if (c) {
00060 snprintf(outname, OUT_BUF, "%#"PRIx64, ptr->i_id);
00061 if ((fp = fopen(outname, "wb")) == NULL) {
00062 printf("Failed to open file %s\n", outname);
00063 continue;
00064 }
00065 pst_fwrite(buf, 1, c, fp);
00066 fclose(fp);
00067 } else {
00068 printf("Failed to read block i_id %#"PRIx64"\n", ptr->i_id);
00069 }
00070 ptr = ptr->next;
00071 }
00072 pst_close(&pstfile);
00073 DEBUG_RET();
00074 return 0;
00075 }