Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 632493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:04:58+00:00 2026-05-13T20:04:58+00:00

Given a path to a file or directory, how can I determine the mount

  • 0

Given a path to a file or directory, how can I determine the mount point for that file? For example, if /tmp is mounted as a tmpfs filesystem then given the file name /tmp/foo/bar I want to know that it’s stored on a tmpfs rooted at /tmp.

This will be in C++ and I’d like to avoid invoking external commands via system(). The code should be robust–not necessarily against deliberate tampering but definitely in the face of nested mountpoints, symlinks, etc.

I haven’t been able to find a simple system call to do this. It looks like I’ll have to write the check myself. Here’s a rough outline of what I’m planning.

  1. Canonicalize the file name a la the readlink shell command. How?
  2. Read /etc/mtab with getmntent() & co.
  3. Determine the corresponding mount entry for the file. How?

For #1 is there a simple system call or do I need to read each directory component of the path and resolve them with readlink(2) if they are symlinks? And handle . and .. myself? Seems like a pain.

For #3 I’ve got various ideas on how to do this. Not sure which is best.

  1. open() the file, its parent, its parent’s parent, etc. using openat(fd, "..") until I reach one of the /etc/mtab entries. (How do I know when I do? fstat() them and compare the inode numbers?)
  2. Find the longest directory name in the mount table which is a substring of my file name.

I’m leaning towards the first option but before I code it all up I want to make sure I’m not overlooking anything–ideally a built-in function that does this already!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:04:58+00:00Added an answer on May 13, 2026 at 8:04 pm

    This is what I’ve come up with. It turns out there’s usually no need to iterate through the parent directories. All you have to do is get the file’s device number and then find the corresponding mount entry with the same device number.

    struct mntent *mountpoint(char *filename, struct mntent *mnt, char *buf, size_t buflen)
    {
        struct stat s;
        FILE *      fp;
        dev_t       dev;
    
        if (stat(filename, &s) != 0) {
            return NULL;
        }
    
        dev = s.st_dev;
    
        if ((fp = setmntent("/proc/mounts", "r")) == NULL) {
            return NULL;
        }
    
        while (getmntent_r(fp, mnt, buf, buflen)) {
            if (stat(mnt->mnt_dir, &s) != 0) {
                continue;
            }
    
            if (s.st_dev == dev) {
                endmntent(fp);
                return mnt;
            }
        }
    
        endmntent(fp);
    
        // Should never reach here.
        errno = EINVAL;
        return NULL;
    }
    

    Thanks to @RichardPennington for the heads up on realpath(), and on comparing device numbers instead of inode numbers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.