Patchwork [3,of,5] statfs: refactor inner function as a mapping from statfs to string

login
register
mail settings
Submitter Yuya Nishihara
Date March 25, 2017, 10:38 a.m.
Message ID <d4f6c6d3f77d40eff1eb.1490438329@mimosa>
Download mbox | patch
Permalink /patch/19664/
State Accepted
Headers show

Comments

Yuya Nishihara - March 25, 2017, 10:38 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1490430201 -32400
#      Sat Mar 25 17:23:21 2017 +0900
# Node ID d4f6c6d3f77d40eff1eb597642b10629d31ee1a7
# Parent  ca3ea70b04791670fa70eabc2531629b4d82d7a2
statfs: refactor inner function as a mapping from statfs to string

The major difference between BSD and Linux is how to get a fstype string.
Let's split the longest part of getfstype() as a pure function.

Patch

diff --git a/mercurial/osutil.c b/mercurial/osutil.c
--- a/mercurial/osutil.c
+++ b/mercurial/osutil.c
@@ -794,18 +794,15 @@  static PyObject *setprocname(PyObject *s
 }
 #endif /* ndef SETPROCNAME_USE_NONE */
 
-#if defined(HAVE_BSD_STATFS) || defined(HAVE_LINUX_STATFS)
-/* given a directory path and a zero-initialized statfs buffer, return
- * filesystem type name (best-effort), or NULL. */
-const char *getfstype(const char *path, struct statfs *pbuf) {
-	int r;
-	r = statfs(path, pbuf);
-	if (r != 0)
-		return NULL;
 #if defined(HAVE_BSD_STATFS)
+static const char *describefstype(const struct statfs *pbuf)
+{
 	/* BSD or OSX provides a f_fstypename field */
 	return pbuf->f_fstypename;
+}
 #elif defined(HAVE_LINUX_STATFS)
+static const char *describefstype(const struct statfs *pbuf)
+{
 	/* Begin of Linux filesystems */
 #ifdef ADFS_SUPER_MAGIC
 	if (pbuf->f_type == ADFS_SUPER_MAGIC)
@@ -1076,19 +1073,25 @@  const char *getfstype(const char *path, 
 		return "xfs";
 #endif
 	/* End of Linux filesystems */
-#endif /* def HAVE_LINUX_STATFS */
 	return NULL;
 }
+#endif /* def HAVE_LINUX_STATFS */
 
+#if defined(HAVE_BSD_STATFS) || defined(HAVE_LINUX_STATFS)
+/* given a directory path, return filesystem type name (best-effort) */
 static PyObject *pygetfstype(PyObject *self, PyObject *args)
 {
 	const char *path = NULL;
 	struct statfs buf;
+	int r;
 	if (!PyArg_ParseTuple(args, "s", &path))
 		return NULL;
 
 	memset(&buf, 0, sizeof(buf));
-	return Py_BuildValue("s", getfstype(path, &buf));
+	r = statfs(path, &buf);
+	if (r != 0)
+		Py_RETURN_NONE;
+	return Py_BuildValue("s", describefstype(&buf));
 }
 #endif /* defined(HAVE_LINUX_STATFS) || defined(HAVE_BSD_STATFS) */