diff --git a/apps/tagcache.c b/apps/tagcache.c index 90763bb9a6..b6ce45fa7f 100644 --- a/apps/tagcache.c +++ b/apps/tagcache.c @@ -264,6 +264,22 @@ const char* tagcache_tag_to_str(int tag) return tags_str[tag]; } +/* Helper functions for the two most read/write data structure: tagfile_entry and index_entry */ +ssize_t ecread_tagfile_entry(int fd, struct tagfile_entry *buf) +{ + return ecread(fd, buf, 1, tagfile_entry_ec, tc_stat.econ); +} + +ssize_t ecread_index_entry(int fd, struct index_entry *buf) +{ + return ecread(fd, buf, 1, index_entry_ec, tc_stat.econ); +} + +ssize_t ecwrite_index_entry(int fd, struct index_entry *buf) +{ + return ecwrite(fd, buf, 1, index_entry_ec, tc_stat.econ); +} + #ifdef HAVE_DIRCACHE /** * Returns true if specified flag is still present, i.e., dircache @@ -448,7 +464,7 @@ static long find_entry_disk(const char *filename, bool localfd) pos_history[i+1] = pos_history[i]; pos_history[0] = pos; - if (ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ) + if (ecread_tagfile_entry(fd, &tfe) != sizeof(struct tagfile_entry)) { break ; @@ -583,7 +599,7 @@ static bool get_index(int masterfd, int idxid, lseek(masterfd, idxid * sizeof(struct index_entry) + sizeof(struct master_header), SEEK_SET); - if (ecread(masterfd, idx, 1, index_entry_ec, tc_stat.econ) + if (ecread_index_entry(masterfd, idx) != sizeof(struct index_entry)) { logf("read error #3"); @@ -638,8 +654,7 @@ static bool write_index(int masterfd, int idxid, struct index_entry *idx) lseek(masterfd, idxid * sizeof(struct index_entry) + sizeof(struct master_header), SEEK_SET); - if (ecwrite(masterfd, idx, 1, index_entry_ec, tc_stat.econ) - != sizeof(struct index_entry)) + if (ecwrite_index_entry(masterfd, idx) != sizeof(struct index_entry)) { logf("write error #3"); logf("idxid: %d", idxid); @@ -717,7 +732,7 @@ static bool retrieve(struct tagcache_search *tcs, struct index_entry *idx, return false; lseek(tcs->idxfd[tag], seek, SEEK_SET); - if (ecread(tcs->idxfd[tag], &tfe, 1, tagfile_entry_ec, tc_stat.econ) + if (ecread_tagfile_entry(tcs->idxfd[tag], &tfe) != sizeof(struct tagfile_entry)) { logf("read error #5"); @@ -960,7 +975,7 @@ static bool check_clauses(struct tagcache_search *tcs, { int fd = tcs->idxfd[clause[i]->tag]; lseek(fd, seek, SEEK_SET); - ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ); + ecread_tagfile_entry(fd, &tfe); if (tfe.tag_length >= (int)sizeof(str)) { logf("Too long tag read!"); @@ -1091,7 +1106,7 @@ static bool build_lookup_list(struct tagcache_search *tcs) lseek(tcs->masterfd, tcs->seek_pos * sizeof(struct index_entry) + sizeof(struct master_header), SEEK_SET); - while (ecread(tcs->masterfd, &entry, 1, index_entry_ec, tc_stat.econ) + while (ecread_index_entry(tcs->masterfd, &entry) == sizeof(struct index_entry)) { struct tagcache_seeklist_entry *seeklist; @@ -1429,8 +1444,7 @@ static bool get_next(struct tagcache_search *tcs) /* Seek stream to the correct position and continue to direct fetch. */ lseek(tcs->idxfd[tcs->type], tcs->position, SEEK_SET); - if (ecread(tcs->idxfd[tcs->type], &entry, 1, - tagfile_entry_ec, tc_stat.econ) != sizeof(struct tagfile_entry)) + if (ecread_tagfile_entry(tcs->idxfd[tcs->type], &entry) != sizeof(struct tagfile_entry)) { logf("read error #5"); tcs->valid = false; @@ -2180,7 +2194,7 @@ static bool build_numeric_indices(struct tagcache_header *h, int tmpfd) for (i = 0; i < tcmh.tch.entry_count; i++) { /* Read the index entry. */ - if (ecread(masterfd, &idx, 1, index_entry_ec, tc_stat.econ) + if (ecread_index_entry(masterfd, &idx) != sizeof(struct index_entry)) { logf("read fail #3"); @@ -2251,8 +2265,7 @@ static bool build_numeric_indices(struct tagcache_header *h, int tmpfd) idx.flag |= FLAG_RESURRECTED; lseek(masterfd, -sizeof(struct index_entry), SEEK_CUR); - if (ecwrite(masterfd, &idx, 1, index_entry_ec, tc_stat.econ) - != sizeof(struct index_entry)) + if (ecwrite_index_entry(masterfd, &idx) != sizeof(struct index_entry)) { logf("masterfd writeback fail #1"); close(masterfd); @@ -2272,8 +2285,7 @@ static bool build_numeric_indices(struct tagcache_header *h, int tmpfd) { int loc = lseek(masterfd, 0, SEEK_CUR); - if (ecread(masterfd, &idx, 1, index_entry_ec, tc_stat.econ) - != sizeof(struct index_entry)) + if (ecread_index_entry(masterfd, &idx) != sizeof(struct index_entry)) { logf("read fail #3"); close(masterfd); @@ -2302,8 +2314,7 @@ static bool build_numeric_indices(struct tagcache_header *h, int tmpfd) /* Write back the updated index. */ lseek(masterfd, loc, SEEK_SET); - if (ecwrite(masterfd, &idx, 1, index_entry_ec, tc_stat.econ) - != sizeof(struct index_entry)) + if (ecwrite_index_entry(masterfd, &idx) != sizeof(struct index_entry)) { logf("write fail"); close(masterfd); @@ -2429,8 +2440,7 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd) int loc = lseek(fd, 0, SEEK_CUR); bool ret; - if (ecread(fd, &entry, 1, tagfile_entry_ec, tc_stat.econ) - != sizeof(struct tagfile_entry)) + if (ecread_tagfile_entry(fd, &entry) != sizeof(struct tagfile_entry)) { logf("read error #7"); close(fd); @@ -3457,8 +3467,7 @@ bool tagcache_create_changelog(struct tagcache_search *tcs) for (i = 0; i < myhdr.tch.entry_count; i++) { - if (ecread(tcs->masterfd, &idx, 1, index_entry_ec, tc_stat.econ) - != sizeof(struct index_entry)) + if (ecread_index_entry(tcs->masterfd, &idx) != sizeof(struct index_entry)) { logf("read error #9"); tagcache_search_finish(tcs); @@ -3522,8 +3531,7 @@ static bool delete_entry(long idx_id) return false; lseek(masterfd, idx_id * sizeof(struct index_entry), SEEK_CUR); - if (ecread(masterfd, &myidx, 1, index_entry_ec, tc_stat.econ) - != sizeof(struct index_entry)) + if (ecread_index_entry(masterfd, &myidx) != sizeof(struct index_entry)) { logf("delete_entry(): read error"); goto cleanup; @@ -3537,8 +3545,7 @@ static bool delete_entry(long idx_id) myidx.flag |= FLAG_DELETED; lseek(masterfd, -sizeof(struct index_entry), SEEK_CUR); - if (ecwrite(masterfd, &myidx, 1, index_entry_ec, tc_stat.econ) - != sizeof(struct index_entry)) + if (ecwrite_index_entry(masterfd, &myidx) != sizeof(struct index_entry)) { logf("delete_entry(): write_error #1"); goto cleanup; @@ -3560,8 +3567,7 @@ static bool delete_entry(long idx_id) else #endif { - if (ecread(masterfd, &idx, 1, index_entry_ec, tc_stat.econ) - != sizeof(struct index_entry)) + if (ecread_index_entry(masterfd, &idx) != sizeof(struct index_entry)) { logf("delete_entry(): read error #2"); goto cleanup; @@ -3617,8 +3623,7 @@ static bool delete_entry(long idx_id) /* Skip the header block */ lseek(fd, myidx.tag_seek[tag], SEEK_SET); - if (ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ) - != sizeof(struct tagfile_entry)) + if (ecread_tagfile_entry(fd, &tfe) != sizeof(struct tagfile_entry)) { logf("delete_entry(): read error #3"); goto cleanup; @@ -3681,8 +3686,7 @@ static bool delete_entry(long idx_id) /* Write index entry back into master index. */ lseek(masterfd, sizeof(struct master_header) + (idx_id * sizeof(struct index_entry)), SEEK_SET); - if (ecwrite(masterfd, &myidx, 1, index_entry_ec, tc_stat.econ) - != sizeof(struct index_entry)) + if (ecwrite_index_entry(masterfd, &myidx) != sizeof(struct index_entry)) { logf("delete_entry(): write_error #2"); goto cleanup; @@ -3872,7 +3876,7 @@ static bool load_tagcache(void) /* Load the master index table. */ for (i = 0; i < hdr->h.tch.entry_count; i++) { - rc = ecread(fd, idx, 1, index_entry_ec, tc_stat.econ); + rc = ecread_index_entry(fd, idx); if (rc != sizeof(struct index_entry)) { logf("read error #10"); @@ -3929,7 +3933,7 @@ static bool load_tagcache(void) fe = (struct tagfile_entry *)p; pos = lseek(fd, 0, SEEK_CUR); - rc = ecread(fd, fe, 1, tagfile_entry_ec, tc_stat.econ); + rc = ecread_tagfile_entry(fd, fe); if (rc != sizeof(struct tagfile_entry)) { /* End of lookup table. */ @@ -4076,8 +4080,7 @@ static bool check_deleted_files(void) } lseek(fd, sizeof(struct tagcache_header), SEEK_SET); - while (ecread(fd, &tfe, 1, tagfile_entry_ec, tc_stat.econ) - == sizeof(struct tagfile_entry) + while (ecread_tagfile_entry(fd, &tfe) == sizeof(struct tagfile_entry) #ifndef __PCTOOL__ && !check_event_queue() #endif @@ -4145,7 +4148,7 @@ static bool check_dir(const char *dirname, int add_files) dir = opendir(dirname); if (!dir) { - logf("tagcache: opendir() failed"); + logf("tagcache: opendir(%s) failed", dirname); return false; }