diff options
author | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2014-08-07 18:52:20 +0800 |
---|---|---|
committer | piaip <piaip@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2014-08-07 18:52:20 +0800 |
commit | 07bea9b1aaa47bb3612b91535631de2296d1173b (patch) | |
tree | 943dbcfcd49ec8589e221ae08334153b0b963911 | |
parent | df2002f26535440d8af75b5a746c1eff3b76a548 (diff) | |
download | pttbbs-07bea9b1aaa47bb3612b91535631de2296d1173b.tar pttbbs-07bea9b1aaa47bb3612b91535631de2296d1173b.tar.gz pttbbs-07bea9b1aaa47bb3612b91535631de2296d1173b.tar.bz2 pttbbs-07bea9b1aaa47bb3612b91535631de2296d1173b.tar.lz pttbbs-07bea9b1aaa47bb3612b91535631de2296d1173b.tar.xz pttbbs-07bea9b1aaa47bb3612b91535631de2296d1173b.tar.zst pttbbs-07bea9b1aaa47bb3612b91535631de2296d1173b.zip |
Support building with parsed contents.
git-svn-id: http://opensvn.csie.org/pttbbs/trunk@6040 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rwxr-xr-x | pttbbs/daemon/postd/postd.py | 36 | ||||
-rw-r--r-- | pttbbs/daemon/postd/rebuild.c | 40 | ||||
-rw-r--r-- | pttbbs/include/daemons.h | 2 |
3 files changed, 61 insertions, 17 deletions
diff --git a/pttbbs/daemon/postd/postd.py b/pttbbs/daemon/postd/postd.py index 1ea052f1..56eedc9f 100755 --- a/pttbbs/daemon/postd/postd.py +++ b/pttbbs/daemon/postd/postd.py @@ -23,7 +23,10 @@ PostKey = collections.namedtuple('PostKey', 'board file') # TODO in future we don't need userref, and ctime should be automatically set. AddRecordFormatString = 'III%ds' % (pttstruct.IDLEN + 1) AddRecord = collections.namedtuple('AddRecord', 'userref ctime ipv4 userid') +CONTENT_LEN_FORMAT = 'I' REQ_ADD = 1 +REQ_ADD2 = 2 +REQ_GET_CONTENT = 3 _SERVER_ADDR = '127.0.0.1' _SERVER_PORT = 5135 _DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), @@ -74,17 +77,25 @@ def LoadPost(query): def SavePost(keypak, data, extra=None): + return SavePost2(None, keypak, data, extra) + +def SavePost2(content, keypak, data, extra=None): if extra: data.update(extra._asdict()) logging.debug("SavePost: %r => %r", keypak, data) key = '%s/%s' % (keypak.board, keypak.file) g_db.set(key, serialize(data)) logging.debug(' Saved: %s', key) - content_file = os.path.join(BBSHOME, 'boards', keypak.board[0], - keypak.board, keypak.file) - content_length = os.path.getsize(content_file) + if content is None: + content_file = os.path.join(BBSHOME, 'boards', keypak.board[0], + keypak.board, keypak.file) + content_length = os.path.getsize(content_file) + else: + logging.debug('got transfered content') + content_length = len(content) start = time.time() - g_db.set(key + ':content', open(content_file).read()) + g_db.set(key + ':content', + open(content_file).read() if content is None else content) exec_time = time.time() - start logging.debug(' Content (%d) save time: %.3fs.', content_length, exec_time) if exec_time > 0.1: @@ -115,18 +126,31 @@ def open_database(db_path): def handle_request(socket, _): fd = socket.makefile('rw') - fmt_len = '@i' try: req = fd.read(struct.calcsize(RequestFormatString)) req = Request._make(struct.unpack(RequestFormatString, req)) logging.debug('Found request: %d' % req.operation) - # TODO check req.cb if req.operation == REQ_ADD: header_blob = fd.read(pttstruct.FILEHEADER_SIZE) addblob = fd.read(struct.calcsize(AddRecordFormatString)) keyblob = fd.read(struct.calcsize(PostKeyFormatString)) SavePost(UnpackPostKey(keyblob), DecodeFileHeader(header_blob), UnpackAddRecord(addblob)) + elif req.operation == REQ_ADD2: + header_blob = fd.read(pttstruct.FILEHEADER_SIZE) + addblob = fd.read(struct.calcsize(AddRecordFormatString)) + keyblob = fd.read(struct.calcsize(PostKeyFormatString)) + content_len = struct.unpack( + CONTENT_LEN_FORMAT, + fd.read(struct.calcsize(CONTENT_LEN_FORMAT)))[0] + content = fd.read(content_len) + SavePost2(content, UnpackPostKey(keyblob), + DecodeFileHeader(header_blob), UnpackAddRecord(addblob)) + elif req.operation == REQ_GET_CONTENT: + keyblob = fd.read(struct.calcsize(PostKeyFormatString)) + content = GetPostContent(UnpackPostKey(keyblob)) + fd.write(struct.pack(CONTENT_LEN_FORMAT, len(content))) + fd.write(content) else: raise ValueError('Unkown operation %d' % req.operation) except: diff --git a/pttbbs/daemon/postd/rebuild.c b/pttbbs/daemon/postd/rebuild.c index dab41afd..f336a78e 100644 --- a/pttbbs/daemon/postd/rebuild.c +++ b/pttbbs/daemon/postd/rebuild.c @@ -72,13 +72,15 @@ int IsCrossPostLog(const char *buf) { // format: "※ " ANSI_COLOR(1;32) "%s" ANSI_COLOR(0;32) ":轉錄至" %s if (!str_starts_with(buf, "※ " ANSI_COLOR(1;32))) return 0; - if (strstr(buf, ANSI_COLOR(0;32) ":轉錄至")) + if (!strstr(buf, ANSI_COLOR(0;32) ":轉錄至")) return 0; + printf("Found XPOST!!!\n"); return 1; } -int ProcessPost(const char *filename) { +char *ProcessPost(const char *filename) { FILE *fp = fopen(filename, "rt"); + char *content; long offBegin, offEnd, off; char buf[ANSILINELEN]; char bufOwner[ANSILINELEN], @@ -132,34 +134,40 @@ int ProcessPost(const char *filename) { // Content: offBegin to offEnd. fseek(fp, offBegin, SEEK_SET); + content = malloc(offEnd - offBegin + 1); + assert(content); + content[offEnd - offBegin] = 0; + fread(content, 1, offEnd - offBegin + 1, fp); // Try to parse comments fseek(fp, offEnd, SEEK_SET); while (fgets(buf, sizeof(buf), fp)) { - - if (buf[0] != ESC_CHR) + if (buf[0] != ESC_CHR) // Includes IsCrossPostLog. continue; // See comments.c:FormatCommentString: kind = CommentsExtract(buf, bufOwner, bufContent, bufTrailing); assert(kind >= 0); chomp(bufTrailing); - printf("K[%d], A[%s], C[%s], T[%s]\n", kind, - bufOwner, bufContent, bufTrailing); + // TODO we should probably upload these comments. + printf("K[%d], A[%s], C[%s], T[%s]\n", + kind, bufOwner, bufContent, bufTrailing); } fclose(fp); - return 0; + return content; } -int PostAddRecord(const char *board, const fileheader_t *fhdr) +int PostAddRecord(const char *board, const fileheader_t *fhdr, const char *fpath) { int s; PostAddRequest req = {0}; char *userid; + char *contents; + uint32_t content_length; char xuid[IDLEN + 1]; req.cb = sizeof(req); - req.operation = POSTD_REQ_ADD; + req.operation = POSTD_REQ_ADD2; 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)); @@ -210,6 +218,16 @@ int PostAddRecord(const char *board, const fileheader_t *fhdr) close(s); return 1; } + + contents = ProcessPost(fpath); + content_length = strlen(contents); + if (towrite(s, &content_length, sizeof(content_length)) < 0 || + towrite(s, contents, content_length) < 0) { + free(contents); + close(s); + return 1; + } + free(contents); close(s); return 0; } @@ -247,8 +265,8 @@ void rebuild_board(int bid GCC_UNUSED, boardheader_t *bp) // TODO Add .DIR sequence number. printf(" - Adding %s", fhdr.filename); - ProcessPost(fpath); - // PostAddRecord(bp->brdname, &fhdr); + if (PostAddRecord(bp->brdname, &fhdr, fpath) != 0) + printf(" (error)"); printf("\n"); } diff --git a/pttbbs/include/daemons.h b/pttbbs/include/daemons.h index c78c9329..1066e602 100644 --- a/pttbbs/include/daemons.h +++ b/pttbbs/include/daemons.h @@ -186,6 +186,8 @@ typedef struct { enum { POSTD_REQ_ADD = 1, + POSTD_REQ_ADD2, + POSTD_REQ_GET_CONTENT, }; typedef struct { |