VirtualBox

Changeset 96077 in vbox


Ignore:
Timestamp:
Aug 6, 2022 1:58:08 AM (3 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
152876
Message:

IPRT/RTStream: Added a RTStrmOpenFileHandle API for implementing fdopen & tmpfile[_s]. bugref:10261

Location:
trunk
Files:
2 edited

Legend:

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

    r95977 r96077  
    127127 */
    128128RTR3DECL(int) RTStrmOpenF(const char *pszMode, PRTSTREAM *ppStream, const char *pszFilenameFmt, ...) RT_IPRT_FORMAT_ATTR(3, 4);
     129
     130/**
     131 * Opens a file stream for a RTFILE handle, taking ownership of the handle.
     132 *
     133 * @returns iprt status code.
     134 * @param   hFile           The file handle to use.  On success, handle
     135 *                          ownership is transfered to the stream and it will be
     136 *                          closed when the stream closes.
     137 * @param   pszMode         The open mode, accept the same as RTStrOpen and
     138 *                          friends however it is only used to figure out what
     139 *                          we can do with the handle.
     140 * @param   fFlags          Reserved, must be zero.
     141 * @param   ppStream        Where to store the opened stream.
     142 */
     143RTR3DECL(int) RTStrmOpenFileHandle(RTFILE hFile, const char *pszMode, uint32_t fFlags, PRTSTREAM *ppStream);
    129144
    130145/**
  • trunk/src/VBox/Runtime/r3/stream.cpp

    r95980 r96077  
    408408 *
    409409 * @returns iprt status code.
    410  * @param   pszFilename     Path to the file to open.
    411  * @param   pszMode         The open mode. See fopen() standard.
    412  *                          Format: <a|r|w>[+][b|t][x][e|N|E]
    413  *                              - 'a': Open or create file and writes
    414  *                                append tos it.
    415  *                              - 'r': Open existing file and read from it.
    416  *                              - 'w': Open or truncate existing file and write
    417  *                                to it.
    418  *                              - '+': Open for both read and write access.
    419  *                              - 'b' / 't': binary / text
    420  *                              - 'x': exclusively create, no open. Only
    421  *                                possible with 'w'.
    422  *                              - 'e' / 'N': No inherit on exec.  (The 'e' is
    423  *                                how Linux and FreeBSD expresses this, the
    424  *                                latter is Visual C++).
     410 * @param   pszFilename     Path to the file to open, hFile must be NIL_RTFILE.
     411 *                          NULL if a hFile is to be used instead.
     412 * @param   hFile           File handle to use when called from
     413 *                          RTStrmOpenFileHandle.  pszFilename must be NULL.
     414 * @param   pszMode         See RTStrmOpen.
    425415 * @param   ppStream        Where to store the opened stream.
    426416 */
    427 RTR3DECL(int) RTStrmOpen(const char *pszFilename, const char *pszMode, PRTSTREAM *ppStream)
     417static int rtStrmOpenComon(const char *pszFilename, RTFILE hFile, const char *pszMode, PRTSTREAM *ppStream)
    428418{
    429419    /*
     
    431421     */
    432422    AssertReturn(pszMode && *pszMode, VERR_INVALID_FLAGS);
    433     AssertReturn(pszFilename, VERR_INVALID_PARAMETER);
    434423
    435424    /*
     
    551540        pStream->pCritSect          = NULL;
    552541#endif
    553         RTFILE       hFile          = NIL_RTFILE;
    554542        RTFILEACTION enmActionTaken = RTFILEACTION_INVALID;
    555         rc = RTFileOpenEx(pszFilename, fOpen, &hFile, &enmActionTaken);
     543        if (pszFilename)
     544            rc = RTFileOpenEx(pszFilename, fOpen, &hFile, &enmActionTaken);
     545        else
     546            rc = VINF_SUCCESS;
    556547        if (RT_SUCCESS(rc))
    557548        {
     
    594585                close(fd);
    595586                hFile = NIL_RTFILE;
     587                /** @todo we're in trouble here when called from RTStrmOpenFileHandle!   */
    596588# endif
    597589            }
     
    604596# endif
    605597            }
    606             RTFileClose(hFile);
    607             if (enmActionTaken == RTFILEACTION_CREATED)
    608                 RTFileDelete(pszFilename);
     598            if (pszFilename)
     599            {
     600                RTFileClose(hFile);
     601                if (enmActionTaken == RTFILEACTION_CREATED)
     602                    RTFileDelete(pszFilename);
     603            }
    609604#endif
    610605        }
     
    612607    }
    613608    return rc;
     609}
     610
     611
     612/**
     613 * Opens a file stream.
     614 *
     615 * @returns iprt status code.
     616 * @param   pszFilename     Path to the file to open.
     617 * @param   pszMode         The open mode. See fopen() standard.
     618 *                          Format: <a|r|w>[+][b|t][x][e|N|E]
     619 *                              - 'a': Open or create file and writes
     620 *                                append tos it.
     621 *                              - 'r': Open existing file and read from it.
     622 *                              - 'w': Open or truncate existing file and write
     623 *                                to it.
     624 *                              - '+': Open for both read and write access.
     625 *                              - 'b' / 't': binary / text
     626 *                              - 'x': exclusively create, no open. Only
     627 *                                possible with 'w'.
     628 *                              - 'e' / 'N': No inherit on exec.  (The 'e' is
     629 *                                how Linux and FreeBSD expresses this, the
     630 *                                latter is Visual C++).
     631 * @param   ppStream        Where to store the opened stream.
     632 */
     633RTR3DECL(int) RTStrmOpen(const char *pszFilename, const char *pszMode, PRTSTREAM *ppStream)
     634{
     635    *ppStream = NULL;
     636    AssertReturn(pszFilename, VERR_INVALID_PARAMETER);
     637    return rtStrmOpenComon(pszFilename, NIL_RTFILE, pszMode, ppStream);
    614638}
    615639
     
    658682    va_end(args);
    659683    return rc;
     684}
     685
     686
     687/**
     688 * Opens a file stream for a RTFILE handle, taking ownership of the handle.
     689 *
     690 * @returns iprt status code.
     691 * @param   hFile           The file handle to use.  On success, handle
     692 *                          ownership is transfered to the stream and it will be
     693 *                          closed when the stream closes.
     694 * @param   pszMode         The open mode, accept the same as RTStrOpen and
     695 *                          friends however it is only used to figure out what
     696 *                          we can do with the handle.
     697 * @param   fFlags          Reserved, must be zero.
     698 * @param   ppStream        Where to store the opened stream.
     699 */
     700RTR3DECL(int) RTStrmOpenFileHandle(RTFILE hFile, const char *pszMode, uint32_t fFlags, PRTSTREAM *ppStream)
     701{
     702    *ppStream = NULL;
     703    AssertReturn(RTFileIsValid(hFile), VERR_INVALID_HANDLE);
     704    AssertReturn(fFlags == 0, VERR_INVALID_FLAGS);
     705    return rtStrmOpenComon(NULL, hFile, pszMode, ppStream);
    660706}
    661707
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