Skip to content

Commit ee397d1

Browse files
committed
Data_read() no longer gives warning messages when reaching the end of the cache file.
1 parent 127c4ce commit ee397d1

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
*.o
22
*.kate-swp
3-
html
4-
mnt
53
httpdirfs

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ opened.
2626
- This problem only occurred during the first time you download a file.
2727
During subsequent accesses, when you are only reading from the cache, this
2828
problem did not occur.
29-
- Cache system: Previously it was possible for cache_bgdl()'s download offset
29+
- Cache system: Previously it was possible for Cache_bgdl()'s download offset
3030
to be modified by the parent thread after the child thread had been
3131
launched. This used to cause permanent cache file corruption.
32-
- Cache system: cache_bgdl() no longer prefetches beyond EOF.
32+
- Cache system: Cache_bgdl() no longer prefetches beyond EOF.
33+
- Cache system: Data_read() no longer gives warning messages when reaching the
34+
end of the cache file.
3335

3436
## [1.1.8] - 2019-08-24
3537
### Changed

src/cache.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -360,20 +360,23 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
360360
#endif
361361
PTHREAD_MUTEX_LOCK(&cf->seek_lock);
362362

363+
long byte_read = 0;
364+
363365
if (fseeko(cf->dfp, offset, SEEK_SET)) {
364366
/* fseeko failed */
365367
fprintf(stderr, "Data_read(): fseeko(): %s\n", strerror(errno));
368+
byte_read = -EIO;
369+
goto end;
370+
}
366371

367-
#ifdef CACHE_LOCK_DEBUG
368-
fprintf(stderr, "Data_read(): thread %lu: unlocking seek_lock;\n",
369-
pthread_self());
370-
#endif
371-
PTHREAD_MUTEX_UNLOCK(&cf->seek_lock);
372-
373-
return -EIO;
372+
if (offset + len > cf->content_length) {
373+
len -= offset + len - cf->content_length;
374+
if (len < 0) {
375+
goto end;
376+
}
374377
}
375378

376-
long byte_read = fread(buf, sizeof(uint8_t), len, cf->dfp);
379+
byte_read = fread(buf, sizeof(uint8_t), len, cf->dfp);
377380
if (byte_read != len) {
378381
fprintf(stderr,
379382
"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)
390393
}
391394
}
392395

396+
end:
393397
#ifdef CACHE_LOCK_DEBUG
394398
fprintf(stderr, "Data_read(): thread %lu: unlocking seek_lock;\n",
395399
pthread_self());
@@ -424,19 +428,16 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len,
424428
#endif
425429
PTHREAD_MUTEX_LOCK(&cf->seek_lock);
426430

431+
long byte_written = 0;
432+
427433
if (fseeko(cf->dfp, offset, SEEK_SET)) {
428434
/* fseeko failed */
429435
fprintf(stderr, "Data_write(): fseeko(): %s\n", strerror(errno));
430-
431-
#ifdef CACHE_LOCK_DEBUG
432-
fprintf(stderr, "Data_write(): thread %lu: unlocking seek_lock;\n",
433-
pthread_self());
434-
#endif
435-
PTHREAD_MUTEX_UNLOCK(&cf->seek_lock);
436-
return -EIO;
436+
byte_written = -EIO;
437+
goto end;
437438
}
438439

439-
long byte_written = fwrite(buf, sizeof(uint8_t), len, cf->dfp);
440+
byte_written = fwrite(buf, sizeof(uint8_t), len, cf->dfp);
440441
if (byte_written != len) {
441442
fprintf(stderr,
442443
"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,
448449
}
449450
}
450451

452+
end:
451453
#ifdef CACHE_LOCK_DEBUG
452454
fprintf(stderr, "Data_write(): thread %lu: unlocking seek_lock;\n",
453455
pthread_self());

0 commit comments

Comments
 (0)