summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2014-08-06 18:19:16 +0800
committerpiaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2014-08-06 18:19:16 +0800
commit191ddeeb26a3b0e9cd6a8e257b7b9f9016e53c65 (patch)
treebbb598e1517b345d8bdc879cb4e26aa755064f22
parent9bc994b48e5b9552aee6a65aecc7efcc217277b8 (diff)
downloadpttbbs-191ddeeb26a3b0e9cd6a8e257b7b9f9016e53c65.tar
pttbbs-191ddeeb26a3b0e9cd6a8e257b7b9f9016e53c65.tar.gz
pttbbs-191ddeeb26a3b0e9cd6a8e257b7b9f9016e53c65.tar.bz2
pttbbs-191ddeeb26a3b0e9cd6a8e257b7b9f9016e53c65.tar.lz
pttbbs-191ddeeb26a3b0e9cd6a8e257b7b9f9016e53c65.tar.xz
pttbbs-191ddeeb26a3b0e9cd6a8e257b7b9f9016e53c65.tar.zst
pttbbs-191ddeeb26a3b0e9cd6a8e257b7b9f9016e53c65.zip
Support rebuilding from existing data.
git-svn-id: http://opensvn.csie.org/pttbbs/trunk@6032 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r--pttbbs/daemon/postd/Makefile29
-rw-r--r--pttbbs/daemon/postd/rebuild.c123
2 files changed, 152 insertions, 0 deletions
diff --git a/pttbbs/daemon/postd/Makefile b/pttbbs/daemon/postd/Makefile
new file mode 100644
index 00000000..5bac96e3
--- /dev/null
+++ b/pttbbs/daemon/postd/Makefile
@@ -0,0 +1,29 @@
+SRCROOT= ../..
+.include "$(SRCROOT)/pttbbs.mk"
+
+PROGRAMS= rebuild
+UTILDIR= $(SRCROOT)/util
+UTILOBJ= $(UTILDIR)/util_var.o
+
+LDLIBS:=$(SRCROOT)/common/bbs/libcmbbs.a \
+ $(SRCROOT)/common/sys/libcmsys.a \
+ $(SRCROOT)/common/osdep/libosdep.a \
+ -levent $(LDLIBS)
+
+all: ${PROGRAMS}
+
+.SUFFIXES: .c .cpp .o
+.c.o:
+ $(CC) $(CFLAGS) -c $*.c
+.cpp.o:
+ $(CXX) $(CXXFLAGS) -c $*.cpp
+
+rebuild: $*.o
+ ${CC} ${CFLAGS} ${LDFLAGS} -o $@ $> $(UTILOBJ) $(LDLIBS)
+
+install: $(PROGRAMS)
+ install -d $(BBSHOME)/bin/
+ install -c -m 755 $(PROGRAMS) $(BBSHOME)/bin/
+
+clean:
+ rm -f *~ ${PROGRAMS} *.o
diff --git a/pttbbs/daemon/postd/rebuild.c b/pttbbs/daemon/postd/rebuild.c
new file mode 100644
index 00000000..db2fc3fc
--- /dev/null
+++ b/pttbbs/daemon/postd/rebuild.c
@@ -0,0 +1,123 @@
+#define _UTIL_C_
+#include "bbs.h"
+#include "daemons.h"
+
+int PostAddRecord(const char *board, const fileheader_t *fhdr)
+{
+ int s;
+ PostAddRequest req = {0};
+ char *userid;
+ char xuid[IDLEN + 1];
+
+ req.cb = sizeof(req);
+ req.operation = POSTD_REQ_ADD;
+ strlcpy(req.key.board, board, sizeof(req.key.board));
+ strlcpy(req.key.file, fhdr->filename, sizeof(req.key.file));
+ memcpy(&req.header, fhdr, sizeof(req.header));
+
+ req.extra.ctime = atoi(fhdr->filename + 2);
+ // It is possible to generate req.extra.ipv4 from fhdr site sig, but we
+ // probably don't really care.
+
+ // Try harder to recognize user
+ if (fhdr->filemode & FILE_ANONYMOUS) {
+ // Theoratically we can look up real id, but let's don't waste time.
+ userid = "-anonymous.";
+ } else if (strchr(fhdr->owner, '.')) {
+ // Post from some unknown user.
+ userid = "-remote.";
+ } else {
+ // Probably a real user. Let's try it.
+ userec_t urec;
+ int uid = searchuser(fhdr->owner, xuid);
+ if (uid > 0) {
+ passwd_query(uid, &urec);
+ if (strcmp(urec.userid, xuid) != 0) {
+ printf(" *** oops, userid changed. ...\n");
+ exit(1);
+ }
+ userid = xuid;
+ if ((uint32_t)urec.firstlogin <= req.extra.ctime) {
+ req.extra.userref = urec.firstlogin;
+ } else {
+ // same id, different user. damn it.
+ req.extra.userref = 0;
+ }
+ } else {
+ // does not exist. Treat like invalid user.
+ userid = "-expired.";
+ }
+ }
+ strlcpy(req.extra.userid, userid, sizeof(req.extra.userid));
+ printf(" (userref: %s.%d)", req.extra.userid, req.extra.userref);
+
+ s = toconnectex(POSTD_ADDR, 10);
+ if (s < 0)
+ return 1;
+ if (towrite(s, &req, sizeof(req)) < 0) {
+ close(s);
+ return 1;
+ }
+ close(s);
+ return 0;
+}
+
+void rebuild_board(int bid GCC_UNUSED, boardheader_t *bp)
+{
+ char dot_dir[PATHLEN], fpath[PATHLEN];
+ FILE *fp;
+ fileheader_t fhdr;
+ printf("Rebuilding board: %s\n", bp->brdname);
+
+ setbfile(dot_dir, bp->brdname, ".DIR");
+
+ fp = fopen(dot_dir, "rb");
+ if (!fp) {
+ printf(" (empty directory)\n");
+ return;
+ }
+ while (fread(&fhdr, sizeof(fhdr), 1, fp)) {
+ // Skip unknown files
+ if (fhdr.filename[0] != 'M' && fhdr.filename[1] != '.') {
+ printf(" - Skipped (%s).\n", fhdr.filename);
+ continue;
+ }
+ setbfile(fpath, bp->brdname, fhdr.filename);
+ if (dashs(fpath) < 1) {
+ printf(" - Non-Exist (%s).\n", fhdr.filename);
+ continue;
+ }
+
+ printf(" - Adding %s", fhdr.filename);
+ PostAddRecord(bp->brdname, &fhdr);
+ printf("\n");
+ }
+
+ fclose(fp);
+}
+
+int main(int argc, char **argv)
+{
+ int bid = 0;
+ chdir(BBSHOME);
+
+ attach_SHM();
+
+ for (bid = 1; bid <= MAX_BOARD; bid++) {
+ int i = 0;
+ boardheader_t *bp = getbcache(bid);
+
+ if (!*bp->brdname)
+ continue;
+
+ for (i = argc; i > 1; i--) {
+ if (strcasecmp(argv[i - 1], bp->brdname) == 0) {
+ break;
+ }
+ }
+
+ if (i > 1)
+ rebuild_board(bid, bp);
+ }
+ return 0;
+}