linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Goffredo Baroncelli <kreijack@libero.it>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.cz, Chris Mason <clm@fb.com>,
	Goffredo Baroncelli <kreijack@inwind.it>
Subject: [PATCH 1/5] Add some helper functions
Date: Sun, 24 Jul 2016 13:03:25 +0200	[thread overview]
Message-ID: <1469358209-9427-2-git-send-email-kreijack@libero.it> (raw)
In-Reply-To: <1469358209-9427-1-git-send-email-kreijack@libero.it>

From: Goffredo Baroncelli <kreijack@inwind.it>

Add the following functions:
- int is_btrfs_fs(const char *path) -> returns 0 if path is a btrfs filesystem
- void check_root_or_exit() -> checks if the user has the root capability or
                               it exits writing an error message
- void check_btrfs_or_exit(const char *path)
				checks if path is a valid btrfs filesystem,
				otherwise it exits

Signed-off-by: Goffredo baroncelli <kreijack@inwind.it>
---
 utils.c | 41 +++++++++++++++++++++++++++++++++++++++++
 utils.h | 14 ++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/utils.c b/utils.c
index 578fdb0..b99706c 100644
--- a/utils.c
+++ b/utils.c
@@ -4131,3 +4131,44 @@ unsigned int rand_range(unsigned int upper)
 	 */
 	return (unsigned int)(jrand48(rand_seed) % upper);
 }
+
+/*
+ * check if path is a btrfs filesystem
+ */
+int is_btrfs_fs(const char *path)
+{
+	struct statfs stfs;
+
+	if (statfs(path, &stfs) != 0) {
+		/* cannot access */
+		return -1;
+	}
+
+	if (stfs.f_type != BTRFS_SUPER_MAGIC) {
+		/* not a btrfs filesystem */
+		return -2;
+	}
+
+	return 0;
+}
+
+/*
+ * check if the user is root
+ */
+void check_root_or_exit()
+{
+    if (geteuid() == 0)
+        return;
+
+    error("You need to be root to execute this command");
+    exit(100);
+}
+
+void check_btrfs_or_exit(const char *path)
+{
+    if (!is_btrfs_fs(path))
+        return;
+
+    error("'%s' must be a valid btrfs filesystem", path);
+    exit(100);
+}
diff --git a/utils.h b/utils.h
index 98bfb34..0bd6ecb 100644
--- a/utils.h
+++ b/utils.h
@@ -399,4 +399,18 @@ unsigned int rand_range(unsigned int upper);
 /* Also allow setting the seed manually */
 void init_rand_seed(u64 seed);
 
+/* return 0 if path is a valid btrfs filesystem */
+int is_btrfs_fs(const char *path);
+
+/*
+ * check if the user has the root capability, otherwise it exits printing an
+ * error message
+ */
+void check_root_or_exit();
+/*
+ * check if path is a valid btrfs filesystem, otherwise it exits printing an
+ * error message
+ */
+void check_btrfs_or_exit(const char *path);
+
 #endif
-- 
2.8.1


  reply	other threads:[~2016-07-24 11:03 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-24 11:03 [BTRFS-PROGS][PATCH] Add two new commands: 'btrfs insp physical-find' and 'btrfs insp physical-dump' Goffredo Baroncelli
2016-07-24 11:03 ` Goffredo Baroncelli [this message]
2016-07-24 11:03 ` [PATCH 2/5] New btrfs command: "btrfs inspect physical-find" Goffredo Baroncelli
2016-07-24 11:03 ` [PATCH 3/5] new command btrfs inspect physical-dump Goffredo Baroncelli
2016-07-24 11:03 ` [PATCH 4/5] Add man page for command btrfs insp physical-find Goffredo Baroncelli
2016-07-24 11:03 ` [PATCH 5/5] Add new command to man pages: btrfs insp physical-dump Goffredo Baroncelli
2016-07-25  2:14 ` [BTRFS-PROGS][PATCH] Add two new commands: 'btrfs insp physical-find' and 'btrfs insp physical-dump' Qu Wenruo
2016-07-25 17:14   ` Goffredo Baroncelli
2016-07-26  1:32     ` Qu Wenruo
  -- strict thread matches above, loose matches on Subject: below --
2016-07-27 17:43 [BTRFS-PROGS][PATCH][V2] " Goffredo Baroncelli
2016-07-27 17:43 ` [PATCH 1/5] Add some helper functions Goffredo Baroncelli
2016-07-28  1:03   ` Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1469358209-9427-2-git-send-email-kreijack@libero.it \
    --to=kreijack@libero.it \
    --cc=clm@fb.com \
    --cc=dsterba@suse.cz \
    --cc=kreijack@inwind.it \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).