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));
__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);
if (res < 0) {
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 ||
cancel ||
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 {
error("not a block device: %s", argv[i]);
ret++;
@ -312,7 +312,7 @@ static int _cmd_device_remove(const struct cmd_struct *cmd,
continue;
}
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);
}
@ -396,7 +396,7 @@ static int btrfs_forget_devices(const char *path)
memset(&args, 0, sizeof(args));
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);
if (ret)
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));
__strncpy_null(args.name, path, sizeof(args.name));
strncpy_null(args.name, path, sizeof(args.name));
ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args);
if (ret < 0) {
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));
if (devid == (u64)-1) {
/* Ok to copy the string verbatim. */
__strncpy_null(args.name, amount, sizeof(args.name));
strncpy_null(args.name, amount, sizeof(args.name));
} else {
/* The implicit devid 1 needs to be adjusted. */
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) {
__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 {
ret = path_cat_out(rctx->cur_subvol_path, rctx->dest_dir_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));
__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);
if (ret < 0) {
ret = -errno;
@ -249,7 +249,7 @@ static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid,
}
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 {
ret = path_cat_out(rctx->cur_subvol_path, rctx->dest_dir_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));
__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_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);
goto out;
}
__strncpy_null(rctx->write_path, path, sizeof(rctx->write_path));
strncpy_null(rctx->write_path, path, sizeof(rctx->write_path));
out:
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 *token;
__strncpy_null(dstdir_dup, dstdir, sizeof(dstdir_dup));
strncpy_null(dstdir_dup, dstdir, sizeof(dstdir_dup));
if (dstdir_dup[0] == '/')
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;
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.size = btrfs_qgroup_inherit_size(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;
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);
}
@ -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.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);
if (res < 0) {

View File

@ -51,6 +51,7 @@
#include "common/utils.h"
#include "common/defs.h"
#include "common/open-utils.h"
#include "common/string-utils.h"
#include "common/units.h"
static int btrfs_scan_done = 0;
@ -237,7 +238,7 @@ int btrfs_register_one_device(const char *fname)
return -errno;
}
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);
if (ret < 0) {
error("device scan failed on '%s': %m", fname);
@ -468,7 +469,7 @@ int btrfs_scan_devices(int verbose)
if (!dev)
continue;
/* 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)
continue;

View File

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

View File

@ -35,6 +35,7 @@
*/
#include <libgen.h>
#include <limits.h>
#include "common/string-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];
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))
__strncpy_null(real_b, b, sizeof(real_b));
strncpy_null(real_b, b, sizeof(real_b));
/* Identical path? */
if (strcmp(real_a, real_b) == 0)
@ -345,26 +346,6 @@ char *path_canonicalize(const char *path)
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
* Returns:
@ -403,7 +384,7 @@ int path_is_in_dir(const char *parent, const char *path)
char *curr_dir = tmp;
int ret;
__strncpy_null(tmp, path, sizeof(tmp));
strncpy_null(tmp, path, sizeof(tmp));
while (strcmp(parent, 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)
return -ENAMETOOLONG;
__strncpy_null(dest, src, destlen);
strncpy_null(dest, src, destlen);
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 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);
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_a_mount_point(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;
}
/*
* 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
* 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_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
* 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 "common/path-utils.h"
#include "common/messages.h"
#include "common/string-utils.h"
#include "common/fsfeatures.h"
#include "mkfs/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_incompat_flags(&super, cfg->features.incompat_flags);
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 */
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);
if (convert_flags & CONVERT_FLAG_COPY_LABEL) {
__strncpy_null(root->fs_info->super_copy->label,
cctx.label, BTRFS_LABEL_SIZE);
strncpy_null(root->fs_info->super_copy->label, cctx.label, BTRFS_LABEL_SIZE);
printf("Copy label '%s'\n", root->fs_info->super_copy->label);
} else if (convert_flags & CONVERT_FLAG_SET_LABEL) {
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",
BTRFS_LABEL_SIZE - 1);
}
__strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE);
strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE);
break;
case 'L':
copylabel = CONVERT_FLAG_COPY_LABEL;

View File

@ -38,6 +38,7 @@
#include "common/path-utils.h"
#include "common/device-utils.h"
#include "common/open-utils.h"
#include "common/string-utils.h"
#include "mkfs/common.h"
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);
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 */
memset(buf->data, 0, cfg->nodesize);