getidblock.c

Go to the documentation of this file.
00001 
00002 #include "define.h"
00003 
00004 int process = 0, binary = 0;
00005 pst_file pstfile;
00006 
00007 
00008 void usage();
00009 void usage()
00010 {
00011     printf("usage: getidblock [options] filename id\n");
00012     printf("\tfilename - name of the file to access\n");
00013     printf("\tid - ID of the block to fetch (0 to fetch all) - can begin with 0x for hex\n");
00014     printf("\toptions\n");
00015     printf("\t\t-p\tProcess the block before finishing.\n");
00016     printf("\t\t-b\tDump the blocks in binary to stdout.\n");
00017     printf("\t\t\tView the debug log for information\n");
00018 }
00019 
00020 
00021 void dumper(uint64_t i_id);
00022 void dumper(uint64_t i_id)
00023 {
00024     char *buf = NULL;
00025     size_t readSize;
00026     pst_desc_tree *ptr;
00027 
00028     DEBUG_INFO(("\n\n\nLooking at block index1 id %#"PRIx64"\n", i_id));
00029 
00030     if ((readSize = pst_ff_getIDblock_dec(&pstfile, i_id, &buf)) <= 0 || buf == 0) {
00031         DIE(("Error loading block\n"));
00032     }
00033 
00034     DEBUG_INFO(("Printing block i_id %#"PRIx64", size %#"PRIx64"\n", i_id, (uint64_t)readSize));
00035     if (binary) {
00036         if (fwrite(buf, 1, readSize, stdout) != 0) {
00037             DIE(("Error occured during writing of buf to stdout\n"));
00038         }
00039     } else {
00040         printf("Block id %#"PRIx64", size %#"PRIx64"\n", i_id, (uint64_t)readSize);
00041         pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
00042     }
00043     if (buf) free(buf);
00044 
00045     if (process) {
00046         DEBUG_INFO(("Parsing block id %#"PRIx64"\n", i_id));
00047         ptr = pstfile.d_head;
00048         while (ptr) {
00049             if (ptr->assoc_tree && ptr->assoc_tree->i_id == i_id)
00050                 break;
00051             if (ptr->desc && ptr->desc->i_id == i_id)
00052                 break;
00053             ptr = pst_getNextDptr(ptr);
00054         }
00055         if (!ptr) {
00056             ptr = (pst_desc_tree *) pst_malloc(sizeof(pst_desc_tree));
00057             memset(ptr, 0, sizeof(pst_desc_tree));
00058             ptr->desc = pst_getID(&pstfile, i_id);
00059         }
00060         pst_item *item = pst_parse_item(&pstfile, ptr, NULL);
00061         if (item) pst_freeItem(item);
00062     }
00063 }
00064 
00065 
00066 void dump_desc(pst_desc_tree *ptr, pst_desc_tree *parent);
00067 void dump_desc(pst_desc_tree *ptr, pst_desc_tree *parent)
00068 {
00069     while (ptr) {
00070         uint64_t parent_d_id = (parent) ? parent->d_id : 0;
00071         printf("Descriptor block d_id %#"PRIx64" parent d_id %#"PRIx64" children %i desc.i_id=%#"PRIx64", assoc tree.i_id=%#"PRIx64"\n",
00072             ptr->d_id, parent_d_id, ptr->no_child,
00073             (ptr->desc       ? ptr->desc->i_id       : (uint64_t)0),
00074             (ptr->assoc_tree ? ptr->assoc_tree->i_id : (uint64_t)0));
00075         if (ptr->desc       && ptr->desc->i_id)       dumper(ptr->desc->i_id);
00076         if (ptr->assoc_tree && ptr->assoc_tree->i_id) dumper(ptr->assoc_tree->i_id);
00077         if (ptr->child) dump_desc(ptr->child, ptr);
00078         ptr = ptr->next;
00079     }
00080 }
00081 
00082 
00083 int main(int argc, char* const* argv)
00084 {
00085     // pass the id number to display on the command line
00086     char *fname, *sid;
00087     uint64_t i_id;
00088     int c;
00089 
00090     DEBUG_INIT("getidblock.log", NULL);
00091     DEBUG_ENT("main");
00092 
00093     while ((c = getopt(argc, argv, "bp")) != -1) {
00094         switch (c) {
00095             case 'b':
00096                 // enable binary output
00097                 binary = 1;
00098                 break;
00099             case 'p':
00100                 // enable procesing of block
00101                 process = 1;
00102                 break;
00103             default:
00104                 usage();
00105                 exit(EXIT_FAILURE);
00106         }
00107     }
00108 
00109     if (optind + 1 >= argc) {
00110         // no more items on the cmd
00111         usage();
00112         exit(EXIT_FAILURE);
00113     }
00114     fname = argv[optind];
00115     sid   = argv[optind + 1];
00116     i_id  = (uint64_t)strtoll(sid, NULL, 0);
00117 
00118     DEBUG_INFO(("Opening file\n"));
00119     memset(&pstfile, 0, sizeof(pstfile));
00120     if (pst_open(&pstfile, fname, NULL)) {
00121         DIE(("Error opening file\n"));
00122     }
00123 
00124     DEBUG_INFO(("Loading Index\n"));
00125     if (pst_load_index(&pstfile) != 0) {
00126         DIE(("Error loading file index\n"));
00127     }
00128 
00129     if (i_id) {
00130         dumper(i_id);
00131     }
00132     else {
00133         size_t i;
00134         for (i = 0; i < pstfile.i_count; i++) {
00135             dumper(pstfile.i_table[i].i_id);
00136         }
00137         dump_desc(pstfile.d_head, NULL);
00138     }
00139 
00140     if (pst_close(&pstfile) != 0) {
00141         DIE(("pst_close failed\n"));
00142     }
00143 
00144     DEBUG_RET();
00145     return 0;
00146 }
00147 

Generated on 6 Jul 2016 for 'LibPst' by  doxygen 1.6.1