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 <dsterba@suse.com>
This commit is contained in:
David Sterba 2022-09-15 23:15:17 +02:00
parent 466e025c74
commit 6edd4b2121
26 changed files with 122 additions and 58 deletions

View File

@ -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 \

View File

@ -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

View File

@ -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"
/*

View File

@ -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

View File

@ -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)
{

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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,

View File

@ -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)

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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];

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -21,6 +21,7 @@
#include <unistd.h>
#include "kernel-shared/ctree.h"
#include "common/fsfeatures.h"
#include "common/string-utils.h"
#include "common/utils.h"
/*

View File

@ -21,6 +21,7 @@
#include <getopt.h>
#include "common/utils.h"
#include "common/help.h"
#include "common/string-utils.h"
#include "cmds/commands.h"
#define USAGE_SHORT 1U

76
common/string-utils.c Normal file
View File

@ -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 <stdlib.h>
#include <stdio.h>
#include <limits.h>
#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;
}

24
common/string-utils.h Normal file
View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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"

View File

@ -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"