VirtualBox

Changeset 94311 in vbox for trunk


Ignore:
Timestamp:
Mar 19, 2022 1:34:37 AM (3 years ago)
Author:
vboxsync
Message:

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

Location:
trunk
Files:
3 edited

Legend:

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

    r94277 r94311  
    958958
    959959/**
    960  * Creates a new file with a unique name using the given template.
     960 * Creates a new file with a unique name using the given template, returning a
     961 * handle to it.
    961962 *
    962963 * One or more trailing X'es in the template will be replaced by random alpha
     
    971972 *          "/tmp/myprog-XXX-XXX.tmp"
    972973 *
     974 * @returns IPRT status code.
     975 * @param   phFile          Where to return the file handle on success.  Set to
     976 *                          NIL on failure.
     977 * @param   pszTemplate     The file name template on input. The actual file
     978 *                          name on success. Empty string on failure.
     979 * @param   fOpen           The RTFILE_O_XXX flags to open the file with.
     980 *                          RTFILE_O_CREATE is mandatory.
     981 * @see     RTFileCreateTemp
     982 */
     983RTDECL(int) RTFileCreateUnique(PRTFILE phFile, char *pszTemplate, uint64_t fOpen);
     984
     985/**
     986 * Creates a new file with a unique name using the given template.
     987 *
     988 * One or more trailing X'es in the template will be replaced by random alpha
     989 * numeric characters until a RTFileOpen with RTFILE_O_CREATE succeeds or we
     990 * run out of patience.
     991 * For instance:
     992 *          "/tmp/myprog-XXXXXX"
     993 *
     994 * As an alternative to trailing X'es, it is possible to put 3 or more X'es
     995 * somewhere inside the file name. In the following string only the last
     996 * bunch of X'es will be modified:
     997 *          "/tmp/myprog-XXX-XXX.tmp"
     998 *
    973999 * @returns iprt status code.
    9741000 * @param   pszTemplate     The file name template on input. The actual file
     
    9761002 * @param   fMode           The mode to create the file with.  Use 0600 unless
    9771003 *                          you have reason not to.
     1004 * @see     RTFileCreateUnique
    9781005 */
    9791006RTDECL(int) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode);
     
    9981025 * @param   pszTemplate        The file name template on input. The actual
    9991026 *                             file name on success. Empty string on failure.
     1027 * @see     RTFileCreateUnique
    10001028 */
    10011029RTDECL(int) RTFileCreateTempSecure(char *pszTemplate);
  • trunk/include/iprt/mangling.h

    r94300 r94311  
    968968# define RTFileCopyPartEx                               RT_MANGLER(RTFileCopyPartEx)
    969969# define RTFileCopyPartPrep                             RT_MANGLER(RTFileCopyPartPrep)
     970# define RTFileCreateUnique                             RT_MANGLER(RTFileCreateUnique)
    970971# define RTFileCreateTemp                               RT_MANGLER(RTFileCreateTemp)
    971972# define RTFileCreateTempSecure                         RT_MANGLER(RTFileCreateTempSecure)
  • 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.

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