btrfs-progs: rename and move __strncpy_null to string-utils

Now that there's only __strncpy_null we can drop the underscore and move
it to string-utils as it's a generic string function rather than
something for paths.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2024-06-17 20:44:25 +02:00
parent 2d95c51b74
commit e673aa22bc
13 changed files with 56 additions and 56 deletions

View File

@ -169,7 +169,7 @@ static int cmd_device_add(const struct cmd_struct *cmd,
} }
memset(&ioctl_args, 0, sizeof(ioctl_args)); memset(&ioctl_args, 0, sizeof(ioctl_args));
__strncpy_null(ioctl_args.name, path, sizeof(ioctl_args.name)); strncpy_null(ioctl_args.name, path, sizeof(ioctl_args.name));
res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args); res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
if (res < 0) { if (res < 0) {
error("error adding device '%s': %m", path); error("error adding device '%s': %m", path);
@ -287,7 +287,7 @@ static int _cmd_device_remove(const struct cmd_struct *cmd,
} else if (strcmp(argv[i], "missing") == 0 || } else if (strcmp(argv[i], "missing") == 0 ||
cancel || cancel ||
path_is_block_device(argv[i]) == 1) { path_is_block_device(argv[i]) == 1) {
__strncpy_null(argv2.name, argv[i], sizeof(argv2.name)); strncpy_null(argv2.name, argv[i], sizeof(argv2.name));
} else { } else {
error("not a block device: %s", argv[i]); error("not a block device: %s", argv[i]);
ret++; ret++;
@ -312,7 +312,7 @@ static int _cmd_device_remove(const struct cmd_struct *cmd,
continue; continue;
} }
memset(&arg, 0, sizeof(arg)); memset(&arg, 0, sizeof(arg));
__strncpy_null(arg.name, argv[i], sizeof(arg.name)); strncpy_null(arg.name, argv[i], sizeof(arg.name));
res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg); res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
} }
@ -396,7 +396,7 @@ static int btrfs_forget_devices(const char *path)
memset(&args, 0, sizeof(args)); memset(&args, 0, sizeof(args));
if (path) if (path)
__strncpy_null(args.name, path, sizeof(args.name)); strncpy_null(args.name, path, sizeof(args.name));
ret = ioctl(fd, BTRFS_IOC_FORGET_DEV, &args); ret = ioctl(fd, BTRFS_IOC_FORGET_DEV, &args);
if (ret) if (ret)
ret = -errno; ret = -errno;
@ -557,7 +557,7 @@ static int cmd_device_ready(const struct cmd_struct *cmd, int argc, char **argv)
} }
memset(&args, 0, sizeof(args)); memset(&args, 0, sizeof(args));
__strncpy_null(args.name, path, sizeof(args.name)); strncpy_null(args.name, path, sizeof(args.name));
ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args); ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
if (ret < 0) { if (ret < 0) {
error("unable to determine if device '%s' is ready for mount: %m", error("unable to determine if device '%s' is ready for mount: %m",

View File

@ -1441,7 +1441,7 @@ static int cmd_filesystem_resize(const struct cmd_struct *cmd,
memset(&args, 0, sizeof(args)); memset(&args, 0, sizeof(args));
if (devid == (u64)-1) { if (devid == (u64)-1) {
/* Ok to copy the string verbatim. */ /* Ok to copy the string verbatim. */
__strncpy_null(args.name, amount, sizeof(args.name)); strncpy_null(args.name, amount, sizeof(args.name));
} else { } else {
/* The implicit devid 1 needs to be adjusted. */ /* The implicit devid 1 needs to be adjusted. */
snprintf(args.name, sizeof(args.name) - 1, "%llu:%s", devid, amount); snprintf(args.name, sizeof(args.name) - 1, "%llu:%s", devid, amount);

View File

@ -179,7 +179,7 @@ static int process_subvol(const char *path, const u8 *uuid, u64 ctransid,
} }
if (*rctx->dest_dir_path == 0) { if (*rctx->dest_dir_path == 0) {
__strncpy_null(rctx->cur_subvol_path, path, sizeof(rctx->cur_subvol_path)); strncpy_null(rctx->cur_subvol_path, path, sizeof(rctx->cur_subvol_path));
} else { } else {
ret = path_cat_out(rctx->cur_subvol_path, rctx->dest_dir_path, ret = path_cat_out(rctx->cur_subvol_path, rctx->dest_dir_path,
path); path);
@ -209,7 +209,7 @@ static int process_subvol(const char *path, const u8 *uuid, u64 ctransid,
} }
memset(&args_v1, 0, sizeof(args_v1)); memset(&args_v1, 0, sizeof(args_v1));
__strncpy_null(args_v1.name, path, sizeof(args_v1.name)); strncpy_null(args_v1.name, path, sizeof(args_v1.name));
ret = ioctl(rctx->dest_dir_fd, BTRFS_IOC_SUBVOL_CREATE, &args_v1); ret = ioctl(rctx->dest_dir_fd, BTRFS_IOC_SUBVOL_CREATE, &args_v1);
if (ret < 0) { if (ret < 0) {
ret = -errno; ret = -errno;
@ -249,7 +249,7 @@ static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid,
} }
if (*rctx->dest_dir_path == 0) { if (*rctx->dest_dir_path == 0) {
__strncpy_null(rctx->cur_subvol_path, path, sizeof(rctx->cur_subvol_path)); strncpy_null(rctx->cur_subvol_path, path, sizeof(rctx->cur_subvol_path));
} else { } else {
ret = path_cat_out(rctx->cur_subvol_path, rctx->dest_dir_path, ret = path_cat_out(rctx->cur_subvol_path, rctx->dest_dir_path,
path); path);
@ -281,7 +281,7 @@ static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid,
} }
memset(&args_v2, 0, sizeof(args_v2)); memset(&args_v2, 0, sizeof(args_v2));
__strncpy_null(args_v2.name, path, sizeof(args_v2.name)); strncpy_null(args_v2.name, path, sizeof(args_v2.name));
parent_subvol = subvol_uuid_search(rctx->mnt_fd, 0, parent_uuid, parent_subvol = subvol_uuid_search(rctx->mnt_fd, 0, parent_uuid,
parent_ctransid, NULL, parent_ctransid, NULL,
@ -663,7 +663,7 @@ static int open_inode_for_write(struct btrfs_receive *rctx, const char *path)
error("cannot open %s: %m", path); error("cannot open %s: %m", path);
goto out; goto out;
} }
__strncpy_null(rctx->write_path, path, sizeof(rctx->write_path)); strncpy_null(rctx->write_path, path, sizeof(rctx->write_path));
out: out:
return ret; return ret;

View File

@ -197,7 +197,7 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
char dstdir_dup[PATH_MAX]; char dstdir_dup[PATH_MAX];
char *token; char *token;
__strncpy_null(dstdir_dup, dstdir, sizeof(dstdir_dup)); strncpy_null(dstdir_dup, dstdir, sizeof(dstdir_dup));
if (dstdir_dup[0] == '/') if (dstdir_dup[0] == '/')
strcat(p, "/"); strcat(p, "/");
@ -233,7 +233,7 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
struct btrfs_ioctl_vol_args_v2 args; struct btrfs_ioctl_vol_args_v2 args;
memset(&args, 0, sizeof(args)); memset(&args, 0, sizeof(args));
__strncpy_null(args.name, newname, sizeof(args.name)); strncpy_null(args.name, newname, sizeof(args.name));
args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT; args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
args.size = btrfs_qgroup_inherit_size(inherit); args.size = btrfs_qgroup_inherit_size(inherit);
args.qgroup_inherit = inherit; args.qgroup_inherit = inherit;
@ -243,7 +243,7 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
struct btrfs_ioctl_vol_args args; struct btrfs_ioctl_vol_args args;
memset(&args, 0, sizeof(args)); memset(&args, 0, sizeof(args));
__strncpy_null(args.name, newname, sizeof(args.name)); strncpy_null(args.name, newname, sizeof(args.name));
ret = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args); ret = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
} }
@ -737,7 +737,7 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char *
args.size = btrfs_qgroup_inherit_size(inherit); args.size = btrfs_qgroup_inherit_size(inherit);
args.qgroup_inherit = inherit; args.qgroup_inherit = inherit;
} }
__strncpy_null(args.name, newname, sizeof(args.name)); strncpy_null(args.name, newname, sizeof(args.name));
res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args); res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
if (res < 0) { if (res < 0) {

View File

@ -51,6 +51,7 @@
#include "common/utils.h" #include "common/utils.h"
#include "common/defs.h" #include "common/defs.h"
#include "common/open-utils.h" #include "common/open-utils.h"
#include "common/string-utils.h"
#include "common/units.h" #include "common/units.h"
static int btrfs_scan_done = 0; static int btrfs_scan_done = 0;
@ -237,7 +238,7 @@ int btrfs_register_one_device(const char *fname)
return -errno; return -errno;
} }
memset(&args, 0, sizeof(args)); memset(&args, 0, sizeof(args));
__strncpy_null(args.name, fname, sizeof(args.name)); strncpy_null(args.name, fname, sizeof(args.name));
ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args); ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args);
if (ret < 0) { if (ret < 0) {
error("device scan failed on '%s': %m", fname); error("device scan failed on '%s': %m", fname);
@ -468,7 +469,7 @@ int btrfs_scan_devices(int verbose)
if (!dev) if (!dev)
continue; continue;
/* if we are here its definitely a btrfs disk*/ /* if we are here its definitely a btrfs disk*/
__strncpy_null(path, blkid_dev_devname(dev), sizeof(path)); strncpy_null(path, blkid_dev_devname(dev), sizeof(path));
if (stat(path, &dev_stat) < 0) if (stat(path, &dev_stat) < 0)
continue; continue;

View File

@ -28,6 +28,7 @@
#include "common/filesystem-utils.h" #include "common/filesystem-utils.h"
#include "common/messages.h" #include "common/messages.h"
#include "common/open-utils.h" #include "common/open-utils.h"
#include "common/string-utils.h"
#include "common/path-utils.h" #include "common/path-utils.h"
/* /*
@ -102,7 +103,7 @@ static int set_label_unmounted(const char *dev, const char *label)
error_msg(ERROR_MSG_START_TRANS, "set label"); error_msg(ERROR_MSG_START_TRANS, "set label");
return PTR_ERR(trans); return PTR_ERR(trans);
} }
__strncpy_null(root->fs_info->super_copy->label, label, BTRFS_LABEL_SIZE); strncpy_null(root->fs_info->super_copy->label, label, BTRFS_LABEL_SIZE);
btrfs_commit_transaction(trans, root); btrfs_commit_transaction(trans, root);
@ -123,7 +124,7 @@ static int set_label_mounted(const char *mount_path, const char *labelp)
} }
memset(label, 0, sizeof(label)); memset(label, 0, sizeof(label));
__strncpy_null(label, labelp, BTRFS_LABEL_SIZE); strncpy_null(label, labelp, BTRFS_LABEL_SIZE);
if (ioctl(fd, BTRFS_IOC_SET_FSLABEL, label) < 0) { if (ioctl(fd, BTRFS_IOC_SET_FSLABEL, label) < 0) {
error("unable to set label of %s: %m", mount_path); error("unable to set label of %s: %m", mount_path);
close(fd); close(fd);
@ -152,8 +153,7 @@ int get_label_unmounted(const char *dev, char *label)
if(!root) if(!root)
return -1; return -1;
__strncpy_null(label, root->fs_info->super_copy->label, strncpy_null(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE);
BTRFS_LABEL_SIZE);
/* Now we close it since we are done. */ /* Now we close it since we are done. */
close_ctree(root); close_ctree(root);
@ -187,7 +187,7 @@ int get_label_mounted(const char *mount_path, char *labelp)
return ret; return ret;
} }
__strncpy_null(labelp, label, BTRFS_LABEL_SIZE); strncpy_null(labelp, label, BTRFS_LABEL_SIZE);
close(fd); close(fd);
return 0; return 0;
} }

View File

@ -35,6 +35,7 @@
*/ */
#include <libgen.h> #include <libgen.h>
#include <limits.h> #include <limits.h>
#include "common/string-utils.h"
#include "common/path-utils.h" #include "common/path-utils.h"
/* /*
@ -193,10 +194,10 @@ static int is_same_blk_file(const char* a, const char* b)
char real_b[PATH_MAX]; char real_b[PATH_MAX];
if (!realpath(a, real_a)) if (!realpath(a, real_a))
__strncpy_null(real_a, a, sizeof(real_a)); strncpy_null(real_a, a, sizeof(real_a));
if (!realpath(b, real_b)) if (!realpath(b, real_b))
__strncpy_null(real_b, b, sizeof(real_b)); strncpy_null(real_b, b, sizeof(real_b));
/* Identical path? */ /* Identical path? */
if (strcmp(real_a, real_b) == 0) if (strcmp(real_a, real_b) == 0)
@ -345,26 +346,6 @@ char *path_canonicalize(const char *path)
return canonical; return canonical;
} }
/*
* __strncpy_null - strncpy with null termination
* @dest: the target array
* @src: the source string
* @n: maximum bytes to copy (size of *dest)
*
* Like strncpy, but ensures destination is null-terminated.
*
* Copies the string pointed to by src, including the terminating null
* byte ('\0'), to the buffer pointed to by dest, up to a maximum
* of n bytes. Then ensure that dest is null-terminated.
*/
char *__strncpy_null(char *dest, const char *src, size_t n)
{
strncpy(dest, src, n);
if (n > 0)
dest[n - 1] = '\0';
return dest;
}
/* /*
* Test if path is a directory * Test if path is a directory
* Returns: * Returns:
@ -403,7 +384,7 @@ int path_is_in_dir(const char *parent, const char *path)
char *curr_dir = tmp; char *curr_dir = tmp;
int ret; int ret;
__strncpy_null(tmp, path, sizeof(tmp)); strncpy_null(tmp, path, sizeof(tmp));
while (strcmp(parent, curr_dir) != 0) { while (strcmp(parent, curr_dir) != 0) {
if (strcmp(curr_dir, "/") == 0) { if (strcmp(curr_dir, "/") == 0) {
@ -432,7 +413,7 @@ int arg_copy_path(char *dest, const char *src, int destlen)
if (len >= PATH_MAX || len >= destlen) if (len >= PATH_MAX || len >= destlen)
return -ENAMETOOLONG; return -ENAMETOOLONG;
__strncpy_null(dest, src, destlen); strncpy_null(dest, src, destlen);
return 0; return 0;
} }

View File

@ -26,11 +26,6 @@ char *path_canonicalize(const char *path);
int arg_copy_path(char *dest, const char *src, int destlen); int arg_copy_path(char *dest, const char *src, int destlen);
int path_cat_out(char *out, const char *p1, const char *p2); int path_cat_out(char *out, const char *p1, const char *p2);
int path_cat3_out(char *out, const char *p1, const char *p2, const char *p3); int path_cat3_out(char *out, const char *p1, const char *p2, const char *p3);
char *__strncpy_null(char *dest, const char *src, size_t n);
/* Helper to always get proper size of the destination string */
#define strncpy_null(dest, src) __strncpy_null(dest, src, sizeof(dest))
int path_is_block_device(const char *file); int path_is_block_device(const char *file);
int path_is_a_mount_point(const char *file); int path_is_a_mount_point(const char *file);
int path_exists(const char *file); int path_exists(const char *file);

View File

@ -44,6 +44,26 @@ int string_has_prefix(const char *str, const char *prefix)
return (unsigned char)*prefix - (unsigned char)*str; return (unsigned char)*prefix - (unsigned char)*str;
} }
/*
* strncpy_null - strncpy with null termination
* @dest: the target array
* @src: the source string
* @n: maximum bytes to copy (size of *dest)
*
* Like strncpy, but ensures destination is null-terminated.
*
* Copies the string pointed to by src, including the terminating null
* byte ('\0'), to the buffer pointed to by dest, up to a maximum
* of n bytes. Then ensure that dest is null-terminated.
*/
char *strncpy_null(char *dest, const char *src, size_t n)
{
strncpy(dest, src, n);
if (n > 0)
dest[n - 1] = '\0';
return dest;
}
/* /*
* This function should be only used when parsing command arg, it won't return * 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(). * error to its caller and rather exit directly just like usage().

View File

@ -22,6 +22,8 @@
int string_is_numerical(const char *str); int string_is_numerical(const char *str);
int string_has_prefix(const char *str, const char *prefix); int string_has_prefix(const char *str, const char *prefix);
char *strncpy_null(char *dest, const char *src, size_t n);
/* /*
* Helpers prefixed by arg_* can exit if the argument is invalid and are supposed * Helpers prefixed by arg_* can exit if the argument is invalid and are supposed
* to be used when parsing command line options where the immediate exit is valid * to be used when parsing command line options where the immediate exit is valid

View File

@ -30,6 +30,7 @@
#include "kernel-shared/uapi/btrfs_tree.h" #include "kernel-shared/uapi/btrfs_tree.h"
#include "common/path-utils.h" #include "common/path-utils.h"
#include "common/messages.h" #include "common/messages.h"
#include "common/string-utils.h"
#include "common/fsfeatures.h" #include "common/fsfeatures.h"
#include "mkfs/common.h" #include "mkfs/common.h"
#include "convert/common.h" #include "convert/common.h"
@ -148,7 +149,7 @@ static int setup_temp_super(int fd, struct btrfs_mkfs_config *cfg,
btrfs_set_super_cache_generation(&super, -1); btrfs_set_super_cache_generation(&super, -1);
btrfs_set_super_incompat_flags(&super, cfg->features.incompat_flags); btrfs_set_super_incompat_flags(&super, cfg->features.incompat_flags);
if (cfg->label) if (cfg->label)
__strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE); strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE);
/* Sys chunk array will be re-initialized at chunk tree init time */ /* Sys chunk array will be re-initialized at chunk tree init time */
super.sys_chunk_array_size = 0; super.sys_chunk_array_size = 0;

View File

@ -1322,8 +1322,7 @@ static int do_convert(const char *devname, u32 convert_flags, u32 nodesize,
memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
if (convert_flags & CONVERT_FLAG_COPY_LABEL) { if (convert_flags & CONVERT_FLAG_COPY_LABEL) {
__strncpy_null(root->fs_info->super_copy->label, strncpy_null(root->fs_info->super_copy->label, cctx.label, BTRFS_LABEL_SIZE);
cctx.label, BTRFS_LABEL_SIZE);
printf("Copy label '%s'\n", root->fs_info->super_copy->label); printf("Copy label '%s'\n", root->fs_info->super_copy->label);
} else if (convert_flags & CONVERT_FLAG_SET_LABEL) { } else if (convert_flags & CONVERT_FLAG_SET_LABEL) {
strcpy(root->fs_info->super_copy->label, fslabel); strcpy(root->fs_info->super_copy->label, fslabel);
@ -1938,7 +1937,7 @@ int BOX_MAIN(convert)(int argc, char *argv[])
"label too long, trimmed to %d bytes", "label too long, trimmed to %d bytes",
BTRFS_LABEL_SIZE - 1); BTRFS_LABEL_SIZE - 1);
} }
__strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE); strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE);
break; break;
case 'L': case 'L':
copylabel = CONVERT_FLAG_COPY_LABEL; copylabel = CONVERT_FLAG_COPY_LABEL;

View File

@ -38,6 +38,7 @@
#include "common/path-utils.h" #include "common/path-utils.h"
#include "common/device-utils.h" #include "common/device-utils.h"
#include "common/open-utils.h" #include "common/open-utils.h"
#include "common/string-utils.h"
#include "mkfs/common.h" #include "mkfs/common.h"
static u64 reference_root_table[] = { static u64 reference_root_table[] = {
@ -476,7 +477,7 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
btrfs_set_super_nr_global_roots(&super, 1); btrfs_set_super_nr_global_roots(&super, 1);
if (cfg->label) if (cfg->label)
__strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE); strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE);
/* create the tree of root objects */ /* create the tree of root objects */
memset(buf->data, 0, cfg->nodesize); memset(buf->data, 0, cfg->nodesize);