Пример #1
0
int delete_all_mail(int dfd, char *touser, char *filename, int size, RECORD_FUNC_ARG fptr, void *farg)
{
    int i;
    char *buf, *buf1;
    off_t filesize;
    char *fname;
    char buf2[80];
    char from[256];
    char to[256];
    struct stat st;
    int subspace = 0;
    struct userec *to_userec;

    printf("Deleting mails of %s... \n", touser);
    BBS_TRY {
        if (safe_mmapfile(filename, O_RDONLY, PROT_READ, MAP_SHARED, &buf, &filesize, NULL) == 0)
            BBS_RETURN(0);
        for (i = 0, buf1 = buf; i < filesize / size; i++, buf1 += size) {
            if ((*fptr)(buf1, farg)) {
                fname = ((struct fileheader *)buf1)->filename;
                sprintf(buf2, "SMTH.DM%s", fname);
                setmailfile(from, touser, fname);
                setmailfile(to, touser, buf2);
                rename(from, to);
                printf("found one %s...\n",fname);
                if (lstat(to, &st) == 0 && S_ISREG(st.st_mode) && st.st_nlink == 1) {
                    subspace += st.st_size;
                }
                continue;
            }
            write(dfd, buf1, size);
        }
        end_mmapfile((void *) buf, filesize, -1);
        if (getuser(touser, &to_userec) != 0) {
            if (to_userec->usedspace > subspace)
                to_userec->usedspace -= subspace;
            else
                to_userec->usedspace = 0;
        }
        BBS_RETURN(0);
    }
    BBS_CATCH {
    }
    BBS_END;
    end_mmapfile((void *) buf, filesize, -1);

    return 0;
}
Пример #2
0
static void strip_index_file(const char *bname, const char *dir)
{
	char old_dir_path[256];
	char dir_path[256];
	char buf[256];
	int old_fd;
	int fd;
	fileheader_v1_2 *ptr;
	size_t fsize;

	setbfile(old_dir_path, bname, dir);
	if ((old_fd = open(old_dir_path, O_RDONLY, 0644)) < 0)
	{
		fprintf(stderr, "Warning: %s/%s not found.\n", bname, dir);
		return;
	}
	sprintf(buf, "%s.NEW", dir);
	setbfile(dir_path, bname, buf);
	if ((fd = open(dir_path, O_RDWR | O_CREAT, 0644)) < 0)
	{
		close(old_fd);
		return;
	}
	if (safe_mmapfile_handle(old_fd, PROT_READ, MAP_SHARED, 
				(void **)&ptr, (off_t *)&fsize))
	{
		int i;
		int rec_count;
		fileheader fh;

		rec_count = fsize / sizeof(fileheader_v1_2);
		for (i = 0; i < rec_count; i++)
		{
			bzero(&fh, sizeof(fh));
			strip_fileheader(ptr + i, &fh, bname, dir);
			write(fd, &fh, sizeof(fh));
		}
		end_mmapfile((void*)ptr, fsize, -1);
		close(fd);
		close(old_fd);
		sprintf(buf, "%s.v1.2", old_dir_path);
		rename(old_dir_path, buf);
		rename(dir_path, old_dir_path);
	}
	else
	{
		close(fd);
		close(old_fd);
	}
}
Пример #3
0
int generate_board_title(struct boardheader *bh,void* arg)
{
    int fd2, total, i;
    char olddirect[PATHLEN];
    char *ptr;
    struct fileheader * ptr1;
    struct BoardStatus* bs;
    struct stat buf;
	unsigned int iid;

    setbdir(DIR_MODE_NORMAL, olddirect, bh->filename);
    
    if ((fd2 = open(olddirect, O_RDWR, 0664)) == -1) {
		perror(olddirect);
        return 0;
    }

        if (safe_mmapfile_handle(fd2, PROT_READ | PROT_WRITE, MAP_SHARED, &ptr, &buf.st_size) == 0) {
            close(fd2);
            return 0;
        }
        total = buf.st_size / (sizeof(struct fileheader));
        ptr1 = (struct fileheader *) ptr;

		iid = ptr1[total-1].id;

		if(iid <=0) iid = 1;

    end_mmapfile((void *) ptr, buf.st_size, -1);
    close(fd2);

    bs=getbstatus(getbid(bh->filename,NULL));
	if(bs)
    	bs->nowid= iid + 1;

    return 0;
}
Пример #4
0
int calcboard(struct boardheader *bh, void *arg)
{
    char fn[80];
    int fd, total, i;
    struct stat buf;
    struct flock ldata;
    struct fileheader *ptr1;
    char *ptr;
    int size = sizeof(struct fileheader);

    setbdir(0, fn, bh->filename);
    printf("\r%s:\n", bh->filename);
    if ((fd = open(fn, O_RDWR, 0664)) == -1) {
        bbslog("user", "%s", "recopen err");
        return 0;               /* 创建文件发生错误 */
    }
    fstat(fd, &buf);
    ldata.l_type = F_RDLCK;
    ldata.l_whence = 0;
    ldata.l_len = 0;
    ldata.l_start = 0;
    fcntl(fd, F_SETLKW, &ldata);
    total = buf.st_size / size;

    if ((i = safe_mmapfile_handle(fd, PROT_READ | PROT_WRITE, MAP_SHARED, (void **) &ptr, &buf.st_size)) != 1) {
        if (i == 2)
            end_mmapfile((void *) ptr, buf.st_size, -1);
        ldata.l_type = F_UNLCK;
        fcntl(fd, F_SETLKW, &ldata);
        close(fd);
        return 0;
    }
    ptr1 = (struct fileheader *) ptr;
    for (i = 0; i < total; i++) {
        struct stat st;
        char *p, *op;
        char ffn[80];
        int j;
        off_t fsize;

        printf("\r%d", i);
        setbfile(ffn, bh->filename, ptr1->filename);
        {
            int k, abssize = 0, entercount = 0, ignoreline = 0;
            char *attach;
            long attach_len;

            j = safe_mmapfile(ffn, O_RDONLY, PROT_READ, MAP_SHARED, (void **) &p, &fsize, NULL);
            op = p;
            if (j) {
                k = fsize;
                while (k) {
			/*
                    if (NULL != (checkattach(p, k, &attach_len, &attach))) {
                        k -= (attach - p) + attach_len;
                        p = attach + attach_len;
                        continue;
                    }  */
                    if (k >= 3 && *p == '\n' && *(p + 1) == '-' && *(p + 2) == '-' && *(p + 3) == '\n')
                        break;
                    if (*p == '\n') {
                        entercount++;
                        ignoreline = 0;
                    }
                    if (k >= 5 && *p == '\n' && *(p + 1) == '\xa1' && *(p + 2) == '\xbe' && *(p + 3) == ' ' && *(p + 4) == '\xd4' && *(p + 5) == '\xda')
                        ignoreline = 1;
                    if (k >= 2 && *p == '\n' && *(p + 1) == ':' && *(p + 2) == ' ')
                        ignoreline = 2;
                    if (k >= 2 && *p == KEY_ESC && *(p + 1) == '[' && *(p + 2) == 'm')
                        ignoreline = 3;

                    k--;
                    p++;
                    if (entercount >= 4 && !ignoreline)
                        abssize++;
                }
                ptr1->eff_size = abssize;
                end_mmapfile((void *) op, fsize, -1);
            }
        }
        //ptr1->posttime = get_posttime(ptr1);
        set_posttime(ptr1);
        ptr1++;
    }
    end_mmapfile((void *) ptr, buf.st_size, -1);
    ldata.l_type = F_UNLCK;
    fcntl(fd, F_SETLKW, &ldata);
    close(fd);
    return 0;
}
Пример #5
0
int generate_board_title(struct boardheader *bh,void* arg)
{
    int fd2, size = sizeof(fileheader), total, i;
    struct boardheader btmp;
    char olddirect[PATHLEN];
    char *ptr, *t, *t2;
#ifdef GEN_ORIGIN
    FILE *fp=NULL;
    char opath[512];
#endif
    struct fileheader * ptr1;
    struct search_temp {
        bool has_pre;
        int digest;
        int thread_id;
        int id;
        int next;
    } *index=NULL;
    struct stat buf;
    int gen_threadid;
    struct BoardStatus* bs;

#ifdef GEN_ORIGIN
    setbdir(DIR_MODE_ORIGIN, opath, bh->filename);
#endif
    setbdir(DIR_MODE_NORMAL, olddirect, bh->filename);

    gen_threadid=1;
    if ((fd2 = open(olddirect, O_RDWR, 0664)) == -1) {
        perror(olddirect);
        return 0;
    }

    BBS_TRY {
        if (!safe_mmapfile_handle(fd2,PROT_READ|PROT_WRITE,MAP_SHARED,&ptr,&buf.st_size)) {
            close(fd2);
            return 0;
        }
#ifdef GEN_ORIGIN
        fp = fopen(opath, "wb");
#endif
        total = buf.st_size / size;
        index = (struct search_temp *) malloc(sizeof(*index) * total);
        ptr1 = (struct fileheader *) ptr;
        for (i = 0; i < total; i++, ptr1++) {
            int j;

            t2 = ptr1->title;
            if (!strncmp(t2, "Re:", 3)) {
                index[i].has_pre = true;
                t2 += 4;
            } else
                index[i].has_pre = false;
            index[i].thread_id = 0;
            index[i].digest = simple_digest(t2, ARTICLE_TITLE_LEN);
            for (j = i - 1; j >= 0; j--) {
                struct fileheader *tmppost;

                if (index[j].digest != index[i].digest)
                    continue;
                tmppost = ((struct fileheader *)(ptr + j * size));
                t = tmppost->title;
                if (index[j].has_pre)
                    t += 4;
                if (!strcmp(t, t2)) {
                    index[j].next = i;
                    index[i].thread_id = index[j].thread_id;
                    index[i].next = 0;
                    break;
                }
            }
            if (index[i].thread_id == 0) {
                index[i].thread_id = gen_threadid;
                index[i].id = gen_threadid;
                index[i].next = 0;
                gen_threadid++;
            } else {
                index[i].id = gen_threadid;
                gen_threadid++;
            }
        }
        ptr1 = (struct fileheader *) ptr;
        for (i = 0; i < total; i++, ptr1++) {
            ptr1->id = index[i].id;
            ptr1->groupid = index[i].thread_id;
            ptr1->reid = index[i].thread_id;
#ifdef GEN_ORIGIN
            if (index[i].has_pre == false)
                fwrite(ptr1, 1, sizeof(struct fileheader), fp);
#endif
        }
    }
    BBS_CATCH {
    }
    BBS_END;
    free(index);
    end_mmapfile((void *) ptr, buf.st_size, -1);
    close(fd2);
#ifdef GEN_ORIGIN
    fclose(fp);
#endif
    memcpy(&btmp,getbcache(bh->filename),sizeof(btmp));
    bs=getbstatus(getbid(bh->filename,NULL));
    bs->nowid=gen_threadid + 1;
    set_board(getbid(bh->filename,NULL), &btmp,NULL);
    setboardmark(bh->filename, 1);
    setboardtitle(bh->filename, 1);
    setboardorigin(bh->filename, 1);
    return 0;
}
Пример #6
0
/* return total size */
int calc_mailsize(char *userid, char *dirname)
{
    char fn[256];
    int fd, total, i;
    struct stat buf;
    struct flock ldata;
    struct fileheader * ptr1;
    char * ptr;
    int totalsize = 0;
    int size=sizeof(struct fileheader);

    setmailfile(fn, userid, dirname);

    if ((fd = open(fn, O_RDWR, 0664)) == -1) {
        /* bbslog("user", "%s", "recopen err"); */
        return 0;      /* 创建文件发生错误*/
    }
    fstat(fd, &buf);
    total = buf.st_size / size;
    if (total == 0) {
        close(fd);
        return 0;
    }
    ldata.l_type = F_RDLCK;
    ldata.l_whence = 0;
    ldata.l_len = 0;
    ldata.l_start = 0;
    fcntl(fd, F_SETLKW, &ldata);

    if((i=safe_mmapfile_handle(fd,PROT_READ|PROT_WRITE,MAP_SHARED, &ptr, &buf.st_size))!=1){
        if (i == 2)
            end_mmapfile((void *) ptr, buf.st_size, -1);
        ldata.l_type = F_UNLCK;
        fcntl(fd, F_SETLKW, &ldata);
        close(fd);
        return 0;
    }
    ptr1 = (struct fileheader *) ptr;
    for (i = 0; i < total; i++) {
        struct stat st;
        char ffn[256];
#ifndef FORCE_SYNC
        if (ptr1->eff_size > 0) {
            totalsize += ptr1->eff_size;
            ptr1++;
            continue;
        }
#endif
        setmailfile(ffn, userid, ptr1->filename);
        if (lstat(ffn, &st) == -1 || !S_ISREG(st.st_mode)) ptr1->eff_size = 0;
        else {
            ptr1->eff_size = st.st_size;
            totalsize += ptr1->eff_size;
        }

        ptr1++;
    }
    end_mmapfile((void *) ptr, buf.st_size, -1);
    ldata.l_type = F_UNLCK;
    fcntl(fd, F_SETLKW, &ldata);
    close(fd);
    return totalsize;
}
Пример #7
0
int gen_super_filter_index2(char *index, struct fileheader* fileinfo, char * boardname, int isbm) {
    struct fileheader *ptr1;
    struct flock ldata, ldata2;
    int fd, fd2, size = sizeof(fileheader), total, i, count = 0;
    char direct[PATHLEN];
    char newdirect[PATHLEN];
    char *ptr;
    struct stat buf;
    int load_content=0, found=0, load_stat=0;
    int gid = fileinfo->groupid;

    //TODO: 这么大的index!
    load_content = (strstr(index, "content")!=NULL||strstr(index, "文章内容")!=NULL);
    load_stat = (strstr(index, "asize")!=NULL||strstr(index, "总长度")!=NULL);
    setbdir(DIR_MODE_NORMAL, direct, boardname);
    setbdir(DIR_MODE_SUPERFITER, newdirect, boardname);
    if ((fd = open(newdirect, O_WRONLY | O_CREAT, 0664)) == -1) {
        bbslog("user", "%s", "recopen err");
        return -9999;
    }
    ldata.l_type = F_WRLCK;
    ldata.l_whence = 0;
    ldata.l_len = 0;
    ldata.l_start = 0;
    if (fcntl(fd, F_SETLKW, &ldata) == -1) {
        bbslog("user", "%s", "reclock err");
        close(fd);
        return -9999;
    }

    if ((fd2 = open(direct, O_RDONLY, 0664)) == -1) {
        bbslog("user", "%s", "recopen err");
        ldata.l_type = F_UNLCK;
        fcntl(fd, F_SETLKW, &ldata);
        close(fd);
        return -9999;
    }
    fstat(fd2, &buf);
    ldata2.l_type = F_RDLCK;
    ldata2.l_whence = 0;
    ldata2.l_len = 0;
    ldata2.l_start = 0;
    fcntl(fd2, F_SETLKW, &ldata2);
    total = buf.st_size / size;

    if ((i = safe_mmapfile_handle(fd2, PROT_READ, MAP_SHARED, (void **) &ptr, &buf.st_size)) != 1) {
        if (i == 2)
            end_mmapfile((void *) ptr, buf.st_size, -1);
        ldata2.l_type = F_UNLCK;
        fcntl(fd2, F_SETLKW, &ldata2);
        close(fd2);
        ldata.l_type = F_UNLCK;
        fcntl(fd, F_SETLKW, &ldata);
        close(fd);
        return -9999;
    }
    ptr1 = (struct fileheader *) ptr;
    libs = (char*)malloc(LIBLEN);
    for (i = 0; i < total; i++) {
        struct stat st;
        char* p;
        char ffn[80];
        int j=0;
        off_t fsize;
        libptr = libs;
        ferr = 0;
        
        set_vard(fvars+fget_var("cid"), fileinfo->id);
        set_vard(fvars+fget_var("creid"), fileinfo->reid);
        set_vard(fvars+fget_var("cgroupid"), fileinfo->groupid);
        set_vars(fvars+fget_var("cauthor"), fileinfo->owner);
        set_vars(fvars+fget_var("cfname"), fileinfo->filename);
        set_vard(fvars+fget_var("cftime"), get_posttime(fileinfo));
        set_vard(fvars+fget_var("ceffsize"), fileinfo->eff_size);
        
        set_vard(fvars+fget_var("no"), i+1); set_vard(fvars+fget_var("文章号"), i+1);
        set_vard(fvars+fget_var("id"), ptr1->id);
        set_vard(fvars+fget_var("reid"), ptr1->reid);
        set_vard(fvars+fget_var("groupid"), ptr1->groupid);
        set_vard(fvars+fget_var("thread"), ptr1->groupid==gid); set_vard(fvars+fget_var("本主题"), ptr1->groupid==gid);
        set_vard(fvars+fget_var("origin"), ptr1->id==ptr1->groupid); set_vard(fvars+fget_var("原作"), ptr1->id==ptr1->groupid);
        set_vard(fvars+fget_var("m"), ptr1->accessed[0]&FILE_MARKED); set_vard(fvars+fget_var("保留"), ptr1->accessed[0]&FILE_MARKED);
        set_vard(fvars+fget_var("g"), ptr1->accessed[0]&FILE_DIGEST); set_vard(fvars+fget_var("文摘"), ptr1->accessed[0]&FILE_DIGEST);
        set_vard(fvars+fget_var("b"), (ptr1->accessed[0]&FILE_MARKED)&&(ptr1->accessed[0]&FILE_DIGEST));
        if (isbm) {
            set_vard(fvars+fget_var("noreply"), ptr1->accessed[1]&FILE_READ); set_vard(fvars+fget_var("不可回复"), ptr1->accessed[1]&FILE_READ);
            set_vard(fvars+fget_var("sign"), ptr1->accessed[0]&FILE_SIGN); set_vard(fvars+fget_var("标记"), ptr1->accessed[0]&FILE_SIGN);
#ifdef PERCENT_SIGN_SUPPORT
	     set_vard(fvars+fget_var("percent"), ptr1->accessed[0]&FILE_PERCENT); set_vard(fvars+fget_var("百分号"), ptr1->accessed[0]&FILE_PERCENT);
#endif
#ifdef FILTER
            set_vard(fvars+fget_var("censor"), ptr1->accessed[1]&FILE_CENSOR); set_vard(fvars+fget_var("审核"), ptr1->accessed[1]&FILE_CENSOR);
#endif
            set_vard(fvars+fget_var("del"), ptr1->accessed[1]&FILE_DEL); set_vard(fvars+fget_var("删除"), ptr1->accessed[1]&FILE_DEL);
            set_vard(fvars+fget_var("import"), ptr1->accessed[0]&FILE_IMPORTED); set_vard(fvars+fget_var("精华"), ptr1->accessed[0]&FILE_IMPORTED);
        }
        set_vard(fvars+fget_var("attach"), ptr1->attachment); set_vard(fvars+fget_var("附件"), ptr1->attachment);
        set_vars(fvars+fget_var("title"), ptr1->title); set_vars(fvars+fget_var("标题"), ptr1->title);
        set_vars(fvars+fget_var("author"), ptr1->owner); set_vars(fvars+fget_var("作者"), ptr1->owner);
        set_vars(fvars+fget_var("fname"), ptr1->filename); set_vars(fvars+fget_var("文件名"), ptr1->filename);
        set_vard(fvars+fget_var("my"), !strcmp(ptr1->owner,getCurrentUser()->userid)); set_vard(fvars+fget_var("我的"), !strcmp(ptr1->owner,getCurrentUser()->userid));
#ifdef HAVE_BRC_CONTROL
        set_vard(fvars+fget_var("unread"), brc_unread(ptr1->id, getSession())); set_vard(fvars+fget_var("未读"), brc_unread(ptr1->id, getSession()));
#endif
        setbfile(ffn, boardname, ptr1->filename);
        set_vard(fvars+fget_var("ftime"), get_posttime(ptr1)); set_vard(fvars+fget_var("时间"), get_posttime(ptr1));
        set_vard(fvars+fget_var("effsize"), ptr1->eff_size); set_vard(fvars+fget_var("有效长度"), ptr1->eff_size);
        if(load_stat) {
            if(stat(ffn, &st)!=-1) {
                set_vard(fvars+fget_var("asize"), st.st_size); set_vard(fvars+fget_var("总长度"), st.st_size);
            }
            else {
                set_vard(fvars+fget_var("asize"), 0); set_vard(fvars+fget_var("总长度"), 0);
            }
        }
        if(load_content) {
            set_vars(fvars+fget_var("content"), ptr1->filename);
            set_vars(fvars+fget_var("文章内容"), ptr1->filename);
            j = safe_mmapfile(ffn, O_RDONLY, PROT_READ, MAP_SHARED, (void **) &p, &fsize, NULL);
            if(j) {
                set_vars(fvars+fget_var("content"), p);
                set_vars(fvars+fget_var("文章内容"), p);
            }
        }
        ferr=0;
        feval(fvars+fget_var("res"), index, 0, strlen(index)-1);
        if(ferr) break;
        if(fvars[fget_var("res")].s) {
            write(fd, ptr1, size);
            count++;
            found++;
        }
        if(load_content) {
            if(j)
                end_mmapfile((void*)p, fsize, -1);
        }
        ptr1++;
    }
    free(libs);
    end_mmapfile((void *) ptr, buf.st_size, -1);
    ldata2.l_type = F_UNLCK;
    fcntl(fd2, F_SETLKW, &ldata2);
    close(fd2);
    ftruncate(fd, count * size);

    ldata.l_type = F_UNLCK;
    fcntl(fd, F_SETLKW, &ldata);        /* 退出互斥区域*/
    close(fd);
    if(ferr) {
        return -ferr;
    }
    else {
        return count;
    }
}
Пример #8
0
int refresh_board(char* bname)
{
    int fd, fd2, origincount, acount, i, j, replycount;
    char dirpath[256], originpath[256];
    char *head, *ahead;
    struct fileheader *ptr, *aptr, *topic, *lastpost;
    struct stat buf, abuf;

    if (setboardorigin(bname, -1))
        board_regenspecial(bname, DIR_MODE_ORIGIN, NULL);

    setbdir(DIR_MODE_NORMAL, dirpath, bname);
    setbdir(DIR_MODE_ORIGIN, originpath, bname);

    fd = open(originpath, O_RDWR, 0644);
    if (fd == -1) {
        printf("cannot open %s.\n", originpath);
        return 0;
    }
    fd2 = open(dirpath, O_RDWR, 0644);
    if (fd2 == -1) {
        close(fd);
        printf("cannot open %s.\n", dirpath);
        return 0;
    }

    BBS_TRY {
        if (!safe_mmapfile_handle(fd, PROT_READ | PROT_WRITE, MAP_SHARED, &head, &buf.st_size)) {
            printf("cannot mmap %s.\n", originpath);
            close(fd);
            return 0;
        }
        if (!safe_mmapfile_handle(fd2, PROT_READ | PROT_WRITE, MAP_SHARED, &ahead, &abuf.st_size)) {
            printf("cannot mmap %s.\n", dirpath);
            close(fd);
            close(fd2);
            return 0;
        }
        origincount = buf.st_size / sizeof(struct fileheader);
        acount = abuf.st_size / sizeof(struct fileheader);
        ptr = (struct fileheader *)head;
        for (i=0; i<origincount; i++) {
            replycount = 0;
            topic = NULL;
            lastpost = NULL;
            aptr = (struct fileheader *)ahead;
            for (j=0; j<acount; j++) {
                if (aptr->groupid == ptr->id) {
                    replycount++;
                    if (aptr->groupid == aptr->id)
                        topic = aptr;
                    lastpost = aptr;
                }
                aptr++;
            }
            ptr->replycount = replycount;
            if (topic)
                topic->replycount = replycount;
            if (lastpost) {
                strcpy(ptr->last_owner, lastpost->owner);
                ptr->last_posttime = get_posttime(lastpost);
                if (topic) {
                    strcpy(topic->last_owner, ptr->owner);
                    topic->last_posttime = ptr->last_posttime;
                }
            }
            ptr++;
        }
    }
    BBS_CATCH {
    }
    BBS_END;

    end_mmapfile((void *)head, buf.st_size, -1);
    end_mmapfile((void *)ahead, abuf.st_size, -1);
    close(fd);
    close(fd2);

    printf("board %s is done.\n", bname);

    return 0;
}
Пример #9
0
int pc_conv_file_to_body(char **body, char *fname)
{
    char *ptr;
    off_t size;
    int fd;

    if ((fd=open(fname, O_RDONLY)) == -1)
        return 0;

    if (*body)
        free(*body);
    *body = NULL;

    BBS_TRY {
        if (safe_mmapfile_handle(fd, PROT_READ, MAP_SHARED, &ptr, &size) == 1) {
            (*body) = (char *)malloc(size+1);
            if ((*body) != NULL) {
                char* data;
                long not;
                int matched=0;
                long bodylen=0;

                data=ptr;
                for (not=0;not<size;not++,data++) {
                    if (*data==0) {
                        matched++;
                        if (matched==ATTACHMENT_SIZE) {
                            int d;
                            long attsize;
                            data++; not++;
                            while (*data) {
                                data++;
                                not++;
                            }
                            data++;
                            not++;
                            memcpy(&d, data, 4);
                            attsize = htonl(d);
                            data+=4+attsize-1;
                            not+=4+attsize-1;
                            matched = 0;
                        }
                        continue;
                    }
                    (*body)[bodylen]=*data;
                    bodylen++;
                }
                (*body)[bodylen]=0;
                if (bodylen != size)
                    *body = realloc(*body, bodylen+1);
            }
        }
    }
    BBS_CATCH {
    }
    BBS_END;
    end_mmapfile((void *)ptr, size, -1);

    close(fd);

    return 1;

}
Пример #10
0
static char * get_file_info(char *boardname, int threadid, char *title, char *userid, char *filename, int pic)
{

    char dirfile[256];
    int fd;
    struct fileheader fh;

    setbdir(DIR_MODE_NORMAL, dirfile, boardname);
    if ((fd = open(dirfile, O_RDWR, 0644)) < 0)
        return NULL;

    if (get_records_from_id(fd, threadid, &fh, 1, NULL) == 0) {
        close(fd);
        return NULL;
    }
    close(fd);

#ifdef NEWSMTH
    // pig2532 Feb 2008: ignore topic with FEN flag in top10
    if (fh.accessed[1] & FILE_FEN) {
        printf("skip:%s/%d/%s/%s\n", boardname, fh.id, fh.owner, fh.title);
        return NULL;
    }
#endif /* NEWSMTH */

    if (pic) {
        if (!fh.attachment) {
            return NULL;
        } else {
            // 读取楼主的第一个附件,判断是否为图片,是则输出该图以备缩略之用,否则返回NULL
            char fn[PATHLEN];
            int fd_pic;
            char *src, *dst, *dot;
            off_t size_src, size_dst;
            char *aname, *start;
            long asize;

            setbfile(fn, boardname, fh.filename);
            if ((fd = open(fn, O_RDONLY)) == -1)
                return NULL;
            if (readw_lock(fd, 0, SEEK_SET, 0) == -1) {
                close(fd);
                return NULL;
            }
            if (safe_mmapfile_handle(fd, PROT_READ, MAP_PRIVATE, &src, &size_src) == 0) {
                un_lock(fd, 0, SEEK_SET, 0);
                close(fd);
                return NULL;
            }
            if (!(aname = checkattach(src + fh.attachment, size_src - fh.attachment, &asize, &start)))
                return NULL;

            dot = strrchr(aname, '.');
            // 是否图片?
            if (get_attachment_type_from_ext(dot) != ATTACH_IMG) {
                end_mmapfile(src, size_src, -1);
                un_lock(fd, 0, SEEK_SET, 0);
                close(fd);
                return NULL;
            }
            sprintf(fn, "hotpic/%s_%d", boardname, threadid);
            if ((fd_pic = open(fn, O_RDWR | O_CREAT, 0600)) == -1) {
                end_mmapfile(src, size_src, -1);
                un_lock(fd, 0, SEEK_SET, 0);
                close(fd);
                return NULL;
            }
            ftruncate(fd_pic, asize);
            if (safe_mmapfile_handle(fd_pic, PROT_WRITE, MAP_SHARED, &dst, &size_dst) == 0) {
                close(fd_pic);
                un_lock(fd, 0, SEEK_SET, 0);
                close(fd);
                return NULL;
            }
            printf("HOTPIC: %s_%d\n", boardname, threadid);
            memcpy(dst, start, asize);
            end_mmapfile(dst, size_dst, -1);
            end_mmapfile(src, size_src, -1);
            close(fd_pic);
            un_lock(fd, 0, SEEK_SET, 0);
            close(fd);
        }
    }

    strncpy(title, fh.title, 80);
    title[80]=0;

    strncpy(userid, fh.owner, IDLEN);
    userid[IDLEN]='\0';

    strncpy(filename, fh.filename, FILENAME_LEN);
    filename[FILENAME_LEN]='\0';

    return title;
}