/* $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 fillauthor(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_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); /* TODO: add doc, rtf, etc. */ } } /* fallbacks */ if (mp->title == NULL) mp->title = mp->dir; if (mp->author == NULL) mp->author = strdup("A. N. Other"); if (mp->language == NULL) mp->language = strdup("en"); 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); } } } fts_close(fts); printf("done\n"); } void fillauthor(char *fname) { FILE *fd; struct author a; char buf[BUFSIZ], *p, *key; char sep[] = ":\n"; fd = fopen(fname, "r"); while ((p = fgets(buf, sizeof(buf), fd)) != NULL) { if (*p == '#') continue; key = strsep(&p, sep); a.name = strsep(&p, sep); a.mail = strsep(&p, sep); db_putauthor(&a, key); } fclose(fd); } int main(int argc, char **argv) { if (argc != 2) return -1; db_open(O_CREAT|O_RDWR); fillauthor(AUTHORDATA); walk(&argv[1]); db_close(); return 0; }