- /* New idmapper cache prototype */
- #include <windows.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <sys/queue.h>
- #define IDMAPCACHE_TTL_SECONDS 60
- #define IDMAPCACHE_MAXNAME_LEN 256
- /*
- * Public API
- */
- typedef signed long idmapcache_idnumber;
- typedef struct _idmap_namestr {
- char buf[IDMAPCACHE_MAXNAME_LEN];
- size_t len;
- } idmap_namestr;
- typedef struct _idmapcache_entry {
- idmap_namestr win32name;
- idmapcache_idnumber localid;
- idmap_namestr nfsname;
- idmapcache_idnumber nfsid;
- time_t last_updated;
- } idmapcache_entry;
- typedef struct _idmapcache_context idmapcache_context;
- idmapcache_context *idmapcache_context_create(void);
- void idmapcache_context_destroy(idmapcache_context *restrict ctx);
- bool idmapcache_add(idmapcache_context *restrict ctx,
- const char *restrict win32name,
- idmapcache_idnumber localid,
- const char *restrict nfsname,
- idmapcache_idnumber nfsid);
- idmapcache_entry *idmapcache_lookup_by_win32name(idmapcache_context *restrict ctx,
- const char *restrict win32name);
- idmapcache_entry *idmapcache_lookup_by_localid(idmapcache_context *restrict ctx,
- idmapcache_idnumber search_localid);
- idmapcache_entry *idmapcache_lookup_by_nfsname(idmapcache_context *restrict ctx,
- const char *restrict nfsname);
- idmapcache_entry *idmapcache_lookup_by_nfsid(idmapcache_context *restrict ctx,
- idmapcache_idnumber search_nfslid);
- void idmapcache_entry_refcount_inc(idmapcache_entry *restrict e);
- void idmapcache_entry_refcount_dec(idmapcache_entry *restrict e);
- /*
- * Private
- */
- struct idmapcache_node {
- struct _idmapcache_entry entry;
- LIST_ENTRY(idmapcache_node) list_node;
- volatile LONG refcounter;
- };
- LIST_HEAD(idmapcache_head, idmapcache_node);
- typedef struct _idmapcache_context {
- struct idmapcache_head head;
- SRWLOCK lock;
- } idmapcache_context;
- typedef bool (*idmapcache_cmp_fn)(const struct idmapcache_node *restrict entry, const void *restrict search_val);
- void idmapcache_entry_refcount_inc(idmapcache_entry *restrict e)
- {
- struct idmapcache_node *en = (struct idmapcache_node *)e;
- (void)InterlockedIncrement(&en->refcounter);
- }
- void idmapcache_entry_refcount_dec(idmapcache_entry *restrict e)
- {
- struct idmapcache_node *en = (struct idmapcache_node *)e;
- if (InterlockedDecrement(&en->refcounter) == 0) {
- }
- }
- static
- bool cmp_by_win32name(const struct idmapcache_node *restrict entry, const void *restrict search_val)
- {
- const idmap_namestr *search_str = (const idmap_namestr *)search_val;
- if (search_str->len != entry->entry.win32name.len) {
- return false;
- }
- }
- static
- bool cmp_by_nfsname(const struct idmapcache_node *restrict entry, const void *restrict search_val)
- {
- const idmap_namestr *search_str = (const idmap_namestr *)search_val;
- if (search_str->len != entry->entry.nfsname.len) {
- return false;
- }
- }
- static
- bool cmp_by_localid(const struct idmapcache_node *restrict entry, const void *restrict search_val)
- {
- return entry->entry.localid == *(const idmapcache_idnumber *)search_val;
- }
- static
- bool cmp_by_nfsid(const struct idmapcache_node *restrict entry, const void *restrict search_val)
- {
- return entry->entry.nfsid == *(const idmapcache_idnumber *)search_val;
- }
- static
- idmapcache_entry *idmapcache_lookup(idmapcache_context *restrict ctx,
- idmapcache_cmp_fn cmp,
- const void *restrict search_val)
- {
- struct idmapcache_node *found_node = NULL;
- time_t current_time;
- struct idmapcache_node *node;
- AcquireSRWLockShared(&ctx->lock);
- LIST_FOREACH(node, &ctx->head, list_node) {
- if ((current_time - node->entry.last_updated) > IDMAPCACHE_TTL_SECONDS)
- continue;
- if (cmp(node, search_val)) {
- found_node = node;
- idmapcache_entry_refcount_inc(&found_node->entry);
- break;
- }
- }
- ReleaseSRWLockShared(&ctx->lock);
- return &found_node->entry;
- }
- static
- void cleanup_expired_entries(idmapcache_context *restrict ctx, time_t current_time)
- {
- struct idmapcache_node *node;
- struct idmapcache_node *tmpnode;
- for (node = LIST_FIRST(&ctx->head) ; node != NULL ; node = tmpnode) {
- tmpnode = LIST_NEXT(node, list_node);
- if ((current_time - node->entry.last_updated) > IDMAPCACHE_TTL_SECONDS) {
- LIST_REMOVE(node, list_node);
- idmapcache_entry_refcount_dec(&node->entry);
- }
- }
- }
- idmapcache_context *idmapcache_context_create(void)
- {
- if (ctx == NULL)
- return NULL;
- InitializeSRWLock(&ctx->lock);
- LIST_INIT(&ctx->head);
- return ctx;
- }
- void idmapcache_context_destroy(idmapcache_context *restrict ctx)
- {
- AcquireSRWLockExclusive(&ctx->lock);
- struct idmapcache_node *node = LIST_FIRST(&ctx->head);
- struct idmapcache_node *tmp;
- while (node != NULL) {
- tmp = LIST_NEXT(node, list_node);
- LIST_REMOVE(node, list_node);
- idmapcache_entry_refcount_dec(&node->entry);
- node = tmp;
- }
- ReleaseSRWLockExclusive(&ctx->lock);
- }
- bool idmapcache_add(idmapcache_context *restrict ctx,
- const char *restrict win32name,
- idmapcache_idnumber localid,
- const char *restrict nfsname,
- idmapcache_idnumber nfsid)
- {
- if (win32name_len >= IDMAPCACHE_MAXNAME_LEN)
- return false;
- if (nfsname_len >= IDMAPCACHE_MAXNAME_LEN)
- return false;
- if (new_entry == NULL)
- return false;
- new_entry->refcounter = 1L;
- new_entry->entry.win32name.buf[win32name_len] = '\0';
- new_entry->entry.win32name.len = win32name_len;
- new_entry->entry.nfsname.buf[nfsname_len] = '\0';
- new_entry->entry.nfsname.len = nfsname_len;
- new_entry->entry.localid = localid;
- new_entry->entry.nfsid = nfsid;
- AcquireSRWLockExclusive(&ctx->lock);
- new_entry->entry.last_updated = current_time;
- cleanup_expired_entries(ctx, current_time);
- LIST_INSERT_HEAD(&ctx->head, new_entry, list_node);
- ReleaseSRWLockExclusive(&ctx->lock);
- return true;
- }
- idmapcache_entry *idmapcache_lookup_by_win32name(idmapcache_context *restrict ctx,
- const char *restrict win32name)
- {
- idmap_namestr search_term;
- if (search_term.len >= IDMAPCACHE_MAXNAME_LEN)
- return NULL;
- return idmapcache_lookup(ctx, cmp_by_win32name, &search_term);
- }
- idmapcache_entry *idmapcache_lookup_by_localid(idmapcache_context *restrict ctx,
- idmapcache_idnumber search_localid)
- {
- return idmapcache_lookup(ctx, cmp_by_localid, &search_localid);
- }
- idmapcache_entry *idmapcache_lookup_by_nfsname(idmapcache_context *restrict ctx,
- const char *restrict nfsname)
- {
- idmap_namestr search_term;
- if (search_term.len >= IDMAPCACHE_MAXNAME_LEN)
- return NULL;
- return idmapcache_lookup(ctx, cmp_by_nfsname, &search_term);
- }
- idmapcache_entry *idmapcache_lookup_by_nfsid(idmapcache_context *restrict ctx,
- idmapcache_idnumber search_nfsid)
- {
- return idmapcache_lookup(ctx, cmp_by_nfsid, &search_nfsid);
- }
- int main(int ac, char *av[])
- {
- (void)ac;
- (void)av;
- idmapcache_context *cache = idmapcache_context_create();
- if (cache == NULL) {
- return 1;
- }
- (void)idmapcache_add(cache, "System2", 100, "Kernel2", 1000);
- (void)idmapcache_add(cache, "User1", 200, "Explorer1", 2000);
- idmapcache_entry *result;
- result = idmapcache_lookup_by_win32name(cache, "System2");
- if (result) {
- (void)printf("[OK] Found by win32name ('System'): localid = %ld, nfsid = %ld\n", (long)result->localid, (long)result->nfsid);
- idmapcache_entry_refcount_dec(result);
- } else {
- }
- result = idmapcache_lookup_by_nfsname(cache, "Explorer1");
- if (result) {
- (void)printf("[OK] Found by nfsname ('Explorer'): win32name = %s, localid = %ld\n", result->win32name.buf, (long)result->localid);
- idmapcache_entry_refcount_dec(result);
- } else {
- }
- idmapcache_idnumber search_localid = 200;
- result = idmapcache_lookup_by_localid(cache, search_localid);
- if (result) {
- (void)printf("[OK] Found by localid (200): win32name = %s, nfsname = %s\n", result->win32name.buf, result->nfsname.buf);
- idmapcache_entry_refcount_dec(result);
- } else {
- }
- idmapcache_idnumber search_nfsid = 2000;
- result = idmapcache_lookup_by_nfsid(cache, search_nfsid);
- if (result) {
- (void)printf("[OK] Found by nfsid (2000): win32name = %s, nfsname = %s\n", result->win32name.buf, result->nfsname.buf);
- idmapcache_entry_refcount_dec(result);
- } else {
- }
- idmapcache_context_destroy(cache);
- return 0;
- }
New idmapper cache prototype
Posted by Anonymous on Mon 30th Mar 2026 15:26
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
nrubsig.kpaste.net RSS