From: Karel Zak Date: Wed, 28 Jul 2010 13:16:49 +0000 (+0200) Subject: libmount: improve mnt_get_fs_root() and mnt_get_mountpoint() X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9758c88ad19c7aa27d2077738729afcbce58bfc3;p=util-linux libmount: improve mnt_get_fs_root() and mnt_get_mountpoint() Signed-off-by: Karel Zak --- diff --git a/shlibs/mount/src/mountP.h b/shlibs/mount/src/mountP.h index 59ffb94e..ea6a87c5 100644 --- a/shlibs/mount/src/mountP.h +++ b/shlibs/mount/src/mountP.h @@ -37,6 +37,8 @@ #define DEBUG_OPTIONS (1 << 3) #define DEBUG_LOCKS (1 << 4) #define DEBUG_TAB (1 << 5) +#define DEBUG_MTAB (1 << 6) +#define DEBUG_UTILS (1 << 7) #define DEBUG_ALL 0xFFFF #ifdef CONFIG_LIBMOUNT_DEBUG @@ -74,7 +76,7 @@ extern char *mnt_get_username(const uid_t uid); extern int mnt_has_regular_mtab(void); extern char *mnt_get_mountpoint(const char *path); -extern char *mnt_get_fs_root(const char *path); +extern char *mnt_get_fs_root(const char *path, const char *mountpoint); /* * Generic iterator diff --git a/shlibs/mount/src/utils.c b/shlibs/mount/src/utils.c index da2799b5..55e06058 100644 --- a/shlibs/mount/src/utils.c +++ b/shlibs/mount/src/utils.c @@ -381,7 +381,7 @@ char *mnt_get_mountpoint(const char *path) if (!mnt) return NULL; if (*mnt == '/' && *(mnt + 1) == '\0') - return mnt; /* root fs */ + goto done; if (stat(mnt, &st)) goto err; @@ -397,31 +397,37 @@ char *mnt_get_mountpoint(const char *path) dir = st.st_dev; if (dir != base) { *(p - 1) = '/'; - return mnt; + goto done; } base = dir; } while (mnt && *(mnt + 1) != '\0'); memcpy(mnt, "/", 2); - return mnt; /* root fs */ +done: + DBG(DEBUG_UTILS, fprintf(stderr, + "libmount: utils: fs-root for %s is %s\n", path, mnt)); + return mnt; err: free(mnt); return NULL; } -char *mnt_get_fs_root(const char *path) +char *mnt_get_fs_root(const char *path, const char *mnt) { - char *mnt = mnt_get_mountpoint(path); + char *m = (char *) mnt; const char *p; size_t sz; - if (!mnt) + if (!m) + m = mnt_get_mountpoint(path); + if (!m) return NULL; - sz = strlen(mnt); + sz = strlen(m); p = sz > 1 ? path + sz : path; - free(mnt); + if (m != mnt) + free(m); return *p ? strdup(p) : strdup("/"); }