VirtualBox

Changeset 13574

Show
Ignore:
Timestamp:
10/27/08 13:13:56 (2 months ago)
Author:
vboxsync
Message:

Guest Properties (HostServices? and Main): redid property flag handling

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/include/VBox/HostServices/GuestPropertySvc.h

    r13558 r13574  
    6565enum ePropFlags 
    6666{ 
    67     NILFLAG    = 0, 
    68     TRANSIENT  = RT_BIT(1), 
    69     HOSTWRITE  = RT_BIT(2), 
    70     GUESTWRITE = RT_BIT(3), 
    71     READONLY   = RT_BIT(4) 
     67    NILFLAG     = 0, 
     68    TRANSIENT   = RT_BIT(1), 
     69    RDONLYGUEST = RT_BIT(2), 
     70    RDONLYHOST  = RT_BIT(3), 
     71    READONLY    = RDONLYGUEST | RDONLYHOST, 
     72    ALLFLAGS    = TRANSIENT | READONLY 
    7273}; 
    7374 
    7475/** 
    75  * A structure to map the guest property flag values to names. I know it is 
    76  * ugly to have constants inline, but it has to be accessible in different 
    77  * modules and it does not seem reasonable to put it into its own library. 
    78  */ 
    79 const struct 
    80 
    81     /** The flag value */ 
    82     uint32_t fFlag; 
    83     /** And the name of the flag */ 
    84     const char *pcszName; 
    85 } flagNames[] = 
    86 
    87     { TRANSIENT,  "TRANSIENT" }, 
    88     { HOSTWRITE,  "HOSTWRITE" }, 
    89     { GUESTWRITE, "GUESTWRITE" }, 
    90     { READONLY,   "READONLY" }, 
    91     { NILFLAG,    NULL } 
    92 }; 
    93  
    94 /** Maximum length for the property flags field */ 
    95 enum { MAX_FLAGS_LEN =   sizeof(flagNames[0]) + 2  /* + 2 for ", " */ 
    96                        + sizeof(flagNames[1]) + 2 
    97                        + sizeof(flagNames[2]) + 2 
    98                        + sizeof(flagNames[3]) + 1  /* + 1 for '\0' */ 
    99      }; 
    100  
    101 /** 
    102  * Parse a guest properties flags string for the flags in flagNames. 
     76 * Get the name of a flag as a string. 
     77 * @returns the name, or NULL if fFlag is invalid. 
     78 * @param   fFlag  the flag.  Must be a value from the ePropFlags enumeration 
     79 *                 list. 
     80 */ 
     81DECLINLINE(const char *) flagName(uint32_t fFlag) 
     82
     83    switch(fFlag) 
     84    { 
     85        case TRANSIENT: 
     86            return "TRANSIENT"; 
     87        case RDONLYGUEST: 
     88            return "RDONLYGUEST"; 
     89        case RDONLYHOST: 
     90            return "RDONLYHOST"; 
     91        case READONLY: 
     92            return "READONLY"; 
     93        default: 
     94            return NULL; 
     95    } 
     96
     97 
     98/** 
     99 * Get the length of a flag name as returned by flagName. 
     100 * @returns the length, or 0 if fFlag is invalid. 
     101 * @param   fFlag  the flag.  Must be a value from the ePropFlags enumeration 
     102 *                 list. 
     103 */ 
     104DECLINLINE(size_t) flagNameLen(uint32_t fFlag) 
     105
     106    const char *pcszName = flagName(fFlag); 
     107    return RT_LIKELY(pcszName != NULL) ? strlen(pcszName) : 0; 
     108
     109 
     110/** 
     111 * Maximum length for the property flags field.  We only ever return one of 
     112 * RDONLYGUEST, RDONLYHOST and RDONLY 
     113 */ 
     114enum { MAX_FLAGS_LEN =   sizeof("TRANSIENT, RDONLYGUEST") }; 
     115 
     116/** 
     117 * Parse a guest properties flags string for flag names and make sure that 
     118 * there is no junk text in the string. 
    103119 * @returns  IPRT status code 
    104120 * @returns  VERR_INVALID_PARAM if the flag string is not valid 
     
    111127DECLINLINE(int) validateFlags(const char *pcszFlags, uint32_t *pfFlags) 
    112128{ 
     129    static uint32_t flagList[] = 
     130    { 
     131        TRANSIENT, READONLY, RDONLYGUEST, RDONLYHOST 
     132    }; 
    113133    const char *pcszNext = pcszFlags; 
    114134    int rc = VINF_SUCCESS; 
     
    120140    while ((*pcszNext != '\0') && RT_SUCCESS(rc)) 
    121141    { 
    122         int i = 0; 
    123         for (; flagNames[i].fFlag != NILFLAG; ++i) 
    124             if (strncasecmp(pcszNext, flagNames[i].pcszName, 
    125                             strlen(flagNames[i].pcszName)) == 0) 
     142        unsigned i = 0; 
     143        for (; i < RT_ELEMENTS(flagList); ++i) 
     144            if (strncasecmp(pcszNext, flagName(flagList[i]), 
     145                            flagNameLen(flagList[i]) 
     146                           ) == 0 
     147               ) 
    126148                break; 
    127         if (NILFLAG == flagNames[i].fFlag
    128             rc = VERR_INVALID_PARAMETER; 
     149        if (RT_ELEMENTS(flagList) == i
     150            rc = VERR_INVALID_PARAMETER; 
    129151        else 
    130152        { 
    131             fFlags |= flagNames[i].fFlag; 
    132             pcszNext += strlen(flagNames[i].pcszName); 
     153            fFlags |= flagList[i]; 
     154            pcszNext += flagNameLen(flagList[i]); 
     155            while (' ' == *pcszNext) 
     156                ++pcszNext; 
     157            if (',' == *pcszNext) 
     158                ++pcszNext; 
     159            while (' ' == *pcszNext) 
     160                ++pcszNext; 
    133161        } 
    134         while (' ' == *pcszNext) 
    135             ++pcszNext; 
    136         if (',' == *pcszNext) 
    137             ++pcszNext; 
    138         while (' ' == *pcszNext) 
    139             ++pcszNext; 
    140162    } 
    141163    if (RT_SUCCESS(rc)) 
     
    149171 * @param    fFlags    the flags to write out 
    150172 * @param    pszFlags  where to write the flags string.  This must point to 
    151  *                     a buffer of size (at least) MAX_FLAGS_LEN + 1
     173 *                     a buffer of size (at least) MAX_FLAGS_LEN
    152174 */ 
    153175DECLINLINE(int) writeFlags(uint32_t fFlags, char *pszFlags) 
    154176{ 
     177    static uint32_t flagList[] = 
     178    { 
     179        TRANSIENT, READONLY, RDONLYGUEST, RDONLYHOST 
     180    }; 
    155181    char *pszNext = pszFlags; 
    156182    int rc = VINF_SUCCESS; 
    157183    AssertLogRelReturn(VALID_PTR(pszFlags), VERR_INVALID_POINTER); 
    158     int i = 0; 
    159     for (; flagNames[i].fFlag != NILFLAG; ++i) 
     184    if ((fFlags & ~ALLFLAGS) != NILFLAG) 
     185        rc = VERR_INVALID_PARAMETER; 
     186    if (RT_SUCCESS(rc)) 
    160187    { 
    161         if (fFlags & flagNames[i].fFlag) 
     188        unsigned i = 0; 
     189        for (; i < RT_ELEMENTS(flagList); ++i) 
    162190        { 
    163             strcpy(pszNext, flagNames[i].pcszName); 
    164             pszNext += strlen(flagNames[i].pcszName); 
    165             fFlags &= ~flagNames[i].fFlag; 
    166             if ((flagNames[i + 1].fFlag != NILFLAG) && (fFlags != NILFLAG)) 
     191            if (fFlags & flagList[i]) 
    167192            { 
    168                 strcpy(pszNext, ", "); 
    169                 pszNext += 2; 
     193                strcpy(pszNext, flagName(flagList[i])); 
     194                pszNext += flagNameLen(flagList[i]); 
     195                fFlags &= ~flagList[i]; 
     196                if (fFlags != NILFLAG) 
     197                { 
     198                    strcpy(pszNext, ", "); 
     199                    pszNext += 2; 
     200                } 
    170201            } 
    171202        } 
     203        *pszNext = '\0'; 
     204        if (fFlags != NILFLAG) 
     205            rc = VERR_INVALID_PARAMETER;  /* But pszFlags will still be set right. */ 
    172206    } 
    173     *pszNext = '\0'; 
    174     if (fFlags != NILFLAG) 
    175         rc = VERR_INVALID_PARAMETER;  /* But pszFlags will still be set right. */ 
    176207    return rc; 
    177208} 
  • trunk/src/VBox/HostServices/GuestProperties/Makefile.kmk

    r12250 r13574  
    2222SUB_DEPTH = ../../../.. 
    2323include $(KBUILD_PATH)/subheader.kmk 
     24 
     25# Include sub-makefile(s). 
     26include $(PATH_SUB_CURRENT)/testcase/Makefile.kmk 
    2427 
    2528# 
  • trunk/src/VBox/HostServices/GuestProperties/service.cpp

    r13556 r13574  
    499499    { 
    500500        CFGMR3QueryU32(mpFlagsNode, pszName, &fFlags);  /* Failure is no problem here. */ 
    501         if (   (fFlags & READONLY) 
    502             || (isGuest && (fFlags & HOSTWRITE)) 
    503             || (!isGuest && (fFlags & GUESTWRITE)) 
     501        if (   (isGuest && (fFlags & RDONLYGUEST)) 
     502            || (!isGuest && (fFlags & RDONLYHOST)) 
    504503           ) 
    505504            rc = VERR_PERMISSION_DENIED; 
     
    593592        uint32_t fFlags = NILFLAG; 
    594593        CFGMR3QueryU32(mpFlagsNode, pszName, &fFlags);  /* Failure is no problem here. */ 
    595         if (   (fFlags & READONLY) 
    596             || (isGuest && (fFlags & HOSTWRITE)) 
    597             || (!isGuest && (fFlags & GUESTWRITE)) 
     594        if (   (isGuest && (fFlags & RDONLYGUEST)) 
     595            || (!isGuest && (fFlags & RDONLYHOST)) 
    598596           ) 
    599597            rc = VERR_PERMISSION_DENIED; 
  • trunk/src/VBox/Main/MachineImpl.cpp

    r13555 r13574  
    28872887                { 
    28882888                    property = *it; 
    2889                     if (it->mFlags & (GUESTWRITE | READONLY)) 
     2889                    if (it->mFlags & (RDONLYHOST)) 
    28902890                        rc = setError (E_ACCESSDENIED, tr ("The property '%ls' cannot be changed by the host"), aName); 
    28912891                    else 

© 2008 Sun Microsystems, Inc.
ContactPrivacy policy