Patchwork [3,of,5] osutil: add a function to show the mount point of the filesystem

login
register
mail settings
Submitter Matt Harbison
Date Dec. 30, 2017, 6:37 a.m.
Message ID <6cfb98df0f014d6ca3b1.1514615852@Envy>
Download mbox | patch
Permalink /patch/26503/
State Accepted, archived
Headers show

Comments

Matt Harbison - Dec. 30, 2017, 6:37 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1514606054 18000
#      Fri Dec 29 22:54:14 2017 -0500
# Node ID 6cfb98df0f014d6ca3b1d80f26ae0744a2c1c1c8
# Parent  32d2484a8565412b23dcb3eda14f5470c9632d13
osutil: add a function to show the mount point of the filesystem

For now, this is Windows only, since Linux doesn't have the value in its statfs
structure, and I don't have a BSD system to test with.  I assume since the
version was just incremented yesterday, it doesn't have to be incremented again.

Patch

diff --git a/mercurial/cext/osutil.c b/mercurial/cext/osutil.c
--- a/mercurial/cext/osutil.c
+++ b/mercurial/cext/osutil.c
@@ -1316,6 +1316,20 @@ 
 	return NULL;
 }
 
+static PyObject *getfsmountpoint(PyObject *self, PyObject *args)
+/* given a directory path, return filesystem mount point (best-effort) */
+{
+	PyObject *val = NULL;
+	char *volume = getvolumepath(args);
+
+	if (!volume)
+		return NULL;
+
+	val = Py_BuildValue("s", volume);
+	free(volume);
+	return val;
+}
+
 static PyObject *getfstype(PyObject *self, PyObject *args)
 /* given a directory path, return filesystem type name (best-effort) */
 {
@@ -1402,6 +1416,11 @@ 
 	{"getfstype", (PyCFunction)getfstype, METH_VARARGS,
 	 "get filesystem type (best-effort)\n"},
 #endif
+#if defined(_WIN32)
+	{"getfsmountpoint", (PyCFunction)getfsmountpoint, METH_VARARGS,
+	 "get filesystem mount point (best-effort)\n"},
+#endif
+
 #ifdef __APPLE__
 	{
 		"isgui", (PyCFunction)isgui, METH_NOARGS,
diff --git a/mercurial/pure/osutil.py b/mercurial/pure/osutil.py
--- a/mercurial/pure/osutil.py
+++ b/mercurial/pure/osutil.py
@@ -157,6 +157,7 @@ 
     from .. import win32
 
     getfstype = win32.getfstype
+    getfsmountpoint = win32.getvolumename
 
     _kernel32 = ctypes.windll.kernel32
 
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1520,6 +1520,13 @@ 
 
     return ''.join(result)
 
+def getfsmountpoint(dirpath):
+    '''Get the filesystem mount point from a directory (best-effort)
+
+    Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
+    '''
+    return getattr(osutil, 'getfsmountpoint', lambda x: None)(dirpath)
+
 def getfstype(dirpath):
     '''Get the filesystem type name from a directory (best-effort)