diff --git a/Makefile b/Makefile index 4dd405d3..1b1c045e 100644 --- a/Makefile +++ b/Makefile @@ -57,9 +57,9 @@ all: version $(progs) manpages version: $(Q)bash version.sh -btrfs: $(objects) btrfs.o help.o common.o $(cmds_objects) +btrfs: $(objects) btrfs.o help.o $(cmds_objects) @echo " [LD] $@" - $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o common.o $(cmds_objects) \ + $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \ $(objects) $(LDFLAGS) $(LIBS) -lpthread calc-size: $(objects) calc-size.o diff --git a/btrfsctl.c b/btrfsctl.c index 518684c6..049a5f35 100644 --- a/btrfsctl.c +++ b/btrfsctl.c @@ -63,7 +63,7 @@ static void print_usage(void) exit(1); } -static int open_file_or_dir(const char *fname) +static int btrfsctl_open_file_or_dir(const char *fname) { int ret; struct stat st; @@ -91,6 +91,7 @@ static int open_file_or_dir(const char *fname) } return fd; } + int main(int ac, char **av) { char *fname = NULL; @@ -128,7 +129,7 @@ int main(int ac, char **av) snap_location = strdup(fullpath); snap_location = dirname(snap_location); - snap_fd = open_file_or_dir(snap_location); + snap_fd = btrfsctl_open_file_or_dir(snap_location); name = strdup(fullpath); name = basename(name); @@ -238,7 +239,7 @@ int main(int ac, char **av) } name = fname; } else { - fd = open_file_or_dir(fname); + fd = btrfsctl_open_file_or_dir(fname); } if (name) { diff --git a/cmds-balance.c b/cmds-balance.c index dfa0dfeb..03057213 100644 --- a/cmds-balance.c +++ b/cmds-balance.c @@ -28,6 +28,7 @@ #include "volumes.h" #include "commands.h" +#include "utils.h" static const char * const balance_cmd_group_usage[] = { "btrfs [filesystem] balance [options] ", diff --git a/cmds-inspect.c b/cmds-inspect.c index 25b83d2f..f10bf55e 100644 --- a/cmds-inspect.c +++ b/cmds-inspect.c @@ -23,6 +23,7 @@ #include "kerncompat.h" #include "ioctl.h" +#include "utils.h" #include "commands.h" #include "btrfs-list.h" diff --git a/cmds-qgroup.c b/cmds-qgroup.c index 3ac2e14e..085881f3 100644 --- a/cmds-qgroup.c +++ b/cmds-qgroup.c @@ -25,6 +25,7 @@ #include "commands.h" #include "qgroup.h" +#include "utils.h" static const char * const qgroup_cmd_group_usage[] = { "btrfs qgroup [options] ", diff --git a/cmds-quota.c b/cmds-quota.c index cf9ad976..8481514c 100644 --- a/cmds-quota.c +++ b/cmds-quota.c @@ -23,6 +23,7 @@ #include "ioctl.h" #include "commands.h" +#include "utils.h" static const char * const quota_cmd_group_usage[] = { "btrfs quota [options] ", diff --git a/cmds-subvolume.c b/cmds-subvolume.c index ac39f7bf..e3cdb1e0 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -32,6 +32,7 @@ #include "ctree.h" #include "commands.h" #include "btrfs-list.h" +#include "utils.h" static const char * const subvolume_cmd_group_usage[] = { "btrfs subvolume ", diff --git a/commands.h b/commands.h index bb6d2dd7..8114a73d 100644 --- a/commands.h +++ b/commands.h @@ -79,9 +79,6 @@ void help_ambiguous_token(const char *arg, const struct cmd_group *grp); void help_command_group(const struct cmd_group *grp, int argc, char **argv); -/* common.c */ -int open_file_or_dir(const char *fname); - extern const struct cmd_group subvolume_cmd_group; extern const struct cmd_group filesystem_cmd_group; extern const struct cmd_group balance_cmd_group; diff --git a/common.c b/common.c deleted file mode 100644 index 03f65703..00000000 --- a/common.c +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 -#include -#include -#include - -int open_file_or_dir(const char *fname) -{ - int ret; - struct stat st; - DIR *dirstream; - int fd; - - ret = stat(fname, &st); - if (ret < 0) { - return -1; - } - if (S_ISDIR(st.st_mode)) { - dirstream = opendir(fname); - if (!dirstream) { - return -2; - } - fd = dirfd(dirstream); - } else { - fd = open(fname, O_RDWR); - } - if (fd < 0) { - return -3; - } - return fd; -} diff --git a/utils.c b/utils.c index ba017fdd..87051359 100644 --- a/utils.c +++ b/utils.c @@ -16,8 +16,9 @@ * Boston, MA 021110-1307, USA. */ -#define _XOPEN_SOURCE 600 -#define __USE_XOPEN2K +#define _XOPEN_SOURCE 700 +#define __USE_XOPEN2K8 +#define __XOPEN2K8 /* due to an error in dirent.h, to get dirfd() */ #include #include #include @@ -1276,3 +1277,28 @@ u64 parse_size(char *s) return strtoull(s, NULL, 10) * mult; } +int open_file_or_dir(const char *fname) +{ + int ret; + struct stat st; + DIR *dirstream; + int fd; + + ret = stat(fname, &st); + if (ret < 0) { + return -1; + } + if (S_ISDIR(st.st_mode)) { + dirstream = opendir(fname); + if (!dirstream) { + return -2; + } + fd = dirfd(dirstream); + } else { + fd = open(fname, O_RDWR); + } + if (fd < 0) { + return -3; + } + return fd; +} diff --git a/utils.h b/utils.h index 714fd7ac..ed43e846 100644 --- a/utils.h +++ b/utils.h @@ -19,6 +19,8 @@ #ifndef __UTILS__ #define __UTILS__ +#include "ctree.h" + #define BTRFS_MKFS_SYSTEM_GROUP_SIZE (4 * 1024 * 1024) int make_btrfs(int fd, const char *device, const char *label, @@ -44,8 +46,7 @@ int btrfs_device_already_in_root(struct btrfs_root *root, int fd, char *pretty_sizes(u64 size); int check_label(char *input); int get_mountpt(char *dev, char *mntpt, size_t size); - int btrfs_scan_block_devices(int run_ioctl); - u64 parse_size(char *s); +int open_file_or_dir(const char *fname); #endif