]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/, src/: Move statfs(2) call out of is_btrfs()
authorAlejandro Colomar <alx@kernel.org>
Wed, 22 Apr 2026 09:14:20 +0000 (11:14 +0200)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Thu, 23 Apr 2026 12:17:18 +0000 (14:17 +0200)
This simplifies the return value of is_btrfs() into a boolean.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/btrfs.c
lib/prototypes.h
src/useradd.c

index fe20cf306acf9987349c383996cbdb65754c4a39..065c79387cdbbd94a2f50414363b3190299ab9cc 100644 (file)
@@ -60,12 +60,13 @@ int btrfs_remove_subvolume(const char *path)
  */
 int btrfs_is_subvolume(const char *path)
 {
-       struct stat st;
-       int ret;
+       struct stat    st;
+       struct statfs  sfs;
 
-       ret = is_btrfs(path);
-       if (ret <= 0)
-               return ret;
+       if (statfs(path, &sfs) == -1)
+               return -1;
+       if (!is_btrfs(&sfs))
+               return 0;
 
        if (stat(path, &st) == -1)
                return -1;
@@ -78,16 +79,8 @@ int btrfs_is_subvolume(const char *path)
 }
 
 
-/* Adapted from btrfsprogs */
-int is_btrfs(const char *path)
+bool
+is_btrfs(const struct statfs *sfs)
 {
-       struct statfs sfs;
-       int ret;
-
-       ret = statfs(path, &sfs);
-       if (ret == -1)
-               return -1;
-
-       return sfs.f_type == BTRFS_SUPER_MAGIC;
+       return sfs->f_type == BTRFS_SUPER_MAGIC;
 }
-
index 9a03e312507947145e607aaa8ae6692e1d45f21f..42aa29230543479ffc70054c735c30b850d20a0e 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/statfs.h>
 #include <sys/types.h>
 #include <pwd.h>
 #include <grp.h>
@@ -54,7 +55,7 @@ extern int isexpired (const struct passwd *, /*@null@*/const struct spwd *);
 extern int btrfs_create_subvolume(const char *path);
 extern int btrfs_remove_subvolume(const char *path);
 extern int btrfs_is_subvolume(const char *path);
-extern int is_btrfs(const char *path);
+extern bool is_btrfs(const struct statfs *sfs);
 #endif
 
 /* basename() renamed to Basename() to avoid libc name space confusion */
index df679d287d0d552056f2611c2dd8774ab8a68bc5..5ce6cd6a2f0cf7ee47047c4aabfe36617f27f5b5 100644 (file)
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <sys/statfs.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <time.h>
@@ -2265,6 +2266,7 @@ static void create_home(const struct option_flags *flags)
 #if WITH_BTRFS
                if (subvolflg && (strlen(prefix_user_home) - (int)strlen(path)) <= 1) {
                        char *btrfs_check = strdup(path);
+                       struct statfs  sfs;
 
                        if (!btrfs_check) {
                                fprintf(stderr,
@@ -2273,13 +2275,18 @@ static void create_home(const struct option_flags *flags)
                                fail_exit(E_HOMEDIR, process_selinux);
                        }
                        stpcpy(&btrfs_check[strlen(path) - strlen(cp) - 1], "");
-                       if (is_btrfs(btrfs_check) <= 0) {
+                       if (statfs(btrfs_check, &sfs) == -1) {
+                               fprintf(stderr, "%s: statfs(\"%s\"): %s\n",
+                                       Prog, btrfs_check, strerrno());
+                               fail_exit(E_HOMEDIR, process_selinux);
+                       }
+                       free(btrfs_check);
+                       if (!is_btrfs(&sfs)) {
                                fprintf(stderr,
                                        _("%s: home directory \"%s\" must be mounted on BTRFS\n"),
                                        Prog, path);
                                fail_exit(E_HOMEDIR, process_selinux);
                        }
-                       free(btrfs_check);
                        // make subvolume to mount for user instead of directory
                        if (btrfs_create_subvolume(path)) {
                                fprintf(stderr,