diff options
Diffstat (limited to 'mbbsd')
-rw-r--r-- | mbbsd/bbs.c | 2 | ||||
-rw-r--r-- | mbbsd/board.c | 13 | ||||
-rw-r--r-- | mbbsd/brc.c | 160 | ||||
-rw-r--r-- | mbbsd/var.c | 4 |
4 files changed, 112 insertions, 67 deletions
diff --git a/mbbsd/bbs.c b/mbbsd/bbs.c index cf7ad993..ba75286c 100644 --- a/mbbsd/bbs.c +++ b/mbbsd/bbs.c @@ -303,7 +303,7 @@ readdoent(int num, fileheader_t * ent) char *mark, *title, color, special = 0, isonline = 0, recom[8]; userinfo_t *uentp; - type = brc_unread(ent->filename, brc_num, brc_list) ? '+' : ' '; + type = brc_unread(currbid, ent->filename) ? '+' : ' '; if ((currmode & MODE_BOARD) && (ent->filemode & FILE_DIGEST)) type = (type == ' ') ? '*' : '#'; else if (currmode & MODE_BOARD || HasUserPerm(PERM_LOGINOK)) { diff --git a/mbbsd/board.c b/mbbsd/board.c index 8a228287..e9267703 100644 --- a/mbbsd/board.c +++ b/mbbsd/board.c @@ -133,9 +133,7 @@ HasBoardPerm(boardheader_t *bptr) static int check_newpost(boardstat_t * ptr) { /* Ptt зя */ - int tbrc_num; time4_t ftime; - time4_t *tbrc_list; ptr->myattr &= ~NBRD_UNREAD; if (B_BH(ptr)->brdattr & (BRD_GROUPBOARD | BRD_SYMBOLIC)) @@ -155,8 +153,7 @@ check_newpost(boardstat_t * ptr) if (ftime > now + 10) ftime = B_LASTPOSTTIME(ptr) = now - 1; - tbrc_list = brc_find_record(ptr->bid, &tbrc_num); - if ( brc_unread_time(ftime, tbrc_num, tbrc_list) ) + if ( brc_unread_time(ptr->bid, ftime) ) ptr->myattr |= NBRD_UNREAD; return 1; @@ -451,7 +448,7 @@ unread_position(char *dirfile, boardstat_t * ptr) while (num > 0) { lseek(fd, (off_t) (num * sizeof(fh)), SEEK_SET); if (read(fd, fname, FNLEN) <= 0 || - !brc_unread(fname, brc_num, brc_list)) + !brc_unread(ptr->bid, fname)) break; num -= step; if (step < 32) @@ -462,7 +459,7 @@ unread_position(char *dirfile, boardstat_t * ptr) while (num < total) { lseek(fd, (off_t) (num * sizeof(fh)), SEEK_SET); if (read(fd, fname, FNLEN) <= 0 || - brc_unread(fname, brc_num, brc_list)) + brc_unread(ptr->bid, fname)) break; num++; } @@ -1129,9 +1126,9 @@ choose_board(int newflag) break; if (ch == 'v') { ptr->myattr &= ~NBRD_UNREAD; - brc_trunc(ptr->bid, now); + brc_toggle_all_read(ptr->bid, 1); } else { - brc_trunc(ptr->bid, 1); + brc_toggle_all_read(ptr->bid, 0); ptr->myattr |= NBRD_UNREAD; } show_brdlist(head, 0, newflag); diff --git a/mbbsd/brc.c b/mbbsd/brc.c index 8c4b86da..8360cedc 100644 --- a/mbbsd/brc.c +++ b/mbbsd/brc.c @@ -6,7 +6,7 @@ */ #ifndef BRC_MAXNUM -#define BRC_STRLEN 15 /* Length of board name */ +#define BRC_STRLEN 15 /* Length of board name, for old brc */ #define BRC_MAXSIZE 24576 /* Effective size of brc rc file, 8192 * 3 */ #define BRC_MAXNUM 80 /* Upper bound of brc_num, size of brc_list */ #endif @@ -26,45 +26,38 @@ typedef unsigned short brcnbrd_t; * brc_num 1 byte, binary integer * brc_list brc_num * sizeof(int) bytes, brc_num binary integer(s) */ +static char brc_initialized = 0; static time4_t brc_expire_time; /* Will be set to the time one year before login. All the files created * before then will be recognized read. */ -static int brc_changed = 0; +static int brc_changed = 0; /**< brc_list/brc_num changed */ /* The below two will be filled by read_brc_buf() and brc_update() */ static char *brc_buf = NULL; static int brc_size; static int brc_alloc; +// read records for currbid +static int brc_currbid; +static int brc_num; +static time4_t brc_list[BRC_MAXNUM]; + static char * const fn_oldboardrc = ".boardrc"; static char * const fn_brc = ".brc2"; -#if 0 -/* unused after brc2 */ -static char * -brc_getrecord(char *ptr, char *endp, brcbid_t *bid, - brcnbrd_t *pnum, time4_t *list) -{ - brcnbrd_t num; - char *tmp; - - if (ptr + sizeof(brcbid_t) + sizeof(brcnbrd_t) > endp) - return endp + 1; /* dangling, ignoring it */ - *bid = *(brcbid_t*)ptr; /* bid */ - ptr += sizeof(brcbid_t); - num = *(brcnbrd_t*)ptr; /* brc_num */ - ptr += sizeof(brcnbrd_t); - tmp = ptr + num * sizeof(time4_t); /* end of this record */ - if (tmp <= endp){ - memcpy(list, ptr, num * sizeof(time4_t)); /* brc_list */ - if (num > BRC_MAXNUM) - num = BRC_MAXNUM; - *pnum = num; - } - return tmp; -} -#endif - +/** + * find read records of bid in given buffer region + * + * @param[in] begin + * @param[in] endp + * @param bid + * @param[out] num number of records, which could be written for \a bid + * = brc_num if found + * = 0 or dangling size if not found + * + * @return address of record. \a begin <= addr < \a endp. + * 0 if not found + */ /* Returns the address of the record strictly between begin and endp with * bid equal to the parameter. Returns 0 if not found. * brcnbrd_t *num is an output parameter which will filled with brc_num @@ -97,7 +90,7 @@ brc_findrecord_in(char *begin, char *endp, brcbid_t bid, brcnbrd_t *num) return 0; } -time4_t * +static time4_t * brc_find_record(int bid, int *num) { char *p; @@ -152,11 +145,13 @@ brc_enlarge_buf(void) #endif buffer = (char*)THE_MALLOC(brc_alloc); + assert(buffer); memcpy(buffer, brc_buf, brc_alloc); free(brc_buf); brc_alloc += BRC_BLOCKSIZE; brc_buf = (char*)malloc(brc_alloc); + assert(brc_buf); memcpy(brc_buf, buffer, brc_alloc - BRC_BLOCKSIZE); #ifdef DEBUG @@ -179,6 +174,7 @@ brc_get_buf(int size){ if (brc_alloc > BRC_MAXSIZE) brc_alloc = BRC_MAXSIZE; brc_buf = (char*)malloc(brc_alloc); + assert(brc_buf); } static inline void @@ -239,10 +235,13 @@ brc_insert_record(brcbid_t bid, brcnbrd_t num, const time4_t* list) brc_changed = 0; } +/** + * write \a brc_num and \a brc_list back to \a brc_buf. + */ void brc_update(){ - if (brc_changed && cuser.userlevel && brc_num > 0) - brc_insert_record(currbid, brc_num, brc_list); + if (brc_currbid && brc_changed && cuser.userlevel && brc_num > 0) + brc_insert_record(brc_currbid, brc_num, brc_list); } /* return 1 if successfully read from old .boardrc file. @@ -314,36 +313,65 @@ read_brc_buf(void) } void +brc_release() +{ + if (brc_buf) { + free(brc_buf); + brc_buf = NULL; + } + brc_currbid = 0; + brc_num = 0; + brc_changed = 0; + brc_size = brc_alloc = 0; +} + +void brc_finalize(){ char brcfile[STRLEN]; char tmpfile[STRLEN]; - int fd; - int ok=0; + brc_update(); setuserfile(brcfile, fn_brc); snprintf(tmpfile, sizeof(tmpfile), "%s.tmp.%x", brcfile, getpid()); - if (brc_buf != NULL && - (fd = open(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0644)) != -1) { - if(write(fd, brc_buf, brc_size)==brc_size) - ok=1; - close(fd); + if (brc_buf != NULL) { + int fd = open(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd != -1) { + int ok=0; + if(write(fd, brc_buf, brc_size)==brc_size) + ok=1; + close(fd); + if(ok) + Rename(tmpfile, brcfile); + else + unlink(tmpfile); + } } - if(ok) - Rename(tmpfile, brcfile); + + brc_release(); + brc_initialized = 0; } int brc_initialize(){ - static char done = 0; - if (done) + if (brc_initialized) return 1; - done = 1; + brc_initialized = 1; brc_expire_time = login_start_time - 365 * 86400; read_brc_buf(); return 0; } -int +/** + * get the read records for bid + * + * @param bid + * @param[out] num number of record for \a bid. 1 <= \a num <= \a BRC_MAXNUM + * \a num = 1 if no records. + * @param[out] list the list of records, length \a num + * + * @return number of read record, 0 if no records + */ +static int brc_read_record(int bid, int *num, time4_t *list){ char *ptr; brcnbrd_t tnum; @@ -358,12 +386,16 @@ brc_read_record(int bid, int *num, time4_t *list){ return 0; } +/** + * @return number of records in \a boardname + */ int brc_initial_board(const char *boardname) { brc_initialize(); if (strcmp(currboard, boardname) == 0) { + assert(currbid == brc_currbid); return brc_num; } @@ -372,16 +404,17 @@ brc_initial_board(const char *boardname) if( currbid == 0 ) currbid = getbnum(DEFAULT_BOARD); assert(0<=currbid-1 && currbid-1<MAX_BOARD); + brc_currbid = currbid; currboard = bcache[currbid - 1].brdname; currbrdattr = bcache[currbid - 1].brdattr; - return brc_read_record(currbid, &brc_num, brc_list); + return brc_read_record(brc_currbid, &brc_num, brc_list); } -void +static void brc_trunc(int bid, time4_t ftime){ brc_insert_record(bid, 1, &ftime); - if ( bid == currbid ){ + if ( bid == brc_currbid ){ brc_num = 1; brc_list[0] = ftime; brc_changed = 0; @@ -389,11 +422,21 @@ brc_trunc(int bid, time4_t ftime){ } void +brc_toggle_all_read(int bid, int is_all_read) +{ + if (is_all_read) + brc_trunc(bid, now); + else + brc_trunc(bid, 1); +} + +void brc_addlist(const char *fname) { int n, i; time4_t ftime; + assert(currbid == brc_currbid); if (!cuser.userlevel) return; @@ -426,30 +469,39 @@ brc_addlist(const char *fname) } int -brc_unread_time(time4_t ftime, int bnum, const time4_t *blist) +brc_unread_time(int bid, time4_t ftime) { - int n; + int i; + int bnum; + const time4_t *blist; if (ftime <= brc_expire_time) /* too old */ return 0; + if (brc_currbid && bid == brc_currbid) { + blist = brc_list; + bnum = brc_num; + } else { + blist = brc_find_record(bid, &bnum); + } + if (bnum <= 0) return 1; - for (n = 0; n < bnum; n++) { /* using linear search */ - if (ftime > blist[n]) + for (i = 0; i < bnum; i++) { /* using linear search */ + if (ftime > blist[i]) return 1; - else if (ftime == blist[n]) + else if (ftime == blist[i]) return 0; } return 0; } int -brc_unread(const char *fname, int bnum, const time4_t *blist) +brc_unread(int bid, const char *fname) { int ftime; ftime = atoi(&fname[2]); /* this will get the time of the file created */ - return brc_unread_time(ftime, bnum, blist); + return brc_unread_time(bid, ftime); } diff --git a/mbbsd/var.c b/mbbsd/var.c index 52864a4f..39c87442 100644 --- a/mbbsd/var.c +++ b/mbbsd/var.c @@ -363,10 +363,6 @@ SHM_t *SHM; boardheader_t *bcache; userinfo_t *currutmp; -/* brc.c */ -int brc_num; -time4_t brc_list[BRC_MAXNUM]; - /* read.c */ int TagNum; /* tag's number */ int TagBoard = -1; /* TagBoard = 0 : user's mailbox */ |