Windows: cm_BPlusDirIsEmpty
authorJeffrey Altman <jaltman@your-file-system.com>
Fri, 12 Apr 2013 05:09:13 +0000 (01:09 -0400)
committerJeffrey Altman <jaltman@your-file-system.com>
Wed, 17 Apr 2013 16:42:15 +0000 (09:42 -0700)
Provide a function to determine if a directory is empty or not.

Change-Id: Ib1fa642b02ad67ffdba73da1b7c6091a799de0c3
Reviewed-on: http://gerrit.openafs.org/9777
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Jeffrey Altman <jaltman@your-file-system.com>

src/WINNT/afsd/cm_btree.c
src/WINNT/afsd/cm_btree.h

index 48e1027..82cd9d3 100644 (file)
@@ -1997,6 +1997,57 @@ int  cm_BPlusDirDeleteEntry(cm_dirOp_t * op, clientchar_t *centry)
 
 }
 
+/*
+   On entry:
+       op->scp->dirlock is read locked
+
+   On exit:
+       op->scp->dirlock is read locked
+
+   Return:
+
+*/
+int cm_BPlusDirIsEmpty(cm_dirOp_t *op, afs_uint32 *pbEmpty)
+{
+    int rc = 0;
+    afs_uint32 count = 0, slot, numentries;
+    Nptr leafNode = NONODE, nextLeafNode;
+    Nptr firstDataNode, dataNode, nextDataNode;
+
+    if (op->scp->dirBplus == NULL ||
+        op->dataVersion != op->scp->dirDataVersion) {
+        rc = EINVAL;
+        goto done;
+    }
+
+    /* If we find any entry that is not "." or "..", the directory is not empty */
+
+    for (count = 0, leafNode = getleaf(op->scp->dirBplus); leafNode; leafNode = nextLeafNode) {
+
+       for ( slot = 1, numentries = numentries(leafNode); slot <= numentries; slot++) {
+           firstDataNode = getnode(leafNode, slot);
+
+           for ( dataNode = firstDataNode; dataNode; dataNode = nextDataNode) {
+
+                if ( cm_ClientStrCmp(getdatavalue(dataNode).cname, L".") &&
+                     cm_ClientStrCmp(getdatavalue(dataNode).cname, L".."))
+                {
+
+                    *pbEmpty = 0;
+                    goto done;
+                }
+
+               nextDataNode = getdatanext(dataNode);
+           }
+       }
+       nextLeafNode = getnextnode(leafNode);
+    }
+
+    *pbEmpty = 1;
+
+  done:
+    return rc;
+}
 
 int cm_BPlusDirFoo(struct cm_scache *scp, struct cm_dirEntry *dep,
                    void *dummy, osi_hyper_t *entryOffsetp)
index 9a61da6..358bcfb 100644 (file)
@@ -151,6 +151,7 @@ long cm_BPlusDirBuildTree(cm_scache_t *scp, cm_user_t *userp, cm_req_t* reqp);
 int  cm_BPlusDirFoo(struct cm_scache *scp, struct cm_dirEntry *dep, void *dummy, osi_hyper_t *entryOffsetp);
 void cm_BPlusDumpStats(void);
 int  cm_MemDumpBPlusStats(FILE *outputFile, char *cookie, int lock);
+int  cm_BPlusDirIsEmpty(cm_dirOp_t *op, afs_uint32 *pbEmpty);
 
 /******************* directory enumeration operations ****************/
 typedef struct cm_direnum_entry {