Changeset 975


Ignore:
Timestamp:
Aug 26, 2016, 6:57:36 PM (9 years ago)
Author:
Yuri Dario
Message:

Made code completely independent from samba data structures. ticket#274.

Location:
trunk/client/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/client/src/dircache.c

    r969 r975  
    7979    }
    8080
    81     pdce->aInfos = (smbwrp_fileinfo *)malloc(cFiles * sizeof (smbwrp_fileinfo));
     81    pdce->aInfos = (DirectoryCacheEntryData *)
     82            malloc(cFiles * sizeof (DirectoryCacheEntryData));
    8283    if (!pdce->aInfos)
    8384    {
     
    103104}
    104105
    105 static void dceDelete (DirectoryCacheEntry *pdce)
    106 {
     106static void dceDelete (DirectoryCache *pdc, DirectoryCacheEntry *pdce)
     107{
     108    int i;
     109
     110    debuglocal(pdc->resource, 9, "dceDelete: pdc %p, pdce %p\n", pdc, pdce);
     111
     112    /* use plugin callback to deallocate memory stored into aInfos */
     113    if (pdc->release)
     114       for (i = 0; i < pdce->cInfos; i++)
     115       {
     116          debuglocal(pdc->resource, 9, "dceDelete: #%d->%s->%p\n",
     117                     i, pdce->aInfos[i].fname, pdce->aInfos[i].customData);
     118          pdc->release( (void*)pdce->aInfos[i].customData);
     119       }
     120
     121    /* now free dce local data */
    107122    free(pdce->aInfos);
    108123    free(pdce->pszPath);
     
    115130}
    116131
    117 static void dceWriteEntry (DirectoryCache *pdc, DirectoryCacheEntry *pdce, const smbwrp_fileinfo *finfo)
     132static void dceWriteEntry (DirectoryCache *pdc, DirectoryCacheEntry *pdce,
     133                           const char *fname, const void *finfo)
    118134{
    119135    if (pdce->cInfos >= pdce->cInfosAllocated)
     
    122138        int cNewAllocated = pdce->cInfosAllocated + pdce->cInfosAllocated / 2 + 1;
    123139
    124         smbwrp_fileinfo *pNewInfos = (smbwrp_fileinfo *)realloc(pdce->aInfos,
    125                                                                 cNewAllocated * sizeof (smbwrp_fileinfo));
     140        DirectoryCacheEntryData *pNewInfos = (DirectoryCacheEntryData *)
     141                realloc(pdce->aInfos, cNewAllocated *
     142                        sizeof (DirectoryCacheEntryData));
    126143
    127144        debuglocal(pdc->resource, 9, "dceWriteEntry: [%s] realloc %d -> %d\n", pdce->pszPath, pdce->cInfosAllocated, cNewAllocated);
     
    139156    }
    140157
    141     pdce->aInfos[pdce->cInfos] = *finfo;
     158    // store entry name and custom data
     159    strcpy( (char*)pdce->aInfos[pdce->cInfos].fname, fname);
     160    pdce->aInfos[pdce->cInfos].customData = finfo;
    142161    pdce->cInfos++;
    143162}
     
    153172        int cNewAllocated = pdce->cInfos + cFree / 2;
    154173
    155         smbwrp_fileinfo *pNewInfos = (smbwrp_fileinfo *)realloc(pdce->aInfos,
    156                                                                 cNewAllocated * sizeof (smbwrp_fileinfo));
     174        DirectoryCacheEntryData *pNewInfos = (DirectoryCacheEntryData *)
     175                realloc(pdce->aInfos,
     176                        cNewAllocated * sizeof (DirectoryCacheEntryData));
    157177
    158178        debuglocal(pdc->resource, 9, "dceAdjust: [%s] realloc %d -> %d\n", pdce->pszPath, pdce->cInfosAllocated, cNewAllocated);
     
    191211    DirectoryCacheEntry *iter = pdc->pEntriesHead;
    192212
    193     debuglocal(pdc->resource, 9, "findDirCache: [%s]\n", path);
     213    debuglocal(pdc->resource, 9, "dcFindDirCache: [%s]\n", path);
     214    debuglocal(pdc->resource, 9, "dcFindDirCache: iter [%p]\n", iter);
    194215
    195216    while (iter != NULL)
    196217    {
    197         debuglocal(pdc->resource, 9, "findDirCache: entry [%s]\n", iter->pszPath);
     218        debuglocal(pdc->resource, 9, "dcFindDirCache: entry [%s]\n", iter->pszPath);
    198219        if (fPrefix)
    199220        {
     
    215236    }
    216237
    217     debuglocal(pdc->resource, 9, "findDirCache: %p\n", iter);
     238    debuglocal(pdc->resource, 9, "dcFindDirCache: %p\n", iter);
    218239
    219240    return iter;
     
    253274{
    254275    DirectoryCacheEntry *iter = pdc->pEntriesHead;
     276    debuglocal(pdc->resource, 9, "dcDelete: pdc %p, iter %p\n", pdc, iter);
    255277
    256278    while (iter != NULL)
     
    258280        DirectoryCacheEntry *next = iter->pNext;
    259281
    260         dceDelete (iter);
     282        dceDelete (pdc, iter);
    261283       
    262284        iter = next;
     
    357379                for (i = 0; i < pdce->cInfos; i++)
    358380                {
    359                     fn ("", &pdce->aInfos[i], path, plist);
     381                    fn (plist, (void*)pdce->aInfos[i].customData);
    360382                }
    361383
     
    368390                    debuglocal(pdc->resource, 9, "dcRead: expired\n");
    369391                    dcRemoveEntry (pdc, pdce);
    370                     dceDelete (pdce);
     392                    dceDelete (pdc, pdce);
    371393                }
    372394                else
    373395                {
     396                    debuglocal(pdc->resource, 9, "dcRead: adding %d entries from cache\n", pdce->cInfos);
    374397                    for (i = 0; i < pdce->cInfos; i++)
    375398                    {
    376                         fn ("", &pdce->aInfos[i], path, plist);
     399                        debuglocal(pdc->resource, 9, "dcRead: #%d->%s->%p\n",
     400                                   i, pdce->aInfos[i].fname, pdce->aInfos[i].customData);
     401                        fn (plist, (void*)pdce->aInfos[i].customData);
    377402                    }
    378403
     
    437462    {
    438463        dcRemoveEntry (pdc, pdce);
    439         dceDelete (pdce);
     464        dceDelete (pdc, pdce);
    440465       
    441466        pdce = dcFindDirCache (pdc, path, fPrefix);
     
    447472                       const char *parent,
    448473                       const char *name,
    449                        smbwrp_fileinfo *finfo,
     474                       void **finfo,
    450475                       unsigned long *pulAge)
    451476{
     
    469494                if (ph->fsphStrICmp (pdce->aInfos[i].fname, name) == 0)
    470495                {
    471                     *finfo = pdce->aInfos[i];
     496                    *finfo = (void*) pdce->aInfos[i].customData;
    472497                    *pulAge = timesec () - pdce->ulLastUpdateTime;
    473                     debuglocal(pdc->resource, 9, "dircache: FindPath %s found, age %d\n", path, *pulAge);
     498                    debuglocal(pdc->resource, 9, "dcFindPath: FindPath %s found, age %d\n", path, *pulAge);
    474499                    return 1;
    475500                }
     
    480505    }
    481506
    482     debuglocal(pdc->resource, 9, "dircache: FindPath %s not found\n", path);
     507    debuglocal(pdc->resource, 9, "dcFindPath: FindPath %s not found\n", path);
    483508    return 0;
    484509}
     
    516541
    517542int dircache_create( DirectoryCache **ppdc, void* pRes,
    518                      int cachetimeout, int cachedepth)
     543                     int cachetimeout, int cachedepth, PFNFREEDIRENTRY _release)
    519544{
    520545    unsigned long ulExpirationTime = cachetimeout;
     
    527552    if (rc == NO_ERROR)
    528553    {
     554        (*ppdc)->release = _release;
    529555        dcEnable (*ppdc, 1, ulExpirationTime, cMaxEntries);
    530556
     
    554580    int cbPath = strlen(fullpath) + 1;
    555581    char *path = (char*) alloca(cbPath);
     582    debuglocal(pdc->resource, 9, "dircache_list_files pdc %p, dir_mask [%s], fullpath [%s]\n",
     583               pdc, dir_mask, fullpath);
     584
    556585    if (!dcCreateDirPath(path, cbPath, dir_mask, fullpath))
    557586    {
     587        debuglocal(pdc->resource, 9, "dircache_list_files pdc %p, dcCreateDirPath failed\n",
     588                   pdc);
    558589        return 0;
    559590    }
     
    571602    lockRelease (pdc->mutex);
    572603
     604    debuglocal(pdc->resource, 9, "dircache_list_files rc=%d\n", (rc == CacheOk));
    573605    return (rc == CacheOk);
    574606}
     
    579611{
    580612    DirectoryCacheEntry *pdce = NULL;
     613    debuglocal(pdc->resource, 9, "dircache_write_begin pdc %p path [%s]\n", pdc, fullpath);
    581614
    582615    int cbPath = strlen(fullpath) + 1;
     
    584617    if (!dcCreateDirPath(path, cbPath, dir_mask, fullpath))
    585618    {
    586         return NULL;
    587     }
    588 
    589     debuglocal(pdc->resource, 9, "dircache_write_begin pdc %p path [%s]\n", pdc, path);
    590 
    591     if (!pdc)
    592     {
     619        debuglocal(pdc->resource, 9, "dircache_write_begin dcCreateDirPath failed\n");
    593620        return NULL;
    594621    }
     
    601628        pdce = pdc->pEntriesTail;
    602629        dcRemoveEntry (pdc, pdce);
    603         dceDelete (pdce);
     630        dceDelete (pdc, pdce);
    604631        pdce = NULL;
    605632    }
     
    614641
    615642void dircache_write_entry(DirectoryCache *pdc, void *dircachectx,
    616                           const smbwrp_fileinfo *finfo)
     643                          const char *fname, const void *finfo)
    617644{
    618645    DirectoryCacheEntry *pdce = (DirectoryCacheEntry *)dircachectx;
     
    627654    lockRequest (pdc->mutex);
    628655   
    629     dceWriteEntry (pdc, pdce, finfo);
     656    dceWriteEntry (pdc, pdce, fname, finfo);
    630657   
    631658    lockRelease (pdc->mutex);
     
    648675    {
    649676        /* Something happened during writing to the entry. Delete it. */
    650         dceDelete (pdce);
     677        dceDelete (pdc, pdce);
    651678    }
    652679    else
     
    702729int dircache_find_path(DirectoryCache *pdc,
    703730                       const char *path,
    704                        smbwrp_fileinfo *finfo,
     731                       void **finfo,
    705732                       unsigned long *pulAge)
    706733{
     
    745772    lockRelease (pdc->mutex);
    746773
     774    debuglocal(pdc->resource, 9, "dircache_find_path fFound %d\n", fFound);
    747775    return fFound;
    748776}
  • trunk/client/src/dircache.h

    r969 r975  
    1313#include <ndextpl2.h>
    1414
    15 //
    16 // enable if not defined in your plugin
    17 //
    18 // dircache code uses only the fname field to scan the directory listing.
    19 // other fields are just passed to FNADD callback as are.
    20 //
    21 #if 0
    22 typedef struct smbwrp_fileinfo
     15
     16/* callback for fsphAddFile32L */
     17typedef void FNADDDIRENTRY(void *plist, void *finfo);
     18typedef FNADDDIRENTRY *PFNADDDIRENTRY;
     19
     20/* callback for releasing memory */
     21typedef void FNFREEDIRENTRY(void *finfo);
     22typedef FNFREEDIRENTRY *PFNFREEDIRENTRY;
     23
     24
     25/*
     26 * An entry holds file name and a pointer to custom data
     27 */
     28typedef struct DirectoryCacheEntryData
    2329{
    24     unsigned long long size;
    25     unsigned long mtime;
    26     unsigned type;
    27     char fname[PATH_MAX];
    28 } smbwrp_fileinfo;
    29 #else
    30 
    31 // define as forward reference here
    32 typedef struct smbwrp_fileinfo smbwrp_fileinfo;
    33 
    34 #endif
     30    const char  fname[PATH_MAX];
     31    const void* customData;
     32} DirectoryCacheEntryData;
    3533
    3634/*
     
    4240    struct DirectoryCacheEntry *pPrev;
    4341
    44     smbwrp_fileinfo *aInfos;
     42    DirectoryCacheEntryData *aInfos;
    4543    int cInfos;
    4644    int cInfosAllocated;
     
    6462    // resource handle, used only for per-share logging
    6563    void* resource;
     64    // callback called to release data structures
     65    PFNFREEDIRENTRY release;
     66
    6667} DirectoryCache;
    6768
     
    7374
    7475/* Directory cache helpers. */
    75 int dircache_create(DirectoryCache **ppdc, void* pRes, int cachetimeout, int cachedepth);
     76int dircache_create(DirectoryCache **ppdc, void* pRes, int cachetimeout, int cachedepth,
     77                    PFNFREEDIRENTRY fn);
    7678void dircache_delete(DirectoryCache *pdc);
    77 
    78 /* callback for fsphAddFile32L */
    79 typedef void FNADDDIRENTRY(const char *mnt, smbwrp_fileinfo *finfo,
    80                            const char *mask, void *state);
    81 typedef FNADDDIRENTRY *PFNADDDIRENTRY;
    8279
    8380/* directory cache scanning */
     
    8885int dircache_find_path(DirectoryCache *pdc,
    8986                       const char *path,
    90                        smbwrp_fileinfo *finfo,
     87                       void **finfo,
    9188                       unsigned long *pulAge);
    9289
     
    9693                           int cFiles);
    9794void dircache_write_entry(DirectoryCache *pdc, void *dircachectx,
    98                           const smbwrp_fileinfo *finfo);
     95                          const char *fname, const void *finfo);
    9996void dircache_write_end(DirectoryCache *pdc, void *dircachectx);
    10097
Note: See TracChangeset for help on using the changeset viewer.