summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniil Cherednik <dan.cherednik@gmail.com>2021-02-21 23:46:29 +0300
committerDaniil Cherednik <dan.cherednik@gmail.com>2021-02-21 23:46:29 +0300
commit040f9a199bc45b53e7c0ce7301fa61abbd0430d4 (patch)
treef65af2b5fa5739de9523ab2dc4faea1522aca8c9
parentc7b09f57b650d1a22fd537d3f45d85b18d5aa253 (diff)
downloadufs_ffinfo-040f9a199bc45b53e7c0ce7301fa61abbd0430d4.tar.gz
Added cmd line parser.HEADmaster
-rw-r--r--src/ufs_ffinfo.c46
1 files changed, 40 insertions, 6 deletions
diff --git a/src/ufs_ffinfo.c b/src/ufs_ffinfo.c
index 9851547..c0f35b3 100644
--- a/src/ufs_ffinfo.c
+++ b/src/ufs_ffinfo.c
@@ -7,6 +7,7 @@
#include <unistd.h>
#include <strings.h>
#include <stdlib.h>
+#include <unistd.h>
#define MAXBINS 20
@@ -14,6 +15,10 @@
# define max(a,b) ((a) > (b) ? (a) : (b))
#endif
+struct ffinfo_fmt {
+ uint8_t short_mode; /* print only summary */
+};
+
struct ffinfo {
uint64_t bins[MAXBINS];
uint64_t free_blocks[MAXBINS];
@@ -143,7 +148,7 @@ ffinfo_print_hysto(uint64_t hysto_blocks_num, uint64_t total_blocks_num, const u
}
static void
-ffinfo_print(const struct ffinfo_ctx *ctx)
+ffinfo_print(const struct ffinfo_ctx *ctx, const struct ffinfo_fmt *fmt)
{
uint32_t c;
uint32_t blocks;
@@ -168,6 +173,8 @@ ffinfo_print(const struct ffinfo_ctx *ctx)
}
total_free_blocks_num += free_blocks;
+ if (fmt->short_mode)
+ continue;
fprintf(stdout, "Cylinder group %d, total blocks: %lu, free blocks: %lu\n", c, ctx->info_cg[c].blocks_num, free_blocks);
ffinfo_print_hysto(ctx->info_cg[c].blocks_num, free_blocks, info->bins, info->free_blocks);
}
@@ -221,7 +228,7 @@ ffinfo_scan_freemap(const struct fs* fs, char* map, uint32_t size, struct ffinfo
}
static int
-ffinfo_run(int fd)
+ffinfo_run(int fd, const struct ffinfo_fmt *fmt)
{
uint32_t c;
char* map;
@@ -245,7 +252,7 @@ ffinfo_run(int fd)
ffinfo_scan_freemap(ctx->fs, map, aocg.cg_ndblk, &ctx->info_cg[c]);
}
- ffinfo_print(ctx);
+ ffinfo_print(ctx, fmt);
ffinfo_ctx_free(ctx);
@@ -257,18 +264,45 @@ err_free_ctx:
return -1;
}
+static void
+print_usage(void)
+{
+ fprintf(stderr, "usage: %s [-s] filesystem ...\n", getprogname());
+ fprintf(stderr, "where:\n"
+ "\t -s - show only summary fragmentation info, "
+ "without details for each cylinder group\n");
+}
+
int
main(int argc, char *argv[])
{
const char* special;
int fsfd;
+ int ch;
- if (argc != 2)
+ struct ffinfo_fmt fmt;
+
+ bzero(&fmt, sizeof(struct ffinfo_fmt));
+
+ while ((ch = getopt(argc, argv, "s")) != -1 ) {
+ switch (ch) {
+ case 's':
+ fmt.short_mode = 1;
+ break;
+ default:
+ print_usage();
+ exit(0);
+ }
+ }
+
+ if (argc == optind) {
fprintf(stderr, "no block device given\n");
+ exit(1);
+ }
- special = argv[1];
+ special = argv[optind];
- if ((fsfd = open(special, O_RDONLY)) < 0 || ffinfo_run(fsfd) < 0)
+ if ((fsfd = open(special, O_RDONLY)) < 0 || ffinfo_run(fsfd, &fmt) < 0)
fprintf(stderr, "err\n");
return 0;