VirtualBox

Changeset 67253 in vbox


Ignore:
Timestamp:
Jun 2, 2017 5:16:05 PM (8 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
115948
Message:

IPRT/tarvfswriter.cpp: Added RTZipTarFsStreamSetOwner, RTZipTarFsStreamSetGroup, RTZipTarFsStreamSetPrefix, RTZipTarFsStreamSetFileMode, RTZipTarFsStreamSetDirMode, and RTZipTarFsStreamSetModTime for overriding attributes in the output archive.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/mangling.h

    r67228 r67253  
    24482448# define RTZipTarFsStreamFromIoStream                   RT_MANGLER(RTZipTarFsStreamFromIoStream)
    24492449# define RTZipTarFsStreamToIoStream                     RT_MANGLER(RTZipTarFsStreamToIoStream)
     2450# define RTZipTarFsStreamSetOwner                       RT_MANGLER(RTZipTarFsStreamSetOwner)
     2451# define RTZipTarFsStreamSetGroup                       RT_MANGLER(RTZipTarFsStreamSetGroup)
     2452# define RTZipTarFsStreamSetPrefix                      RT_MANGLER(RTZipTarFsStreamSetPrefix)
     2453# define RTZipTarFsStreamSetFileMode                    RT_MANGLER(RTZipTarFsStreamSetFileMode)
     2454# define RTZipTarFsStreamSetDirMode                     RT_MANGLER(RTZipTarFsStreamSetDirMode)
     2455# define RTZipTarFsStreamSetModTime                     RT_MANGLER(RTZipTarFsStreamSetModTime)
    24502456# define RTZipXarFsStreamFromIoStream                   RT_MANGLER(RTZipXarFsStreamFromIoStream)
    24512457
  • trunk/include/iprt/zip.h

    r67134 r67253  
    315315
    316316/**
     317 * Set the owner to store the archive entries with.
     318 *
     319 * @returns IPRT status code.
     320 * @param   hVfsFss             The handle to a TAR creator.
     321 * @param   uid                 The UID value to set.  Passing NIL_RTUID makes
     322 *                              it use the value found in RTFSOBJINFO.
     323 * @param   pszOwner            The owner name to store.  Passing NULL makes it
     324 *                              use the value found in RTFSOBJINFO.
     325 */
     326RTDECL(int) RTZipTarFsStreamSetOwner(RTVFSFSSTREAM hVfsFss, RTUID uid, const char *pszOwner);
     327
     328/**
     329 * Set the group to store the archive entries with.
     330 *
     331 * @returns IPRT status code.
     332 * @param   hVfsFss             The handle to a TAR creator.
     333 * @param   gid                 The GID value to set.  Passing NIL_RTUID makes
     334 *                              it use the value found in RTFSOBJINFO.
     335 * @param   pszGroup            The group name to store.  Passing NULL makes it
     336 *                              use the value found in RTFSOBJINFO.
     337 */
     338RTDECL(int) RTZipTarFsStreamSetGroup(RTVFSFSSTREAM hVfsFss, RTGID gid, const char *pszGroup);
     339
     340/**
     341 * Set path prefix to store the archive entries with.
     342 *
     343 * @returns IPRT status code.
     344 * @param   hVfsFss             The handle to a TAR creator.
     345 * @param   pszPrefix           The path prefix to join the names with.  Pass
     346 *                              NULL for no prefix.
     347 */
     348RTDECL(int) RTZipTarFsStreamSetPrefix(RTVFSFSSTREAM hVfsFss, const char *pszPrefix);
     349
     350/**
     351 * Set the AND and OR masks to apply to file (non-dir) modes in the archive.
     352 *
     353 * @returns IPRT status code.
     354 * @param   hVfsFss             The handle to a TAR creator.
     355 * @param   fAndMode            The bits to keep
     356 * @param   fOrMode             The bits to set.
     357 */
     358RTDECL(int) RTZipTarFsStreamSetFileMode(RTVFSFSSTREAM hVfsFss, RTFMODE fAndMode, RTFMODE fOrMode);
     359
     360/**
     361 * Set the AND and OR masks to apply to directory modes in the archive.
     362 *
     363 * @returns IPRT status code.
     364 * @param   hVfsFss             The handle to a TAR creator.
     365 * @param   fAndMode            The bits to keep
     366 * @param   fOrMode             The bits to set.
     367 */
     368RTDECL(int) RTZipTarFsStreamSetDirMode(RTVFSFSSTREAM hVfsFss, RTFMODE fAndMode, RTFMODE fOrMode);
     369
     370/**
     371 * Set the modification time to store the archive entires with.
     372 *
     373 * @returns IPRT status code.
     374 * @param   hVfsFss             The handle to a TAR creator.
     375 * @param   pModificationTime   The modification time to use.  Pass NULL to use
     376 *                              the value found in RTFSOBJINFO.
     377 */
     378RTDECL(int) RTZipTarFsStreamSetMTime(RTVFSFSSTREAM hVfsFss, PCRTTIMESPEC pModificationTime);
     379
     380
     381/**
    317382 * A mini TAR program.
    318383 *
  • trunk/src/VBox/Runtime/common/zip/tarvfswriter.cpp

    r67220 r67253  
    167167    uint64_t                cbWritten;
    168168
     169    /** @name Attribute overrides.
     170     * @{
     171     */
     172    RTUID                   uidOwner;           /**< Owner, NIL_RTUID if no change. */
     173    char                   *pszOwner;           /**< Owner, NULL if no change. */
     174    RTGID                   gidGroup;           /**< Group, NIL_RTGID if no change. */
     175    char                   *pszGroup;           /**< Group, NULL if no change. */
     176    char                   *pszPrefix;          /**< Path prefix, NULL if no change. */
     177    size_t                  cchPrefix;          /**< The length of pszPrefix. */
     178    PRTTIMESPEC             pModTime;           /**< Modification time, NULL of no change. */
     179    RTTIMESPEC              ModTime;            /**< pModTime points to this. */
     180    RTFMODE                 fFileModeAndMask;   /**< File mode AND mask. */
     181    RTFMODE                 fFileModeOrMask;    /**< File mode OR mask. */
     182    RTFMODE                 fDirModeAndMask;    /**< Directory mode AND mask. */
     183    RTFMODE                 fDirModeOrMask;     /**< Directory mode OR mask. */
     184    /** @} */
     185
     186
    169187    /** Number of headers returned by rtZipTarFssWriter_ObjInfoToHdr. */
    170188    uint32_t                cHdrs;
     
    281299    {
    282300        memcpy(pThis->aHdrs[0].Common.name, pszPath, cchPath + 1);
    283 #if RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX
     301#if RTPATH_STYLE != RTPATH_STR_F_STYLE_UNIX
    284302        char *pszDosSlash = strchr(pThis->aHdrs[0].Common.name, '\\');
    285303        while (pszDosSlash)
     
    300318     * compatible with the TAR/Unix world.
    301319     */
    302     int rc;
    303     rc = RTStrFormatU32(pThis->aHdrs[0].Common.mode, sizeof(pThis->aHdrs[0].Common.mode), pObjInfo->Attr.fMode & RTFS_UNIX_MASK,
    304                         8 /*uBase*/, -1 /*cchWidth*/, sizeof(pThis->aHdrs[0].Common.mode) - 1, RTSTR_F_ZEROPAD | RTSTR_F_PRECISION);
     320    uint32_t uValue = pObjInfo->Attr.fMode & RTFS_UNIX_MASK;
     321    if (RTFS_IS_DIRECTORY(pObjInfo->Attr.fMode))
     322        uValue = (uValue & pThis->fDirModeAndMask) | pThis->fDirModeOrMask;
     323    else
     324        uValue = (uValue & pThis->fFileModeAndMask) | pThis->fFileModeOrMask;
     325    int rc = RTStrFormatU32(pThis->aHdrs[0].Common.mode, sizeof(pThis->aHdrs[0].Common.mode), uValue, 8 /*uBase*/,
     326                            -1 /*cchWidth*/, sizeof(pThis->aHdrs[0].Common.mode) - 1, RTSTR_F_ZEROPAD | RTSTR_F_PRECISION);
    305327    AssertRCReturn(rc, VERR_TAR_NUM_VALUE_TOO_LARGE);
    306328
    307 
    308329    /*
    309330     * uid & gid.  Just guard against NIL values as they won't fit.
    310331     */
    311     uint32_t uValue = pObjInfo->Attr.u.Unix.uid != NIL_RTUID ? pObjInfo->Attr.u.Unix.uid : 0;
     332    uValue = pThis->uidOwner != NIL_RTUID ? pThis->uidOwner
     333           : pObjInfo->Attr.u.Unix.uid != NIL_RTUID ? pObjInfo->Attr.u.Unix.uid : 0;
    312334    rc = RTStrFormatU32(pThis->aHdrs[0].Common.uid, sizeof(pThis->aHdrs[0].Common.uid), uValue,
    313335                        8 /*uBase*/, -1 /*cchWidth*/, sizeof(pThis->aHdrs[0].Common.uid) - 1, RTSTR_F_ZEROPAD | RTSTR_F_PRECISION);
    314336    AssertRCReturn(rc, VERR_TAR_NUM_VALUE_TOO_LARGE);
    315337
    316     uValue = pObjInfo->Attr.u.Unix.gid != NIL_RTUID ? pObjInfo->Attr.u.Unix.gid : 0;
     338    uValue = pThis->gidGroup != NIL_RTGID ? pThis->gidGroup
     339           : pObjInfo->Attr.u.Unix.gid != NIL_RTGID ? pObjInfo->Attr.u.Unix.gid : 0;
    317340    rc = RTStrFormatU32(pThis->aHdrs[0].Common.gid, sizeof(pThis->aHdrs[0].Common.gid), uValue,
    318341                        8 /*uBase*/, -1 /*cchWidth*/, sizeof(pThis->aHdrs[0].Common.gid) - 1, RTSTR_F_ZEROPAD | RTSTR_F_PRECISION);
     
    329352     */
    330353    rc = RTStrFormatU64(pThis->aHdrs[0].Common.mtime, sizeof(pThis->aHdrs[0].Common.mtime),
    331                         RTTimeSpecGetSeconds(&pObjInfo->ModificationTime),
     354                        RTTimeSpecGetSeconds(pThis->pModTime ? pThis->pModTime : &pObjInfo->ModificationTime),
    332355                        8 /*uBase*/, -1 /*cchWidth*/, sizeof(pThis->aHdrs[0].Common.mtime) - 1, RTSTR_F_ZEROPAD | RTSTR_F_PRECISION);
    333356    AssertRCReturn(rc, rc);
     
    371394     * Owner and group names.  Silently truncate them for now.
    372395     */
    373     RTStrCopy(pThis->aHdrs[0].Common.uname, sizeof(pThis->aHdrs[0].Common.uname), pszOwnerNm);
    374     RTStrCopy(pThis->aHdrs[0].Common.gname, sizeof(pThis->aHdrs[0].Common.uname), pszGroupNm);
     396    RTStrCopy(pThis->aHdrs[0].Common.uname, sizeof(pThis->aHdrs[0].Common.uname), pThis->pszOwner ? pThis->pszOwner : pszOwnerNm);
     397    RTStrCopy(pThis->aHdrs[0].Common.gname, sizeof(pThis->aHdrs[0].Common.uname), pThis->pszGroup ? pThis->pszGroup : pszGroupNm);
    375398
    376399    /*
     
    15341557    if (RT_SUCCESS(rc))
    15351558    {
    1536 #if RTPATH_STYLE == RTPATH_STR_F_STYLE_UNIX
     1559#if RTPATH_STYLE != RTPATH_STR_F_STYLE_UNIX
    15371560        char *pszDosSlash = strchr(szTarget, '\\');
    15381561        while (pszDosSlash)
     
    16251648    }
    16261649
     1650    if (pThis->pszOwner)
     1651    {
     1652        RTStrFree(pThis->pszOwner);
     1653        pThis->pszOwner = NULL;
     1654    }
     1655    if (pThis->pszGroup)
     1656    {
     1657        RTStrFree(pThis->pszGroup);
     1658        pThis->pszGroup = NULL;
     1659    }
     1660    if (pThis->pszPrefix)
     1661    {
     1662        RTStrFree(pThis->pszPrefix);
     1663        pThis->pszPrefix = NULL;
     1664    }
     1665
    16271666    return VINF_SUCCESS;
    16281667}
     
    18951934 * Tar filesystem stream operations.
    18961935 */
    1897 static const RTVFSFSSTREAMOPS rtZipTarFssOps =
     1936static const RTVFSFSSTREAMOPS g_rtZipTarFssOps =
    18981937{
    18991938    { /* Obj */
     
    19411980    PRTZIPTARFSSTREAMWRITER pThis;
    19421981    RTVFSFSSTREAM           hVfsFss;
    1943     int rc = RTVfsNewFsStream(&rtZipTarFssOps, sizeof(*pThis), NIL_RTVFS, NIL_RTVFSLOCK, false /*fReadOnly*/,
     1982    int rc = RTVfsNewFsStream(&g_rtZipTarFssOps, sizeof(*pThis), NIL_RTVFS, NIL_RTVFSLOCK, false /*fReadOnly*/,
    19441983                              &hVfsFss, (void **)&pThis);
    19451984    if (RT_SUCCESS(rc))
    19461985    {
    1947         pThis->hVfsIos   = hVfsIosOut;
    1948         pThis->hVfsFile  = RTVfsIoStrmToFile(hVfsIosOut);
    1949 
    1950         pThis->enmFormat = enmFormat;
    1951         pThis->fFlags    = fFlags;
    1952         pThis->rcFatal   = VINF_SUCCESS;
     1986        pThis->hVfsIos          = hVfsIosOut;
     1987        pThis->hVfsFile         = RTVfsIoStrmToFile(hVfsIosOut);
     1988
     1989        pThis->enmFormat        = enmFormat;
     1990        pThis->fFlags           = fFlags;
     1991        pThis->rcFatal          = VINF_SUCCESS;
     1992
     1993        pThis->uidOwner         = NIL_RTUID;
     1994        pThis->pszOwner         = NULL;
     1995        pThis->gidGroup         = NIL_RTGID;
     1996        pThis->pszGroup         = NULL;
     1997        pThis->pszPrefix        = NULL;
     1998        pThis->pModTime         = NULL;
     1999        pThis->fFileModeAndMask = ~(RTFMODE)0;
     2000        pThis->fFileModeOrMask  = 0;
     2001        pThis->fDirModeAndMask  = ~(RTFMODE)0;
     2002        pThis->fDirModeOrMask   = 0;
    19532003
    19542004        *phVfsFss = hVfsFss;
     
    19602010}
    19612011
     2012
     2013RTDECL(int) RTZipTarFsStreamSetOwner(RTVFSFSSTREAM hVfsFss, RTUID uid, const char *pszOwner)
     2014{
     2015    PRTZIPTARFSSTREAMWRITER pThis = (PRTZIPTARFSSTREAMWRITER)RTVfsFsStreamToPrivate(hVfsFss, &g_rtZipTarFssOps);
     2016    AssertReturn(pThis, VERR_WRONG_TYPE);
     2017
     2018    pThis->uidOwner = uid;
     2019    if (pThis->pszOwner)
     2020    {
     2021        RTStrFree(pThis->pszOwner);
     2022        pThis->pszOwner = NULL;
     2023    }
     2024    if (pszOwner)
     2025    {
     2026        pThis->pszOwner = RTStrDup(pszOwner);
     2027        AssertReturn(pThis->pszOwner, VERR_NO_STR_MEMORY);
     2028    }
     2029
     2030    return VINF_SUCCESS;
     2031}
     2032
     2033
     2034RTDECL(int) RTZipTarFsStreamSetGroup(RTVFSFSSTREAM hVfsFss, RTGID gid, const char *pszGroup)
     2035{
     2036    PRTZIPTARFSSTREAMWRITER pThis = (PRTZIPTARFSSTREAMWRITER)RTVfsFsStreamToPrivate(hVfsFss, &g_rtZipTarFssOps);
     2037    AssertReturn(pThis, VERR_WRONG_TYPE);
     2038
     2039    pThis->gidGroup = gid;
     2040    if (pThis->pszGroup)
     2041    {
     2042        RTStrFree(pThis->pszGroup);
     2043        pThis->pszGroup = NULL;
     2044    }
     2045    if (pszGroup)
     2046    {
     2047        pThis->pszGroup = RTStrDup(pszGroup);
     2048        AssertReturn(pThis->pszGroup, VERR_NO_STR_MEMORY);
     2049    }
     2050
     2051    return VINF_SUCCESS;
     2052}
     2053
     2054
     2055RTDECL(int) RTZipTarFsStreamSetPrefix(RTVFSFSSTREAM hVfsFss, const char *pszPrefix)
     2056{
     2057    PRTZIPTARFSSTREAMWRITER pThis = (PRTZIPTARFSSTREAMWRITER)RTVfsFsStreamToPrivate(hVfsFss, &g_rtZipTarFssOps);
     2058    AssertReturn(pThis, VERR_WRONG_TYPE);
     2059    AssertReturn(!pszPrefix || *pszPrefix, VERR_INVALID_NAME);
     2060
     2061    if (pThis->pszPrefix)
     2062    {
     2063        RTStrFree(pThis->pszPrefix);
     2064        pThis->pszPrefix = NULL;
     2065        pThis->cchPrefix = 0;
     2066    }
     2067    if (pszPrefix)
     2068    {
     2069        /*
     2070         * Make a copy of the prefix, make sure it ends with a slash,
     2071         * then flip DOS slashes.
     2072         */
     2073        size_t cchPrefix = strlen(pszPrefix);
     2074        char *pszCopy = RTStrAlloc(cchPrefix + 3);
     2075        AssertReturn(pszCopy, VERR_NO_STR_MEMORY);
     2076        memcpy(pszCopy, pszPrefix, cchPrefix + 1);
     2077
     2078        RTPathEnsureTrailingSeparator(pszCopy, cchPrefix + 3);
     2079
     2080#if RTPATH_STYLE != RTPATH_STR_F_STYLE_UNIX
     2081        char *pszDosSlash = strchr(pszCopy, '\\');
     2082        while (pszDosSlash)
     2083        {
     2084            *pszDosSlash = '/';
     2085            pszDosSlash = strchr(pszDosSlash + 1, '\\');
     2086        }
     2087#endif
     2088
     2089        pThis->cchPrefix = cchPrefix + strlen(&pszCopy[cchPrefix]);
     2090        pThis->pszPrefix = pszCopy;
     2091    }
     2092
     2093    return VINF_SUCCESS;
     2094}
     2095
     2096
     2097RTDECL(int) RTZipTarFsStreamSetModTime(RTVFSFSSTREAM hVfsFss, PCRTTIMESPEC pModificationTime)
     2098{
     2099    PRTZIPTARFSSTREAMWRITER pThis = (PRTZIPTARFSSTREAMWRITER)RTVfsFsStreamToPrivate(hVfsFss, &g_rtZipTarFssOps);
     2100    AssertReturn(pThis, VERR_WRONG_TYPE);
     2101
     2102    if (pModificationTime)
     2103    {
     2104        pThis->ModTime  = *pModificationTime;
     2105        pThis->pModTime = &pThis->ModTime;
     2106    }
     2107    else
     2108        pThis->pModTime = NULL;
     2109
     2110    return VINF_SUCCESS;
     2111}
     2112
     2113
     2114RTDECL(int) RTZipTarFsStreamSetFileMode(RTVFSFSSTREAM hVfsFss, RTFMODE fAndMode, RTFMODE fOrMode)
     2115{
     2116    PRTZIPTARFSSTREAMWRITER pThis = (PRTZIPTARFSSTREAMWRITER)RTVfsFsStreamToPrivate(hVfsFss, &g_rtZipTarFssOps);
     2117    AssertReturn(pThis, VERR_WRONG_TYPE);
     2118
     2119    pThis->fFileModeAndMask = fAndMode | ~RTFS_UNIX_ALL_PERMS;
     2120    pThis->fFileModeOrMask  = fOrMode  & RTFS_UNIX_ALL_PERMS;
     2121    return VINF_SUCCESS;
     2122}
     2123
     2124
     2125RTDECL(int) RTZipTarFsStreamSetDirMode(RTVFSFSSTREAM hVfsFss, RTFMODE fAndMode, RTFMODE fOrMode)
     2126{
     2127    PRTZIPTARFSSTREAMWRITER pThis = (PRTZIPTARFSSTREAMWRITER)RTVfsFsStreamToPrivate(hVfsFss, &g_rtZipTarFssOps);
     2128    AssertReturn(pThis, VERR_WRONG_TYPE);
     2129
     2130    pThis->fDirModeAndMask = fAndMode | ~RTFS_UNIX_ALL_PERMS;
     2131    pThis->fDirModeOrMask  = fOrMode  & RTFS_UNIX_ALL_PERMS;
     2132    return VINF_SUCCESS;
     2133}
     2134
Note: See TracChangeset for help on using the changeset viewer.

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