VirtualBox

Changeset 30254 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Jun 16, 2010 2:37:24 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
62754
Message:

iprt: introduced RTFsQueryType()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/posix/fs-posix.cpp

    r28915 r30254  
    3232#include <sys/statvfs.h>
    3333#include <errno.h>
     34#include <stdio.h>
     35#ifdef RT_OS_LINUX
     36# include <mntent.h>
     37#endif
    3438
    3539#include <iprt/fs.h>
     
    163167}
    164168
     169
     170RTR3DECL(int) RTFsQueryType(const char *pszFsPath, uint32_t *pu32Type)
     171{
     172    /*
     173     * Validate input.
     174     */
     175    AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
     176
     177    /*
     178     * Convert the path and query the stats.
     179     * We're simply return the device id.
     180     */
     181    char const *pszNativeFsPath;
     182    int rc = rtPathToNative(&pszNativeFsPath, pszFsPath, NULL);
     183    if (RT_SUCCESS(rc))
     184    {
     185        *pu32Type = RTFS_FS_TYPE_UNKNOWN;
     186
     187        struct stat Stat;
     188        if (!stat(pszNativeFsPath, &Stat))
     189        {
     190#if defined(RT_OS_LINUX)
     191            FILE *mounted = setmntent("/proc/mounts", "r");
     192            if (!mounted)
     193                mounted = setmntent("/etc/mtab", "r");
     194            if (mounted)
     195            {
     196                char szBuf[1024];
     197                struct stat mntStat;
     198                struct mntent mntEnt;
     199                while (getmntent_r(mounted, &mntEnt, szBuf, sizeof(szBuf)))
     200                {
     201                    if (!stat(mntEnt.mnt_dir, &mntStat))
     202                    {
     203                        if (mntStat.st_dev == Stat.st_dev)
     204                        {
     205                            if (!strcmp("ext4", mntEnt.mnt_type))
     206                                *pu32Type = RTFS_FS_TYPE_EXT4;
     207                            else if (!strcmp("ext3", mntEnt.mnt_type))
     208                                *pu32Type = RTFS_FS_TYPE_EXT3;
     209                            else if (!strcmp("ext2", mntEnt.mnt_type))
     210                                *pu32Type = RTFS_FS_TYPE_EXT2;
     211                            else if (   !strcmp("vfat", mntEnt.mnt_type)
     212                                     || !strcmp("msdos", mntEnt.mnt_type))
     213                                *pu32Type = RTFS_FS_TYPE_FAT;
     214                            else if (!strcmp("tmpfs", mntEnt.mnt_type))
     215                                *pu32Type = RTFS_FS_TYPE_TMPFS;
     216                            else
     217                            {
     218                                /* sometimes there are more than one entry for the same partition */
     219                                continue;
     220                            }
     221                            break;
     222                        }
     223                    }
     224                }
     225                endmntent(mounted);
     226            }
     227#elif defined(RT_OS_SOLARIS)
     228            if (!strcmp("zfs", Stat.st_fstype))
     229                *pu32Type = RTFS_FS_TYPE_ZFS;
     230#endif
     231        }
     232        else
     233            rc = RTErrConvertFromErrno(errno);
     234        rtPathFreeNative(pszNativeFsPath, pszFsPath);
     235    }
     236
     237    return rc;
     238}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette