diff --git a/.gitignore b/.gitignore index edab585..5dbbc4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ *.o *.kate-swp -html -mnt httpdirfs diff --git a/CHANGELOG.md b/CHANGELOG.md index f3dea65..95273b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,10 +26,12 @@ opened. - This problem only occurred during the first time you download a file. During subsequent accesses, when you are only reading from the cache, this problem did not occur. -- Cache system: Previously it was possible for cache_bgdl()'s download offset +- Cache system: Previously it was possible for Cache_bgdl()'s download offset to be modified by the parent thread after the child thread had been launched. This used to cause permanent cache file corruption. -- Cache system: cache_bgdl() no longer prefetches beyond EOF. +- Cache system: Cache_bgdl() no longer prefetches beyond EOF. +- Cache system: Data_read() no longer gives warning messages when reaching the +end of the cache file. ## [1.1.8] - 2019-08-24 ### Changed diff --git a/src/cache.c b/src/cache.c index 0a182ae..9933fc8 100644 --- a/src/cache.c +++ b/src/cache.c @@ -360,20 +360,23 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset) #endif PTHREAD_MUTEX_LOCK(&cf->seek_lock); + long byte_read = 0; + if (fseeko(cf->dfp, offset, SEEK_SET)) { /* fseeko failed */ fprintf(stderr, "Data_read(): fseeko(): %s\n", strerror(errno)); + byte_read = -EIO; + goto end; + } - #ifdef CACHE_LOCK_DEBUG - fprintf(stderr, "Data_read(): thread %lu: unlocking seek_lock;\n", - pthread_self()); - #endif - PTHREAD_MUTEX_UNLOCK(&cf->seek_lock); - - return -EIO; + if (offset + len > cf->content_length) { + len -= offset + len - cf->content_length; + if (len < 0) { + goto end; + } } - long byte_read = fread(buf, sizeof(uint8_t), len, cf->dfp); + byte_read = fread(buf, sizeof(uint8_t), len, cf->dfp); if (byte_read != len) { fprintf(stderr, "Data_read(): fread(): requested %ld, returned %ld!\n", @@ -390,6 +393,7 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset) } } + end: #ifdef CACHE_LOCK_DEBUG fprintf(stderr, "Data_read(): thread %lu: unlocking seek_lock;\n", pthread_self()); @@ -424,19 +428,16 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len, #endif PTHREAD_MUTEX_LOCK(&cf->seek_lock); + long byte_written = 0; + if (fseeko(cf->dfp, offset, SEEK_SET)) { /* fseeko failed */ fprintf(stderr, "Data_write(): fseeko(): %s\n", strerror(errno)); - - #ifdef CACHE_LOCK_DEBUG - fprintf(stderr, "Data_write(): thread %lu: unlocking seek_lock;\n", - pthread_self()); - #endif - PTHREAD_MUTEX_UNLOCK(&cf->seek_lock); - return -EIO; + byte_written = -EIO; + goto end; } - long byte_written = fwrite(buf, sizeof(uint8_t), len, cf->dfp); + byte_written = fwrite(buf, sizeof(uint8_t), len, cf->dfp); if (byte_written != len) { fprintf(stderr, "Data_write(): fwrite(): requested %ld, returned %ld!\n", @@ -448,6 +449,7 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len, } } + end: #ifdef CACHE_LOCK_DEBUG fprintf(stderr, "Data_write(): thread %lu: unlocking seek_lock;\n", pthread_self());