summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-06-24 09:00:15 +0200
committerThomas Haller <thaller@redhat.com>2019-06-26 09:53:54 +0200
commit02ac5693d398519af42d59c3fc151cd692f6906e (patch)
treed7aacb38d7622ee736c2e72f26cb9a69480406ba /shared
parentec707f56c1ea951b808a90a595019de021b092d9 (diff)
downloadNetworkManager-02ac5693d398519af42d59c3fc151cd692f6906e.tar.gz
shared: add nm_utils_file_stat() util
A small convenience function to call stat(). The difference is that the function returns an error code.
Diffstat (limited to 'shared')
-rw-r--r--shared/nm-glib-aux/nm-io-utils.c23
-rw-r--r--shared/nm-glib-aux/nm-io-utils.h4
2 files changed, 27 insertions, 0 deletions
diff --git a/shared/nm-glib-aux/nm-io-utils.c b/shared/nm-glib-aux/nm-io-utils.c
index 7b9ca0218a..23133ec568 100644
--- a/shared/nm-glib-aux/nm-io-utils.c
+++ b/shared/nm-glib-aux/nm-io-utils.c
@@ -436,3 +436,26 @@ nm_utils_file_set_contents (const char *filename,
return TRUE;
}
+
+/**
+ * nm_utils_file_stat:
+ * @filename: the filename to stat.
+ * @out_st: (allow-none) (out): if given, this will be passed to stat().
+ *
+ * Just wraps stat() and gives the errno number as function result instead
+ * of setting the errno (though, errno is also set). It's only for convenience
+ * with
+ *
+ * if (nm_utils_file_stat (filename, NULL) == -ENOENT) {
+ * }
+ *
+ * Returns: 0 on success a negative errno on failure. */
+int
+nm_utils_file_stat (const char *filename, struct stat *out_st)
+{
+ struct stat st;
+
+ if (stat (filename, out_st ?: &st) != 0)
+ return -NM_ERRNO_NATIVE (errno);
+ return 0;
+}
diff --git a/shared/nm-glib-aux/nm-io-utils.h b/shared/nm-glib-aux/nm-io-utils.h
index 6037242b02..121fc481d9 100644
--- a/shared/nm-glib-aux/nm-io-utils.h
+++ b/shared/nm-glib-aux/nm-io-utils.h
@@ -59,4 +59,8 @@ gboolean nm_utils_file_set_contents (const char *filename,
mode_t mode,
GError **error);
+struct stat;
+
+int nm_utils_file_stat (const char *filename, struct stat *out_st);
+
#endif /* __NM_IO_UTILS_H__ */