Changeset 3184 for trunk/src/kmk


Ignore:
Timestamp:
Mar 23, 2018, 11:36:43 PM (7 years ago)
Author:
bird
Message:

kFsCache,dir-nt-bird: Added locking to the cache to make it accessible from winchildren worker threads.

Location:
trunk/src/kmk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/dir-nt-bird.c

    r3169 r3184  
    3131*   Header Files                                                                                                                 *
    3232*********************************************************************************************************************************/
     33#include <Windows.h> /* locking */
    3334#include "nt/kFsCache.h"
    3435#include "makeint.h"
     
    147148 * @returns 1 if it does exist, 0 if it doesn't.
    148149 * @param   pszPath     The path to check out.
     150 * @note    Multi-thread safe.
    149151 */
    150152int file_exists_p(const char *pszPath)
    151153{
    152     int fRc;
    153     if (GetCurrentThreadId() == g_idMainThread)
    154     {
    155         KFSLOOKUPERROR  enmError;
    156         PKFSOBJ         pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
    157         if (pPathObj)
    158         {
    159             fRc = pPathObj->bObjType != KFSOBJ_TYPE_MISSING;
    160             kFsCacheObjRelease(g_pFsCache, pPathObj);
    161         }
    162         else
    163             fRc = 0;
    164     }
    165     else
    166         fRc = GetFileAttributesA(pszPath) != INVALID_FILE_ATTRIBUTES;
     154    int             fRc;
     155    KFSLOOKUPERROR  enmError;
     156    PKFSOBJ         pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
     157    if (pPathObj)
     158    {
     159        fRc = pPathObj->bObjType != KFSOBJ_TYPE_MISSING;
     160        kFsCacheObjRelease(g_pFsCache, pPathObj);
     161    }
     162    else
     163        fRc = 0;
     164    return fRc;
     165}
     166
     167
     168/**
     169 * Checks if a file exists and is a regular file, given a UTF-16 string.
     170 *
     171 * @returns 1 if it regular file, 0 if doesn't exist or isn't a file
     172 * @param   pwszPath    The UTF-16 path to check out.
     173 * @note    Multi-thread safe.
     174 */
     175int utf16_regular_file_p(const wchar_t *pwszPath)
     176{
     177    int             fRc;
     178    KFSLOOKUPERROR  enmError;
     179    PKFSOBJ         pPathObj = kFsCacheLookupW(g_pFsCache, pwszPath, &enmError);
     180    if (pPathObj)
     181    {
     182        fRc = pPathObj->bObjType == KFSOBJ_TYPE_FILE;
     183        kFsCacheObjRelease(g_pFsCache, pPathObj);
     184    }
     185    else
     186        fRc = 0;
    167187    return fRc;
    168188}
     
    483503void nt_fullpath_cached(const char *pszPath, char *pszFull, size_t cbFull)
    484504{
    485     if (GetCurrentThreadId() == g_idMainThread)
    486     {
    487         KFSLOOKUPERROR  enmError;
    488         PKFSOBJ         pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
    489         if (pPathObj)
    490         {
    491             KSIZE off = pPathObj->cchParent;
    492             if (off > 0)
    493             {
    494                 KSIZE offEnd = off + pPathObj->cchName;
    495                 if (offEnd < cbFull)
     505    KFSLOOKUPERROR  enmError;
     506    PKFSOBJ         pPathObj;
     507
     508    KFSCACHE_LOCK(g_pFsCache); /* let's start out being careful. */
     509
     510    pPathObj = kFsCacheLookupA(g_pFsCache, pszPath, &enmError);
     511    if (pPathObj)
     512    {
     513        KSIZE off = pPathObj->cchParent;
     514        if (off > 0)
     515        {
     516            KSIZE offEnd = off + pPathObj->cchName;
     517            if (offEnd < cbFull)
     518            {
     519                PKFSDIR pAncestor;
     520
     521                pszFull[offEnd] = '\0';
     522                memcpy(&pszFull[off], pPathObj->pszName, pPathObj->cchName);
     523
     524                for (pAncestor = pPathObj->pParent; off > 0; pAncestor = pAncestor->Obj.pParent)
    496525                {
    497                     PKFSDIR pAncestor;
    498 
    499                     pszFull[offEnd] = '\0';
    500                     memcpy(&pszFull[off], pPathObj->pszName, pPathObj->cchName);
    501 
    502                     for (pAncestor = pPathObj->pParent; off > 0; pAncestor = pAncestor->Obj.pParent)
     526                    kHlpAssert(off > 1);
     527                    kHlpAssert(pAncestor != NULL);
     528                    kHlpAssert(pAncestor->Obj.cchName > 0);
     529                    pszFull[--off] = '/';
     530#ifdef KFSCACHE_CFG_SHORT_NAMES
     531                    if (   pAncestor->Obj.pszName == pAncestor->Obj.pszShortName
     532                        || memchr(pAncestor->Obj.pszName, ' ', pAncestor->Obj.cchName) == NULL)
     533#endif
    503534                    {
    504                         kHlpAssert(off > 1);
    505                         kHlpAssert(pAncestor != NULL);
    506                         kHlpAssert(pAncestor->Obj.cchName > 0);
    507                         pszFull[--off] = '/';
     535                        off -= pAncestor->Obj.cchName;
     536                        kHlpAssert(pAncestor->Obj.cchParent == off);
     537                        memcpy(&pszFull[off], pAncestor->Obj.pszName, pAncestor->Obj.cchName);
     538                    }
    508539#ifdef KFSCACHE_CFG_SHORT_NAMES
    509                         if (   pAncestor->Obj.pszName == pAncestor->Obj.pszShortName
    510                             || memchr(pAncestor->Obj.pszName, ' ', pAncestor->Obj.cchName) == NULL)
     540                    else
     541                    {
     542                        /*
     543                         * The long name constains a space, so use the alternative name instead.
     544                         * Most likely the alternative name differs in length, usually it's shorter,
     545                         * so we have to shift the part of the path we've already assembled
     546                         * accordingly.
     547                         */
     548                        KSSIZE cchDelta = (KSSIZE)pAncestor->Obj.cchShortName - (KSSIZE)pAncestor->Obj.cchName;
     549                        if (cchDelta != 0)
     550                        {
     551                            if ((KSIZE)(offEnd + cchDelta) >= cbFull)
     552                                goto l_fallback;
     553                            memmove(&pszFull[off + cchDelta], &pszFull[off], offEnd + 1 - off);
     554                            off    += cchDelta;
     555                            offEnd += cchDelta;
     556                        }
     557                        off -= pAncestor->Obj.cchShortName;
     558                        kHlpAssert(pAncestor->Obj.cchParent == off);
     559                        memcpy(&pszFull[off], pAncestor->Obj.pszShortName, pAncestor->Obj.cchShortName);
     560                    }
    511561#endif
    512                         {
    513                             off -= pAncestor->Obj.cchName;
    514                             kHlpAssert(pAncestor->Obj.cchParent == off);
    515                             memcpy(&pszFull[off], pAncestor->Obj.pszName, pAncestor->Obj.cchName);
    516                         }
    517 #ifdef KFSCACHE_CFG_SHORT_NAMES
    518                         else
    519                         {
    520                             /*
    521                              * The long name constains a space, so use the alternative name instead.
    522                              * Most likely the alternative name differs in length, usually it's shorter,
    523                              * so we have to shift the part of the path we've already assembled
    524                              * accordingly.
    525                              */
    526                             KSSIZE cchDelta = (KSSIZE)pAncestor->Obj.cchShortName - (KSSIZE)pAncestor->Obj.cchName;
    527                             if (cchDelta != 0)
    528                             {
    529                                 if ((KSIZE)(offEnd + cchDelta) >= cbFull)
    530                                     goto l_fallback;
    531                                 memmove(&pszFull[off + cchDelta], &pszFull[off], offEnd + 1 - off);
    532                                 off    += cchDelta;
    533                                 offEnd += cchDelta;
    534                             }
    535                             off -= pAncestor->Obj.cchShortName;
    536                             kHlpAssert(pAncestor->Obj.cchParent == off);
    537                             memcpy(&pszFull[off], pAncestor->Obj.pszShortName, pAncestor->Obj.cchShortName);
    538                         }
    539 #endif
    540                     }
    541                     kFsCacheObjRelease(g_pFsCache, pPathObj);
    542                     return;
    543562                }
    544             }
    545             else
    546             {
    547                 if ((size_t)pPathObj->cchName + 1 < cbFull)
    548                 {
    549                     /* Assume no spaces here. */
    550                     memcpy(pszFull, pPathObj->pszName, pPathObj->cchName);
    551                     pszFull[pPathObj->cchName] = '/';
    552                     pszFull[pPathObj->cchName + 1] = '\0';
    553 
    554                     kFsCacheObjRelease(g_pFsCache, pPathObj);
    555                     return;
    556                 }
    557             }
    558 
    559             /* do fallback. */
     563                kFsCacheObjRelease(g_pFsCache, pPathObj);
     564                KFSCACHE_UNLOCK(g_pFsCache);
     565                return;
     566            }
     567        }
     568        else
     569        {
     570            if ((size_t)pPathObj->cchName + 1 < cbFull)
     571            {
     572                /* Assume no spaces here. */
     573                memcpy(pszFull, pPathObj->pszName, pPathObj->cchName);
     574                pszFull[pPathObj->cchName] = '/';
     575                pszFull[pPathObj->cchName + 1] = '\0';
     576
     577                kFsCacheObjRelease(g_pFsCache, pPathObj);
     578                KFSCACHE_UNLOCK(g_pFsCache);
     579                return;
     580            }
     581        }
     582
     583        /* do fallback. */
    560584#ifdef KFSCACHE_CFG_SHORT_NAMES
    561585l_fallback:
    562586#endif
    563             kHlpAssertFailed();
    564             kFsCacheObjRelease(g_pFsCache, pPathObj);
    565         }
    566     }
     587        kHlpAssertFailed();
     588        kFsCacheObjRelease(g_pFsCache, pPathObj);
     589    }
     590    KFSCACHE_UNLOCK(g_pFsCache);
    567591
    568592    nt_fullpath(pszPath, pszFull, cbFull);
  • trunk/src/kmk/makeint.h

    r3170 r3184  
    720720void dir_setup_glob (glob_t *);
    721721void hash_init_directories (void);
     722#if defined (KMK) && defined (KBUILD_OS_WINDOWS)
     723int utf16_regular_file_p(const wchar_t *pwszPath);
     724#endif
    722725
    723726void define_default_variables (void);
Note: See TracChangeset for help on using the changeset viewer.