int open_source(char* source, cmd_opt* opt) { int ret = 0; int debug = opt->debug; char *mp; int flags = O_RDONLY | O_LARGEFILE; struct stat st_dev; int ddd_block_device = -1; log_mesg(1, 0, 0, debug, "open source file/device %s\n", source); if (opt->ddd) { if (stat(source, &st_dev) != -1) { if (S_ISBLK(st_dev.st_mode)) ddd_block_device = 1; else ddd_block_device = 0; }else{ ddd_block_device = 0; } log_mesg(1, 0, 0, debug, "ddd source file(0) or device(1) ? %i \n", ddd_block_device); } if ((opt->clone) || (opt->dd) || (opt->domain) || (ddd_block_device == 1)) { /// always is device, clone from device=source mp = malloc(PATH_MAX + 1); if (!mp) log_mesg(0, 1, 1, debug, "%s, %i, not enough memory\n", __func__, __LINE__); if (check_mount(source, mp) == 1) { log_mesg(0, 0, 1, debug, "device (%s) is mounted at %s\n", source, mp); free(mp); log_mesg(0, 1, 1, debug, "error exit\n"); } free(mp); if ((ret = open(source, flags, S_IRUSR)) == -1) log_mesg(0, 1, 1, debug, "clone: open %s error\n", source); } else if ((opt->restore) || (ddd_block_device == 0)) { if (strcmp(source, "-") == 0) { if ((ret = fileno(stdin)) == -1) log_mesg(0, 1, 1, debug, "restore: open %s(stdin) error\n", source); } else { if ((ret = open (source, flags, S_IRWXU)) == -1) log_mesg(0, 1, 1, debug, "restore: open %s error\n", source); } } return ret; }
/// get fet type static void get_fat_type(){ /// fix, 1. make sure fat_sb; 2. the method shoud be check again if (fat_sb.u.fat16.ext_signature == 0x29){ if (fat_sb.u.fat16.fat_name[4] == '6'){ FS = FAT_16; fat_type = "FAT16"; log_mesg(2, 0, 0, fs_opt.debug, "%s: FAT Type : FAT 16\n", __FILE__); } else if (fat_sb.u.fat16.fat_name[4] == '2'){ FS = FAT_12; fat_type = "FAT12"; log_mesg(2, 0, 0, fs_opt.debug, "%s: FAT Type : FAT 12\n", __FILE__); } else { log_mesg(2, 1, 1, fs_opt.debug, "%s: FAT Type : unknow\n" __FILE__); } } else if (fat_sb.u.fat32.fat_name[4] == '2'){ FS = FAT_32; fat_type = "FAT32"; log_mesg(2, 0, 0, fs_opt.debug, "%s: FAT Type : FAT 32\n", __FILE__); } else { log_mesg(0, 1, 1, fs_opt.debug, "%s: Unknown fat type!!\n", __FILE__); } log_mesg(2, 0, 0, fs_opt.debug, "%s: FS = %i\n", __FILE__, FS); }
/// read super block and write to image head extern void initial_image_hdr(char* device, image_head* image_hdr) { char sig; unsigned long long total_sector = 0; unsigned long long bused = 0; unsigned long long data_sec = 0; unsigned long long sec_per_fat = 0; unsigned long long cluster_count = 0; unsigned long long free_blocks = 0; log_mesg(2, 0, 0, fs_opt.debug, "%s: initial_image start\n", __FILE__); fs_open(device); get_fat_type(); total_sector = get_total_sector(); bused = get_used_block();//so I need calculate by myself. strncpy(image_hdr->magic, IMAGE_MAGIC, IMAGE_MAGIC_SIZE); strncpy(image_hdr->fs, fat_type, FS_MAGIC_SIZE); image_hdr->block_size = (int)fat_sb.sector_size; image_hdr->totalblock = (unsigned long long)total_sector; image_hdr->device_size = (unsigned long long)(total_sector * image_hdr->block_size); image_hdr->usedblocks = (unsigned long long)bused; log_mesg(2, 0, 0, fs_opt.debug, "%s: Block Size:%i\n", __FILE__, image_hdr->block_size); log_mesg(2, 0, 0, fs_opt.debug, "%s: Total Blocks:%llu\n", __FILE__, image_hdr->totalblock); log_mesg(2, 0, 0, fs_opt.debug, "%s: Used Blocks:%llu\n", __FILE__, image_hdr->usedblocks); log_mesg(2, 0, 0, fs_opt.debug, "%s: Device Size:%llu\n", __FILE__, image_hdr->device_size); fs_close(); log_mesg(2, 0, 0, fs_opt.debug, "%s: initial_image down\n", __FILE__); }
/// readbitmap - read bitmap extern void readbitmap(char* device, image_head image_hdr, char*bitmap, int pui) { reiser4_bitmap_t *fs_bitmap; unsigned long long bit, block, bused = 0, bfree = 0; int start = 0; int bit_size = 1; fs_open(device); fs_bitmap = reiser4_bitmap_create(reiser4_format_get_len(fs->format)); reiser4_alloc_extract(fs->alloc, fs_bitmap); /// init progress progress_bar prog; /// progress_bar structure defined in progress.h progress_init(&prog, start, image_hdr.totalblock, bit_size); for(bit = 0; bit < reiser4_format_get_len(fs->format); bit++){ block = bit ; if(reiser4_bitmap_test(fs_bitmap, bit)){ bused++; bitmap[block] = 1; log_mesg(3, 0, 0, fs_opt.debug, "%s: bitmap is used %lli", block, __FILE__); } else { bitmap[block] = 0; bfree++; log_mesg(3, 0, 0, fs_opt.debug, "%s: bitmap is free %lli", block, __FILE__); } /// update progress update_pui(&prog, bit, 0); } if(bfree != reiser4_format_get_free(fs->format)) log_mesg(0, 1, 1, fs_opt.debug, "%s: bitmap free count err, bfree:%lli, sfree=%lli\n", __FILE__, bfree, reiser4_format_get_free(fs->format)); fs_close(); /// update progress update_pui(&prog, bit, 1); }
/// open device static void fs_open(char* device){ unsigned long long int state, extended; if (libreiser4_init()) { log_mesg(0, 1, 1, fs_opt.debug, "%s: Can't initialize libreiser4.\n", __FILE__); } if (!(fs_device = aal_device_open(&file_ops, device, 512, O_RDONLY))) { log_mesg(0, 1, 1, fs_opt.debug, "%s: Cannot open the partition (%s).\n", __FILE__, device); } if (!(fs = reiser4_fs_open(fs_device, 0))) { log_mesg(0, 1, 1, fs_opt.debug, "%s: Can't open reiser4 on %s\n", __FILE__, device); } //reiser4_opset_profile(fs->tree->ent.opset); if (!(fs->journal = reiser4_journal_open(fs, fs_device))) { log_mesg(0, 1, 1, fs_opt.debug, "%s: Can't open journal on %s", __FILE__, device); } state = get_ss_status(STATUS(fs->status)); extended = get_ss_extended(STATUS(fs->status)); if(fs_opt.ignore_fschk){ log_mesg(1, 0, 0, fs_opt.debug, "%s: Ignore filesystem check\n", __FILE__); }else{ if (!state) log_mesg(0, 1, 1, fs_opt.debug, "%s: REISER4 can't get status\n", __FILE__); if (state) log_mesg(3, 0, 0, fs_opt.debug, "%s: REISER4 stat : %i\n", __FILE__, state); if (state != FS_OK) log_mesg(0, 1, 1, fs_opt.debug, "%s: Filesystem isn't in valid state. May be it is not cleanly unmounted.\n\n", __FILE__); if (extended) log_mesg(3, 0, 0, fs_opt.debug, "%s: Extended status: %0xllx\n", extended, __FILE__); } //reiser4_opset_profile(fs->tree->ent.opset); fs->format = reiser4_format_open(fs); }
/// get image_head from image file void restore_image_hdr(int* ret, cmd_opt* opt, image_head* image_hdr) { int r_size; char* buffer; unsigned long long dev_size; int debug = opt->debug; buffer = (char*)malloc(sizeof(image_head)); if (buffer == NULL) log_mesg(0, 1, 1, debug, "%s, %i, not enough memory\n", __func__, __LINE__); memset(buffer, 0, sizeof(image_head)); r_size = read_all(ret, buffer, sizeof(image_head), opt); if (r_size == -1) log_mesg(0, 1, 1, debug, "read image_hdr error\n"); memcpy(image_hdr, buffer, sizeof(image_head)); free(buffer); dev_size = (unsigned long long)(image_hdr->totalblock * image_hdr->block_size); if (image_hdr->device_size != dev_size) image_hdr->device_size = dev_size; }
/// get bitmap from image file to restore data void get_image_bitmap(int* ret, cmd_opt opt, image_head image_hdr, unsigned long* bitmap) { unsigned long long size, r_size, r_need; char buffer[16384]; unsigned long long offset = 0; unsigned long long bused = 0, bfree = 0; int i, debug = opt.debug; int err_exit = 1; size = sizeof(char)*image_hdr.totalblock; while (size > 0) { r_need = size > sizeof(buffer) ? sizeof(buffer) : size; r_size = read_all(ret, buffer, r_need, &opt); if (r_size < r_need) log_mesg(0, 1, 1, debug, "Unable to read bitmap.\n"); for (i = 0; i < r_need; i++) { if (buffer[i] == 1) { pc_set_bit(offset + i, bitmap); bused++; } else { pc_clear_bit(offset + i, bitmap); bfree++; } } offset += r_need; size -= r_need; } if (debug >= 2) { if (image_hdr.usedblocks != bused) { if (opt.force) err_exit = 0; else err_exit = 1; log_mesg(0, err_exit, 1, debug, "The Used Block count is different. (bitmap %llu != image_head %llu)\n" "Try to use --force to skip the metadata error.\n", bused, image_hdr.usedblocks); } } }
/// readbitmap - read bitmap extern void readbitmap(char* device, image_head image_hdr, unsigned long* bitmap, int pui) { uint32_t current = 0, used_block = 0, free_block = 0, err_block = 0, total = 0, alloc = 0; int status = 0; int start = 0; int bit_size = 1; fs_open(device); /// init progress progress_bar prog; /// progress_bar structure defined in progress.h progress_init(&prog, start, image_hdr.totalblock, image_hdr.totalblock, BITMAP, bit_size); total = fs->fbb->bmh.total_items; alloc = vmfs_bitmap_allocated_items(fs->fbb); for(current = 0; current < total; current++){ status = vmfs_block_get_status(fs, VMFS_BLK_FB_BUILD(current)); if (status == -1) { err_block++; pc_clear_bit(current, bitmap); } else if (status == 1){ used_block++; pc_set_bit(current, bitmap); } else if (status == 0){ free_block++; pc_clear_bit(current, bitmap); } log_mesg(2, 0, 0, fs_opt.debug, "%s: Block 0x%8.8x status: %i\n", __FILE__, current, status); update_pui(&prog, current, current, 0); } fs_close(); update_pui(&prog, 1, 1, 1); log_mesg(0, 0, 0, fs_opt.debug, "%s: Used:%lld, Free:%lld, Status err:%lld\n", __FILE__, used_block, free_block, err_block); }
/// open device static void fs_open(char* device){ errcode_t retval; int use_superblock = 0; int use_blocksize = 0; int flags; #ifdef EXTFS_1_41 flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES; #else flags = EXT2_FLAG_JOURNAL_DEV_OK | EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; #endif if (use_superblock && !use_blocksize) { for (use_blocksize = EXT2_MIN_BLOCK_SIZE; use_blocksize <= EXT2_MAX_BLOCK_SIZE; use_blocksize *= 2) { retval = ext2fs_open (device, flags, use_superblock, use_blocksize, unix_io_manager, &fs); if (!retval) break; } } else retval = ext2fs_open (device, flags, use_superblock, use_blocksize, unix_io_manager, &fs); if (retval) log_mesg(0, 1, 1, fs_opt.debug, "%s: Couldn't find valid filesystem superblock.\n", __FILE__); ext2fs_mark_valid(fs); if(fs_opt.ignore_fschk){ log_mesg(1, 0, 0, fs_opt.debug, "%s: Ignore filesystem check\n", __FILE__); } else { if ((fs->super->s_state & EXT2_ERROR_FS) || !ext2fs_test_valid(fs)) log_mesg(0, 1, 1, fs_opt.debug, "%s: FS contains a file system with errors\n", __FILE__); else if ((fs->super->s_state & EXT2_VALID_FS) == 0) log_mesg(0, 1, 1, fs_opt.debug, "%s: FS was not cleanly unmounted\n", __FILE__); else if ((fs->super->s_max_mnt_count > 0) && (fs->super->s_mnt_count >= (unsigned) fs->super->s_max_mnt_count)) { log_mesg(0, 1, 1, fs_opt.debug, "%s: FS has been mounted %"PRIu16" times without being checked\n", __FILE__, fs->super->s_mnt_count); } } }
/// read super block and write to image head extern void initial_image_hdr(char* device, image_head* image_hdr) { int fs_type = 0; fs_type = test_extfs_type(device); log_mesg(1, 0, 0, fs_opt.debug, "%s: extfs version is %i\n", __FILE__, fs_type); strncpy(image_hdr->magic, IMAGE_MAGIC, IMAGE_MAGIC_SIZE); strncpy(image_hdr->fs, extfs_MAGIC, FS_MAGIC_SIZE); fs_open(device); image_hdr->block_size = (int)block_size(); image_hdr->totalblock = (unsigned long long)block_count(); image_hdr->usedblocks = (unsigned long long)get_used_blocks(); image_hdr->device_size = (unsigned long long)(image_hdr->block_size * image_hdr->totalblock); fs_close(); }
/// read super block and write to image head extern void initial_image_hdr(char* device, image_head* image_hdr) { int src; if ((src = open_source(device, &opt)) == -1) { log_mesg(0, 1, 1, opt.debug, "Error exit\n"); } strncpy(image_hdr->magic, IMAGE_MAGIC, IMAGE_MAGIC_SIZE); strncpy(image_hdr->fs, raw_MAGIC, FS_MAGIC_SIZE); image_hdr->block_size = PART_SECTOR_SIZE; image_hdr->device_size = get_partition_size(&src); image_hdr->totalblock = image_hdr->device_size / PART_SECTOR_SIZE; image_hdr->usedblocks = image_hdr->device_size / PART_SECTOR_SIZE; close(src); }
/* Build a data packet */ void build_pkt_data (session_t *session, char *data, char *packet) { data_pkt dpkt_t; dpkt_t.type = PKT_TYPE_DATA; dpkt_t.session_id = session->session_id; dpkt_t.length = min_value(MAX_FRAME_SIZE, session->no_of_bytes); dpkt_t.seq_num = session->next_frame_number; data += session->next_frame_number * MAX_FRAME_SIZE; memcpy(&dpkt_t.data, data, dpkt_t.length); log_mesg("Buildinf DATA packet Type : %d Session : %d Length %d Seq_num %d Data pointer %d\n", dpkt_t.type, dpkt_t.session_id, dpkt_t.length, dpkt_t.seq_num, session->next_frame_number * MAX_FRAME_SIZE); memcpy(packet, &dpkt_t, PKT_SIZE_DATA); }
/// check partition size int check_size(int* ret, unsigned long long size) { unsigned long long dest_size; int debug = 1; dest_size = get_partition_size(ret); if (dest_size < size){ log_mesg(0, 1, 1, debug, "Target partition size(%llu MB) is smaller than source(%llu MB). Use option -C to disable size checking(Dangerous).\n", print_size(dest_size, MBYTE), print_size(size, MBYTE)); return 1; } return 0; }
void write_image_desc(int* ret, file_system_info fs_info, image_options img_opt, cmd_opt* opt) { image_desc_v2 buf_v2; init_image_head_v2(&buf_v2.head); memcpy(&buf_v2.fs_info, &fs_info, sizeof(file_system_info)); memcpy(&buf_v2.options, &img_opt, sizeof(image_options)); init_crc32(&buf_v2.crc); buf_v2.crc = crc32(buf_v2.crc, &buf_v2, sizeof(image_desc_v2) - CRC32_SIZE); if (write_all(ret, (char*)&buf_v2, sizeof(image_desc_v2), opt) != sizeof(image_desc_v2)) log_mesg(0, 1, 1, opt->debug, "error writing image header to image: %s\n", strerror(errno)); }
static void set_bitmap(unsigned long* bitmap, uint64_t pos, int length) { uint64_t pos_block; uint64_t block_count; uint64_t block; pos_block = pos/source_blocksize; block_count = length/source_blocksize; for (block = pos_block; block < pos_block+block_count; block++){ pc_set_bit(block, bitmap); log_mesg(3, 0, 0, fs_opt.debug, "block %i is used\n", block); } }
/// print image head void print_file_system_info(file_system_info fs_info, cmd_opt opt) { unsigned int block_s = fs_info.block_size; unsigned long long total = fs_info.totalblock; unsigned long long used = fs_info.usedblocks; int debug = opt.debug; char size_str[11]; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); log_mesg(0, 0, 1, debug, _("File system: %s\n"), fs_info.fs); print_readable_size_str(total*block_s, size_str); log_mesg(0, 0, 1, debug, _("Device size: %s = %llu Blocks\n"), size_str, total); print_readable_size_str(used*block_s, size_str); log_mesg(0, 0, 1, debug, _("Space in use: %s = %llu Blocks\n"), size_str, used); print_readable_size_str((total-used)*block_s, size_str); log_mesg(0, 0, 1, debug, _("Free Space: %s = %llu Blocks\n"), size_str, (total-used)); log_mesg(0, 0, 1, debug, _("Block size: %i Byte\n"), block_s); }
/// check per FAT16 entry unsigned long long check_fat16_entry(unsigned long* fat_bitmap, unsigned long long block, unsigned long long* bfree, unsigned long long* bused, unsigned long long* DamagedClusters) { uint16_t Fat16_Entry = 0; int rd = 0; unsigned long long i = 0; rd = read(ret, &Fat16_Entry, sizeof(Fat16_Entry)); if (rd == -1) log_mesg(2, 0, 0, fs_opt.debug, "%s: read Fat16_Entry error\n", __FILE__); if (Fat16_Entry == 0xFFF7) { /// bad FAT16 cluster DamagedClusters++; log_mesg(2, 0, 0, fs_opt.debug, "%s: bad sec %llu\n", __FILE__, block); for (i=0; i < fat_sb.cluster_size; i++,block++) pc_clear_bit(block, fat_bitmap); } else if (Fat16_Entry == 0x0000){ /// free bfree++; for (i=0; i < fat_sb.cluster_size; i++,block++) pc_clear_bit(block, fat_bitmap); } else { bused++; for (i=0; i < fat_sb.cluster_size; i++,block++) pc_set_bit(block, fat_bitmap); } return block; }
void check_mem_size(file_system_info fs_info, image_options img_opt, cmd_opt opt) { const unsigned long long bitmap_size = BITS_TO_BYTES(fs_info.totalblock); const uint32_t blkcs = img_opt.blocks_per_checksum; const uint32_t block_size = fs_info.block_size; const unsigned int buffer_capacity = opt.buffer_size > block_size ? opt.buffer_size / block_size : 1; // in blocks const unsigned long long raw_io_size = buffer_capacity * block_size; unsigned long long cs_size = 0, needed_size = 0; void *test_bitmap, *test_read, *test_write; if (img_opt.checksum_mode != CSM_NONE) { unsigned long long cs_in_buffer = buffer_capacity / blkcs; cs_size = cs_in_buffer * img_opt.checksum_size; } needed_size = bitmap_size + 2 * raw_io_size + cs_size; log_mesg(0, 0, 0, 1, "memory needed: %llu bytes\nbitmap %llu bytes, blocks 2*%llu bytes, checksum %llu bytes\n", needed_size, bitmap_size, raw_io_size, cs_size); test_bitmap = malloc(bitmap_size); test_read = malloc(raw_io_size); test_write = malloc(raw_io_size + cs_size); if (test_bitmap == NULL || test_read == NULL || test_write == NULL) log_mesg(0, 1, 1, opt.debug, "There is not enough free memory, partclone suggests you should have %llu bytes memory\n", needed_size); else { free(test_bitmap); free(test_read); free(test_write); } }
static unsigned long count_used_block(){ unsigned long zones = get_nzones(); unsigned long imaps = get_nimaps(); unsigned long zmaps = get_nzmaps(); char * inode_map; char * zone_map; ssize_t rc; unsigned long test_block = 0, test_zone = 0; unsigned long used_block = 0; unsigned long block_size = get_block_size(); if (fs_version == 3){ if (lseek(dev, block_size*2, SEEK_SET) != 8192) log_mesg(0, 1, 1, fs_opt.debug, "%s: seek failed", __FILE__); } inode_map = malloc(imaps * block_size); if (!inode_map) log_mesg(0, 1, 1, fs_opt.debug, "%s: Unable to allocate buffer for inode map", __FILE__); zone_map = malloc(zmaps * block_size); if (!inode_map) log_mesg(0, 1, 1, fs_opt.debug, "%s: Unable to allocate buffer for zone map", __FILE__); memset(inode_map,0,sizeof(inode_map)); memset(zone_map,0,sizeof(zone_map)); rc = read(dev, inode_map, imaps * block_size); if (rc < 0 || imaps * block_size != (size_t) rc) log_mesg(0, 1, 1, fs_opt.debug, "%s: Unable to read inode map", __FILE__); rc = read(dev, zone_map, zmaps * block_size); if (rc < 0 || zmaps * block_size != (size_t) rc) log_mesg(0, 1, 1, fs_opt.debug, "%s: Unable to read zone map", __FILE__); for (test_block = 0; test_block < zones; test_block++){ test_zone = test_block - get_first_zone()+1; if ((test_zone < 0) || (test_zone > zones+get_first_zone())) test_zone = 0; if(isset(zone_map,test_zone)){ log_mesg(3, 0, 0, fs_opt.debug, "%s: test_block %lu in use\n", __FILE__, test_block); used_block++; }else{ log_mesg(3, 0, 0, fs_opt.debug, "%s: test_block %lu not use\n", __FILE__, test_block); } } return used_block; }
/// readbitmap - read bitmap extern void readbitmap(char* device, image_head image_hdr, char* bitmap, int pui) { reiserfs_bitmap_t *fs_bitmap; reiserfs_tree_t *tree; reiserfs_block_t *node; blk_t blk; unsigned long long bused = 0, bfree = 0; int start = 0; int bit_size = 1; int done = 0; fs_open(device); tree = reiserfs_fs_tree(fs); fs_bitmap = tree->fs->bitmap; /// init progress progress_bar bprog; /// progress_bar structure defined in progress.h progress_init(&bprog, start, fs->super->s_v1.sb_block_count, bit_size); for(blk = 0 ; (int)blk < fs->super->s_v1.sb_block_count; blk++){ if(reiserfs_tools_test_bit(blk, fs_bitmap->bm_map)){ bused++; bitmap[blk] = 1; }else{ bfree++; bitmap[blk] = 0; } /// update progress update_pui(&bprog, blk, done); } if(bfree != fs->super->s_v1.sb_free_blocks) log_mesg(0, 1, 1, fs_opt.debug, "%s: bitmap free count err, free:%i\n", __FILE__, bfree); fs_close(); /// update progress done = 1; update_pui(&bprog, 1, done); }
/// check free memory size int check_mem_size(image_head image_hdr, cmd_opt opt, unsigned long long *mem_size) { unsigned long long image_head_size = 0; unsigned long long bitmap_size = 0; int crc_io_size = 0; void *test_mem; image_head_size = sizeof(image_head); bitmap_size = sizeof(unsigned long)*LONGS(image_hdr.totalblock); crc_io_size = CRC_SIZE+image_hdr.block_size; *mem_size = image_head_size + bitmap_size + crc_io_size; log_mesg(0, 0, 0, 1, "we need memory: %llu bytes\nimage head %llu, bitmap %llu, crc %i bytes\n", *mem_size, image_head_size, bitmap_size, crc_io_size); test_mem = malloc(*mem_size); if (test_mem == NULL){ free(test_mem); return -1; } else { free(test_mem); } return 1; }
/* Build the data structure for the current session */ void session_build_parameters (char *ip, char *host_name, session_t *session) { /* Assign the host name */ session->host_name = host_name; session->host_name_len = strlen (session->host_name); /* Assign the current session id */ session->session_id = SESSION_ID; log_mesg("Building %s %d\n", session->host_name, session->session_id); /* Initially we will send a hello packet to the common port number and the given IP */ session->port = COMMON_CONNECT_PORT; /* Create the connection */ session->socket = create_connection(); memset((char *) &session->address, 0, sizeof(session->address)); session->address.sin_family = AF_INET; session->address.sin_addr.s_addr = inet_addr(ip); session->address.sin_port = htons(session->port); session->address_len = sizeof(session->address); }
void load_image_desc_v1(file_system_info* fs_info, image_options* img_opt, const image_head_v1 img_hdr_v1, const file_system_info_v1 fs_info_v1, cmd_opt* opt) { unsigned long long dev_size = 0; strncpy(fs_info->fs, img_hdr_v1.fs, FS_MAGIC_SIZE); fs_info->block_size = fs_info_v1.block_size; fs_info->device_size = fs_info_v1.device_size; fs_info->totalblock = fs_info_v1.totalblock; fs_info->usedblocks = fs_info_v1.usedblocks; dev_size = fs_info->totalblock * fs_info->block_size; if (fs_info->device_size != dev_size) { log_mesg(1, 0, 0, opt->debug, "INFO: adjusted device size reported by the image [%llu -> %llu]\n", fs_info->device_size, dev_size); fs_info->device_size = dev_size; } set_image_options_v1(img_opt); }
/// get bitmap from image file to restore data void load_image_bitmap(int* ret, cmd_opt opt, file_system_info fs_info, image_options img_opt, unsigned long* bitmap) { switch(img_opt.bitmap_mode) { case BM_BIT: load_image_bitmap_bits(ret, opt, fs_info, bitmap); break; case BM_BYTE: load_image_bitmap_bytes(ret, opt, fs_info, bitmap); break; case BM_NONE: // All blocks are present pc_init_bitmap(bitmap, 0xFF, fs_info.totalblock); break; default: log_mesg(0, 1, 1, opt.debug, "ERROR: unknown bitmap mode [%d]\n", img_opt.bitmap_mode); break; } }
/// readbitmap - read bitmap extern void readbitmap(char* device, image_head image_hdr, char* bitmap, int pui) { unsigned long long total_block, block, bused = 0, bfree = 0; int done = 0, i = 0, start = 0, bit_size = 1; char* p; fs_open(device); /// init progress progress_bar bprog; /// progress_bar structure defined in progress.h progress_init(&bprog, start, image_hdr.totalblock, bit_size); total_block = 0; /// read group while ((i = cgread(&disk)) != 0) { log_mesg(2, 0, 0, fs_opt.debug, "%s: \ncg = %d\n", __FILE__, disk.d_lcg); log_mesg(2, 0, 0, fs_opt.debug, "%s: blocks = %i\n", __FILE__, acg.cg_ndblk); p = cg_blksfree(&acg); for (block = 0; block < acg.cg_ndblk; block++) { if (isset(p, block)) { bitmap[total_block] = 0; bfree++; log_mesg(3, 0, 0, fs_opt.debug, "%s: bitmap is free %lli\n", __FILE__, block); } else { bitmap[total_block] = 1; bused++; log_mesg(3, 0, 0, fs_opt.debug, "%s: bitmap is used %lli\n", __FILE__, block); } total_block++; update_pui(&bprog, total_block ,done); } log_mesg(1, 0, 0, fs_opt.debug, "%s: read bitmap done\n", __FILE__); } fs_close(); log_mesg(1, 0, 0, fs_opt.debug, "%s: total used = %lli, total free = %lli\n", __FILE__, bused, bfree); done = 1; update_pui(&bprog, 1, done); }
/// open device static void fs_open(char* device){ #ifndef VMFS5_ZLA_BASE vmfs_lvm_t *lvm; #endif vmfs_flags_t flags; char *mdev[] = {device, NULL}; vmfs_host_init(); flags.packed = 0; flags.allow_missing_extents = 1; log_mesg(3, 0, 0, fs_opt.debug, "%s: device %s\n", __FILE__, device); #ifdef VMFS5_ZLA_BASE if (!(fs=vmfs_fs_open(mdev, flags))) { #else if (!(lvm = vmfs_lvm_create(flags))) { log_mesg(0, 1, 1, fs_opt.debug, "%s: Unable to create LVM structure\n", __FILE__); } if (vmfs_lvm_add_extent(lvm, vmfs_vol_open(device, flags)) == -1) { log_mesg(0, 1, 1, fs_opt.debug, "%s: Unable to open device/file \"%s\".\n", __FILE__, device); } if (!(fs = vmfs_fs_create(lvm))) { log_mesg(0, 1, 1, fs_opt.debug, "%s: Unable to open filesystem\n", __FILE__); } if (vmfs_fs_open(fs) == -1) { #endif log_mesg(0, 1, 1, fs_opt.debug, "%s: Unable to open volume.\n", __FILE__); } if (!(root_dir = vmfs_dir_open_from_blkid(fs,VMFS_BLK_FD_BUILD(0,0,0)))) { log_mesg(0, 1, 1, fs_opt.debug, "%s: Unable to open root directory\n", __FILE__); } } /// close device static void fs_close(){ vmfs_dir_close(root_dir); vmfs_fs_close(fs); }
static ssize_t lssu_print_suinfo(struct nilfs *nilfs, __u64 segnum, ssize_t nsi, __u64 protseq, unsigned long* bitmap) { ssize_t i, n = 0; int all = 1; for (i = 0; i < nsi; i++, segnum++) { if (!all && nilfs_suinfo_clean(&suinfos[i])) continue; log_mesg(3, 0, 0, fs_opt.debug, "%s: seg %llu, %c %c %c , %i\n", __FILE__, (unsigned long long)segnum, nilfs_suinfo_active(&suinfos[i]) ? 'a' : '-', nilfs_suinfo_dirty(&suinfos[i]) ? 'd' : '-', nilfs_suinfo_error(&suinfos[i]) ? 'e' : '-', suinfos[i].sui_nblocks); if (nilfs_suinfo_active(&suinfos[i]) || nilfs_suinfo_dirty(&suinfos[i])){ set_bitmap(bitmap, (unsigned long long)segnum, suinfos[i].sui_nblocks); } n++; } return n; }
void sync_data(int fd, cmd_opt* opt) { log_mesg(0, 0, 1, opt->debug, "Syncing... "); if (fsync(fd) && errno != EINVAL) log_mesg(0, 1, 1, opt->debug, "fsync error: errno = %i\n", errno); log_mesg(0, 0, 1, opt->debug, "OK!\n"); }
/** * Ncurses Text User Interface * open_ncurses - open text window * close_ncurses - close text window */ int open_ncurses() { #ifdef HAVE_LIBNCURSESW int debug = 1; FILE *in = fopen( "/dev/stderr", "r" ); FILE *out = fopen( "/dev/stderr", "w" ); int terminal_x = 0; int terminal_y = 0; ptclscr = newterm(NULL, out, in); refresh(); if (!ptclscr) log_mesg(0, 1, 1, debug, "partclone ncurses initial error\n"); if (!set_term(ptclscr)) log_mesg(0, 1, 1, debug, "partclone ncurses set term error\n"); ptclscr_win = newwin(LINES, COLS, 0, 0); // check terminal width and height getmaxyx(ptclscr_win, terminal_y, terminal_x); // set window position int log_line = 12; int log_row = 60; int log_y_pos = (terminal_y-24)/2+2; int log_x_pos = (terminal_x-log_row)/2; int gap = 0; int p_line = 8; int p_row = log_row; int p_y_pos = log_y_pos+log_line+gap; int p_x_pos = log_x_pos; int size_ok = 1; if (terminal_y < (log_line+gap+p_line+3)) size_ok = 0; if (terminal_x < (log_row+2)) size_ok = 0; if (size_ok == 0) { log_mesg(0, 0, 0, debug, "Terminal width(%i) or height(%i) too small\n", terminal_x, terminal_y); return 0; } /// check color pair if (!has_colors()) { log_mesg(0, 0, 0, debug, "Terminal color error\n"); return 0; } if (start_color() != OK){ log_mesg(0, 0, 0, debug, "Terminal can't start color mode\n"); return 0; } /// define color init_pair(1, COLOR_WHITE, COLOR_BLUE); ///stdscr init_pair(2, COLOR_RED, COLOR_WHITE); ///sub window init_pair(3, COLOR_BLUE, COLOR_WHITE); ///sub window /// write background color bkgd(COLOR_PAIR(1)); wbkgd(ptclscr_win,COLOR_PAIR(2)); touchwin(ptclscr_win); refresh(); /// init main box attrset(COLOR_PAIR(2)); box_win = subwin(ptclscr_win, (log_line+gap+p_line+2), log_row+2, log_y_pos-1, log_x_pos-1); box(box_win, ACS_VLINE, ACS_HLINE); wrefresh(box_win); wbkgd(box_win, COLOR_PAIR(2)); mvprintw((log_y_pos-1), ((terminal_x-9)/2), " Partclone "); attroff(COLOR_PAIR(2)); attrset(COLOR_PAIR(3)); /// init log window log_win = subwin(ptclscr_win, log_line, log_row, log_y_pos, log_x_pos); wbkgd(log_win, COLOR_PAIR(3)); touchwin(log_win); refresh(); // init progress window p_win = subwin(ptclscr_win, p_line, p_row, p_y_pos, p_x_pos); wbkgd(p_win, COLOR_PAIR(3)); touchwin(p_win); refresh(); // init progress window bar_win = subwin(ptclscr_win, 1, p_row-10, p_y_pos+4, p_x_pos); wbkgd(bar_win, COLOR_PAIR(1)); touchwin(bar_win); refresh(); // init total block progress window tbar_win = subwin(ptclscr_win, 1, p_row-10, p_y_pos+7, p_x_pos); wbkgd(tbar_win, COLOR_PAIR(1)); touchwin(tbar_win); refresh(); scrollok(log_win, TRUE); if (touchwin(ptclscr_win) == ERR) return 0; refresh(); #endif return 1; }
/// get_used_block - get FAT used blocks static unsigned long long get_used_block() { unsigned long long i = 0; int fat_stat = 0; unsigned long long block = 0, bfree = 0, bused = 0, DamagedClusters = 0; unsigned long long cluster_count = 0, total_sector = 0; unsigned long long real_back_block= 0; int FatReservedBytes = 0; unsigned long *fat_bitmap; log_mesg(2, 0, 0, fs_opt.debug, "%s: get_used_block start\n", __FILE__); total_sector = get_total_sector(); cluster_count = get_cluster_count(); fat_bitmap = (unsigned long *)calloc(sizeof(unsigned long), LONGS(total_sector)); if (fat_bitmap == NULL) log_mesg(2, 1, 1, fs_opt.debug, "%s: bitmapalloc error\n", __FILE__); memset(fat_bitmap, 0xFF, sizeof(unsigned long)*LONGS(total_sector)); /// A) B) C) block = mark_reserved_sectors(fat_bitmap, block); /// D) The clusters FatReservedBytes = fat_sb.sector_size * fat_sb.reserved; /// The first fat will be seek lseek(ret, FatReservedBytes, SEEK_SET); /// The second fat is used to check FAT status fat_stat = check_fat_status(); if (fat_stat == 1) log_mesg(0, 1, 1, fs_opt.debug, "%s: Filesystem isn't in valid state. May be it is not cleanly unmounted.\n\n", __FILE__); else if (fat_stat == 2) log_mesg(0, 1, 1, fs_opt.debug, "%s: I/O error! %X\n", __FILE__); for (i=0; i < cluster_count; i++){ /// If FAT16 if(FS == FAT_16){ block = check_fat16_entry(fat_bitmap, block, &bfree, &bused, &DamagedClusters); } else if (FS == FAT_32){ /// FAT32 block = check_fat32_entry(fat_bitmap, block, &bfree, &bused, &DamagedClusters); } else if (FS == FAT_12){ /// FAT12 block = check_fat12_entry(fat_bitmap, block, &bfree, &bused, &DamagedClusters); } else log_mesg(2, 0, 0, fs_opt.debug, "%s: error fs\n", __FILE__); } while(block < total_sector){ pc_set_bit(block, fat_bitmap); block++; } for (block = 0; block < total_sector; block++) { if (pc_test_bit(block, fat_bitmap)) { real_back_block++; } } free(fat_bitmap); log_mesg(2, 0, 0, fs_opt.debug, "%s: get_used_block down\n", __FILE__); return real_back_block; }