VirtualBox

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


Ignore:
Timestamp:
Aug 9, 2012 2:36:02 PM (13 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
79919
Message:

Runtime: add RTFileCreateTemp, RTFileCreateTempSecure and RTDirCreateTempSecure (the last two are not yet implemented).

File:
1 edited

Legend:

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

    r42709 r42712  
    11/* $Id$ */
    22/** @file
    3  * IPRT - RTDirCreateTemp, generic implementation.
     3 * IPRT - temporary file and directory creation, generic implementation.
    44 */
    55
    66/*
    7  * Copyright (C) 2009 Oracle Corporation
     7 * Copyright (C) 2009-2012 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3434#include <iprt/assert.h>
    3535#include <iprt/err.h>
     36#include <iprt/file.h>
    3637#include <iprt/path.h>
    3738#include <iprt/rand.h>
     
    108109RTDECL(int) RTDirCreateTemp(char *pszTemplate, RTFMODE fMode)
    109110{
    110     char       *pszX = NULL;  /* Initialise to make gcc happy. */
     111    char       *pszX = NULL;
    111112    unsigned    cXes = 0;
    112113    int rc = rtCreateTempValidateTemplate(pszTemplate, &pszX, &cXes);
     
    139140RT_EXPORT_SYMBOL(RTDirCreateTemp);
    140141
     142
     143/** @todo Test case for this once it is implemented. */
     144RTDECL(int) RTDirCreateTempSecure(char *pszTemplate)
     145{
     146    size_t cchDir;
     147    char chOld;
     148    int rc;
     149    /* bool fSafe; */
     150
     151    /* Temporarily convert pszTemplate to a path. */
     152    RTPathParse(pszTemplate, &cchDir, NULL, NULL);
     153    chOld = pszTemplate[cchDir];
     154    pszTemplate[cchDir] = '\0';
     155    /** @todo Implement this. */
     156    rc = /* RTPathIsSecure(pszTemplate, &fSafe) */ VERR_NOT_SUPPORTED;
     157    pszTemplate[cchDir] = chOld;
     158    if (RT_SUCCESS(rc) /* && fSafe */)
     159        return RTDirCreateTemp(pszTemplate, 0700);
     160    else
     161    {
     162        *pszTemplate = '\0';
     163        /** @todo Replace VERR_PERMISSION_DENIED.  VERR_INSECURE? */
     164        return RT_FAILURE(rc) ? rc : VERR_PERMISSION_DENIED;
     165    }
     166}
     167RT_EXPORT_SYMBOL(RTDirCreateTempSecure);
     168
     169
     170RTDECL(int) RTFileCreateTemp(char *pszTemplate, RTFMODE fMode)
     171{
     172    char       *pszX = NULL;
     173    unsigned    cXes = 0;
     174    RTFILE      hFile;
     175    int rc = rtCreateTempValidateTemplate(pszTemplate, &pszX, &cXes);
     176    if (RT_FAILURE(rc))
     177    {
     178        *pszTemplate = '\0';
     179        return rc;
     180    }
     181    /*
     182     * Try ten thousand times.
     183     */
     184    int i = 10000;
     185    while (i-- > 0)
     186    {
     187        uint64_t fOpen =   RTFILE_O_WRITE | RTFILE_O_DENY_ALL
     188                         | RTFILE_O_CREATE | RTFILE_O_NOT_CONTENT_INDEXED
     189                         | fMode << RTFILE_O_CREATE_MODE_SHIFT;
     190        rtCreateTempFillTemplate(pszX, cXes);
     191        rc = RTFileOpen(&hFile, pszTemplate, fOpen);
     192        if (RT_SUCCESS(rc))
     193        {
     194            RTFileClose(hFile);
     195            return rc;
     196        }
     197        /** @todo Anything else to consider? */
     198        if (rc != VERR_ALREADY_EXISTS)
     199        {
     200            *pszTemplate = '\0';
     201            return rc;
     202        }
     203    }
     204
     205    /* we've given up. */
     206    *pszTemplate = '\0';
     207    return VERR_ALREADY_EXISTS;
     208}
     209RT_EXPORT_SYMBOL(RTFileCreateTemp);
     210
     211
     212/** @todo Test case for this once it is implemented. */
     213RTDECL(int) RTFileCreateTempSecure(char *pszTemplate)
     214{
     215    size_t cchDir;
     216    char chOld;
     217    int rc;
     218    /* bool fSafe; */
     219
     220    /* Temporarily convert pszTemplate to a path. */
     221    RTPathParse(pszTemplate, &cchDir, NULL, NULL);
     222    chOld = pszTemplate[cchDir];
     223    pszTemplate[cchDir] = '\0';
     224    /** @todo Implement this. */
     225    rc = /* RTPathIsSecure(pszTemplate, &fSafe) */ VERR_NOT_SUPPORTED;
     226    pszTemplate[cchDir] = chOld;
     227    if (RT_SUCCESS(rc) /* && fSafe */)
     228        return RTFileCreateTemp(pszTemplate, 0600);
     229    else
     230    {
     231        *pszTemplate = '\0';
     232        /** @todo Replace VERR_PERMISSION_DENIED.  VERR_INSECURE? */
     233        return RT_FAILURE(rc) ? rc : VERR_PERMISSION_DENIED;
     234    }
     235}
     236RT_EXPORT_SYMBOL(RTFileCreateTempSecure);
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