From e7803f7520bd591c37557ffd853dd40541854357 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 9 Aug 2006 13:01:38 +0000 Subject: Maple Worksheet repository CGI frontend --- math/mkdb.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 math/mkdb.c (limited to 'math/mkdb.c') 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 + * + * 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 +#include +#include +#include +#include +#include +#include +#include + +#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; +} -- cgit v1.2.3