@@ -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);
+}
@@ -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