+typedef struct B_DBx
+{
+ POOLMEM *fname;
+ int fnl;
+ POOLMEM *path;
+ int pnl;
+} B_DBx;
+
+/*
+ * Given a full filename, split it into its path
+ * and filename parts. They are returned in pool memory
+ * in the mdb structure.
+ */
+void split_path_and_file(B_DBx *mdb, const char *fname)
+{
+ const char *p, *f;
+
+ /* Find path without the filename.
+ * I.e. everything after the last / is a "filename".
+ * OK, maybe it is a directory name, but we treat it like
+ * a filename. If we don't find a / then the whole name
+ * must be a path name (e.g. c:).
+ */
+ for (p=f=fname; *p; p++) {
+ if (IsPathSeparator(*p)) {
+ f = p; /* set pos of last slash */
+ }
+ }
+ if (IsPathSeparator(*f)) { /* did we find a slash? */
+ f++; /* yes, point to filename */
+ } else { /* no, whole thing must be path name */
+ f = p;
+ }
+
+ /* If filename doesn't exist (i.e. root directory), we
+ * simply create a blank name consisting of a single
+ * space. This makes handling zero length filenames
+ * easier.
+ */
+ mdb->fnl = p - f;
+ if (mdb->fnl > 0) {
+ mdb->fname = check_pool_memory_size(mdb->fname, mdb->fnl+1);
+ memcpy(mdb->fname, f, mdb->fnl); /* copy filename */
+ mdb->fname[mdb->fnl] = 0;
+ } else {
+ mdb->fname[0] = 0;
+ mdb->fnl = 0;
+ }
+
+ mdb->pnl = f - fname;
+ if (mdb->pnl > 0) {
+ mdb->path = check_pool_memory_size(mdb->path, mdb->pnl+1);
+ memcpy(mdb->path, fname, mdb->pnl);
+ mdb->path[mdb->pnl] = 0;
+ } else {
+ mdb->path[0] = 0;
+ mdb->pnl = 0;
+ }
+
+ Dmsg2(500, "split path=%s file=%s\n", mdb->path, mdb->fname);
+}
+
+#ifdef _TEST_TREE
+int main()
+{
+ AccurateElt *elt;
+ char *start_heap = (char *)sbrk(0);
+ MY *myp, *res;
+ MY_INDEX *idx;
+
+ FILE *fp;
+ fp = fopen("lst", "r");
+ if (!fp) {
+ exit (5);
+ }
+ rblist *index, *rb_file, *rb_path;
+ index = New(rblist(idx, &idx->link)); /* (pathid, filenameid) => index */
+ rb_path = New(rblist(myp, &myp->link)); /* pathid => path */
+ rb_file = New(rblist(myp, &myp->link)); /* filenameid => filename */
+
+ B_DBx mdb;
+ mdb.fname = get_pool_memory(PM_FNAME);
+ mdb.path = get_pool_memory(PM_FNAME);
+
+ AccurateBuffer *accbuf = new AccurateBuffer;
+ char buf[4096];
+ int64_t count=0;
+ printf("loading...\n");
+ while (fgets(buf, sizeof(buf), fp)) {
+ int len = strlen(buf);
+ if (len > 0) {
+ buf[len-1]=0; /* zap \n */
+ }
+ split_path_and_file(&mdb, buf);
+
+ MY_INDEX *idx = (MY_INDEX *)malloc(sizeof(MY_INDEX));
+ memset(idx, 0, sizeof(MY_INDEX));
+
+ myp = (MY *)malloc(sizeof(MY));
+ memset(myp, 0, sizeof(MY));
+ myp->buf = bstrdup(mdb.path);
+ res = (MY *)rb_path->insert(myp, my_cmp);
+ if (res != myp) {
+ free(myp->buf);
+ free(myp);
+ }
+
+ idx->path = res->buf;
+
+ myp = (MY *)malloc(sizeof(MY));
+ memset(myp, 0, sizeof(MY));
+ myp->buf = bstrdup(mdb.fname);
+ res = (MY *)rb_file->insert(myp, my_cmp);
+ if (res != myp) {
+ free(myp->buf);
+ free(myp);
+ }
+
+ idx->fname = res->buf;
+
+ idx->index = count;
+ index->insert(idx, my_index_cmp);
+
+ elt = (AccurateElt *)accbuf->get_elt(count);
+ elt->mtime = count;
+ elt->path = idx->path;
+ elt->fname = idx->fname;
+ accbuf->update_elt(count);
+
+ count++;
+ }
+
+ printf("fetch and update...\n");
+ fprintf(stderr, "count;%lli\n", count);
+ fprintf(stderr, "heap;%lld\n", (long long)((char *)sbrk(0) - start_heap));
+
+ fseek(fp, 0, SEEK_SET);
+ int toggle=0;
+ while (fgets(buf, sizeof(buf), fp)) {
+ int len = strlen(buf);
+ if (len > 0) {
+ buf[len-1]=0; /* zap \n */
+ }
+ split_path_and_file(&mdb, buf);
+
+ MY search;
+ search.buf = mdb.fname;
+ MY *f = (MY *)rb_file->search(&search, my_cmp);
+
+ if (!f) {
+ continue; /* not found, skip */
+ }