# HG changeset patch # User Yuya Nishihara # Date 2017-03-25 08:23:21 # Node ID f7aeb1f4b11028e39c1f778174eae2bf63528006 # Parent da191ba2576f5d7c5f3ae01efb5f02dddc0d8a32 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. 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) @@ -1092,19 +1089,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) */