summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2022-12-01 01:53:58 +0000
committerAidan MacDonald <amachronic@protonmail.com>2023-10-01 12:10:41 +0100
commitd3b588678f9d330d1273143855f9705c4e980030 (patch)
tree1d602265e509470fdc9c1921aa1590e13ecec440
parent825ec0b601410740028daea6abd1f6543c74eb2c (diff)
downloadrockbox-d3b588678f9d330d1273143855f9705c4e980030.tar.gz
rockbox-d3b588678f9d330d1273143855f9705c4e980030.zip
Remove structec API from tagcache
Replace structec usage with explicit endian swapping routines for the various data types. Simplify endianness detection when loading the database. Make foreign endianness support optional, but leave it enabled for now. Change-Id: Ia1efb72c8830f63bbe0e51f7271c4552b5279878
-rw-r--r--apps/tagcache.c314
1 files changed, 201 insertions, 113 deletions
diff --git a/apps/tagcache.c b/apps/tagcache.c
index 0e07c4804b..6abf957bc2 100644
--- a/apps/tagcache.c
+++ b/apps/tagcache.c
@@ -81,7 +81,6 @@
81#include "settings.h" 81#include "settings.h"
82#include "dir.h" 82#include "dir.h"
83#include "pathfuncs.h" 83#include "pathfuncs.h"
84#include "structec.h"
85#include "debug.h" 84#include "debug.h"
86#include "dircache.h" 85#include "dircache.h"
87#include "errno.h" 86#include "errno.h"
@@ -91,6 +90,16 @@
91#include "eeprom_settings.h" 90#include "eeprom_settings.h"
92#endif 91#endif
93 92
93/*
94 * Define this to support non-native endian tagcache files.
95 * Databases are always written in native endian so this is
96 * basically only necessary to support databases generated
97 * by the PC database tool.
98 *
99 * Adds around 0.5-1.0k of code.
100 */
101#define TAGCACHE_SUPPORT_FOREIGN_ENDIAN
102
94/* Maximum length of a single tag. */ 103/* Maximum length of a single tag. */
95#define TAG_MAXLEN (MAX_PATH*2) 104#define TAG_MAXLEN (MAX_PATH*2)
96 105
@@ -284,16 +293,6 @@ struct master_header {
284 int32_t dirty; 293 int32_t dirty;
285}; 294};
286 295
287/* For the endianess correction */
288static const char * const tagfile_entry_ec = "ll";
289/**
290 Note: This should be (1 + TAG_COUNT) amount of l's.
291 */
292static const char * const index_entry_ec = "llllllllllllllllllllllll";
293
294static const char * const tagcache_header_ec = "lll";
295static const char * const master_header_ec = "llllll";
296
297static struct master_header current_tcmh; 296static struct master_header current_tcmh;
298 297
299#ifdef HAVE_TC_RAMCACHE 298#ifdef HAVE_TC_RAMCACHE
@@ -438,62 +437,157 @@ const char* tagcache_tag_to_str(int tag)
438 return tags_str[tag]; 437 return tags_str[tag];
439} 438}
440 439
441/* Helper functions for the two most read/write data structure: tagfile_entry and index_entry */ 440#ifdef TAGCACHE_SUPPORT_FOREIGN_ENDIAN
442static ssize_t ecread_tagfile_entry(int fd, struct tagfile_entry *buf) 441static void swap_tagfile_entry(struct tagfile_entry *buf)
442{
443 if (tc_stat.econ)
444 {
445 buf->tag_length = swap32(buf->tag_length);
446 buf->idx_id = swap32(buf->idx_id);
447 }
448}
449
450static void swap_index_entry(struct index_entry *buf)
451{
452 if (tc_stat.econ)
453 {
454 for (int i = 0; i < TAG_COUNT; ++i)
455 buf->tag_seek[i] = swap32(buf->tag_seek[i]);
456 buf->flag = swap32(buf->flag);
457 }
458}
459
460static void swap_tagcache_header(struct tagcache_header *buf)
461{
462 if (tc_stat.econ)
463 {
464 buf->magic = swap32(buf->magic);
465 buf->datasize = swap32(buf->datasize);
466 buf->entry_count = swap32(buf->entry_count);
467 }
468}
469
470static void swap_master_header(struct master_header *buf)
443{ 471{
444 return ecread(fd, buf, 1, tagfile_entry_ec, tc_stat.econ); 472 if (tc_stat.econ)
473 {
474 swap_tagcache_header(&buf->tch);
475 buf->serial = swap32(buf->serial);
476 buf->commitid = swap32(buf->commitid);
477 buf->dirty = swap32(buf->dirty);
478 }
445} 479}
480#else
481static void swap_tagfile_entry(struct tagfile_entry *buf) { (void)buf; }
482static void swap_index_entry(struct index_entry *buf) { (void)buf; }
483static void swap_tagcache_header(struct tagcache_header *buf) { (void)buf; }
484static void swap_master_header(struct master_header *buf) { (void)buf; }
485#endif
486
487static ssize_t read_tagfile_entry(int fd, struct tagfile_entry *buf)
488{
489 ssize_t ret = read(fd, buf, sizeof(*buf));
490 if (ret == sizeof(*buf) && tc_stat.econ)
491 swap_tagfile_entry(buf);
446 492
493 return ret;
494}
447 495
448enum e_ecread_errors 496static ssize_t write_tagfile_entry(int fd, struct tagfile_entry *buf)
449{ 497{
498 struct tagfile_entry e = *buf;
499
500 swap_tagfile_entry(&e);
501
502 return write(fd, &e, sizeof(e));
503}
504
505enum e_read_errors {
450 e_SUCCESS = 0, 506 e_SUCCESS = 0,
451 e_SUCCESS_LEN_ZERO = 1, 507 e_SUCCESS_LEN_ZERO = 1,
452 e_ENTRY_SIZEMISMATCH, 508 e_ENTRY_SIZEMISMATCH,
453 e_TAG_TOOLONG, 509 e_TAG_TOOLONG,
454 e_TAG_SIZEMISMATCH 510 e_TAG_SIZEMISMATCH
455}; 511};
456static enum e_ecread_errors ecread_tagfile_entry_and_tag 512
457 (int fd, struct tagfile_entry *tfe, char* buf, int bufsz) 513static enum e_read_errors
514read_tagfile_entry_and_tag(int fd, struct tagfile_entry *tfe,
515 char* buf, int bufsz)
458{ 516{
459 enum e_ecread_errors e_res = e_SUCCESS; 517 if (read_tagfile_entry(fd, tfe) != sizeof(struct tagfile_entry))
460 long tag_length = 0; 518 return e_ENTRY_SIZEMISMATCH;
461 519
462 if (ecread_tagfile_entry(fd, tfe)!= sizeof(struct tagfile_entry)) 520 long tag_length = tfe->tag_length;
463 { 521 if (tag_length >= bufsz)
464 e_res = e_ENTRY_SIZEMISMATCH; 522 return e_TAG_TOOLONG;
465 } 523
466 else 524 if (tag_length > 0 && read(fd, buf, tag_length) != tag_length)
525 return e_TAG_SIZEMISMATCH;
526
527 str_setlen(buf, tag_length);
528 return tag_length > 0 ? e_SUCCESS : e_SUCCESS_LEN_ZERO;
529}
530
531static ssize_t read_index_entries(int fd, struct index_entry *buf, size_t count)
532{
533 ssize_t ret = read(fd, buf, sizeof(*buf) * count);
534 for (ssize_t i = 0; i < ret; i += sizeof(*buf))
535 swap_index_entry(buf++);
536
537 return ret;
538}
539
540static ssize_t write_index_entries(int fd, struct index_entry *buf, size_t count)
541{
542#ifdef TAGCACHE_SUPPORT_FOREIGN_ENDIAN
543 ssize_t ret = 0;
544 for (; count > 0; count--)
467 { 545 {
468 if (tfe->tag_length == 0) 546 struct index_entry e = *buf++;
469 { 547 swap_index_entry(&e);
470 e_res = e_SUCCESS_LEN_ZERO; 548
471 } 549 ssize_t rc = write(fd, &e, sizeof(e));
472 else if (tfe->tag_length >= bufsz) 550 if (rc < 0)
473 { 551 return rc;
474 e_res = e_TAG_TOOLONG; 552 ret += rc;
475 }
476 else if(read(fd, buf, tfe->tag_length) != tfe->tag_length)
477 {
478 e_res = e_TAG_SIZEMISMATCH;
479 }
480 else
481 {
482 tag_length = tfe->tag_length;
483 }
484 } 553 }
485 str_setlen(buf, tag_length); 554
486 return e_res; 555 return ret;
556#else
557 return write(fd, buf, sizeof(*buf) * count);
558#endif
559}
560
561static ssize_t read_tagcache_header(int fd, struct tagcache_header *buf)
562{
563 ssize_t ret = read(fd, buf, sizeof(*buf));
564 if (ret == sizeof(*buf))
565 swap_tagcache_header(buf);
566
567 return ret;
568}
569
570static ssize_t write_tagcache_header(int fd, struct tagcache_header *buf)
571{
572 struct tagcache_header e = *buf;
573 swap_tagcache_header(&e);
574 return write(fd, &e, sizeof(e));
487} 575}
488 576
489static ssize_t ecread_index_entry(int fd, struct index_entry *buf) 577static ssize_t read_master_header(int fd, struct master_header *buf)
490{ 578{
491 return ecread(fd, buf, 1, index_entry_ec, tc_stat.econ); 579 ssize_t ret = read(fd, buf, sizeof(*buf));
580 if (ret == sizeof(*buf))
581 swap_master_header(buf);
582
583 return ret;
492} 584}
493 585
494static ssize_t ecwrite_index_entry(int fd, struct index_entry *buf) 586static ssize_t write_master_header(int fd, struct master_header *buf)
495{ 587{
496 return ecwrite(fd, buf, 1, index_entry_ec, tc_stat.econ); 588 struct master_header e = *buf;
589 swap_master_header(&e);
590 return write(fd, &e, sizeof(e));
497} 591}
498 592
499/* 593/*
@@ -526,7 +620,6 @@ static int NO_INLINE remove_db_file(const char* filename)
526static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write) 620static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write)
527{ 621{
528 int fd; 622 int fd;
529 int rc;
530 char fname[MAX_PATH]; 623 char fname[MAX_PATH];
531 624
532 if (TAGCACHE_IS_NUMERIC(tag) || tag < 0 || tag >= TAG_COUNT) 625 if (TAGCACHE_IS_NUMERIC(tag) || tag < 0 || tag >= TAG_COUNT)
@@ -544,8 +637,8 @@ static int open_tag_fd(struct tagcache_header *hdr, int tag, bool write)
544 } 637 }
545 638
546 /* Check the header. */ 639 /* Check the header. */
547 rc = ecread(fd, hdr, 1, tagcache_header_ec, tc_stat.econ); 640 if (read_tagcache_header(fd, hdr) != sizeof(struct tagcache_header) ||
548 if (hdr->magic != TAGCACHE_MAGIC || rc != sizeof(struct tagcache_header)) 641 hdr->magic != TAGCACHE_MAGIC)
549 { 642 {
550 logf("header error"); 643 logf("header error");
551 tc_stat.ready = false; 644 tc_stat.ready = false;
@@ -569,30 +662,33 @@ static int open_master_fd(struct master_header *hdr, bool write)
569 return fd; 662 return fd;
570 } 663 }
571 664
572 tc_stat.econ = false;
573
574 /* Check the header. */
575 rc = read(fd, hdr, sizeof(struct master_header)); 665 rc = read(fd, hdr, sizeof(struct master_header));
576 if (rc == sizeof(struct master_header) && hdr->tch.magic == TAGCACHE_MAGIC) 666 if (rc != sizeof(struct master_header))
577 { 667 {
578 /* Success. */ 668 logf("master file read failed");
579 return fd; 669 close(fd);
670 return -1;
580 } 671 }
581 672
582 /* Trying to read again, this time with endianess correction enabled. */ 673 /* Tagcache files can have either endianness. A device will always
583 lseek(fd, 0, SEEK_SET); 674 * create files in its native endianness, but we accept non-native
584 675 * endian files for compatibility reasons. */
585 rc = ecread(fd, hdr, 1, master_header_ec, true); 676 if (hdr->tch.magic == TAGCACHE_MAGIC)
586 if (rc != sizeof(struct master_header) || hdr->tch.magic != TAGCACHE_MAGIC) 677 tc_stat.econ = false;
678#ifdef TAGCACHE_SUPPORT_FOREIGN_ENDIAN
679 else if (hdr->tch.magic == swap32(TAGCACHE_MAGIC))
587 { 680 {
588 logf("header error"); 681 tc_stat.econ = true;
589 tc_stat.ready = false; 682 swap_master_header(hdr);
683 }
684#endif
685 else
686 {
687 logf("master file bad magic: %08lx\n", (unsigned long)hdr->tch.magic);
590 close(fd); 688 close(fd);
591 return -2; 689 return -2;
592 } 690 }
593 691
594 tc_stat.econ = true;
595
596 return fd; 692 return fd;
597} 693}
598 694
@@ -717,7 +813,7 @@ static long find_entry_disk(const char *filename_raw, bool localfd)
717 pos_history[i+1] = pos_history[i]; 813 pos_history[i+1] = pos_history[i];
718 pos_history[0] = pos; 814 pos_history[0] = pos;
719 815
720 if (ecread_tagfile_entry(fd, &tfe)!= sizeof(struct tagfile_entry)) 816 if (read_tagfile_entry(fd, &tfe) != sizeof(struct tagfile_entry))
721 { 817 {
722 logf("size mismatch find entry"); 818 logf("size mismatch find entry");
723 break; 819 break;
@@ -841,8 +937,7 @@ static bool get_index(int masterfd, int idxid,
841 937
842 lseek(masterfd, idxid * sizeof(struct index_entry) 938 lseek(masterfd, idxid * sizeof(struct index_entry)
843 + sizeof(struct master_header), SEEK_SET); 939 + sizeof(struct master_header), SEEK_SET);
844 if (ecread_index_entry(masterfd, idx) 940 if (read_index_entries(masterfd, idx, 1) != sizeof(struct index_entry))
845 != sizeof(struct index_entry))
846 { 941 {
847 logf("read error #3"); 942 logf("read error #3");
848 if (localfd) 943 if (localfd)
@@ -897,7 +992,7 @@ static bool write_index(int masterfd, int idxid, struct index_entry *idx)
897 992
898 lseek(masterfd, idxid * sizeof(struct index_entry) 993 lseek(masterfd, idxid * sizeof(struct index_entry)
899 + sizeof(struct master_header), SEEK_SET); 994 + sizeof(struct master_header), SEEK_SET);
900 if (ecwrite_index_entry(masterfd, idx) != sizeof(struct index_entry)) 995 if (write_index_entries(masterfd, idx, 1) != sizeof(struct index_entry))
901 { 996 {
902 logf("write error #3"); 997 logf("write error #3");
903 logf("idxid: %d", idxid); 998 logf("idxid: %d", idxid);
@@ -975,7 +1070,7 @@ static bool retrieve(struct tagcache_search *tcs, IF_DIRCACHE(int idx_id,)
975 if (!success && open_files(tcs, tag)) 1070 if (!success && open_files(tcs, tag))
976 { 1071 {
977 lseek(tcs->idxfd[tag], seek, SEEK_SET); 1072 lseek(tcs->idxfd[tag], seek, SEEK_SET);
978 switch (ecread_tagfile_entry_and_tag(tcs->idxfd[tag], &tfe, buf, bufsz)) 1073 switch (read_tagfile_entry_and_tag(tcs->idxfd[tag], &tfe, buf, bufsz))
979 { 1074 {
980 case e_ENTRY_SIZEMISMATCH: 1075 case e_ENTRY_SIZEMISMATCH:
981 logf("read error #5"); 1076 logf("read error #5");
@@ -1330,7 +1425,7 @@ static bool check_clauses(struct tagcache_search *tcs,
1330 int fd = tcs->idxfd[tag]; 1425 int fd = tcs->idxfd[tag];
1331 lseek(fd, seek, SEEK_SET); 1426 lseek(fd, seek, SEEK_SET);
1332 1427
1333 switch (ecread_tagfile_entry_and_tag(fd, &tfe, str, bufsz)) 1428 switch (read_tagfile_entry_and_tag(fd, &tfe, str, bufsz))
1334 { 1429 {
1335 case e_SUCCESS_LEN_ZERO: /* Check if entry has been deleted. */ 1430 case e_SUCCESS_LEN_ZERO: /* Check if entry has been deleted. */
1336 return false; 1431 return false;
@@ -1494,8 +1589,7 @@ static bool build_lookup_list(struct tagcache_search *tcs)
1494 lseek(tcs->masterfd, tcs->seek_pos * sizeof(struct index_entry) + 1589 lseek(tcs->masterfd, tcs->seek_pos * sizeof(struct index_entry) +
1495 sizeof(struct master_header), SEEK_SET); 1590 sizeof(struct master_header), SEEK_SET);
1496 1591
1497 while (ecread_index_entry(tcs->masterfd, &entry) 1592 while (read_index_entries(tcs->masterfd, &entry, 1) == sizeof(struct index_entry))
1498 == sizeof(struct index_entry))
1499 { 1593 {
1500 struct tagcache_seeklist_entry *seeklist; 1594 struct tagcache_seeklist_entry *seeklist;
1501 1595
@@ -1844,7 +1938,7 @@ static bool get_next(struct tagcache_search *tcs, bool is_numeric)
1844 /* Seek stream to the correct position and continue to direct fetch. */ 1938 /* Seek stream to the correct position and continue to direct fetch. */
1845 lseek(tcs->idxfd[tcs->type], tcs->position, SEEK_SET); 1939 lseek(tcs->idxfd[tcs->type], tcs->position, SEEK_SET);
1846 1940
1847 switch (ecread_tagfile_entry_and_tag(tcs->idxfd[tcs->type], &entry, buf, bufsz)) 1941 switch (read_tagfile_entry_and_tag(tcs->idxfd[tcs->type], &entry, buf, bufsz))
1848 { 1942 {
1849 case e_SUCCESS_LEN_ZERO: 1943 case e_SUCCESS_LEN_ZERO:
1850 case e_SUCCESS: 1944 case e_SUCCESS:
@@ -1927,7 +2021,7 @@ static bool update_master_header(void)
1927 2021
1928 /* Write it back */ 2022 /* Write it back */
1929 lseek(fd, 0, SEEK_SET); 2023 lseek(fd, 0, SEEK_SET);
1930 ecwrite(fd, &myhdr, 1, master_header_ec, tc_stat.econ); 2024 write_master_header(fd, &myhdr);
1931 close(fd); 2025 close(fd);
1932 2026
1933 return true; 2027 return true;
@@ -2422,8 +2516,7 @@ static int tempbuf_sort(int fd)
2422 % TAGFILE_ENTRY_CHUNK_LENGTH); 2516 % TAGFILE_ENTRY_CHUNK_LENGTH);
2423 } 2517 }
2424 2518
2425 if (ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ) != 2519 if (write_tagfile_entry(fd, &fe) != sizeof(struct tagfile_entry))
2426 sizeof(struct tagfile_entry))
2427 { 2520 {
2428 logf("tempbuf_sort: write error #1"); 2521 logf("tempbuf_sort: write error #1");
2429 return -1; 2522 return -1;
@@ -2563,8 +2656,7 @@ static bool build_numeric_indices(struct tagcache_header *h, int tmpfd)
2563 for (i = 0; i < tcmh.tch.entry_count; i++) 2656 for (i = 0; i < tcmh.tch.entry_count; i++)
2564 { 2657 {
2565 /* Read the index entry. */ 2658 /* Read the index entry. */
2566 if (ecread_index_entry(masterfd, &idx) 2659 if (read_index_entries(masterfd, &idx, 1) != sizeof(struct index_entry))
2567 != sizeof(struct index_entry))
2568 { 2660 {
2569 logf("read fail #3"); 2661 logf("read fail #3");
2570 close(masterfd); 2662 close(masterfd);
@@ -2636,7 +2728,7 @@ static bool build_numeric_indices(struct tagcache_header *h, int tmpfd)
2636 idx.flag |= FLAG_RESURRECTED; 2728 idx.flag |= FLAG_RESURRECTED;
2637 2729
2638 lseek(masterfd, -(off_t)sizeof(struct index_entry), SEEK_CUR); 2730 lseek(masterfd, -(off_t)sizeof(struct index_entry), SEEK_CUR);
2639 if (ecwrite_index_entry(masterfd, &idx) != sizeof(struct index_entry)) 2731 if (write_index_entries(masterfd, &idx, 1) != sizeof(struct index_entry))
2640 { 2732 {
2641 logf("masterfd writeback fail #1"); 2733 logf("masterfd writeback fail #1");
2642 close(masterfd); 2734 close(masterfd);
@@ -2656,7 +2748,7 @@ static bool build_numeric_indices(struct tagcache_header *h, int tmpfd)
2656 { 2748 {
2657 int loc = lseek(masterfd, 0, SEEK_CUR); 2749 int loc = lseek(masterfd, 0, SEEK_CUR);
2658 2750
2659 if (ecread_index_entry(masterfd, &idx) != sizeof(struct index_entry)) 2751 if (read_index_entries(masterfd, &idx, 1) != sizeof(struct index_entry))
2660 { 2752 {
2661 logf("read fail #3"); 2753 logf("read fail #3");
2662 close(masterfd); 2754 close(masterfd);
@@ -2685,7 +2777,7 @@ static bool build_numeric_indices(struct tagcache_header *h, int tmpfd)
2685 2777
2686 /* Write back the updated index. */ 2778 /* Write back the updated index. */
2687 lseek(masterfd, loc, SEEK_SET); 2779 lseek(masterfd, loc, SEEK_SET);
2688 if (ecwrite_index_entry(masterfd, &idx) != sizeof(struct index_entry)) 2780 if (write_index_entries(masterfd, &idx, 1) != sizeof(struct index_entry))
2689 { 2781 {
2690 logf("write fail"); 2782 logf("write fail");
2691 close(masterfd); 2783 close(masterfd);
@@ -2812,7 +2904,7 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2812 struct tagfile_entry entry; 2904 struct tagfile_entry entry;
2813 int loc = lseek(fd, 0, SEEK_CUR); 2905 int loc = lseek(fd, 0, SEEK_CUR);
2814 bool ret; 2906 bool ret;
2815 switch (ecread_tagfile_entry_and_tag(fd, &entry, buf, bufsz)) 2907 switch (read_tagfile_entry_and_tag(fd, &entry, buf, bufsz))
2816 { 2908 {
2817 case e_SUCCESS_LEN_ZERO: /* Skip deleted entries. */ 2909 case e_SUCCESS_LEN_ZERO: /* Skip deleted entries. */
2818 continue; 2910 continue;
@@ -2874,8 +2966,7 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2874 tch.entry_count = 0; 2966 tch.entry_count = 0;
2875 tch.datasize = 0; 2967 tch.datasize = 0;
2876 2968
2877 if (ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ) 2969 if (write_tagcache_header(fd, &tch) != sizeof(struct tagcache_header))
2878 != sizeof(struct tagcache_header))
2879 { 2970 {
2880 logf("header write failed"); 2971 logf("header write failed");
2881 close(fd); 2972 close(fd);
@@ -2905,7 +2996,7 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2905 tcmh.tch.entry_count = 0; 2996 tcmh.tch.entry_count = 0;
2906 tcmh.tch.datasize = 0; 2997 tcmh.tch.datasize = 0;
2907 tcmh.dirty = true; 2998 tcmh.dirty = true;
2908 ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ); 2999 write_master_header(masterfd, &tcmh);
2909 init = true; 3000 init = true;
2910 masterfd_pos = lseek(masterfd, 0, SEEK_CUR); 3001 masterfd_pos = lseek(masterfd, 0, SEEK_CUR);
2911 } 3002 }
@@ -2917,8 +3008,8 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
2917 */ 3008 */
2918 init = false; 3009 init = false;
2919 3010
2920 if (ecread(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ) != 3011 if (read_master_header(masterfd, &tcmh) != sizeof(struct master_header) ||
2921 sizeof(struct master_header) || tcmh.tch.magic != TAGCACHE_MAGIC) 3012 tcmh.tch.magic != TAGCACHE_MAGIC)
2922 { 3013 {
2923 logf("header error"); 3014 logf("header error");
2924 close(fd); 3015 close(fd);
@@ -3025,8 +3116,8 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
3025 3116
3026 idxbuf_pos = MIN(tcmh.tch.entry_count - i, IDX_BUF_DEPTH); 3117 idxbuf_pos = MIN(tcmh.tch.entry_count - i, IDX_BUF_DEPTH);
3027 3118
3028 if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ) 3119 if (read_index_entries(masterfd, idxbuf, idxbuf_pos) !=
3029 != (int)sizeof(struct index_entry)*idxbuf_pos) 3120 (ssize_t)sizeof(struct index_entry) * idxbuf_pos)
3030 { 3121 {
3031 logf("read fail #5"); 3122 logf("read fail #5");
3032 error = true; 3123 error = true;
@@ -3059,9 +3150,8 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
3059 } 3150 }
3060 3151
3061 /* Write back the updated index. */ 3152 /* Write back the updated index. */
3062 if (ecwrite(masterfd, idxbuf, idxbuf_pos, 3153 if (write_index_entries(masterfd, idxbuf, idxbuf_pos) !=
3063 index_entry_ec, tc_stat.econ) != 3154 (ssize_t)sizeof(struct index_entry) * idxbuf_pos)
3064 (int)sizeof(struct index_entry)*idxbuf_pos)
3065 { 3155 {
3066 logf("write fail"); 3156 logf("write fail");
3067 error = true; 3157 error = true;
@@ -3092,8 +3182,8 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
3092 { 3182 {
3093 int loc = lseek(masterfd, 0, SEEK_CUR); 3183 int loc = lseek(masterfd, 0, SEEK_CUR);
3094 3184
3095 if (ecread(masterfd, idxbuf, idxbuf_pos, index_entry_ec, tc_stat.econ) 3185 if (read_index_entries(masterfd, idxbuf, idxbuf_pos) !=
3096 != (int)sizeof(struct index_entry)*idxbuf_pos) 3186 (ssize_t)sizeof(struct index_entry) * idxbuf_pos)
3097 { 3187 {
3098 logf("read fail #6"); 3188 logf("read fail #6");
3099 error = true; 3189 error = true;
@@ -3143,7 +3233,7 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
3143 idxbuf[j].tag_seek[index_type] = lseek(fd, 0, SEEK_CUR); 3233 idxbuf[j].tag_seek[index_type] = lseek(fd, 0, SEEK_CUR);
3144 fe.tag_length = entry.tag_length[index_type]; 3234 fe.tag_length = entry.tag_length[index_type];
3145 fe.idx_id = tcmh.tch.entry_count + i + j; 3235 fe.idx_id = tcmh.tch.entry_count + i + j;
3146 ecwrite(fd, &fe, 1, tagfile_entry_ec, tc_stat.econ); 3236 write_tagfile_entry(fd, &fe);
3147 write(fd, buf, fe.tag_length); 3237 write(fd, buf, fe.tag_length);
3148 tempbufidx++; 3238 tempbufidx++;
3149 3239
@@ -3165,9 +3255,8 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
3165 } 3255 }
3166 3256
3167 /* Write index. */ 3257 /* Write index. */
3168 if (ecwrite(masterfd, idxbuf, idxbuf_pos, 3258 if (write_index_entries(masterfd, idxbuf, idxbuf_pos) !=
3169 index_entry_ec, tc_stat.econ) != 3259 (ssize_t)sizeof(struct index_entry) * idxbuf_pos)
3170 (int)sizeof(struct index_entry)*idxbuf_pos)
3171 { 3260 {
3172 logf("tagcache: write fail #4"); 3261 logf("tagcache: write fail #4");
3173 error = true; 3262 error = true;
@@ -3183,7 +3272,7 @@ static int build_index(int index_type, struct tagcache_header *h, int tmpfd)
3183 tch.entry_count = tempbufidx; 3272 tch.entry_count = tempbufidx;
3184 tch.datasize = lseek(fd, 0, SEEK_END) - sizeof(struct tagcache_header); 3273 tch.datasize = lseek(fd, 0, SEEK_END) - sizeof(struct tagcache_header);
3185 lseek(fd, 0, SEEK_SET); 3274 lseek(fd, 0, SEEK_SET);
3186 ecwrite(fd, &tch, 1, tagcache_header_ec, tc_stat.econ); 3275 write_tagcache_header(fd, &tch);
3187 3276
3188 if (index_type != tag_filename) 3277 if (index_type != tag_filename)
3189 h->datasize += tch.datasize; 3278 h->datasize += tch.datasize;
@@ -3346,7 +3435,7 @@ static bool commit(void)
3346 tcmh.commitid++; 3435 tcmh.commitid++;
3347 3436
3348 lseek(masterfd, 0, SEEK_SET); 3437 lseek(masterfd, 0, SEEK_SET);
3349 ecwrite(masterfd, &tcmh, 1, master_header_ec, tc_stat.econ); 3438 write_master_header(masterfd, &tcmh);
3350 close(masterfd); 3439 close(masterfd);
3351 3440
3352 logf("tagcache committed"); 3441 logf("tagcache committed");
@@ -3822,14 +3911,14 @@ bool tagcache_create_changelog(struct tagcache_search *tcs)
3822 else 3911 else
3823 { 3912 {
3824 lseek(tcs->masterfd, 0, SEEK_SET); 3913 lseek(tcs->masterfd, 0, SEEK_SET);
3825 ecread(tcs->masterfd, &myhdr, 1, master_header_ec, tc_stat.econ); 3914 read_master_header(tcs->masterfd, &myhdr);
3826 } 3915 }
3827 3916
3828 write(clfd, "## Changelog version 1\n", 23); 3917 write(clfd, "## Changelog version 1\n", 23);
3829 3918
3830 for (i = 0; i < myhdr.tch.entry_count; i++) 3919 for (i = 0; i < myhdr.tch.entry_count; i++)
3831 { 3920 {
3832 if (ecread_index_entry(tcs->masterfd, &idx) != sizeof(struct index_entry)) 3921 if (read_index_entries(tcs->masterfd, &idx, 1) != sizeof(struct index_entry))
3833 { 3922 {
3834 logf("read error #9"); 3923 logf("read error #9");
3835 tagcache_search_finish(tcs); 3924 tagcache_search_finish(tcs);
@@ -3894,7 +3983,7 @@ static bool delete_entry(long idx_id)
3894 return false; 3983 return false;
3895 3984
3896 lseek(masterfd, idx_id * sizeof(struct index_entry), SEEK_CUR); 3985 lseek(masterfd, idx_id * sizeof(struct index_entry), SEEK_CUR);
3897 if (ecread_index_entry(masterfd, &myidx) != sizeof(struct index_entry)) 3986 if (read_index_entries(masterfd, &myidx, 1) != sizeof(struct index_entry))
3898 { 3987 {
3899 logf("delete_entry(): read error"); 3988 logf("delete_entry(): read error");
3900 goto cleanup; 3989 goto cleanup;
@@ -3908,7 +3997,7 @@ static bool delete_entry(long idx_id)
3908 3997
3909 myidx.flag |= FLAG_DELETED; 3998 myidx.flag |= FLAG_DELETED;
3910 lseek(masterfd, -(off_t)sizeof(struct index_entry), SEEK_CUR); 3999 lseek(masterfd, -(off_t)sizeof(struct index_entry), SEEK_CUR);
3911 if (ecwrite_index_entry(masterfd, &myidx) != sizeof(struct index_entry)) 4000 if (write_index_entries(masterfd, &myidx, 1) != sizeof(struct index_entry))
3912 { 4001 {
3913 logf("delete_entry(): write_error #1"); 4002 logf("delete_entry(): write_error #1");
3914 goto cleanup; 4003 goto cleanup;
@@ -3930,7 +4019,7 @@ static bool delete_entry(long idx_id)
3930 else 4019 else
3931#endif 4020#endif
3932 { 4021 {
3933 if (ecread_index_entry(masterfd, &idx) != sizeof(struct index_entry)) 4022 if (read_index_entries(masterfd, &idx, 1) != sizeof(struct index_entry))
3934 { 4023 {
3935 logf("delete_entry(): read error #2"); 4024 logf("delete_entry(): read error #2");
3936 goto cleanup; 4025 goto cleanup;
@@ -3988,7 +4077,7 @@ static bool delete_entry(long idx_id)
3988 /* Skip the header block */ 4077 /* Skip the header block */
3989 lseek(fd, myidx.tag_seek[tag], SEEK_SET); 4078 lseek(fd, myidx.tag_seek[tag], SEEK_SET);
3990 4079
3991 switch (ecread_tagfile_entry_and_tag(fd, &tfe, buf, bufsz)) 4080 switch (read_tagfile_entry_and_tag(fd, &tfe, buf, bufsz))
3992 { 4081 {
3993 case e_SUCCESS_LEN_ZERO: 4082 case e_SUCCESS_LEN_ZERO:
3994 logf("deleted_entry(): SUCCESS"); 4083 logf("deleted_entry(): SUCCESS");
@@ -4058,7 +4147,7 @@ static bool delete_entry(long idx_id)
4058 /* Write index entry back into master index. */ 4147 /* Write index entry back into master index. */
4059 lseek(masterfd, sizeof(struct master_header) + 4148 lseek(masterfd, sizeof(struct master_header) +
4060 (idx_id * sizeof(struct index_entry)), SEEK_SET); 4149 (idx_id * sizeof(struct index_entry)), SEEK_SET);
4061 if (ecwrite_index_entry(masterfd, &myidx) != sizeof(struct index_entry)) 4150 if (write_index_entries(masterfd, &myidx, 1) != sizeof(struct index_entry))
4062 { 4151 {
4063 logf("delete_entry(): write_error #2"); 4152 logf("delete_entry(): write_error #2");
4064 goto cleanup; 4153 goto cleanup;
@@ -4282,9 +4371,8 @@ static bool load_tagcache(void)
4282 } 4371 }
4283 4372
4284 struct master_header tcmh; 4373 struct master_header tcmh;
4285 if (ecread(fd, &tcmh, 1, master_header_ec, tc_stat.econ) 4374 if (read_master_header(fd, &tcmh) != sizeof(struct master_header) ||
4286 != sizeof(struct master_header) 4375 tcmh.tch.magic != TAGCACHE_MAGIC)
4287 || tcmh.tch.magic != TAGCACHE_MAGIC)
4288 { 4376 {
4289 logf("incorrect header"); 4377 logf("incorrect header");
4290 goto failure; 4378 goto failure;
@@ -4303,7 +4391,7 @@ static bool load_tagcache(void)
4303 goto failure; 4391 goto failure;
4304 } 4392 }
4305 4393
4306 int rc = ecread_index_entry(fd, &tcramcache.hdr->indices[i]); 4394 int rc = read_index_entries(fd, &tcramcache.hdr->indices[i], 1);
4307 if (rc != sizeof (struct index_entry)) 4395 if (rc != sizeof (struct index_entry))
4308 { 4396 {
4309 logf("read error #10"); 4397 logf("read error #10");
@@ -4364,7 +4452,7 @@ static bool load_tagcache(void)
4364 off_t pos = lseek(fd, 0, SEEK_CUR); 4452 off_t pos = lseek(fd, 0, SEEK_CUR);
4365 4453
4366 /* Load the header for the tag itself */ 4454 /* Load the header for the tag itself */
4367 if (ecread_tagfile_entry(fd, fe) != sizeof(struct tagfile_entry)) 4455 if (read_tagfile_entry(fd, fe) != sizeof(struct tagfile_entry))
4368 { 4456 {
4369 /* End of lookup table. */ 4457 /* End of lookup table. */
4370 logf("read error #11"); 4458 logf("read error #11");
@@ -4521,7 +4609,7 @@ static bool check_file_refs(bool auto_update)
4521 4609
4522 while (!check_event_queue()) 4610 while (!check_event_queue())
4523 { 4611 {
4524 int res = ecread_tagfile_entry_and_tag(fd, &tfe, buf, bufsz); 4612 int res = read_tagfile_entry_and_tag(fd, &tfe, buf, bufsz);
4525 processed_dir_count++; 4613 processed_dir_count++;
4526 4614
4527 switch (res) 4615 switch (res)