文件安装在哪里? [英] Where is a file mounted?

查看:240
本文介绍了文件安装在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定文件或目录的路径,我如何确定该文件的安装点?例如,如果 / tmp 安装为 tmpfs 文件系统,则给出文件名 / tmp / foo / bar 我想知道它存储在 tmpfs 根源于 / tmp

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.

这将是在C ++中,我想避免通过 system()调用外部命令 。代码应该是鲁棒的 - 不一定反对故意篡改,但绝对面对嵌套的挂载点,符号链接等。

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. readlink shell命令。 如何?

  2. 使用 getmntent()读取 / etc / mtab code>&

  3. 确定文件的相应装入条目。 如何?

  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?

#1是一个简单的系统调用或者我需要读取每个目录组件的路径,并解决他们与 readlink(2)如果他们是符号链接?并处理 .. 自己?看起来像一个痛苦。

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.

对于#3我有如何做到这一点的各种想法。不确定哪个是最好的。

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


  1. open()其父项的父项等使用 openat(fd,..),直到我到达 / etc / mtab 条目。 (我如何知道我什么时候? fstat()他们并比较inode号?

  2. 在挂载表中查找最长的目录名,它是我的文件名的子串。

  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!

推荐答案

这是我想出的。事实证明,通常不需要遍历父目录。所有你需要做的是获取文件的设备号,然后找到具有相同设备号的相应挂载条目。

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;
}

感谢@RichardPennington在 (),并比较设备号,而不是inode号。

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

这篇关于文件安装在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆