summaryrefslogtreecommitdiff
path: root/math/mkdb.c
diff options
context:
space:
mode:
Diffstat (limited to 'math/mkdb.c')
-rw-r--r--math/mkdb.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/math/mkdb.c b/math/mkdb.c
new file mode 100644
index 0000000..db07eff
--- /dev/null
+++ b/math/mkdb.c
@@ -0,0 +1,130 @@
+/* $Id$ */
+
+/*
+ * Copyright (c) 2006 Dimitri A. Sokolyuk <demon@vhost.dyndns.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <fts.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "crc.h"
+#include "metadb.h"
+
+void twiddle(void);
+void walk(char **);
+
+void
+twiddle(void)
+{
+ static int i;
+
+ putchar("|/-\\"[i++ & 3]);
+ putchar('\b');
+ fflush(stdout);
+}
+
+void
+walk(char **fname)
+{
+ FTS *fts;
+ FTSENT *ftp, *chld, *cp;
+ char *p, pathmeta[BUFSIZ], pathset[BUFSIZ];
+ int parent, id;
+ struct meta *mp;
+
+ fts = fts_open(fname, FTS_LOGICAL, NULL);
+ assert(fts);
+
+ printf("Scanning: ");
+
+ db_open(O_CREAT|O_RDWR);
+
+// db_wipe();
+
+ while ((ftp = fts_read(fts)) != NULL) {
+ twiddle();
+ if (ftp->fts_info == FTS_D) {
+ p = ftp->fts_parent->fts_name;
+ if (ftp->fts_parent->fts_level <= 0)
+ parent = 0;
+ else
+ parent = crc32(p, strlen(p));
+
+ p = ftp->fts_name;
+ if (ftp->fts_level <= 0)
+ id = 0;
+ else
+ id = crc32(p, strlen(p));
+
+ snprintf(pathmeta, sizeof(pathmeta), "%s/%s", ftp->fts_accpath, METADATA);
+ snprintf(pathset, sizeof(pathset), "%s/%s", ftp->fts_accpath, SECTION);
+
+ if ((mp = openmeta(pathmeta)) != NULL) {
+ mp->dir = strdup(p);
+ mp->date = ftp->fts_statp->st_ctimespec.tv_sec;
+ mp->type = M_RECORD;
+
+ chld = fts_children(fts, 0);
+ for (cp = chld; cp != NULL; cp = cp->fts_link) {
+ if (cp->fts_info == FTS_F) {
+ p = cp->fts_name;
+ if (strstr(p, ".htm") != NULL)
+ mp->html = strdup(p);
+ else if (strstr(p, ".mws") != NULL)
+ mp->mws = strdup(p);
+ else if (strstr(p, ".mw") != NULL)
+ mp->mw = strdup(p);
+ else if (strstr(p, ".pdf") != NULL)
+ mp->pdf = strdup(p);
+ else if (strstr(p, ".zip") != NULL)
+ mp->code = strdup(p);
+ }
+ }
+
+
+ db_add(mp, parent, id);
+ closemeta(mp);
+ } else if ((mp = openmeta(pathset)) != NULL) {
+ mp->dir = strdup(p);
+ mp->date = ftp->fts_statp->st_ctimespec.tv_sec;
+ mp->type = M_SET;
+ db_add(mp, parent, id);
+ }
+ }
+ }
+
+ db_close();
+ fts_close(fts);
+
+ printf("done\n");
+}
+
+int
+main(int argc, char **argv)
+{
+
+ if (argc != 2)
+ return -1;
+
+ walk(&argv[1]);
+
+ return 0;
+}