@@ -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