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\tView the debug log for information\n");
00017 }
00018
00019
00020 void dumper(uint64_t i_id);
00021 void dumper(uint64_t i_id)
00022 {
00023 char *buf = NULL;
00024 size_t readSize;
00025 pst_desc_ll *ptr;
00026
00027 DEBUG_MAIN(("\n\n\nLooking at block index1 id %#"PRIx64"\n", i_id));
00028
00029 if ((readSize = pst_ff_getIDblock_dec(&pstfile, i_id, &buf)) <= 0 || buf == 0) {
00030 DIE(("Error loading block\n"));
00031 }
00032
00033 DEBUG_MAIN(("Printing block id %#"PRIx64", size %#"PRIx64"\n", i_id, (uint64_t)readSize));
00034 if (binary) {
00035 if (fwrite(buf, 1, readSize, stdout) != 0) {
00036 DIE(("Error occured during writing of buf to stdout\n"));
00037 }
00038 } else {
00039 printf("Block id %#"PRIx64", size %#"PRIx64"\n", i_id, (uint64_t)readSize);
00040 pst_debug_hexdumper(stdout, buf, readSize, 0x10, 0);
00041 }
00042 if (buf) free(buf);
00043
00044 if (process) {
00045 DEBUG_MAIN(("Parsing block id %#"PRIx64"\n", i_id));
00046 ptr = pstfile.d_head;
00047 while (ptr) {
00048 if (ptr->assoc_tree && ptr->assoc_tree->i_id == i_id)
00049 break;
00050 if (ptr->desc && ptr->desc->i_id == i_id)
00051 break;
00052 ptr = pst_getNextDptr(ptr);
00053 }
00054 if (!ptr) {
00055 ptr = (pst_desc_ll *) xmalloc(sizeof(pst_desc_ll));
00056 memset(ptr, 0, sizeof(pst_desc_ll));
00057 ptr->desc = pst_getID(&pstfile, i_id);
00058 }
00059 pst_item *item = pst_parse_item(&pstfile, ptr, NULL);
00060 if (item) pst_freeItem(item);
00061 }
00062 }
00063
00064
00065 void dump_desc(pst_desc_ll *ptr);
00066 void dump_desc(pst_desc_ll *ptr)
00067 {
00068 while (ptr) {
00069 DEBUG_MAIN(("\n\n\nLooking at block desc id %#"PRIx64"\n", ptr->d_id));
00070 if (ptr->desc && ptr->desc->i_id) dumper(ptr->desc->i_id);
00071 if (ptr->assoc_tree && ptr->assoc_tree->i_id) dumper(ptr->assoc_tree->i_id);
00072 if (ptr->child) dump_desc(ptr->child);
00073 ptr = ptr->next;
00074 }
00075 }
00076
00077
00078 int main(int argc, char* const* argv)
00079 {
00080
00081 char *fname, *sid;
00082 uint64_t i_id;
00083 int c;
00084
00085 DEBUG_INIT("getidblock.log");
00086 DEBUG_REGISTER_CLOSE();
00087 DEBUG_ENT("main");
00088
00089 while ((c = getopt(argc, argv, "bdp")) != -1) {
00090 switch (c) {
00091 case 'b':
00092
00093 binary = 1;
00094 break;
00095 case 'p':
00096
00097 process = 1;
00098 break;
00099 default:
00100 usage();
00101 exit(EXIT_FAILURE);
00102 }
00103 }
00104
00105 if (optind + 1 >= argc) {
00106
00107 usage();
00108 exit(EXIT_FAILURE);
00109 }
00110 fname = argv[optind];
00111 sid = argv[optind + 1];
00112 i_id = (uint64_t)strtoll(sid, NULL, 0);
00113
00114 DEBUG_MAIN(("Opening file\n"));
00115 memset(&pstfile, 0, sizeof(pstfile));
00116 if (pst_open(&pstfile, fname)) {
00117 DIE(("Error opening file\n"));
00118 }
00119
00120 DEBUG_MAIN(("Loading Index\n"));
00121 if (pst_load_index(&pstfile) != 0) {
00122 DIE(("Error loading file index\n"));
00123 }
00124
00125 if (i_id) {
00126 dumper(i_id);
00127 }
00128 else {
00129 pst_index_ll *ptr = pstfile.i_head;
00130 while (ptr) {
00131 dumper(ptr->i_id);
00132 ptr = ptr->next;
00133 }
00134 dump_desc(pstfile.d_head);
00135 }
00136
00137 if (pst_close(&pstfile) != 0) {
00138 DIE(("pst_close failed\n"));
00139 }
00140
00141 DEBUG_RET();
00142 return 0;
00143 }
00144