summaryrefslogtreecommitdiff
path: root/mkfs/common.c
diff options
context:
space:
mode:
authorMisono, Tomohiro <misono.tomohiro@jp.fujitsu.com>2017-11-24 14:21:15 +0900
committerDavid Sterba <dsterba@suse.com>2018-01-03 17:09:57 +0100
commite460ccd6d13bb2cc4939fb860b50964dfbd3c247 (patch)
treeaa77d4581e99d7e78cb7d01fd411a739cc34c390 /mkfs/common.c
parentdc06cda3d6afb4fad5ddfd1508a33f79c2e68efb (diff)
btrfs-progs: mkfs: check the status of file at mkfs
Currently, only the status of block devices is checked at mkfs, but we should also check for regular files whether they are already formatted or mounted to prevent overwrite accidentally. Device status is checked by test_dev_for_mkfs(). The part which is not related to block device is split from this and used for both block device and regular file. Signed-off-by: Tomohiro Misono <misono.tomohiro@jp.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'mkfs/common.c')
-rw-r--r--mkfs/common.c46
1 files changed, 30 insertions, 16 deletions
diff --git a/mkfs/common.c b/mkfs/common.c
index 71318b10..ba47684b 100644
--- a/mkfs/common.c
+++ b/mkfs/common.c
@@ -632,23 +632,9 @@ int test_dev_for_mkfs(const char *file, int force_overwrite)
error("%s is a swap device", file);
return 1;
}
- if (!force_overwrite) {
- if (check_overwrite(file)) {
- error("use the -f option to force overwrite of %s",
- file);
- return 1;
- }
- }
- ret = check_mounted(file);
- if (ret < 0) {
- error("cannot check mount status of %s: %s", file,
- strerror(-ret));
+ ret = test_status_for_mkfs(file, force_overwrite);
+ if (ret)
return 1;
- }
- if (ret == 1) {
- error("%s is mounted", file);
- return 1;
- }
/* check if the device is busy */
fd = open(file, O_RDWR|O_EXCL);
if (fd < 0) {
@@ -669,6 +655,34 @@ int test_dev_for_mkfs(const char *file, int force_overwrite)
return 0;
}
+/*
+ * check if the file (device) is formatted or mounted
+ */
+int test_status_for_mkfs(const char *file, bool force_overwrite)
+{
+ int ret;
+
+ if (!force_overwrite) {
+ if (check_overwrite(file)) {
+ error("use the -f option to force overwrite of %s",
+ file);
+ return 1;
+ }
+ }
+ ret = check_mounted(file);
+ if (ret < 0) {
+ error("cannot check mount status of %s: %s", file,
+ strerror(-ret));
+ return 1;
+ }
+ if (ret == 1) {
+ error("%s is mounted", file);
+ return 1;
+ }
+
+ return 0;
+}
+
int is_vol_small(const char *file)
{
int fd = -1;