VirtualBox

Changeset 2620 in kBuild for trunk/src/kObjCache


Ignore:
Timestamp:
Aug 2, 2012 4:02:29 AM (13 years ago)
Author:
bird
Message:

Try hardlink object files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kObjCache/kObjCache.c

    r2619 r2620  
    38683868
    38693869/**
     3870 * Tries to hardlink a file.
     3871 *
     3872 * @returns 1 if it succeeded, 0 if it didn't.
     3873 * @param   pszLink     The name of the hardlink.
     3874 * @param   pszLinkTo   The file to hardlinkg @a pszDst to.
     3875 */
     3876static int kOCEntryTryHardlink(const char *pszLink, const char *pszLinkTo)
     3877{
     3878#ifdef __WIN__
     3879    typedef BOOL (WINAPI *PFNCREATEHARDLINKA)(LPCSTR, LPCSTR, LPSECURITY_ATTRIBUTES);
     3880    static PFNCREATEHARDLINKA   s_pfnCreateHardLinkA = NULL;
     3881    static int                  s_fTried = FALSE;
     3882
     3883    /* The API was introduced in Windows 2000, so resolve it dynamically. */
     3884    if (!s_pfnCreateHardLinkA)
     3885    {
     3886        if (!s_fTried)
     3887        {
     3888            HMODULE hmod = LoadLibrary("KERNEL32.DLL");
     3889            if (hmod)
     3890                *(FARPROC *)&s_pfnCreateHardLinkA = GetProcAddress(hmod, "CreateHardLinkA");
     3891            s_fTried = TRUE;
     3892        }
     3893        if (!s_pfnCreateHardLinkA)
     3894            return 0;
     3895    }
     3896
     3897    if (!s_pfnCreateHardLinkA(pszLink, pszLinkTo, NULL))
     3898        return 0;
     3899#else
     3900    if (link(pszLinkTo, pszLink) != 0)
     3901        return 0;
     3902#endif
     3903    return 1;
     3904}
     3905
     3906
     3907
     3908/**
    38703909 * Worker function for kOCEntryCopy.
    38713910 *
     
    38773916{
    38783917    char *pszDst = MakePathFromDirAndFile(pszTo, pEntry->pszDir);
    3879     char *pszBuf = xmalloc(256 * 1024);
    3880     char *psz;
    3881     int fdSrc;
    3882     int fdDst;
    3883 
    3884     /*
    3885      * Open the files.
    3886      */
    3887     fdSrc = open(pszSrc, O_RDONLY | O_BINARY);
    3888     if (fdSrc == -1)
    3889         FatalDie("failed to open '%s': %s\n", pszSrc, strerror(errno));
    3890 
    38913918    unlink(pszDst);
    3892     fdDst = open(pszDst, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
    3893     if (fdDst == -1)
    3894         FatalDie("failed to create '%s': %s\n", pszDst, strerror(errno));
    3895 
    3896     /*
    3897      * Copy them.
    3898      */
    3899     for (;;)
    3900     {
    3901         /* read a chunk. */
    3902         long cbRead = read(fdSrc, pszBuf, 256*1024);
    3903         if (cbRead < 0)
     3919    if (!kOCEntryTryHardlink(pszDst, pszSrc))
     3920    {
     3921        char *pszBuf = xmalloc(256 * 1024);
     3922        char *psz;
     3923        int fdSrc;
     3924        int fdDst;
     3925
     3926        /*
     3927         * Open the files.
     3928         */
     3929        fdSrc = open(pszSrc, O_RDONLY | O_BINARY);
     3930        if (fdSrc == -1)
     3931            FatalDie("failed to open '%s': %s\n", pszSrc, strerror(errno));
     3932
     3933        fdDst = open(pszDst, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
     3934        if (fdDst == -1)
     3935            FatalDie("failed to create '%s': %s\n", pszDst, strerror(errno));
     3936
     3937        /*
     3938         * Copy them.
     3939         */
     3940        for (;;)
    39043941        {
    3905             if (errno == EINTR)
    3906                 continue;
    3907             FatalDie("read '%s' failed: %s\n", pszSrc, strerror(errno));
    3908         }
    3909         if (!cbRead)
    3910             break; /* eof */
    3911 
    3912         /* write the chunk. */
    3913         psz = pszBuf;
    3914         do
    3915         {
    3916             long cbWritten = write(fdDst, psz, cbRead);
    3917             if (cbWritten < 0)
     3942            /* read a chunk. */
     3943            long cbRead = read(fdSrc, pszBuf, 256*1024);
     3944            if (cbRead < 0)
    39183945            {
    39193946                if (errno == EINTR)
    39203947                    continue;
    3921                 FatalDie("write '%s' failed: %s\n", pszSrc, strerror(errno));
     3948                FatalDie("read '%s' failed: %s\n", pszSrc, strerror(errno));
    39223949            }
    3923             psz += cbWritten;
    3924             cbRead -= cbWritten;
    3925         } while (cbRead > 0);
    3926     }
    3927 
    3928     /* cleanup */
    3929     if (close(fdDst) != 0)
    3930         FatalDie("closing '%s' failed: %s\n", pszDst, strerror(errno));
    3931     close(fdSrc);
    3932     free(pszBuf);
     3950            if (!cbRead)
     3951                break; /* eof */
     3952
     3953            /* write the chunk. */
     3954            psz = pszBuf;
     3955            do
     3956            {
     3957                long cbWritten = write(fdDst, psz, cbRead);
     3958                if (cbWritten < 0)
     3959                {
     3960                    if (errno == EINTR)
     3961                        continue;
     3962                    FatalDie("write '%s' failed: %s\n", pszSrc, strerror(errno));
     3963                }
     3964                psz += cbWritten;
     3965                cbRead -= cbWritten;
     3966            } while (cbRead > 0);
     3967        }
     3968
     3969        /* cleanup */
     3970        if (close(fdDst) != 0)
     3971            FatalDie("closing '%s' failed: %s\n", pszDst, strerror(errno));
     3972        close(fdSrc);
     3973        free(pszBuf);
     3974    }
    39333975    free(pszDst);
    39343976    free(pszSrc);
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