diff options
Diffstat (limited to 'misc/e2freefrag.c')
-rw-r--r-- | misc/e2freefrag.c | 74 |
1 files changed, 47 insertions, 27 deletions
diff --git a/misc/e2freefrag.c b/misc/e2freefrag.c index a09df6bf..0964e66a 100644 --- a/misc/e2freefrag.c +++ b/misc/e2freefrag.c @@ -34,7 +34,9 @@ void usage(const char *prog) { fprintf(stderr, "usage: %s [-c chunksize in kb] [-h] " "device_name\n", prog); +#ifndef DEBUGFS exit(1); +#endif } static int ul_log2(unsigned long arg) @@ -140,7 +142,7 @@ void scan_block_bitmap(ext2_filsys fs, struct chunk_info *info) update_chunk_stats(info, last_chunk_size); } -errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info) +errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info, FILE *f) { unsigned long total_chunks; char *unitp = "KMGTPEZY"; @@ -150,20 +152,20 @@ errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info) scan_block_bitmap(fs, info); - printf("Total blocks: %llu\nFree blocks: %u (%0.1f%%)\n", - ext2fs_blocks_count(fs->super), fs->super->s_free_blocks_count, - (double)fs->super->s_free_blocks_count * 100 / - ext2fs_blocks_count(fs->super)); + fprintf(f, "Total blocks: %llu\nFree blocks: %u (%0.1f%%)\n", + ext2fs_blocks_count(fs->super), fs->super->s_free_blocks_count, + (double)fs->super->s_free_blocks_count * 100 / + ext2fs_blocks_count(fs->super)); if (info->chunkbytes) { - printf("\nChunksize: %lu bytes (%u blocks)\n", - info->chunkbytes, info->blks_in_chunk); + fprintf(f, "\nChunksize: %lu bytes (%u blocks)\n", + info->chunkbytes, info->blks_in_chunk); total_chunks = (ext2fs_blocks_count(fs->super) + info->blks_in_chunk) >> (info->chunkbits - info->blocksize_bits); - printf("Total chunks: %lu\nFree chunks: %lu (%0.1f%%)\n", - total_chunks, info->free_chunks, - (double)info->free_chunks * 100 / total_chunks); + fprintf(f, "Total chunks: %lu\nFree chunks: %lu (%0.1f%%)\n", + total_chunks, info->free_chunks, + (double)info->free_chunks * 100 / total_chunks); } /* Display chunk information in KB */ @@ -176,13 +178,13 @@ errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info) info->min = 0; } - printf("\nMin. free extent: %lu KB \nMax. free extent: %lu KB\n" - "Avg. free extent: %lu KB\n", info->min, info->max, info->avg); - printf("Num. free extent: %lu\n", info->real_free_chunks); + fprintf(f, "\nMin. free extent: %lu KB \nMax. free extent: %lu KB\n" + "Avg. free extent: %lu KB\n", info->min, info->max, info->avg); + fprintf(f, "Num. free extent: %lu\n", info->real_free_chunks); - printf("\nHISTOGRAM OF FREE EXTENT SIZES:\n"); - printf("%s : %12s %12s %7s\n", "Extent Size Range", "Free extents", - "Free Blocks", "Percent"); + fprintf(f, "\nHISTOGRAM OF FREE EXTENT SIZES:\n"); + fprintf(f, "%s : %12s %12s %7s\n", "Extent Size Range", + "Free extents", "Free Blocks", "Percent"); for (i = 0; i < MAX_HIST; i++) { end = 1 << (i + info->blocksize_bits - units); if (info->histogram.fc_chunks[i] != 0) { @@ -191,12 +193,12 @@ errcode_t get_chunk_info(ext2_filsys fs, struct chunk_info *info) sprintf(end_str, "%5lu%c-", end, *unitp); if (i == MAX_HIST-1) strcpy(end_str, "max "); - printf("%5lu%c...%7s : %12lu %12lu %6.2f%%\n", - start, *unitp, end_str, - info->histogram.fc_chunks[i], - info->histogram.fc_blocks[i], - (double)info->histogram.fc_blocks[i] * 100 / - fs->super->s_free_blocks_count); + fprintf(f, "%5lu%c...%7s : %12lu %12lu %6.2f%%\n", + start, *unitp, end_str, + info->histogram.fc_chunks[i], + info->histogram.fc_blocks[i], + (double)info->histogram.fc_blocks[i] * 100 / + fs->super->s_free_blocks_count); } start = end; if (start == 1<<10) { @@ -217,12 +219,12 @@ void close_device(char *device_name, ext2_filsys fs) com_err(device_name, retval, "while closing the filesystem.\n"); } -void collect_info(ext2_filsys fs, struct chunk_info *chunk_info) +void collect_info(ext2_filsys fs, struct chunk_info *chunk_info, FILE *f) { unsigned int retval = 0; - printf("Device: %s\n", fs->device_name); - printf("Blocksize: %u bytes\n", fs->blocksize); + fprintf(f, "Device: %s\n", fs->device_name); + fprintf(f, "Blocksize: %u bytes\n", fs->blocksize); retval = ext2fs_read_block_bitmap(fs); if (retval) { @@ -233,7 +235,7 @@ void collect_info(ext2_filsys fs, struct chunk_info *chunk_info) init_chunk_info(fs, chunk_info); - retval = get_chunk_info(fs, chunk_info); + retval = get_chunk_info(fs, chunk_info, f); if (retval) { com_err(fs->device_name, retval, "while collecting chunk info"); close_device(fs->device_name, fs); @@ -253,7 +255,14 @@ void open_device(char *device_name, ext2_filsys *fs) } } +#ifdef DEBUGFS + +#include "debugfs.h" + +void do_freefrag(int argc, char **argv) +#else int main(int argc, char *argv[]) +#endif { struct chunk_info chunk_info = { }; errcode_t retval = 0; @@ -263,7 +272,12 @@ int main(int argc, char *argv[]) char *end; int c; +#ifdef DEBUGFS + if (check_fs_open(argv[0])) + return; +#else add_error_table(&et_ext2_error_table); +#endif progname = argv[0]; while ((c = getopt(argc, argv, "c:h")) != EOF) { @@ -290,6 +304,7 @@ int main(int argc, char *argv[]) } } +#ifndef DEBUGFS if (optind == argc) { fprintf(stderr, "%s: missing device name.\n", progname); usage(progname); @@ -298,14 +313,19 @@ int main(int argc, char *argv[]) device_name = argv[optind]; open_device(device_name, &fs); +#else + fs = current_fs; +#endif if (chunk_info.chunkbytes && (chunk_info.chunkbytes < fs->blocksize)) { fprintf(stderr, "%s: chunksize must be greater than or equal " "to filesystem blocksize.\n", progname); exit(1); } - collect_info(fs, &chunk_info); + collect_info(fs, &chunk_info, stdout); +#ifndef DEBUGFS close_device(device_name, fs); return retval; +#endif } |