f2fstat: add option to select partition to see status
authorChangman Lee <cm224.lee@samsung.com>
Wed, 29 Jan 2014 04:36:54 +0000 (13:36 +0900)
committerJaegeuk Kim <jaegeuk.kim@samsung.com>
Mon, 3 Feb 2014 06:05:03 +0000 (15:05 +0900)
If there are some partitions, this may select a partition to see its
status. If no selection, you will see first section of status.

e.g.) f2fstat -p /dev/sda3

Signed-off-by: Changman Lee <cm224.lee@samsung.com>
Signed-off-by: Jaegeuk Kim <jaegeuk.kim@samsung.com>
tools/f2fstat.c

index e26e174..b4f22ae 100644 (file)
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
+#include <libgen.h>
 
 #ifdef DEBUG
 #define dbg(fmt, args...)      printf(fmt, __VA_ARGS__);
@@ -47,6 +48,7 @@ unsigned long lfs_blks;
 struct options {
        int delay;
        int interval;
+       char partname[32];
 };
 
 struct mm_table {
@@ -69,7 +71,7 @@ again:
        }
 }
 
-void f2fstat(void)
+void f2fstat(struct options *opt)
 {
        int fd;
        int ret;
@@ -79,6 +81,7 @@ void f2fstat(void)
        struct mm_table *found;
        int f2fstat_table_cnt;
        char *head, *tail;
+       int found_cnt = 0;
 
        static struct mm_table f2fstat_table[] = {
                { "  - Data",           &used_data_blks },
@@ -114,6 +117,13 @@ void f2fstat(void)
        buf[ret] = '\0';
 
        head = buf;
+
+       if (opt->partname[0] != '\0') {
+               head = strstr(buf, opt->partname);
+               if (head == NULL)
+                       exit(EXIT_FAILURE);
+       }
+
        for (;;) {
                remove_newline(&head);
                tail = strchr(head, ':');
@@ -137,6 +147,8 @@ void f2fstat(void)
                        goto nextline;
 
                *(found->val) = strtoul(head, &tail, 10);
+               if (++found_cnt == f2fstat_table_cnt)
+                       break;
 nextline:
                tail = strchr(head, '\n');
                if (!tail)
@@ -151,14 +163,15 @@ void usage(void)
 {
        printf("Usage: f2fstat [option]\n"
                        "    -d    delay (secs)\n"
-                       "    -i    interval of head info\n");
+                       "    -i    interval of head info\n"
+                       "    -p    partition name (e.g. /dev/sda3)\n");
        exit(EXIT_FAILURE);
 }
 
 void parse_option(int argc, char *argv[], struct options *opt)
 {
        char option;
-       const char *option_string = "d:i:h";
+       const char *option_string = "d:i:p:h";
 
        while ((option = getopt(argc, argv, option_string)) != EOF) {
                switch (option) {
@@ -168,6 +181,9 @@ void parse_option(int argc, char *argv[], struct options *opt)
                case 'i':
                        opt->interval = atoi(optarg);
                        break;
+               case 'p':
+                       strcpy(opt->partname, basename(optarg));
+                       break;
                default:
                        usage();
                        break;
@@ -188,6 +204,7 @@ int main(int argc, char *argv[])
        struct options opt = {
                .delay = 1,
                .interval = 20,
+               .partname = { 0, },
        };
 
        parse_option(argc, argv, &opt);
@@ -200,7 +217,7 @@ int main(int argc, char *argv[])
                        head_interval = opt.interval;
                }
 
-               f2fstat();
+               f2fstat(&opt);
 
                printf(format, util, used_node_blks, used_data_blks,
                                free_segs, valid_segs, dirty_segs, prefree_segs,