Skip to content

Commit

Permalink
mount: Add function to get the mount point given a path
Browse files Browse the repository at this point in the history
Given a path, the function returns the mount point to which
the path belongs. This is required for getting the device name
to which a file system path belongs.

Signed-off-by: Archana Shinde <[email protected]>
  • Loading branch information
amshinde committed Jun 21, 2017
1 parent f9c0d36 commit b1768e8
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -756,3 +756,69 @@ cc_is_blockdevice(uint major, uint minor)
return true;
}

/*!
* Get the mount point for a path.
*
* For eg. if a device is mounted at "/a/b", passing path
* "a/b/c" to this function should return "/a/b".
*
* \param path Path to get the mount point for.
*
* \return Newly-allocated string on success, else \c NULL.
*/
private gchar *
cc_mount_point_for_path(const gchar *path) {
int ret = -1;
gchar *mount_point = NULL;
gchar *parent_dir = NULL;
struct stat parent_stat, cur_stat;

if (! path) {
return NULL;
}

if ( *path != '/') {
g_warning("Absolute path not provided %s", path);
return NULL;
}

if (g_strcmp0(path, "/") == 0 ) {
return g_strdup(path);
}

if (stat(path, &cur_stat) == -1) {
g_warning("Could not stat %s: %s", path, strerror(errno));
return NULL;
}

mount_point = strdup(path);

while (g_strcmp0(mount_point, "/") != 0) {
parent_dir = g_path_get_dirname(mount_point);

ret = lstat(parent_dir, &parent_stat);
if ( ret == -1) {
g_free(parent_dir);
g_free(mount_point);
return NULL;
}

if (cur_stat.st_dev != parent_stat.st_dev) {
break;
}

g_free(mount_point);
mount_point = parent_dir;
cur_stat = parent_stat;
}

g_free(parent_dir);

if (g_strcmp0(mount_point, "/") == 0) {
g_free(mount_point);
return NULL;
}

return mount_point;
}

0 comments on commit b1768e8

Please sign in to comment.