Pablo Greco 48fc63
From 5d73882b52d92e38b39ec84255e4b44ccbc4b3f8 Mon Sep 17 00:00:00 2001
Pablo Greco 48fc63
From: David Tardon <dtardon@redhat.com>
Pablo Greco 48fc63
Date: Thu, 3 Jan 2019 13:04:10 +0100
Pablo Greco 48fc63
Subject: [PATCH] backport fd_is_fs_type
Pablo Greco 48fc63
Pablo Greco 48fc63
Related: #1663143
Pablo Greco 48fc63
---
Pablo Greco 48fc63
 src/shared/util.c | 16 ++++++++++++++++
Pablo Greco 48fc63
 src/shared/util.h |  8 ++++++++
Pablo Greco 48fc63
 2 files changed, 24 insertions(+)
Pablo Greco 48fc63
Pablo Greco 48fc63
diff --git a/src/shared/util.c b/src/shared/util.c
Pablo Greco 48fc63
index 4ba4693668..2838d50f6f 100644
Pablo Greco 48fc63
--- a/src/shared/util.c
Pablo Greco 48fc63
+++ b/src/shared/util.c
Pablo Greco 48fc63
@@ -9186,3 +9186,19 @@ int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout) {
Pablo Greco 48fc63
 
Pablo Greco 48fc63
         return -EPROTO;
Pablo Greco 48fc63
 }
Pablo Greco 48fc63
+
Pablo Greco 48fc63
+bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) {
Pablo Greco 48fc63
+        assert(s);
Pablo Greco 48fc63
+        assert_cc(sizeof(statfs_f_type_t) >= sizeof(s->f_type));
Pablo Greco 48fc63
+
Pablo Greco 48fc63
+        return F_TYPE_EQUAL(s->f_type, magic_value);
Pablo Greco 48fc63
+}
Pablo Greco 48fc63
+
Pablo Greco 48fc63
+int fd_is_fs_type(int fd, statfs_f_type_t magic_value) {
Pablo Greco 48fc63
+        struct statfs s;
Pablo Greco 48fc63
+
Pablo Greco 48fc63
+        if (fstatfs(fd, &s) < 0)
Pablo Greco 48fc63
+                return -errno;
Pablo Greco 48fc63
+
Pablo Greco 48fc63
+        return is_fs_type(&s, magic_value);
Pablo Greco 48fc63
+}
Pablo Greco 48fc63
diff --git a/src/shared/util.h b/src/shared/util.h
Pablo Greco 48fc63
index 8fc237495a..f768936ab1 100644
Pablo Greco 48fc63
--- a/src/shared/util.h
Pablo Greco 48fc63
+++ b/src/shared/util.h
Pablo Greco 48fc63
@@ -36,6 +36,7 @@
Pablo Greco 48fc63
 #include <limits.h>
Pablo Greco 48fc63
 #include <sys/types.h>
Pablo Greco 48fc63
 #include <sys/stat.h>
Pablo Greco 48fc63
+#include <sys/statfs.h>
Pablo Greco 48fc63
 #include <dirent.h>
Pablo Greco 48fc63
 #include <sys/resource.h>
Pablo Greco 48fc63
 #include <stddef.h>
Pablo Greco 48fc63
@@ -1147,3 +1148,10 @@ static inline void block_signals_reset(sigset_t *ss) {
Pablo Greco 48fc63
 
Pablo Greco 48fc63
 char* set_iovec_string_field(struct iovec *iovec, unsigned int *n_iovec, const char *field, const char *value);
Pablo Greco 48fc63
 char* set_iovec_field_free(struct iovec *iovec, unsigned int *n_iovec, const char *field, char *value);
Pablo Greco 48fc63
+
Pablo Greco 48fc63
+/* The .f_type field of struct statfs is really weird defined on
Pablo Greco 48fc63
+ * different archs. Let's give its type a name. */
Pablo Greco 48fc63
+typedef typeof(((struct statfs*)NULL)->f_type) statfs_f_type_t;
Pablo Greco 48fc63
+
Pablo Greco 48fc63
+bool is_fs_type(const struct statfs *s, statfs_f_type_t magic_value) _pure_;
Pablo Greco 48fc63
+int fd_is_fs_type(int fd, statfs_f_type_t magic_value);