Logo Search packages:      
Sourcecode: virtualbox-ose version File versions

static int krdrFileCreate ( PPKRDR  ppRdr,
const char *  pszFilename 
) [static]

Try create a new file provider instance.

Returns:
0 on success, OS specific error code on failure.
Parameters:
ppRdr Where to store the file provider instance.
pszFilename The filename to open.

Todo:
normalize the filename.

Definition at line 1152 of file kRdrFile.cpp.

References KRDRFILE::cb, KRDRFILE::cMappings, KRDRFILE::Core, KRDRFILE::cPreps, DWORD, KRDRFILE::File, KERR_NO_MEMORY, KERR_OUT_OF_RANGE, KRDR_MAGIC, KSIZE, NULL, KRDRFILE::off, KRDR::pOps, KRDRFILE::szFilename, and KRDR::u32Magic.

{
    KSIZE       cchFilename;
    PKRDRFILE   pRdrFile;

    /*
     * Open the file, determin its size and correct filename.
     */
#if K_OS == K_OS_DARWIN \
 || K_OS == K_OS_FREEBSD \
 || K_OS == K_OS_LINUX \
 || K_OS == K_OS_NETBSD \
 || K_OS == K_OS_OPENBSD \
 || K_OS == K_OS_SOLARIS
    int         File;
    KFOFF       cb;
    KFOFF       rc;
    char        szFilename[1024];

    cchFilename = kHlpStrLen(pszFilename);
    if (cchFilename >= sizeof(szFilename))
        return KERR_OUT_OF_RANGE;
    kHlpMemCopy(szFilename, pszFilename, cchFilename + 1);
    /** @todo normalize the filename. */

# ifdef O_BINARY
    File = kHlpSys_open(pszFilename, O_RDONLY | O_BINARY, 0);
# else
    File = kHlpSys_open(pszFilename, O_RDONLY, 0);
# endif
    if (File < 0)
        return -File;

    cb = kHlpSys_lseek(File, SEEK_END, 0);
    rc = kHlpSys_lseek(File, SEEK_SET, 0);
    if (    cb < 0
        ||  rc < 0)
    {
        kHlpSys_close(File);
        return cb < 0 ? -cb : -rc;
    }

#elif K_OS == K_OS_OS2
    ULONG       ulAction = 0;
    FILESTATUS3 Info;
    APIRET      rc;
    HFILE       File = 0;
    KFOFF       cb;
    char        szFilename[CCHMAXPATH];

    if ((uintptr_t)pszFilename >= 0x20000000)
    {
        char *psz;
        cchFilename = kHlpStrLen(szFilename);
        psz = (char *)kHlpAllocA(cchFilename + 1);
        kHlpMemCopy(psz, pszFilename, cchFilename + 1);
        pszFilename = psz;
    }
    rc = DosOpen((PCSZ)pszFilename, &File, &ulAction, 0, FILE_NORMAL,
                 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
                 OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE | OPEN_ACCESS_READONLY | OPEN_FLAGS_RANDOMSEQUENTIAL,
                 NULL);
    if (rc)
        return rc;

    rc = DosQueryPathInfo((PCSZ)pszFilename, FIL_QUERYFULLNAME, szFilename, sizeof(szFilename));
    if (rc)
    {
        DosClose(File);
        return rc;
    }

    rc = DosQueryFileInfo(File, FIL_STANDARD, &Info, sizeof(Info));
    if (rc)
    {
        DosClose(File);
        return rc;
    }
    cb = Info.cbFile;

#elif K_OS == K_OS_WINDOWS
    SECURITY_ATTRIBUTES SecAttr;
    DWORD               High;
    DWORD               Low;
    int                 rc;
    HANDLE              File;
    KFOFF            cb;
    char                szFilename[MAX_PATH];

    SecAttr.bInheritHandle = FALSE;
    SecAttr.lpSecurityDescriptor = NULL;
    SecAttr.nLength = 0;
    File = CreateFile(pszFilename, GENERIC_READ | GENERIC_EXECUTE, FILE_SHARE_READ, &SecAttr, OPEN_EXISTING, 0, NULL);
    if (File == INVALID_HANDLE_VALUE)
        return GetLastError();

    if (!GetFullPathName(pszFilename, sizeof(szFilename), szFilename, NULL))
    {
        rc = GetLastError();
        CloseHandle(File);
        return rc;
    }

    SetLastError(0);
    Low = GetFileSize(File, &High);
    rc = GetLastError();
    if (rc)
    {
        CloseHandle(File);
        return rc;
    }
    cb = ((KFOFF)High << 32) | Low;

#else
# error "port me"
#endif


    /*
     * Allocate the reader instance.
     */
    cchFilename = kHlpStrLen(szFilename);
    pRdrFile = (PKRDRFILE)kHlpAlloc(sizeof(*pRdrFile) + cchFilename);
    if (!pRdrFile)
    {
#if K_OS == K_OS_DARWIN \
 || K_OS == K_OS_FREEBSD \
 || K_OS == K_OS_LINUX \
 || K_OS == K_OS_NETBSD \
 || K_OS == K_OS_OPENBSD \
 || K_OS == K_OS_SOLARIS
        kHlpSys_close(File);
#elif K_OS == K_OS_OS2
        DosClose(File);
#elif K_OS == K_OS_WINDOWS
        CloseHandle(File);
#else
# error "port me"
#endif
        return KERR_NO_MEMORY;
    }

    /*
     * Initialize it and return successfully.
     */
    pRdrFile->Core.u32Magic = KRDR_MAGIC;
    pRdrFile->Core.pOps = &g_kRdrFileOps;
    pRdrFile->File = File;
    pRdrFile->cb = cb;
    pRdrFile->off = 0;
    pRdrFile->cMappings = 0;
    pRdrFile->cPreps = 0;
    kHlpMemCopy(&pRdrFile->szFilename[0], szFilename, cchFilename + 1);

    *ppRdr = &pRdrFile->Core;
    return 0;
}


Generated by  Doxygen 1.6.0   Back to index