/* perform inform command */ static int procinform(const char *path, int omode){ TCTDB *tdb = tctdbnew(); if(g_dbgfd != INVALID_HANDLE_VALUE) tctdbsetdbgfd(tdb, g_dbgfd); tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL); if(!tctdbopen(tdb, path, TDBOREADER | omode)){ printerr(tdb); tctdbdel(tdb); return 1; } bool err = false; const char *npath = tctdbpath(tdb); if(!npath) npath = "(unknown)"; printf("path: %s\n", npath); printf("database type: table\n"); uint8_t flags = tctdbflags(tdb); printf("additional flags:"); if(flags & TDBFOPEN) printf(" open"); if(flags & TDBFFATAL) printf(" fatal"); printf("\n"); printf("bucket number: %llu\n", (unsigned long long)tctdbbnum(tdb)); if(tdb->hdb->cnt_writerec >= 0) printf("used bucket number: %lld\n", (long long)tctdbbnumused(tdb)); printf("alignment: %u\n", tctdbalign(tdb)); printf("free block pool: %u\n", tctdbfbpmax(tdb)); printf("index number: %d\n", tctdbinum(tdb)); TDBIDX *idxs = tdb->idxs; int inum = tdb->inum; for(int i = 0; i < inum; i++){ TDBIDX *idxp = idxs + i; switch(idxp->type){ case TDBITLEXICAL: printf(" name=%s, type=lexical, rnum=%lld, fsiz=%lld\n", idxp->name, (long long)tcbdbrnum(idxp->db), (long long)tcbdbfsiz(idxp->db)); break; case TDBITDECIMAL: printf(" name=%s, type=decimal, rnum=%lld, fsiz=%lld\n", idxp->name, (long long)tcbdbrnum(idxp->db), (long long)tcbdbfsiz(idxp->db)); break; case TDBITTOKEN: printf(" name=%s, type=token, rnum=%lld, fsiz=%lld\n", idxp->name, (long long)tcbdbrnum(idxp->db), (long long)tcbdbfsiz(idxp->db)); break; case TDBITQGRAM: printf(" name=%s, type=qgram, rnum=%lld, fsiz=%lld\n", idxp->name, (long long)tcbdbrnum(idxp->db), (long long)tcbdbfsiz(idxp->db)); break; } } printf("unique ID seed: %lld\n", (long long)tctdbuidseed(tdb)); printf("inode number: %lld\n", (long long)tctdbinode(tdb)); char date[48]; tcdatestrwww(tctdbmtime(tdb), INT_MAX, date); printf("modified time: %s\n", date); uint8_t opts = tctdbopts(tdb); printf("options:"); if(opts & TDBTLARGE) printf(" large"); if(opts & TDBTDEFLATE) printf(" deflate"); if(opts & TDBTBZIP) printf(" bzip"); if(opts & TDBTTCBS) printf(" tcbs"); if(opts & TDBTEXCODEC) printf(" excodec"); printf("\n"); printf("record number: %llu\n", (unsigned long long)tctdbrnum(tdb)); printf("file size: %llu\n", (unsigned long long)tctdbfsiz(tdb)); if(!tctdbclose(tdb)){ if(!err) printerr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0; }
/* perform list command */ static int proclist(const char *path, int omode, int max, bool pv, bool px, const char *fmstr){ TCTDB *tdb = tctdbnew(); if(g_dbgfd != INVALID_HANDLE_VALUE) tctdbsetdbgfd(tdb, g_dbgfd); if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb); if(!tctdbopen(tdb, path, TDBOREADER | omode)){ printerr(tdb); tctdbdel(tdb); return 1; } bool err = false; if(fmstr){ TCLIST *pkeys = tctdbfwmkeys2(tdb, fmstr, max); for(int i = 0; i < tclistnum(pkeys); i++){ int pksiz; const char *pkbuf = tclistval(pkeys, i, &pksiz); printdata(pkbuf, pksiz, px); if(pv){ TCMAP *cols = tctdbget(tdb, pkbuf, pksiz); if(cols){ tcmapiterinit(cols); const char *kbuf; int ksiz; while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){ int vsiz; const char *vbuf = tcmapiterval(kbuf, &vsiz); putchar('\t'); printdata(kbuf, ksiz, px); putchar('\t'); printdata(vbuf, vsiz, px); } tcmapdel(cols); } } putchar('\n'); } tclistdel(pkeys); } else { if(!tctdbiterinit(tdb)){ printerr(tdb); err = true; } int cnt = 0; TCMAP *cols; while((cols = tctdbiternext3(tdb)) != NULL){ int pksiz; const char *pkbuf = tcmapget(cols, "", 0, &pksiz); if(pkbuf){ printdata(pkbuf, pksiz, px); if(pv){ tcmapiterinit(cols); const char *kbuf; int ksiz; while((kbuf = tcmapiternext(cols, &ksiz)) != NULL){ if(*kbuf == '\0') continue; int vsiz; const char *vbuf = tcmapiterval(kbuf, &vsiz); putchar('\t'); printdata(kbuf, ksiz, px); putchar('\t'); printdata(vbuf, vsiz, px); } } } tcmapdel(cols); putchar('\n'); if(max >= 0 && ++cnt >= max) break; } } if(!tctdbclose(tdb)){ if(!err) printerr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0; }
/* perform importtsv command */ static int procimporttsv(const char *path, const char *file, int omode, bool sc){ FILE *ifp = file ? fopen(file, "rb") : stdin; if(!ifp){ fprintf(stderr, "%s: could not open\n", file ? file : "(stdin)"); return 1; } TCTDB *tdb = tctdbnew(); if(g_dbgfd != INVALID_HANDLE_VALUE) tctdbsetdbgfd(tdb, g_dbgfd); if(!tctdbsetcodecfunc(tdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(tdb); int64_t msiz = 0; TCMAP *info = tcsysinfo(); if(info){ msiz = tcatoi(tcmapget4(info, "total", "0")); tcmapdel(info); } if(!tctdbsetinvcache(tdb, msiz >= (1 << 30) ? msiz / 4 : 0, 1.0)) printerr(tdb); if(!tctdbopen(tdb, path, TDBOWRITER | TDBOCREAT | omode)){ printerr(tdb); tctdbdel(tdb); if(ifp != stdin) fclose(ifp); return 1; } bool err = false; char *line, numbuf[TCNUMBUFSIZ]; int cnt = 0; while(!err && (line = mygetline(ifp)) != NULL){ char *pv = strchr(line, '\t'); if(!pv){ tcfree(line); continue; } *pv = '\0'; if(sc) tcstrutfnorm(line, TCUNSPACE | TCUNLOWER | TCUNNOACC | TCUNWIDTH); const char *pkey; if(*line == '\0'){ sprintf(numbuf, "%lld", (long long)tctdbgenuid(tdb)); pkey = numbuf; } else { pkey = line; } if(!tctdbput3(tdb, pkey, pv + 1)){ printerr(tdb); err = true; } tcfree(line); if(cnt > 0 && cnt % 100 == 0){ putchar('.'); fflush(stdout); if(cnt % 5000 == 0) printf(" (%08d)\n", cnt); } cnt++; } printf(" (%08d)\n", cnt); if(!tctdbclose(tdb)){ if(!err) printerr(tdb); err = true; } tctdbdel(tdb); if(ifp != stdin) fclose(ifp); return err ? 1 : 0; }
/* * Sets up the user_session structure. This contains various bits of * information pertaining to the users session. */ void set_user_session(void) { TCTDB *tdb; TDBQRY *qry; TCLIST *res; TCMAP *cols; int rsize; int primary_key_size; char pkbuf[256]; char session_id[SID_LEN + 1]; char login_at[21]; char last_seen[21]; char uid[11]; char sid[21]; char restrict_ip[2]; char capabilities[4]; char user_hdr[1025]; char *xss_string; const char *rbuf; /* * Don't assume the order we get the cookies back is the * same order as we sent them. */ if (strncmp(env_vars.http_cookie, "session_id", 10) == 0) snprintf(session_id, sizeof(session_id), "%s", env_vars.http_cookie + 11); else snprintf(session_id, sizeof(session_id), "%s", env_vars.http_cookie + 88); tdb = tctdbnew(); tctdbopen(tdb, SESSION_DB, TDBOREADER | TDBOWRITER); /* Get the users stored session */ qry = tctdbqrynew(tdb); tctdbqryaddcond(qry, "session_id", TDBQCSTREQ, session_id); res = tctdbqrysearch(qry); rbuf = tclistval(res, 0, &rsize); cols = tctdbget(tdb, rbuf, rsize); tcmapiterinit(cols); memset(&user_session, 0, sizeof(user_session)); snprintf(user_session.tenant, sizeof(user_session.tenant), "%s", tcmapget2(cols, "tenant")); user_session.sid = strtoull(tcmapget2(cols, "sid"), NULL, 10); user_session.uid = atoi(tcmapget2(cols, "uid")); user_session.username = strdup(tcmapget2(cols, "username")); user_session.name = strdup(tcmapget2(cols, "name")); user_session.login_at = atol(tcmapget2(cols, "login_at")); user_session.last_seen = time(NULL); snprintf(user_session.origin_ip, sizeof(user_session.origin_ip), "%s", tcmapget2(cols, "origin_ip")); user_session.client_id = strdup(tcmapget2(cols, "client_id")); snprintf(user_session.session_id, sizeof(user_session.session_id), "%s", tcmapget2(cols, "session_id")); snprintf(user_session.csrf_token, sizeof(user_session.csrf_token), "%s", tcmapget2(cols, "csrf_token")); user_session.restrict_ip = atoi(tcmapget2(cols, "restrict_ip")); user_session.capabilities = atoi(tcmapget2(cols, "capabilities")); tcmapdel(cols); tclistdel(res); tctdbqrydel(qry); /* * Set the user header banner, which displays the users name, uid and * whether they are an Approver and or Admin. */ xss_string = xss_safe_string(user_session.name); snprintf(user_hdr, sizeof(user_hdr), "<big><big> %s</big></big><small>" "<span class = \"lighter\"> (%d) </span>" "</small>", xss_string, user_session.uid); free(xss_string); if (IS_APPROVER() && IS_ADMIN()) strncat(user_hdr, "<span class = \"t_red\">(Approver / Admin)" "</span>", 1024 - strlen(user_hdr)); else if (IS_APPROVER()) strncat(user_hdr, "<span class = \"t_red\">(Approver)" "</span>", 1024 - strlen(user_hdr)); else if (IS_ADMIN()) strncat(user_hdr, "<span class = \"t_red\">(Admin)" "</span>", 1024 - strlen(user_hdr)); strncat(user_hdr, " ", 1024 - strlen(user_hdr)); user_session.user_hdr = strdup(user_hdr); /* * We want to update the last_seen timestamp in the users session. * This entails removing the old session first then storing the new * updated session. */ qry = tctdbqrynew(tdb); tctdbqryaddcond(qry, "session_id", TDBQCSTREQ, session_id); res = tctdbqrysearch(qry); rbuf = tclistval(res, 0, &rsize); tctdbout(tdb, rbuf, strlen(rbuf)); tclistdel(res); tctdbqrydel(qry); primary_key_size = sprintf(pkbuf, "%ld", (long)tctdbgenuid(tdb)); snprintf(login_at, sizeof(login_at), "%ld", user_session.login_at); snprintf(last_seen, sizeof(last_seen), "%ld", user_session.last_seen); snprintf(uid, sizeof(uid), "%u", user_session.uid); snprintf(sid, sizeof(sid), "%llu", user_session.sid); snprintf(restrict_ip, sizeof(restrict_ip), "%d", user_session.restrict_ip); snprintf(capabilities, sizeof(capabilities), "%d", user_session.capabilities); cols = tcmapnew3("tenant", user_session.tenant, "sid", sid, "uid", uid, "username", user_session.username, "name", user_session.name, "login_at", login_at, "last_seen", last_seen, "origin_ip", user_session.origin_ip, "client_id", user_session.client_id, "session_id", user_session.session_id, "csrf_token", user_session.csrf_token, "restrict_ip", restrict_ip, "capabilities", capabilities, NULL); tctdbput(tdb, pkbuf, primary_key_size, cols); tcmapdel(cols); tctdbclose(tdb); tctdbdel(tdb); }
/* perform tblwrite command */ int dotblwrite(char *name, int rnum){ TCTDB *tdb; int i, err, pksiz, vsiz; char pkbuf[RECBUFSIZ], vbuf[RECBUFSIZ]; TCMAP *cols; if(showprgr) printf("<Writing Test of Table>\n name=%s rnum=%d\n\n", name, rnum); /* open a database */ tdb = tctdbnew(); tctdbtune(tdb, rnum * 3, 0, 0, 0); tctdbsetxmsiz(tdb, rnum * 80); tctdbsetcache(tdb, -1, rnum / 100, -1); if(!tctdbopen(tdb, name, TDBOWRITER | TDBOCREAT | TDBOTRUNC)){ fprintf(stderr, "tctdbopen failed\n"); tctdbdel(tdb); return 1; } if(!tctdbsetindex(tdb, "s", TDBITLEXICAL)){ fprintf(stderr, "tctdbsetindex failed\n"); tctdbdel(tdb); return 1; } if(!tctdbsetindex(tdb, "n", TDBITDECIMAL)){ fprintf(stderr, "tctdbsetindex failed\n"); tctdbdel(tdb); return 1; } err = FALSE; /* loop for each record */ for(i = 1; i <= rnum; i++){ /* store a record */ pksiz = sprintf(pkbuf, "%d", i); cols = tcmapnew2(7); vsiz = sprintf(vbuf, "%08d", i); tcmapput(cols, "s", 1, vbuf, vsiz); vsiz = sprintf(vbuf, "%08d", myrand() % i); tcmapput(cols, "n", 1, vbuf, vsiz); vsiz = sprintf(vbuf, "%08d", i); tcmapput(cols, "t", 1, vbuf, vsiz); vsiz = sprintf(vbuf, "%08d", myrand() % rnum); tcmapput(cols, "f", 1, vbuf, vsiz); if(!tctdbput(tdb, pkbuf, pksiz, cols)){ fprintf(stderr, "tctdbput failed\n"); err = TRUE; break; } tcmapdel(cols); /* print progression */ if(showprgr && rnum > 250 && i % (rnum / 250) == 0){ putchar('.'); fflush(stdout); if(i == rnum || i % (rnum / 10) == 0){ printf(" (%08d)\n", i); fflush(stdout); } } } /* close the database */ if(!tctdbclose(tdb)){ fprintf(stderr, "tctdbclose failed\n"); tctdbdel(tdb); return 1; } tctdbdel(tdb); if(showprgr && !err) printf("ok\n\n"); return err ? 1 : 0; }
/* perform export command */ static int procexport(const char *dbpath, int64_t id, const char *dirpath){ TCTDB *tdb = tctdbnew(); if(!tctdbopen(tdb, dbpath, TDBOREADER)){ printdberr(tdb); tctdbdel(tdb); return 1; } bool err = false; if(id > 0){ char pkbuf[NUMBUFSIZ]; int pksiz = sprintf(pkbuf, "%lld", (long long)id); TCMAP *cols = tctdbget(tdb, pkbuf, pksiz); if(cols){ TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ); wikidump(rbuf, cols); fwrite(tcxstrptr(rbuf), 1, tcxstrsize(rbuf), stdout); tcxstrdel(rbuf); tcmapdel(cols); } else { printdberr(tdb); err = true; } } else { if(!dirpath) dirpath = "."; if(!tctdbiterinit(tdb)){ printdberr(tdb); err = true; } char *pkbuf; int pksiz; while((pkbuf = tctdbiternext(tdb, &pksiz)) != NULL){ TCMAP *cols = tctdbget(tdb, pkbuf, pksiz); if(cols){ char *name = tcstrdup(tcmapget4(cols, "name", "")); tcstrcututf(name, 32); char *enc = pathencode(name); char *path = tcsprintf("%s/%s-%s.tpw", dirpath, pkbuf, enc); TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ); wikidump(rbuf, cols); if(tcwritefile(path, tcxstrptr(rbuf), tcxstrsize(rbuf))){ printf("%s: exported: id=%s name=%s\n", path, pkbuf, name); } else { printf("%s: writing failed\n", path); err = true; } tcxstrdel(rbuf); tcfree(path); tcfree(enc); tcfree(name); tcmapdel(cols); } else { printdberr(tdb); err = true; } tcfree(pkbuf); } } if(!tctdbclose(tdb)){ printdberr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0; }
/* perform import command */ static int procimport(const char *dbpath, TCLIST *files, TCLIST *sufs){ TCTDB *tdb = tctdbnew(); if(!tctdbtune(tdb, TUNEBNUM, TUNEAPOW, TUNEFPOW, 0)){ printdberr(tdb); tctdbdel(tdb); return 1; } if(!tctdbopen(tdb, dbpath, TDBOWRITER | TDBOCREAT)){ printdberr(tdb); tctdbdel(tdb); return 1; } bool err = false; if(!tctdbsetindex(tdb, "name", TDBITLEXICAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "cdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "mdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "xdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } tclistinvert(files); char *fpath; while((fpath = tclistpop2(files)) != NULL){ TCLIST *cfiles = tcreaddir(fpath); if(cfiles){ tclistsort(cfiles); for(int i = tclistnum(cfiles) - 1; i >= 0; i--){ const char *cfile = tclistval2(cfiles, i); bool hit = false; for(int j = 0; j < tclistnum(sufs); j++){ if(tcstribwm(cfile, tclistval2(sufs, j))){ hit = true; break; } } if(!hit) continue; char *lpath = tcsprintf("%s/%s", fpath, cfile); tclistpush2(files, lpath); tcfree(lpath); } tclistdel(cfiles); } else { int isiz; char *ibuf = tcreadfile(fpath, IOMAXSIZ, &isiz); if(ibuf){ TCMAP *cols = tcmapnew2(TINYBNUM); wikiload(cols, ibuf); const char *name = tcmapget2(cols, "name"); if(name && *name != '\0'){ int64_t id = tcatoi(tcmapget4(cols, "id", "")); if(dbputart(tdb, id, cols)){ id = tcatoi(tcmapget4(cols, "id", "")); printf("%s: imported: id=%lld name=%s\n", fpath, (long long)id, name); } else { printdberr(tdb); err = true; } } else { printf("%s: ignored because there is no name\n", fpath); } tcmapdel(cols); tcfree(ibuf); } } tcfree(fpath); } if(!tctdbclose(tdb)){ printdberr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0; }