int ext2fs_mark_generic_bmap(ext2fs_generic_bitmap bitmap, __u64 arg) { if (!bitmap) return 0; if (EXT2FS_IS_32_BITMAP(bitmap)) { if (arg & ~0xffffffffULL) { ext2fs_warn_bitmap2(bitmap, EXT2FS_MARK_ERROR, 0xffffffff); return 0; } return ext2fs_mark_generic_bitmap(bitmap, arg); } if (!EXT2FS_IS_64_BITMAP(bitmap)) return 0; arg >>= bitmap->cluster_bits; if ((arg < bitmap->start) || (arg > bitmap->end)) { warn_bitmap(bitmap, EXT2FS_MARK_ERROR, arg); return 0; } return bitmap->bitmap_ops->mark_bmap(bitmap, arg); }
//Subfunction for "local_block_iterate3()" read a blocks of a long symlink static int read_syslink_block ( ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt, blk64_t /*ref_blk*/x, int /*ref_offset*/y, void *priv ) { char *charbuf =((struct privat*)priv)->buf; // __u32 nbytes; errcode_t retval; // int blocksize = fs->blocksize; if (*blocknr >= fs->super->s_blocks_count) return BLOCK_ERROR; if (((struct privat*)priv)->flag){ int allocated = ext2fs_test_block_bitmap ( fs->block_map, *blocknr ); if ( allocated ){ ((struct privat*)priv)->error = 1; // fprintf(stderr,"Block %10lu is allocated.\n",*blocknr); return (BLOCK_ABORT | BLOCK_ERROR); } } retval = io_channel_read_blk ( fs->io, *blocknr, 1, charbuf ); if (retval){ ((struct privat*)priv)->error = retval; return (BLOCK_ERROR); } if (bmap) ext2fs_mark_generic_bitmap(bmap, *blocknr); return 0; }
// Subfunction for "local_block_iterate3()" for check if the blocks allocated static int check_block(ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt, blk64_t /*ref_blk*/x, int /*ref_offset*/y, void *priv ) { //FIXME: if (*blocknr >= fs->super->s_blocks_count) return BLOCK_ERROR; struct alloc_stat *stat = priv; if ( ext2fs_test_block_bitmap ( fs->block_map, *blocknr )) (stat->allocated)++ ; else (stat->not_allocated)++ ; if (bmap) ext2fs_mark_generic_bitmap(bmap, *blocknr); return 0; }
//Subfunction for "local_block_iterate3()" for recover the blocks of a real file static int write_block ( ext2_filsys fs, blk64_t *blocknr, e2_blkcnt_t blockcnt, blk64_t /*ref_blk*/x, int /*ref_offset*/y, void *priv ) { int fd = ((struct privat*)priv)->fd; char *charbuf =((struct privat*)priv)->buf; __u32 nbytes; errcode_t retval; int blocksize = fs->blocksize; if (*blocknr >= fs->super->s_blocks_count) return BLOCK_ERROR; #ifdef DEBUG printf("%c",(ext2fs_test_block_bitmap ( fs->block_map, *blocknr ))? 'X' : 'O'); #endif if (((struct privat*)priv)->flag){ int allocated = ext2fs_test_block_bitmap ( fs->block_map, *blocknr ); if ( allocated ){ // fprintf(stderr,"Block %10lu is allocated.\n",*blocknr); ((struct privat*)priv)->error = BLOCK_ABORT | BLOCK_ERROR ; return (BLOCK_ABORT | BLOCK_ERROR); } } retval = io_channel_read_blk ( fs->io, *blocknr, 1, charbuf ); if (retval){ ((struct privat*)priv)->error = BLOCK_ERROR ; return (BLOCK_ERROR); } if (bmap) ext2fs_mark_generic_bitmap(bmap, *blocknr); lseek(fd,(unsigned long long )blocksize * blockcnt, SEEK_SET); nbytes = write(fd, charbuf, blocksize); if ((unsigned) nbytes != blocksize){ fprintf(stderr, "Error while writing file\n"); ((struct privat*)priv)->error = BLOCK_ERROR ; return 8; } return retval; }
int ext2fs_mark_generic_bmap(ext2fs_generic_bitmap bitmap, __u64 arg) { if (!bitmap) return 0; if (EXT2FS_IS_32_BITMAP(bitmap)) { if (arg & ~0xffffffffULL) { ext2fs_warn_bitmap2(bitmap, EXT2FS_MARK_ERROR, 0xffffffff); return 0; } return ext2fs_mark_generic_bitmap(bitmap, arg); } if (!EXT2FS_IS_64_BITMAP(bitmap)) return 0; arg >>= bitmap->cluster_bits; #ifdef BMAP_STATS_OPS if (arg == bitmap->stats.last_marked + 1) bitmap->stats.mark_seq++; if (arg < bitmap->stats.last_marked) bitmap->stats.mark_back++; bitmap->stats.last_marked = arg; bitmap->stats.mark_count++; #endif if ((arg < bitmap->start) || (arg > bitmap->end)) { warn_bitmap(bitmap, EXT2FS_MARK_ERROR, arg); return 0; } return bitmap->bitmap_ops->mark_bmap(bitmap, arg); }
// search inode by use imap (step1: flag 1 = only directory ; step2: flag 0 = only file) static void search_imap_inode(char* des_dir, __u32 t_after, __u32 t_before, int flag) { struct ext2_group_desc *gdp; struct ext2_inode_large *inode; //struct dir_list_head_t *dir = NULL; struct ring_buf* i_list = NULL; r_item* item = NULL; int zero_flag, retval, load, x ,i ; char *pathname = NULL; char *i_pathname = NULL; char *buf= NULL; unsigned char *tmp_buf = NULL; __u32 blocksize, inodesize, inode_max, inode_per_group, block_count; __u32 inode_per_block , inode_block_group, group; blk_t block_nr; __u32 c_time, d_time, mode; ext2_ino_t first_block_inode_nr , inode_nr; pathname = malloc(26); blocksize = current_fs->blocksize; inodesize = current_fs->super->s_inode_size; inode_max = current_fs->super->s_inodes_count; inode_per_group = current_fs->super->s_inodes_per_group; buf = malloc(blocksize); if (! (flag & 0x01) ){ tmp_buf = malloc (12 * blocksize); if (!tmp_buf) goto errout; cookie = magic_open(MAGIC_MIME | MAGIC_NO_CHECK_COMPRESS | MAGIC_NO_CHECK_ELF | MAGIC_CONTINUE); if ((! cookie) || magic_load(cookie, NULL)){ fprintf(stderr,"ERROR: can't find libmagic\n"); goto errout; } } inode_per_block = blocksize / inodesize; inode_block_group = inode_per_group / inode_per_block; for (group = 0 ; group < current_fs->group_desc_count ; group++){ #ifdef EXT2_FLAG_64BITS gdp = ext2fs_group_desc(current_fs, current_fs->group_desc, group); #else gdp = ¤t_fs->group_desc[group]; #endif zero_flag = 0; if (!(flag & 0x02)){ //skip this in disaster mode // NEXT GROUP IF INODE NOT INIT if (gdp->bg_flags & (EXT2_BG_INODE_UNINIT)) continue; // SET ZERO-FLAG IF FREE INODES == INODE/GROUP for fast ext3 if (gdp->bg_free_inodes_count == inode_per_group) zero_flag = 1; } //FIXME for struct ext4_group_desc 48/64BIT for (block_nr = gdp->bg_inode_table , block_count = 0 ; block_nr < (gdp->bg_inode_table + inode_block_group); block_nr++, block_count++) { if (!(flag & 0x02)){ //skip this in disaster mode // break if the first block only zero inode if ((block_count ==1) && (zero_flag == (inode_per_block + 1))) break; } //FIXME inode_max ???? first_block_inode_nr = (group * inode_per_group) + (block_count * inode_per_block) + 1; load = 0; for (i = 0; i<inode_per_block;i++){ if ( ! ext2fs_test_block_bitmap(imap,first_block_inode_nr + i)){ load++; break; } } if (load){ retval = read_block ( current_fs , &block_nr , buf); if (retval) return; for (inode_nr = first_block_inode_nr ,x = 0; x < inode_per_block ; inode_nr++ , x++){ if ( ! ext2fs_test_block_bitmap(imap,inode_nr)){ inode = (struct ext2_inode_large*) (buf + (x*inodesize)); c_time = ext2fs_le32_to_cpu(inode->i_ctime); mode = ext2fs_le32_to_cpu(inode->i_mode); if ( ! ( flag & 0x02)) { //no check this inode in disaster mode if ((! c_time ) && (!(inode->i_mode & LINUX_S_IFMT)) ) { if(zero_flag) zero_flag++ ; continue; } d_time = ext2fs_le32_to_cpu(inode->i_dtime); if ( (! d_time) || d_time <= t_after){ ext2fs_mark_generic_bitmap(imap,inode_nr); continue; } } // 1. magical step if (LINUX_S_ISDIR(mode) && ( flag & 0x01) && (pathname)){ sprintf(pathname,"<%lu>",(long unsigned int)inode_nr); struct dir_list_head_t * dir = NULL; if (flag & 0x02){ //disaster mode //only search for undeleted entry dir = get_dir3(NULL,0, inode_nr , "MAGIC-1",pathname, t_after,t_before, 0); if (dir) { lookup_local(des_dir, dir,t_after,t_before, RECOV_ALL | LOST_DIR_SEARCH ); clear_dir_list(dir); } } else{ //search for all dir = get_dir3(NULL,0, inode_nr , "MAGIC-1",pathname, t_after,t_before, DELETED_OPT); if (dir) { lookup_local(des_dir,dir,t_after,t_before,DELETED_OPT|RECOV_ALL|LOST_DIR_SEARCH); clear_dir_list(dir); } } } // 2. magical step if (! (flag & 0x01) ){ i_list = get_j_inode_list(current_fs->super, inode_nr); item = get_undel_inode(i_list,t_after,t_before); ext2fs_mark_generic_bitmap(imap,inode_nr); if (item) { if (! LINUX_S_ISDIR(item->inode->i_mode) ) { i_pathname = identify_filename(i_pathname, tmp_buf, (struct ext2_inode*)item->inode, inode_nr); sprintf(pathname,"<%lu>",(long unsigned int)inode_nr); recover_file(des_dir,"MAGIC-2", ((i_pathname)?i_pathname : pathname), (struct ext2_inode*)item->inode, inode_nr, 0); if(i_pathname){ free(i_pathname); i_pathname = NULL; } } } if (i_list) ring_del(i_list); } } } } } } errout: if (pathname) free(pathname); if(buf) { free(buf); buf = NULL; } if (tmp_buf){ free(tmp_buf); tmp_buf = NULL; } if (cookie){ magic_close(cookie); cookie = 0; } return; }