Whamcloud - gitweb
LU-17263 utils: 'lfs find -blocks' to use 512-byte units 93/52993/3
authorAndreas Dilger <adilger@whamcloud.com>
Sun, 5 Nov 2023 05:32:19 +0000 (23:32 -0600)
committerOleg Drokin <green@whamcloud.com>
Sat, 18 Nov 2023 21:47:02 +0000 (21:47 +0000)
Change the default units for 'lfs find -blocks' from 1KiB blocks
to 512-byte blocks to better match the behavior of find(1).  This
also matches what "-printf %b" will print.

Change llapi_parse_size() to accept a 'c' argument to specify
characters, and accept a "B" or "iB" suffix if provided.

Fixes: c043f46025 ("LU-10705 utils: add "lfs find --blocks"")
Signed-off-by: Andreas Dilger <adilger@whamcloud.com>
Change-Id: If8345f15bf53912501cadc0fa7f981a9f787b767
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/52993
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Arshad Hussain <arshad.hussain@aeoncomputing.com>
Reviewed-by: Vitaliy Kuznetsov <vkuznetsov@ddn.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/doc/lfs-find.1
lustre/utils/lfs.c
lustre/utils/liblustreapi.c

index 4f4a458..d9f8726 100644 (file)
@@ -70,8 +70,8 @@ Compressed (c), Immutable (i), Append_Only (a), No_Dump (d), Encrypted (E),
 Automount (M)
 .TP
 .BR --blocks | -b
-Blocks allocated by the file is \fIn\fR Kibibytes (if no units are given),
-\fIn\fR 512-byte \fBb\fRlocks, or \fBK\fRibi-, \fBM\fRebi-, \fBG\fRibi-,
+Blocks allocated by the file is \fIn\fR 512-byte \fBb\fRlocks (if no units
+are given), or \fBc\fRhars (bytes), \fBK\fRibi-, \fBM\fRebi-, \fBG\fRibi-,
 \fBT\fRebi-, \fBP\fRebi-, or \fBE\fRbi-bytes if that suffix is given.
 .TP
 .BR --btime | --Btime | -B
index 14832cb..c387aa5 100644 (file)
@@ -5339,7 +5339,7 @@ static int lfs_find(int argc, char **argv)
                                optarg++;
                        }
 
-                       param.fp_blocks_units = 1024;
+                       param.fp_blocks_units = 512;
                        ret = llapi_parse_size(optarg, &param.fp_blocks,
                                               &param.fp_blocks_units, 0);
                        if (ret) {
index eca1453..ddad8a0 100644 (file)
@@ -285,41 +285,56 @@ int llapi_parse_size(const char *optarg, unsigned long long *size,
        }
 
        if (*end != '\0') {
-               if ((*end == 'b') && *(end + 1) == '\0' &&
-                   (*size & (~0ULL << (64 - 9))) == 0 &&
-                   !bytes_spec) {
-                       *size_units = 1 << 9;
-               } else if ((*end == 'b') &&
-                          *(end + 1) == '\0' &&
-                          bytes_spec) {
+               char next = tolower(*(end + 1));
+
+               switch (tolower(*end)) {
+               case 'b':
+                       if (bytes_spec) {
+                               *size_units = 1;
+                       } else {
+                               if (*size & (~0ULL << (64 - 9)))
+                                       return -EINVAL;
+                               *size_units = 1 << 9;
+                       }
+                       break;
+               case 'c':
                        *size_units = 1;
-               } else if ((*end == 'k' || *end == 'K') &&
-                          *(end + 1) == '\0' &&
-                          (*size & (~0ULL << (64 - 10))) == 0) {
+                       break;
+               case 'k':
+                       if (*size & (~0ULL << (64 - 10)))
+                               return -EINVAL;
                        *size_units = 1 << 10;
-               } else if ((*end == 'm' || *end == 'M') &&
-                          *(end + 1) == '\0' &&
-                          (*size & (~0ULL << (64 - 20))) == 0) {
+                       break;
+               case 'm':
+                       if (*size & (~0ULL << (64 - 20)))
+                               return -EINVAL;
                        *size_units = 1 << 20;
-               } else if ((*end == 'g' || *end == 'G') &&
-                          *(end + 1) == '\0' &&
-                          (*size & (~0ULL << (64 - 30))) == 0) {
+                       break;
+               case 'g':
+                       if (*size & (~0ULL << (64 - 30)))
+                               return -EINVAL;
                        *size_units = 1 << 30;
-               } else if ((*end == 't' || *end == 'T') &&
-                          *(end + 1) == '\0' &&
-                          (*size & (~0ULL << (64 - 40))) == 0) {
+                       break;
+               case 't':
+                       if (*size & (~0ULL << (64 - 40)))
+                               return -EINVAL;
                        *size_units = 1ULL << 40;
-               } else if ((*end == 'p' || *end == 'P') &&
-                          *(end + 1) == '\0' &&
-                          (*size & (~0ULL << (64 - 50))) == 0) {
+                       break;
+               case 'p':
+                       if (*size & (~0ULL << (64 - 50)))
+                               return -EINVAL;
                        *size_units = 1ULL << 50;
-               } else if ((*end == 'e' || *end == 'E') &&
-                          *(end + 1) == '\0' &&
-                          (*size & (~0ULL << (64 - 60))) == 0) {
+                       break;
+               case 'e':
+                       if (*size & (~0ULL << (64 - 60)))
+                               return -EINVAL;
                        *size_units = 1ULL << 60;
-               } else {
+                       break;
+               default:
                        return -EINVAL;
                }
+               if (next != '\0' && next != 'i' && next != 'b')
+                       return -EINVAL;
        }
        *size = *size * *size_units + frac * *size_units / frac_d;