From 6edd4b21211f91341e9d02ac7aa189b7111656f6 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 15 Sep 2022 23:15:17 +0200 Subject: [PATCH] btrfs-progs: factor string helpers out of utils.c Utils is the catch-all file, we can now separate some string utility functions. Signed-off-by: David Sterba --- Makefile | 1 + btrfs-corrupt-block.c | 1 + btrfs-find-root.c | 1 + btrfs-map-logical.c | 1 + btrfs-select-super.c | 1 + btrfs.c | 1 + btrfstune.c | 1 + check/main.c | 1 + cmds/device.c | 1 + cmds/inspect-dump-super.c | 1 + cmds/inspect-dump-tree.c | 1 + cmds/inspect.c | 1 + cmds/receive.c | 1 + cmds/replace.c | 1 + cmds/restore.c | 1 + cmds/send.c | 1 + cmds/subvolume-list.c | 1 + cmds/subvolume.c | 1 + common/fsfeatures.c | 1 + common/help.c | 1 + common/string-utils.c | 76 +++++++++++++++++++++++++++++++++++++++ common/string-utils.h | 24 +++++++++++++ common/utils.c | 54 ---------------------------- common/utils.h | 4 --- image/main.c | 1 + mkfs/main.c | 1 + 26 files changed, 122 insertions(+), 58 deletions(-) create mode 100644 common/string-utils.c create mode 100644 common/string-utils.h diff --git a/Makefile b/Makefile index 2c436d8d..fe29a152 100644 --- a/Makefile +++ b/Makefile @@ -186,6 +186,7 @@ objects = \ common/send-stream.o \ common/send-utils.o \ common/string-table.o \ + common/string-utils.o \ common/task-utils.o \ common/units.o \ common/utils.o \ diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c index 93bd709a..5472944f 100644 --- a/btrfs-corrupt-block.c +++ b/btrfs-corrupt-block.c @@ -33,6 +33,7 @@ #include "common/help.h" #include "common/extent-cache.h" #include "common/messages.h" +#include "common/string-utils.h" #define FIELD_BUF_LEN 80 diff --git a/btrfs-find-root.c b/btrfs-find-root.c index cab1a52f..fa6717ea 100644 --- a/btrfs-find-root.c +++ b/btrfs-find-root.c @@ -29,6 +29,7 @@ #include "common/extent-cache.h" #include "common/help.h" #include "common/messages.h" +#include "common/string-utils.h" #include "cmds/commands.h" /* diff --git a/btrfs-map-logical.c b/btrfs-map-logical.c index 06b34672..6c2ad2e3 100644 --- a/btrfs-map-logical.c +++ b/btrfs-map-logical.c @@ -34,6 +34,7 @@ #include "common/utils.h" #include "common/help.h" #include "common/extent-cache.h" +#include "common/string-utils.h" #define BUFFER_SIZE SZ_64K diff --git a/btrfs-select-super.c b/btrfs-select-super.c index ef8d4bd9..f84c8e68 100644 --- a/btrfs-select-super.c +++ b/btrfs-select-super.c @@ -29,6 +29,7 @@ #include "common/help.h" #include "common/open-utils.h" #include "common/messages.h" +#include "common/string-utils.h" static void print_usage(void) { diff --git a/btrfs.c b/btrfs.c index 049ecdf0..4c64f953 100644 --- a/btrfs.c +++ b/btrfs.c @@ -24,6 +24,7 @@ #include "kernel-shared/volumes.h" #include "crypto/crc32c.h" #include "common/utils.h" +#include "common/string-utils.h" #include "common/help.h" #include "common/box.h" #include "cmds/commands.h" diff --git a/btrfstune.c b/btrfstune.c index c36a95a8..9b739576 100644 --- a/btrfstune.c +++ b/btrfstune.c @@ -38,6 +38,7 @@ #include "common/parse-utils.h" #include "common/device-scan.h" #include "common/messages.h" +#include "common/string-utils.h" #include "common/help.h" #include "common/box.h" #include "ioctl.h" diff --git a/check/main.c b/check/main.c index a0777c6a..df1db36b 100644 --- a/check/main.c +++ b/check/main.c @@ -53,6 +53,7 @@ #include "common/rbtree-utils.h" #include "common/help.h" #include "common/open-utils.h" +#include "common/string-utils.h" #include "cmds/commands.h" #include "mkfs/common.h" #include "check/common.h" diff --git a/cmds/device.c b/cmds/device.c index 389ed7c6..f6a912f1 100644 --- a/cmds/device.c +++ b/cmds/device.c @@ -36,6 +36,7 @@ #include "common/format-output.h" #include "common/open-utils.h" #include "common/units.h" +#include "common/string-utils.h" #include "cmds/commands.h" #include "cmds/filesystem-usage.h" #include "mkfs/common.h" diff --git a/cmds/inspect-dump-super.c b/cmds/inspect-dump-super.c index 77a1ba1a..26e2b4ba 100644 --- a/cmds/inspect-dump-super.c +++ b/cmds/inspect-dump-super.c @@ -27,6 +27,7 @@ #include "kernel-shared/zoned.h" #include "common/utils.h" #include "common/help.h" +#include "common/string-utils.h" #include "cmds/commands.h" static int load_and_dump_sb(char *filename, int fd, u64 sb_bytenr, int full, diff --git a/cmds/inspect-dump-tree.c b/cmds/inspect-dump-tree.c index a6b2983d..0ef9d41c 100644 --- a/cmds/inspect-dump-tree.c +++ b/cmds/inspect-dump-tree.c @@ -32,6 +32,7 @@ #include "common/utils.h" #include "common/help.h" #include "common/device-scan.h" +#include "common/string-utils.h" #include "cmds/commands.h" static void print_extents(struct extent_buffer *eb) diff --git a/cmds/inspect.c b/cmds/inspect.c index 116319b1..14d73909 100644 --- a/cmds/inspect.c +++ b/cmds/inspect.c @@ -30,6 +30,7 @@ #include "common/help.h" #include "common/open-utils.h" #include "common/units.h" +#include "common/string-utils.h" #include "cmds/commands.h" #include "ioctl.h" diff --git a/cmds/receive.c b/cmds/receive.c index ff6839f8..a19af7ad 100644 --- a/cmds/receive.c +++ b/cmds/receive.c @@ -55,6 +55,7 @@ #include "common/send-utils.h" #include "common/help.h" #include "common/path-utils.h" +#include "common/string-utils.h" #include "cmds/commands.h" #include "cmds/receive-dump.h" #include "ioctl.h" diff --git a/cmds/replace.c b/cmds/replace.c index 0f0060e5..16527fba 100644 --- a/cmds/replace.c +++ b/cmds/replace.c @@ -38,6 +38,7 @@ #include "common/help.h" #include "common/path-utils.h" #include "common/device-utils.h" +#include "common/string-utils.h" #include "cmds/commands.h" #include "mkfs/common.h" #include "ioctl.h" diff --git a/cmds/restore.c b/cmds/restore.c index 4ea91e15..123f5b49 100644 --- a/cmds/restore.c +++ b/cmds/restore.c @@ -45,6 +45,7 @@ #include "common/utils.h" #include "common/help.h" #include "common/open-utils.h" +#include "common/string-utils.h" #include "cmds/commands.h" static char fs_name[PATH_MAX]; diff --git a/cmds/send.c b/cmds/send.c index d9b5aa52..5d53c3b0 100644 --- a/cmds/send.c +++ b/cmds/send.c @@ -39,6 +39,7 @@ #include "common/send-utils.h" #include "common/help.h" #include "common/path-utils.h" +#include "common/string-utils.h" #include "cmds/commands.h" #include "ioctl.h" diff --git a/cmds/subvolume-list.c b/cmds/subvolume-list.c index 4418fdf0..9f6c4228 100644 --- a/cmds/subvolume-list.c +++ b/cmds/subvolume-list.c @@ -25,6 +25,7 @@ #include "common/help.h" #include "common/messages.h" #include "common/open-utils.h" +#include "common/string-utils.h" #include "common/utils.h" #include "cmds/commands.h" #include "ioctl.h" diff --git a/cmds/subvolume.c b/cmds/subvolume.c index e63ae081..78b658cf 100644 --- a/cmds/subvolume.c +++ b/cmds/subvolume.c @@ -36,6 +36,7 @@ #include "common/path-utils.h" #include "common/device-scan.h" #include "common/open-utils.h" +#include "common/string-utils.h" #include "common/units.h" #include "cmds/commands.h" #include "ioctl.h" diff --git a/common/fsfeatures.c b/common/fsfeatures.c index 7aad85da..d9e5bb55 100644 --- a/common/fsfeatures.c +++ b/common/fsfeatures.c @@ -21,6 +21,7 @@ #include #include "kernel-shared/ctree.h" #include "common/fsfeatures.h" +#include "common/string-utils.h" #include "common/utils.h" /* diff --git a/common/help.c b/common/help.c index 0441bdbd..9cbef7f4 100644 --- a/common/help.c +++ b/common/help.c @@ -21,6 +21,7 @@ #include #include "common/utils.h" #include "common/help.h" +#include "common/string-utils.h" #include "cmds/commands.h" #define USAGE_SHORT 1U diff --git a/common/string-utils.c b/common/string-utils.c new file mode 100644 index 00000000..016c96ac --- /dev/null +++ b/common/string-utils.c @@ -0,0 +1,76 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License v2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 021110-1307, USA. + */ + +#include "kerncompat.h" +#include +#include +#include +#include "string-utils.h" + +int string_is_numerical(const char *str) +{ + if (!str) + return 0; + if (!(*str >= '0' && *str <= '9')) + return 0; + while (*str >= '0' && *str <= '9') + str++; + if (*str != '\0') + return 0; + return 1; +} + +int prefixcmp(const char *str, const char *prefix) +{ + for (; ; str++, prefix++) + if (!*prefix) + return 0; + else if (*str != *prefix) + return (unsigned char)*prefix - (unsigned char)*str; +} + +/* + * This function should be only used when parsing command arg, it won't return + * error to its caller and rather exit directly just like usage(). + */ +u64 arg_strtou64(const char *str) +{ + u64 value; + char *ptr_parse_end = NULL; + + value = strtoull(str, &ptr_parse_end, 0); + if (ptr_parse_end && *ptr_parse_end != '\0') { + fprintf(stderr, "ERROR: %s is not a valid numeric value.\n", + str); + exit(1); + } + + /* + * if we pass a negative number to strtoull, it will return an + * unexpected number to us, so let's do the check ourselves. + */ + if (str[0] == '-') { + fprintf(stderr, "ERROR: %s: negative value is invalid.\n", + str); + exit(1); + } + if (value == ULLONG_MAX) { + fprintf(stderr, "ERROR: %s is too large.\n", str); + exit(1); + } + return value; +} + diff --git a/common/string-utils.h b/common/string-utils.h new file mode 100644 index 00000000..110d34f3 --- /dev/null +++ b/common/string-utils.h @@ -0,0 +1,24 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License v2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 021110-1307, USA. + */ + +#ifndef __BTRFS_STRING_UTILS_H__ +#define __BTRFS_STRING_UTILS_H__ + +int string_is_numerical(const char *str); +int prefixcmp(const char *str, const char *prefix); +u64 arg_strtou64(const char *str); + +#endif diff --git a/common/utils.c b/common/utils.c index 00732a21..76ddbb43 100644 --- a/common/utils.c +++ b/common/utils.c @@ -1060,28 +1060,6 @@ int count_digits(u64 num) return ret; } -int string_is_numerical(const char *str) -{ - if (!str) - return 0; - if (!(*str >= '0' && *str <= '9')) - return 0; - while (*str >= '0' && *str <= '9') - str++; - if (*str != '\0') - return 0; - return 1; -} - -int prefixcmp(const char *str, const char *prefix) -{ - for (; ; str++, prefix++) - if (!*prefix) - return 0; - else if (*str != *prefix) - return (unsigned char)*prefix - (unsigned char)*str; -} - const char *subvol_strip_mountpoint(const char *mnt, const char *full_path) { int len = strlen(mnt); @@ -1603,38 +1581,6 @@ out: return ret; } -/* - * This function should be only used when parsing command arg, it won't return - * error to its caller and rather exit directly just like usage(). - */ -u64 arg_strtou64(const char *str) -{ - u64 value; - char *ptr_parse_end = NULL; - - value = strtoull(str, &ptr_parse_end, 0); - if (ptr_parse_end && *ptr_parse_end != '\0') { - fprintf(stderr, "ERROR: %s is not a valid numeric value.\n", - str); - exit(1); - } - - /* - * if we pass a negative number to strtoull, it will return an - * unexpected number to us, so let's do the check ourselves. - */ - if (str[0] == '-') { - fprintf(stderr, "ERROR: %s: negative value is invalid.\n", - str); - exit(1); - } - if (value == ULLONG_MAX) { - fprintf(stderr, "ERROR: %s is too large.\n", str); - exit(1); - } - return value; -} - /* * For a given: * - file or directory return the containing tree root id diff --git a/common/utils.h b/common/utils.h index d5ca1103..0c4d6fad 100644 --- a/common/utils.h +++ b/common/utils.h @@ -46,7 +46,6 @@ enum exclusive_operation { #define BTRFS_CSUM_STRING_LEN (2 + 2 * BTRFS_CSUM_SIZE + 1) void btrfs_format_csum(u16 csum_type, const u8 *data, char *output); u64 parse_qgroupid_or_path(const char *p); -u64 arg_strtou64(const char *str); int get_fs_info(const char *path, struct btrfs_ioctl_fs_info_args *fi_args, struct btrfs_ioctl_dev_info_args **di_ret); int get_fsid(const char *path, u8 *fsid, int silent); @@ -77,9 +76,6 @@ const char* btrfs_group_profile_str(u64 flag); int count_digits(u64 num); u64 div_factor(u64 num, int factor); -int string_is_numerical(const char *str); -int prefixcmp(const char *str, const char *prefix); - unsigned long total_memory(void); void print_device_info(struct btrfs_device *device, char *prefix); diff --git a/image/main.c b/image/main.c index 22986694..1e6736a9 100644 --- a/image/main.c +++ b/image/main.c @@ -48,6 +48,7 @@ #include "common/help.h" #include "common/device-utils.h" #include "common/open-utils.h" +#include "common/string-utils.h" #include "image/metadump.h" #include "image/sanitize.h" #include "ioctl.h" diff --git a/mkfs/main.c b/mkfs/main.c index 6d6aaafe..5332ec34 100644 --- a/mkfs/main.c +++ b/mkfs/main.c @@ -50,6 +50,7 @@ #include "common/fsfeatures.h" #include "common/box.h" #include "common/units.h" +#include "common/string-utils.h" #include "check/qgroup-verify.h" #include "mkfs/common.h" #include "mkfs/rootdir.h"