int delete_range(const char *fpath, int id1, int id2) { fileheader_t fhdr; nol_t my; char fullpath[STRLEN], *t; int fdr, fdw, fd; int count, dcount=0; nolfilename(&my, fpath); if ((fd = OpenCreate(my.lockfn, O_RDWR | O_APPEND)) == -1) return -1; flock(fd, LOCK_EX); if ((fdr = open(fpath, O_RDONLY)) == -1) { flock(fd, LOCK_UN); close(fd); return -1; } if ( ((fdw = OpenCreate(my.newfn, O_WRONLY | O_EXCL)) == -1) && ((fdw = force_open(my.newfn)) == -1)) { close(fdr); flock(fd, LOCK_UN); close(fd); return -1; } count = 1; strlcpy(fullpath, fpath, sizeof(fullpath)); t = strrchr(fullpath, '/'); assert(t); t++; while (read(fdr, &fhdr, sizeof(fileheader_t)) == sizeof(fileheader_t)) { strcpy(t, fhdr.filename); /* rocker.011018: add new tag delete */ if ( (fhdr.filemode & FILE_MARKED) || /* 標記 */ ((fhdr.filemode & FILE_DIGEST) && (currstat != RMAIL) )|| /* 文摘 , FILE_DIGEST is used as REPLIED in mail menu.*/ (id1 && (count < id1 || count > id2)) || /* range */ (!id1 && !FindTaggedItem(&fhdr))) { /* TagList */ if ((safewrite(fdw, &fhdr, sizeof(fileheader_t)) == -1)) { close(fdr); close(fdw); unlink(my.newfn); flock(fd, LOCK_UN); close(fd); return -1; } } else { unlink(fullpath); dcount++; RemoveTagItem(&fhdr); } ++count; } close(fdr); close(fdw); if (Rename(fpath, my.oldfn) == -1 || Rename(my.newfn, fpath) == -1) { flock(fd, LOCK_UN); close(fd); return -1; } flock(fd, LOCK_UN); close(fd); return dcount; }
static int delete_range2(char *fpath, int id1, int id2) { fileheader fhdr; nol my; char fullpath[STRLEN], *t; int fdr, fdw, fd; register int count; nolfilename(&my, fpath); if ((fd = open(my.lockfn, O_RDWR | O_CREAT | O_APPEND, 0644)) < 0) return -1; flock(fd, LOCK_EX); if ((fdr = open(fpath, O_RDONLY, 0)) < 0) { flock(fd, LOCK_UN); close(fd); return -1; } if ((fdw = open(my.newfn, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0) { close(fdr); flock(fd, LOCK_UN); close(fd); return -1; } strcpy(fullpath, fpath); t = (char *) strrchr(fullpath, '/') + 1; count = 0; while (read(fdr, &fhdr, sizeof(fileheader)) == sizeof(fileheader)) { count++; if ((fhdr.filemode & FILE_MARKED) || /* 標記 */ (id1 && (count < id1 || count > id2)) || /* range */ (!id1 && Tagger(atoi(fhdr.filename+2), count, TAG_COMP))) /* TagList */ { if ((write(fdw, &fhdr, sizeof(fileheader)) < 0)) { close(fdr); close(fdw); unlink(my.newfn); flock(fd, LOCK_UN); close(fd); return -1; } } else { strcpy(t, fhdr.filename); /* 若為看板就連線砍信 */ if (currstat == READING) { cancel_post(&fhdr, fullpath); } unlink(fullpath); } } close(fdr); close(fdw); count = rename(fpath, my.oldfn); if (!count) { if (count = rename(my.newfn, fpath)) rename(my.oldfn, fpath); /* 萬一出鎚,再救回來 */ } flock(fd, LOCK_UN); close(fd); return count; }