fd = btrfsctl_open_file_or_dir(fname);
}
- if (name) {
- strncpy(args.name, name, BTRFS_PATH_NAME_MAX + 1);
- args.name[BTRFS_PATH_NAME_MAX] = 0;
- } else
+ if (name)
+ strncpy_null(args.name, name);
+ else
args.name[0] = '\0';
if (command == BTRFS_IOC_SNAP_CREATE) {
}
close(devfd);
- strncpy(ioctl_args.name, argv[i], BTRFS_PATH_NAME_MAX);
- ioctl_args.name[BTRFS_PATH_NAME_MAX-1] = 0;
+ strncpy_null(ioctl_args.name, argv[i]);
res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args);
e = errno;
if(res<0){
struct btrfs_ioctl_vol_args arg;
int res;
- strncpy(arg.name, argv[i], BTRFS_PATH_NAME_MAX);
- arg.name[BTRFS_PATH_NAME_MAX-1] = 0;
+ strncpy_null(arg.name, argv[i]);
res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg);
e = errno;
if(res<0){
printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]);
- strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX);
- args.name[BTRFS_PATH_NAME_MAX-1] = 0;
+ strncpy_null(args.name, argv[i]);
/*
* FIXME: which are the error code returned by this ioctl ?
* it seems that is impossible to understand if there no is
}
printf("Resize '%s' of '%s'\n", path, amount);
- strncpy(args.name, amount, BTRFS_PATH_NAME_MAX);
- args.name[BTRFS_PATH_NAME_MAX-1] = 0;
+ strncpy_null(args.name, amount);
res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
e = errno;
close(fd);
#include "ctree.h"
#include "commands.h"
+#include "utils.h"
#include "btrfs-list.h"
#include "utils.h"
struct btrfs_ioctl_vol_args_v2 args;
memset(&args, 0, sizeof(args));
- strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
- args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
+ strncpy_null(args.name, newname);
args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
args.size = qgroup_inherit_size(inherit);
args.qgroup_inherit = inherit;
struct btrfs_ioctl_vol_args args;
memset(&args, 0, sizeof(args));
- strncpy(args.name, newname, BTRFS_PATH_NAME_MAX);
- args.name[BTRFS_PATH_NAME_MAX-1] = 0;
+ strncpy_null(args.name, newname);
res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
}
}
printf("Delete subvolume '%s/%s'\n", dname, vname);
- strncpy(args.name, vname, BTRFS_PATH_NAME_MAX);
- args.name[BTRFS_PATH_NAME_MAX-1] = 0;
+ strncpy_null(args.name, vname);
res = ioctl(fd, BTRFS_IOC_SNAP_DESTROY, &args);
e = errno;
args.size = qgroup_inherit_size(inherit);
args.qgroup_inherit = inherit;
}
- strncpy(args.name, newname, BTRFS_SUBVOL_NAME_MAX);
- args.name[BTRFS_SUBVOL_NAME_MAX-1] = 0;
+ strncpy_null(args.name, newname);
res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
e = errno;
}
/*
+ * __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;
+}
+
+/*
* Checks to make sure that the label matches our requirements.
* Returns:
0 if everything is safe and usable
struct btrfs_ioctl_dev_info_args *di_args);
int get_fs_info(int fd, char *path, struct btrfs_ioctl_fs_info_args *fi_args,
struct btrfs_ioctl_dev_info_args **di_ret);
+
+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))
+
#endif