VirtualBox

Changeset 30365 in vbox for trunk/src/VBox/Runtime/r3


Ignore:
Timestamp:
Jun 22, 2010 12:08:20 PM (15 years ago)
Author:
vboxsync
Message:

RTFsQueryType: Use an enum. Added RTFsTypeName() for translating a enum value into a string. Added more file system types.

Location:
trunk/src/VBox/Runtime/r3
Files:
3 edited

Legend:

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

    r28800 r30365  
    55
    66/*
    7  * Copyright (C) 2006-2007 Oracle Corporation
     7 * Copyright (C) 2006-2010 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    4040
    4141#include <iprt/fs.h>
     42#include "internal/iprt.h"
     43
     44#include <iprt/asm.h>
    4245#include <iprt/assert.h>
     46#include <iprt/ctype.h>
     47#include <iprt/path.h>
     48#include <iprt/string.h>
    4349#include <iprt/time.h>
    44 #include <iprt/string.h>
    45 #include <iprt/path.h>
    46 #include <iprt/ctype.h>
    4750#include "internal/fs.h"
    4851
     
    292295    pObjInfo->Attr.u.Unix.Device          = pStat->st_rdev;
    293296}
    294 
    295297#endif /* !RT_OS_WINDOWS */
     298
     299
     300RTDECL(const char *) RTFsTypeName(RTFSTYPE enmType)
     301{
     302    switch (enmType)
     303    {
     304        case RTFSTYPE_UNKNOWN:      return "unknown";
     305        case RTFSTYPE_UDF:          return "udf";
     306        case RTFSTYPE_ISO9660:      return "iso9660";
     307        case RTFSTYPE_FUSE:         return "fuse";
     308        case RTFSTYPE_VBOXSHF:      return "vboxshf";
     309
     310        case RTFSTYPE_EXT:          return "ext";
     311        case RTFSTYPE_EXT2:         return "ext2";
     312        case RTFSTYPE_EXT3:         return "ext3";
     313        case RTFSTYPE_EXT4:         return "ext4";
     314        case RTFSTYPE_XFS:          return "xfs";
     315        case RTFSTYPE_CIFS:         return "cifs";
     316        case RTFSTYPE_SMBFS:        return "smbfs";
     317        case RTFSTYPE_TMPFS:        return "tmpfs";
     318        case RTFSTYPE_SYSFS:        return "sysfs";
     319        case RTFSTYPE_PROC:         return "proc";
     320
     321        case RTFSTYPE_NTFS:         return "ntfs";
     322        case RTFSTYPE_FAT:          return "fat";
     323
     324        case RTFSTYPE_ZFS:          return "zfs";
     325        case RTFSTYPE_UFS:          return "ufs";
     326        case RTFSTYPE_NFS:          return "nfs";
     327
     328        case RTFSTYPE_HFS:          return "hfs";
     329        case RTFSTYPE_AUTOFS:       return "autofs";
     330        case RTFSTYPE_DEVFS:        return "devfs";
     331
     332        case RTFSTYPE_HPFS:         return "hpfs";
     333        case RTFSTYPE_JFS:          return "jfs";
     334
     335        case RTFSTYPE_END:          return "end";
     336        case RTFSTYPE_32BIT_HACK:   break;
     337    }
     338
     339    /* Don't put this in as 'default:', we wish GCC to warn about missing cases. */
     340    static char                 s_asz[4][64];
     341    static uint32_t volatile    s_i = 0;
     342    uint32_t i = ASMAtomicIncU32(&s_i) % RT_ELEMENTS(s_asz);
     343    RTStrPrintf(s_asz[i], sizeof(s_asz[i]), "type=%d", enmType);
     344    return s_asz[i];
     345}
     346
  • trunk/src/VBox/Runtime/r3/posix/fs-posix.cpp

    r30324 r30365  
    171171
    172172
    173 RTR3DECL(int) RTFsQueryType(const char *pszFsPath, uint32_t *pu32Type)
    174 {
     173RTR3DECL(int) RTFsQueryType(const char *pszFsPath, PRTFSTYPE penmType)
     174{
     175    *penmType = RTFSTYPE_UNKNOWN;
     176
    175177    /*
    176178     * Validate input.
    177179     */
    178     AssertMsgReturn(VALID_PTR(pszFsPath) && *pszFsPath, ("%p", pszFsPath), VERR_INVALID_PARAMETER);
     180    AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
     181    AssertReturn(*pszFsPath, VERR_INVALID_PARAMETER);
    179182
    180183    /*
     
    186189    if (RT_SUCCESS(rc))
    187190    {
    188         *pu32Type = RTFS_FS_TYPE_UNKNOWN;
    189 
    190191        struct stat Stat;
    191192        if (!stat(pszNativeFsPath, &Stat))
     
    197198            if (mounted)
    198199            {
    199                 char szBuf[1024];
    200                 struct stat mntStat;
    201                 struct mntent mntEnt;
     200                char            szBuf[1024];
     201                struct stat     mntStat;
     202                struct mntent   mntEnt;
    202203                while (getmntent_r(mounted, &mntEnt, szBuf, sizeof(szBuf)))
    203204                {
     
    207208                        {
    208209                            if (!strcmp("ext4", mntEnt.mnt_type))
    209                                 *pu32Type = RTFS_FS_TYPE_EXT4;
     210                                *penmType = RTFSTYPE_EXT4;
    210211                            else if (!strcmp("ext3", mntEnt.mnt_type))
    211                                 *pu32Type = RTFS_FS_TYPE_EXT3;
     212                                *penmType = RTFSTYPE_EXT3;
    212213                            else if (!strcmp("ext2", mntEnt.mnt_type))
    213                                 *pu32Type = RTFS_FS_TYPE_EXT2;
     214                                *penmType = RTFSTYPE_EXT2;
     215                            else if (!strcmp("jfs", mntEnt.mnt_type))
     216                                *penmType = RTFSTYPE_JFS;
    214217                            else if (!strcmp("xfs", mntEnt.mnt_type))
    215                                 *pu32Type = RTFS_FS_TYPE_XFS;
     218                                *penmType = RTFSTYPE_XFS;
    216219                            else if (   !strcmp("vfat", mntEnt.mnt_type)
    217220                                     || !strcmp("msdos", mntEnt.mnt_type))
    218                                 *pu32Type = RTFS_FS_TYPE_FAT;
     221                                *penmType = RTFSTYPE_FAT;
     222                            else if (!strcmp("ntfs", mntEnt.mnt_type))
     223                                *penmType = RTFSTYPE_NTFS;
     224                            else if (!strcmp("hpfs", mntEnt.mnt_type))
     225                                *penmType = RTFSTYPE_HPFS;
     226                            else if (!strcmp("ufs", mntEnt.mnt_type))
     227                                *penmType = RTFSTYPE_UFS;
    219228                            else if (!strcmp("tmpfs", mntEnt.mnt_type))
    220                                 *pu32Type = RTFS_FS_TYPE_TMPFS;
     229                                *penmType = RTFSTYPE_TMPFS;
    221230                            else if (!strcmp("hfsplus", mntEnt.mnt_type))
    222                                 *pu32Type = RTFS_FS_TYPE_HFS;
     231                                *penmType = RTFSTYPE_HFS;
     232                            else if (!strcmp("udf", mntEnt.mnt_type))
     233                                *penmType = RTFSTYPE_UDF;
     234                            else if (!strcmp("iso9660", mntEnt.mnt_type))
     235                                *penmType = RTFSTYPE_ISO9660;
     236                            else if (!strcmp("smbfs", mntEnt.mnt_type))
     237                                *penmType = RTFSTYPE_SMBFS;
     238                            else if (!strcmp("cifs", mntEnt.mnt_type))
     239                                *penmType = RTFSTYPE_CIFS;
     240                            else if (!strcmp("nfs", mntEnt.mnt_type))
     241                                *penmType = RTFSTYPE_NFS;
     242                            else if (!strcmp("nfs4", mntEnt.mnt_type))
     243                                *penmType = RTFSTYPE_NFS;
     244                            else if (!strcmp("sysfs", mntEnt.mnt_type))
     245                                *penmType = RTFSTYPE_SYSFS;
     246                            else if (!strcmp("proc", mntEnt.mnt_type))
     247                                *penmType = RTFSTYPE_PROC;
     248                            else if (   !strcmp("fuse", mntEnt.mnt_type)
     249                                     || !strncmp("fuse.", mntEnt.mnt_type, 5))
     250                                *penmType = RTFSTYPE_FUSE;
    223251                            else
    224252                            {
     
    232260                endmntent(mounted);
    233261            }
     262
    234263#elif defined(RT_OS_SOLARIS)
    235264            if (!strcmp("zfs", Stat.st_fstype))
    236                 *pu32Type = RTFS_FS_TYPE_ZFS;
     265                *penmType = RTFSTYPE_ZFS;
     266            else if (!strcmp("ufs", Stat.st_fstype))
     267                *penmType = RTFSTYPE_UFS;
     268            else if (!strcmp("nfs", Stat.st_fstype))
     269                *penmType = RTFSTYPE_NFS;
     270
    237271#elif defined(RT_OS_DARWIN)
    238272            struct statfs statfsBuf;
     
    240274            {
    241275                if (!strcmp("hfs", statfsBuf.f_fstypename))
    242                     *pu32Type = RTFS_FS_TYPE_HFS;
     276                    *penmType = RTFSTYPE_HFS;
    243277                else if (   !strcmp("fat", statfsBuf.f_fstypename)
    244278                         || !strcmp("msdos", statfsBuf.f_fstypename))
    245                     *pu32Type = RTFS_FS_TYPE_FAT;
     279                    *penmType = RTFSTYPE_FAT;
    246280                else if (!strcmp("ntfs", statfsBuf.f_fstypename))
    247                     *pu32Type = RTFS_FS_TYPE_NTFS;
     281                    *penmType = RTFSTYPE_NTFS;
    248282                else if (!strcmp("autofs", statfsBuf.f_fstypename))
    249                     *pu32Type = RTFS_FS_TYPE_AUTOFS;
     283                    *penmType = RTFSTYPE_AUTOFS;
    250284                else if (!strcmp("devfs", statfsBuf.f_fstypename))
    251                     *pu32Type = RTFS_FS_TYPE_DEVFS;
     285                    *penmType = RTFSTYPE_DEVFS;
    252286                else if (!strcmp("nfs", statfsBuf.f_fstypename))
    253                     *pu32Type = RTFS_FS_TYPE_NFS;
     287                    *penmType = RTFSTYPE_NFS;
    254288            }
    255289            else
  • trunk/src/VBox/Runtime/r3/win/fs-win.cpp

    r30279 r30365  
    334334}
    335335
    336 RTR3DECL(int) RTFsQueryType(const char *pszFsPath, uint32_t *pu32Type)
    337 {
    338     int rc = VINF_SUCCESS;
    339 
    340     *pu32Type = RTFS_FS_TYPE_UNKNOWN;
    341 
    342     HANDLE hFile = CreateFile(pszFsPath,
    343                               GENERIC_READ,
    344                               FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
    345                               NULL,
    346                               OPEN_EXISTING,
    347                               FILE_FLAG_BACKUP_SEMANTICS,
    348                               NULL);
    349     if (hFile != INVALID_HANDLE_VALUE)
    350     {
    351         char abBuf[8192];
    352         IO_STATUS_BLOCK Ios;
    353         NTSTATUS rcNt = NtQueryVolumeInformationFile(hFile, &Ios,
    354                                                      abBuf, sizeof(abBuf),
    355                                                      FileFsAttributeInformation);
    356         if (rcNt >= 0)
    357         {
    358             PFILE_FS_ATTRIBUTE_INFORMATION pFsAttrInfo = (PFILE_FS_ATTRIBUTE_INFORMATION)abBuf;
    359             if (   pFsAttrInfo->FileSystemName[0] == 'N'
    360                 && pFsAttrInfo->FileSystemName[1] == 'T'
    361                 && pFsAttrInfo->FileSystemName[2] == 'F'
    362                 && pFsAttrInfo->FileSystemName[3] == 'S'
    363                 && pFsAttrInfo->FileSystemName[4] == '\0')
    364                 *pu32Type = RTFS_FS_TYPE_NTFS;
    365             else if (   pFsAttrInfo->FileSystemName[0] == 'F'
    366                      && pFsAttrInfo->FileSystemName[1] == 'A'
    367                      && pFsAttrInfo->FileSystemName[2] == 'T'
    368                      && (   (   pFsAttrInfo->FileSystemName[3] == '3'
    369                              && pFsAttrInfo->FileSystemName[4] == '2'
    370                              && pFsAttrInfo->FileSystemName[5] == '\0')
    371                          || pFsAttrInfo->FileSystemName[3] == '\0'))
    372                 /* This is either FAT32 or FAT12/16. IMO it doesn't make
    373                  * sense to distinguish more detailed because we cannot
    374                  * easily distinguish between these FAT types on Linux
    375                  * and users who put some image file on an FAT16 partition
    376                  * should know what they are doing. */
    377                 *pu32Type = RTFS_FS_TYPE_FAT;
     336
     337/**
     338 * Internal helper for comparing a WCHAR string with a char string.
     339 *
     340 * @returns 0 if equal, -1 if @a psz1 < @a psz2, 1 if @a psz1 < @a psz2.
     341 * @param   pwsz1               The first string.
     342 * @param   psz2                The second string.
     343 */
     344static int rtFsWinCmpUtf16(WCHAR const *pwsz1, const char *psz2)
     345{
     346    for (;;)
     347    {
     348        unsigned ch1 = *pwsz1++;
     349        unsigned ch2 = (unsigned char)*psz2++;
     350        if (ch1 != ch2)
     351            return ch1 < ch2 ? -1 : 1;
     352        if (!ch1)
     353            return 0;
     354    }
     355}
     356
     357
     358RTR3DECL(int) RTFsQueryType(const char *pszFsPath, PRTFSTYPE penmType)
     359{
     360    *penmType = RTFSTYPE_UNKNOWN;
     361
     362    AssertPtrReturn(pszFsPath, VERR_INVALID_POINTER);
     363    AssertReturn(*pszFsPath, VERR_INVALID_PARAMETER);
     364
     365    /*
     366     * Convert the path and try open it.
     367     */
     368    PRTUTF16 pwszFsPath;
     369    int rc = RTStrToUtf16(pszFsPath, &pwszFsPath);
     370    if (RT_SUCCESS(rc))
     371    {
     372        HANDLE hFile = CreateFileW(pwszFsPath,
     373                                   GENERIC_READ,
     374                                   FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
     375                                   NULL,
     376                                   OPEN_EXISTING,
     377                                   FILE_FLAG_BACKUP_SEMANTICS,
     378                                   NULL);
     379        if (hFile != INVALID_HANDLE_VALUE)
     380        {
     381            /*
     382             * Use the NT api directly to get the file system name.
     383             */
     384            char            abBuf[8192];
     385            IO_STATUS_BLOCK Ios;
     386            NTSTATUS        rcNt = NtQueryVolumeInformationFile(hFile, &Ios,
     387                                                                abBuf, sizeof(abBuf),
     388                                                                FileFsAttributeInformation);
     389            if (rcNt >= 0)
     390            {
     391                PFILE_FS_ATTRIBUTE_INFORMATION pFsAttrInfo = (PFILE_FS_ATTRIBUTE_INFORMATION)abBuf;
     392
     393                if (!rtFsWinCmpUtf16(pFsAttrInfo->FileSystemName, "NTFS"))
     394                    *penmType = RTFSTYPE_NTFS;
     395                else if (!rtFsWinCmpUtf16(pFsAttrInfo->FileSystemName, "FAT"))
     396                    *penmType = RTFSTYPE_FAT;
     397                else if (!rtFsWinCmpUtf16(pFsAttrInfo->FileSystemName, "FAT32"))
     398                    *penmType = RTFSTYPE_FAT;
     399                else if (!rtFsWinCmpUtf16(pFsAttrInfo->FileSystemName, "VBoxSharedFolderFS"))
     400                    *penmType = RTFSTYPE_VBOXSHF;
     401            }
     402            else
     403                rc = RTErrConvertFromNtStatus(rcNt);
     404            CloseHandle(hFile);
    378405        }
    379         /* else: Is RTFS_FS_UNKNOWN suffient or should we return an error? */
    380         CloseHandle(hFile);
    381     }
    382 
     406        else
     407            rc = RTErrConvertFromWin32(GetLastError());
     408        RTUtf16Free(pwszFsPath);
     409    }
    383410    return rc;
    384411}
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