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 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);
00067 void dump_desc(pst_desc_tree *ptr)
00068 {
00069 while (ptr) {
00070 DEBUG_INFO(("\n\n\nLooking at block desc id %#"PRIx64"\n", ptr->d_id));
00071 if (ptr->desc && ptr->desc->i_id) dumper(ptr->desc->i_id);
00072 if (ptr->assoc_tree && ptr->assoc_tree->i_id) dumper(ptr->assoc_tree->i_id);
00073 if (ptr->child) dump_desc(ptr->child);
00074 ptr = ptr->next;
00075 }
00076 }
00077
00078
00079 int main(int argc, char* const* argv)
00080 {
00081
00082 char *fname, *sid;
00083 uint64_t i_id;
00084 int c;
00085
00086 DEBUG_INIT("getidblock.log", NULL);
00087 DEBUG_ENT("main");
00088
00089 while ((c = getopt(argc, argv, "bp")) != -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_INFO(("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_INFO(("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