VirtualBox

Changeset 94311 in vbox for trunk/src/VBox/Runtime/generic


Ignore:
Timestamp:
Mar 19, 2022 1:34:37 AM (3 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
150596
Message:

IPRT: Added RTFileCreateUnique, a saner version of RTFileCreateTemp in that it returns the file handle. bugref:10201

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/generic/createtemp-generic.cpp

    r93115 r94311  
    158158
    159159
    160 RTDECL(int) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode)
    161 {
     160RTDECL(int) RTFileCreateUnique(PRTFILE phFile, char *pszTemplate, uint64_t fOpen)
     161{
     162    /*
     163     * Validate input.
     164     */
     165    *phFile = NIL_RTFILE;
     166    AssertReturn((fOpen & RTFILE_O_ACTION_MASK) == RTFILE_O_CREATE, VERR_INVALID_FLAGS);
    162167    char       *pszX = NULL;
    163168    unsigned    cXes = 0;
    164169    int rc = rtCreateTempValidateTemplate(pszTemplate, &pszX, &cXes);
    165     if (RT_FAILURE(rc))
    166     {
    167         *pszTemplate = '\0';
    168         return rc;
    169     }
    170 
    171     /*
    172      * Try ten thousand times.
    173      */
    174     int i = 10000;
    175     while (i-- > 0)
    176     {
    177         uint64_t fOpen = RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE | RTFILE_O_NOT_CONTENT_INDEXED
    178                        | fMode << RTFILE_O_CREATE_MODE_SHIFT;
    179         rtCreateTempFillTemplate(pszX, cXes);
    180         RTFILE hFile = NIL_RTFILE;
    181         rc = RTFileOpen(&hFile, pszTemplate, fOpen);
    182         if (RT_SUCCESS(rc))
    183         {
    184             RTFileClose(hFile);
    185             return rc;
    186         }
    187         /** @todo Anything else to consider? */
    188         if (rc != VERR_ALREADY_EXISTS)
    189         {
    190             *pszTemplate = '\0';
    191             return rc;
    192         }
    193     }
    194 
    195     /* we've given up. */
    196     *pszTemplate = '\0';
    197     return VERR_ALREADY_EXISTS;
     170    if (RT_SUCCESS(rc))
     171    {
     172        /*
     173         * Try ten thousand times.
     174         */
     175        int i = 10000;
     176        while (i-- > 0)
     177        {
     178            rtCreateTempFillTemplate(pszX, cXes);
     179            RTFILE hFile = NIL_RTFILE;
     180            rc = RTFileOpen(&hFile, pszTemplate, fOpen);
     181            if (RT_SUCCESS(rc))
     182            {
     183                *phFile = hFile;
     184                return rc;
     185            }
     186            /** @todo Anything else to consider? */
     187            if (rc != VERR_ALREADY_EXISTS)
     188            {
     189                *pszTemplate = '\0';
     190                return rc;
     191            }
     192        }
     193
     194        /* we've given up. */
     195        rc = VERR_ALREADY_EXISTS;
     196    }
     197    *pszTemplate = '\0';
     198    return rc;
     199}
     200RT_EXPORT_SYMBOL(RTFileCreateUnique);
     201
     202
     203RTDECL(int) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode)
     204{
     205    RTFILE hFile = NIL_RTFILE;
     206    int rc = RTFileCreateUnique(&hFile, pszTemplate,
     207                                RTFILE_O_WRITE | RTFILE_O_DENY_ALL | RTFILE_O_CREATE | RTFILE_O_NOT_CONTENT_INDEXED
     208                                | fMode << RTFILE_O_CREATE_MODE_SHIFT);
     209    if (RT_SUCCESS(rc))
     210        RTFileClose(hFile);
     211    return rc;
    198212}
    199213RT_EXPORT_SYMBOL(RTFileCreateTemp);
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