implement upwards path traversal to find fs type
authorPhil <n0-1@users.sourceforge.net>
Sat, 29 Mar 2008 11:09:47 +0000 (11:09 +0000)
committerPhil <n0-1@users.sourceforge.net>
Sat, 29 Mar 2008 11:09:47 +0000 (11:09 +0000)
* yes, this is a feature and right now there is something
  like a feature freeze.
* BUT I tested this with: /, /tmp, /dev/mapper, /dev, //,
  /mnt/, /tmp/bla (the last one was non-existent, creating
  it during runtime and even mounting tmpfs on it led to
  expected results.

git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky1@1074 7f574dfc-610e-0410-a909-a81674777703

src/fs.c

index 7ea5c59..650be32 100644 (file)
--- a/src/fs.c
+++ b/src/fs.c
@@ -138,10 +138,11 @@ void get_fs_type(const char *path, char *result)
 
 #else                          /* HAVE_STRUCT_STATFS_F_FSTYPENAME */
 
-       /* TODO: walk up the directory tree so it works on on paths that are not actually mount points. */
-
        struct mntent *me;
        FILE *mtab = setmntent("/etc/mtab", "r");
+       char *search_path;
+       int match;
+       char *slash;
 
        if (mtab == NULL) {
                ERR("setmntent /etc/mtab: %s", strerror(errno));
@@ -152,11 +153,28 @@ void get_fs_type(const char *path, char *result)
        me = getmntent(mtab);
 
        // find our path in the mtab
-       while (strcmp(path, me->mnt_dir) && getmntent(mtab));
+       search_path = strdup(path);
+       do {
+               while ((match = strcmp(search_path, me->mnt_dir))
+                               && getmntent(mtab));
+               if (!match)
+                       break;
+               fseek(mtab, 0, SEEK_SET);
+               slash = strrchr(search_path, '/');
+               if (slash == NULL)
+                       CRIT_ERR("invalid path '%s'", path);
+               if (strlen(slash) == 1)         /* trailing slash */
+                       *(slash) = '\0';
+               else if (strlen(slash) > 1)
+                       *(slash + 1) = '\0';
+               else
+                       CRIT_ERR("found a crack in the matrix!");
+       } while (strlen(search_path) > 0);
+       free(search_path);
 
        endmntent(mtab);
 
-       if (me && !strcmp(path, me->mnt_dir)) {
+       if (me && !match) {
                strncpy(result, me->mnt_type, DEFAULT_TEXT_BUFFER_SIZE);
                return;
        }