2 * Copyright (C) 2002-2015 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2008-2009 David Robillard <d@drobilla.net>
4 * Copyright (C) 2013 John Emmas <john@creativepost.co.uk>
5 * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "pbd/mountpoint.h"
33 #include "libpbd-config.h"
39 struct mntent_sorter {
40 bool operator() (const mntent *a, const mntent *b) {
41 return strcmp (a->mnt_dir, b->mnt_dir);
46 mountpoint (string path)
50 unsigned int maxmatch = 0;
51 unsigned int matchlen;
52 const char *cpath = path.c_str();
53 char best[PATH_MAX+1];
55 if ((mntf = setmntent ("/etc/mtab", "r")) == 0) {
61 while ((mnt = getmntent (mntf))) {
67 /* note: strcmp's semantics are not
68 strict enough to use for this.
71 while (cpath[n] && mnt->mnt_dir[n]) {
72 if (cpath[n] != mnt->mnt_dir[n]) {
79 if (cpath[matchlen] == '\0') {
86 if (matchlen > maxmatch) {
87 snprintf (best, sizeof(best), "%s", mnt->mnt_dir);
98 #elif defined(PLATFORM_WINDOWS)
101 mountpoint (string path)
103 /* this function is currently only called from 'old_peak_path()'
104 * via find_broken_peakfile() - only relevant for loading pre
105 * libsndfile Ardour 2.0 sessions.
108 return ""; // TODO ... if needed
111 #else // !HAVE_GETMNTENT
113 #include <sys/param.h>
114 #include <sys/ucred.h>
115 #include <sys/mount.h>
118 mountpoint (string path)
120 #if defined(__NetBSD__)
121 struct statvfs *mntbufp = 0;
123 struct statfs *mntbufp = 0;
126 unsigned int maxmatch = 0;
127 unsigned int matchlen;
128 const char *cpath = path.c_str();
129 char best[PATH_MAX+1];
131 /* From the manpage, under "BUGS" : "The memory allocated by getmntinfo() cannot be free(3)'d by the
134 Thus: we do NOT try to free memory allocated by getmntinfo()
137 if ((count = getmntinfo(&mntbufp, MNT_NOWAIT)) == 0) {
143 for (int i = 0; i < count; ++i) {
147 /* note: strcmp's semantics are not
148 strict enough to use for this.
151 while (cpath[n] && mntbufp[i].f_mntonname[n]) {
152 if (cpath[n] != mntbufp[i].f_mntonname[n]) {
159 if (cpath[matchlen] == '\0') {
160 snprintf(best, sizeof(best), "%s", mntbufp[i].f_mntonname);
165 if (matchlen > maxmatch) {
166 snprintf (best, sizeof(best), "%s", mntbufp[i].f_mntonname);
174 #endif // HAVE_GETMNTENT
176 #ifdef TEST_MOUNTPOINT
178 main (int argc, char *argv[])
180 printf ("mp of %s = %s\n", argv[1], mountpoint (argv[1]).c_str());
184 #endif // TEST_MOUNTPOINT
186 #else // COMPILER_MSVC
187 const char* pbd_mountpoint = "pbd/msvc/mountpoint.cc takes precedence over this file";
188 #endif // COMPILER_MSVC