[PATCH 1 of 2] statfs: change Linux feature detection

Jun Wu quark at fb.com
Fri Mar 24 22:13:25 UTC 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1490392759 25200
#      Fri Mar 24 14:59:19 2017 -0700
# Node ID 2afcc92acbfb675f7088242428a3bff718196651
# Parent  6359976b43be2024191a68a9605db9a378d2cce0
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 2afcc92acbfb
statfs: change Linux feature detection

Previously we check three things: "statfs" function, "linux/magic.h" and
"sys/vfs.h" headers. But we didn't check "struct statfs" or the "f_type"
field. That means if a system has "statfs" but "struct statfs" is not
defined in the two header files we check, or defined without the "f_type"
field, the compilation will fail.

This patch combines the checks (2 headers + 1 function + 1 field) together
and sets "HAVE_LINUX_STATFS". It makes setup.py faster (less checks), and
more reliable (immutable to the issue above).

diff --git a/mercurial/osutil.c b/mercurial/osutil.c
--- a/mercurial/osutil.c
+++ b/mercurial/osutil.c
@@ -25,6 +25,7 @@
 #include <sys/types.h>
 #include <unistd.h>
-#ifdef HAVE_LINUX_MAGIC_H
+#ifdef HAVE_LINUX_STATFS
 #include <linux/magic.h>
+#include <sys/vfs.h>
 #endif
 #ifdef HAVE_BSD_STATFS
@@ -32,7 +33,4 @@
 #include <sys/param.h>
 #endif
-#ifdef HAVE_SYS_VFS_H
-#include <sys/vfs.h>
-#endif
 #endif
 
@@ -797,5 +795,5 @@ static PyObject *setprocname(PyObject *s
 #endif /* ndef SETPROCNAME_USE_NONE */
 
-#ifdef HAVE_STATFS
+#if defined(HAVE_BSD_STATFS) || defined(HAVE_LINUX_STATFS)
 /* given a directory path, return filesystem type (best-effort), or None */
 const char *getfstype(const char *path) {
@@ -811,8 +809,8 @@ const char *getfstype(const char *path) 
 	if (r != 0)
 		return NULL;
-#ifdef HAVE_BSD_STATFS
+#if defined(HAVE_BSD_STATFS)
 	/* BSD or OSX provides a f_fstypename field */
 	return buf.f_fstypename;
-#endif
+#elif defined(HAVE_LINUX_STATFS)
 	/* Begin of Linux filesystems */
 #ifdef ADFS_SUPER_MAGIC
@@ -1085,4 +1083,5 @@ const char *getfstype(const char *path) 
 #endif
 	/* End of Linux filesystems */
+#endif /* def HAVE_LINUX_STATFS */
 	return NULL;
 }
@@ -1101,5 +1100,5 @@ static PyObject *pygetfstype(PyObject *s
 	return result;
 }
-#endif /* def HAVE_STATFS */
+#endif /* defined(HAVE_LINUX_STATFS) || defined(HAVE_BSD_STATFS) */
 
 #endif /* ndef _WIN32 */
@@ -1279,5 +1278,5 @@ static PyMethodDef methods[] = {
 	 "set process title (best-effort)\n"},
 #endif
-#ifdef HAVE_STATFS
+#if defined(HAVE_BSD_STATFS) || defined(HAVE_LINUX_STATFS)
 	{"getfstype", (PyCFunction)pygetfstype, METH_VARARGS,
 	 "get filesystem type (best-effort)\n"},
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -592,16 +592,8 @@ osutil_ldflags = []
 
 # platform specific macros
-for plat, func in [('bsd', 'setproctitle'), ('bsd|darwin|linux', 'statfs')]:
+for plat, func in [('bsd', 'setproctitle')]:
     if re.search(plat, sys.platform) and hasfunction(new_compiler(), func):
         osutil_cflags.append('-DHAVE_%s' % func.upper())
 
-for plat, header in [
-    ('linux', 'linux/magic.h'),
-    ('linux', 'sys/vfs.h'),
-]:
-    if re.search(plat, sys.platform) and hasheader(new_compiler(), header):
-        macro = header.replace('/', '_').replace('.', '_').upper()
-        osutil_cflags.append('-DHAVE_%s' % macro)
-
 for plat, macro, code in [
     ('bsd|darwin', 'BSD_STATFS', '''
@@ -610,4 +602,9 @@ for plat, macro, code in [
      int main() { struct statfs s; return sizeof(s.f_fstypename); }
      '''),
+    ('linux', 'LINUX_STATFS', '''
+     #include <linux/magic.h>
+     #include <sys/vfs.h>
+     int main() { struct statfs s; return sizeof(s.f_type); }
+     '''),
 ]:
     if re.search(plat, sys.platform) and cancompile(new_compiler(), code):


More information about the Mercurial-devel mailing list