int get_superblock(struct ext_superblock_t *sb, int fd, char *buf, uint32_t size, int fs) { int offset = align_read(fd, buf, SUPERBLOCKOFFSET, SUPERBLOCKSIZE, size, DOM0BLOCKSIZE); if (offset < 0) { printf("offset:%d\n", offset); return -1; } if (parse_superblock(sb, buf + offset, size - offset, fs) < 0) { printf("parse error\n"); return -1; } return 0; }
struct data *parse_file(char *buf,const char *filename,char *_filename){ struct zfs_superblock *sblk = malloc(sizeof(struct zfs_superblock *)); parse_superblock(filename,sblk); int end = 0; while(1){ if(buf[end] == 0x0F && buf[end + 1] == 'E' && buf[end + 2] == 0x0F && buf[end + 3] == 'O' && buf[end + 4] == 0x0F && buf[end + 5] == 'F' && buf[end + 6] == 0x0F) break; end++; } end--; printf("[end] %d [sblk.fds] %d\n",end,sblk->fds); struct data *ret = malloc(sizeof(struct data *)); int *se = malloc(80); if((end % 512) == 0) se = find_unalloc_block(filename,end,*sblk); else se = find_unalloc_block(filename,end,*sblk); if(!(se)){ printf("Error finding unallocated block!\n"); return -1; } printf("[se(0)] %d [se(1)] %d\n",se[0],se[1]); ret->alloc = 1; ret->startingblock = se[0]; ret->endingblock = se[1]; if(strlen(_filename) > 10) ret->islongname = 1; else ret->islongname = 0; if(ret->islongname) ret->namelen = strlen(filename); else ret->namelen = 0; ret->name = _filename; ret->type = TYPE_FILE; ret->blockpos = ret->startingblock; return ret; }
int main(int argc, char* argv[]){ if(argc != 3){ usage(argv[0]); return -1; } FILE *f; if(!(f = fopen(argv[1],"r"))){ printf("Error opening file:%s\n",argv[1]); return -1; } fclose(f); if(!(f = fopen(argv[2],"r"))){ printf("Error opening file:%s\n",argv[2]); } char *buf = malloc(get_end(argv[1])); printf("Reading file to buf\n"); if(!(read_buf(buf,argv[1]))){ printf("Error reading to buffer!\n"); return -1; } printf("Erasing %s\n",argv[1]); if(!(zero_out(argv[1]))){ printf("Error erasing %s\n",argv[1]); } printf("Rewriting\n"); if(!(write_buf(buf,argv[1]))){ printf("Error restoring %s\n",argv[1]); } printf("freeing memory\n"); free(buf); printf("Reading buffer\n"); buf = malloc(get_end(argv[1])); read_buf(buf,argv[1]); printf("Parsing superblock\n"); struct zfs_superblock *sblk = malloc(sizeof(struct zfs_superblock *)); parse_superblock(argv[1],sblk); if(!(sblk)) return -1; printf("Dumping: [allocblk] %d [unallocblk] %d [tblk] %d\n [dataresv] %d [rootfsblk] %d [numoff] %d\n [numofd] %d [fds] %d\n",sblk->allocblk,sblk->unallocblk,sblk->tblk,sblk->dataresv,sblk->rootfsblk,sblk->numoff,sblk->numofd,sblk->fds); printf("Finding datablock\n"); int db = find_datablk(*sblk,argv[1]); if(db < 0){ printf("Error finding datablock!\n"); return -1; } printf("freeing buffer\n"); free(buf); printf("Allocating memory\n"); buf = malloc(get_end(argv[2])); if(!(buf)){ printf("Error allocating %d bytes of memory!\n",get_end(argv[2])); } printf("Reading buffer\n"); if(!(read_buf(buf,argv[2]))){ printf("Error reading buffer!\n"); return -1;; } printf("Parsing file\n"); struct data *d = parse_file(buf,argv[1],argv[2]); if(!(d)){ printf("Error parsing file!\n"); return -1; } printf("Dumping: [alloc] %d [islongname] %d [namelen] %d [name] %s\n [type] %d [offset] %d [endingpos] %d [blockpos] %d\n [startingblock] %d [endingblock] %d\n" ,d->alloc,d->islongname,d->namelen,d->name,d->type,d->offset,d->endingpos,d->blockpos,d->startingblock,d->endingblock); printf("Writing datablock\n"); if(!(write_datablk(*sblk,d,argv[1],db))){ printf("Error writing datablock!\n"); return -1; } }