@@ -360,20 +360,23 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
360
360
#endif
361
361
PTHREAD_MUTEX_LOCK (& cf -> seek_lock );
362
362
363
+ long byte_read = 0 ;
364
+
363
365
if (fseeko (cf -> dfp , offset , SEEK_SET )) {
364
366
/* fseeko failed */
365
367
fprintf (stderr , "Data_read(): fseeko(): %s\n" , strerror (errno ));
368
+ byte_read = - EIO ;
369
+ goto end ;
370
+ }
366
371
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
+ }
374
377
}
375
378
376
- long byte_read = fread (buf , sizeof (uint8_t ), len , cf -> dfp );
379
+ byte_read = fread (buf , sizeof (uint8_t ), len , cf -> dfp );
377
380
if (byte_read != len ) {
378
381
fprintf (stderr ,
379
382
"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)
390
393
}
391
394
}
392
395
396
+ end :
393
397
#ifdef CACHE_LOCK_DEBUG
394
398
fprintf (stderr , "Data_read(): thread %lu: unlocking seek_lock;\n" ,
395
399
pthread_self ());
@@ -424,19 +428,16 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len,
424
428
#endif
425
429
PTHREAD_MUTEX_LOCK (& cf -> seek_lock );
426
430
431
+ long byte_written = 0 ;
432
+
427
433
if (fseeko (cf -> dfp , offset , SEEK_SET )) {
428
434
/* fseeko failed */
429
435
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 ;
437
438
}
438
439
439
- long byte_written = fwrite (buf , sizeof (uint8_t ), len , cf -> dfp );
440
+ byte_written = fwrite (buf , sizeof (uint8_t ), len , cf -> dfp );
440
441
if (byte_written != len ) {
441
442
fprintf (stderr ,
442
443
"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,
448
449
}
449
450
}
450
451
452
+ end :
451
453
#ifdef CACHE_LOCK_DEBUG
452
454
fprintf (stderr , "Data_write(): thread %lu: unlocking seek_lock;\n" ,
453
455
pthread_self ());
0 commit comments