@@ -574,6 +574,7 @@ LinkTable *LinkTable_new(const char *url)
574
574
{
575
575
LinkTable * linktbl = LinkTable_alloc (url );
576
576
linktbl -> index_time = time (NULL );
577
+ lprintf (debug , "linktbl->index_time: %d\n" , linktbl -> index_time );
577
578
578
579
/*
579
580
* start downloading the base URL
@@ -660,6 +661,14 @@ static void LinkTable_disk_delete(const char *dirn)
660
661
FREE (metadirn );
661
662
}
662
663
664
+ /* This is necessary to get the compiler on some platforms to stop
665
+ complaining about the fact that we're not using the return value of
666
+ fread, when we know we aren't and that's fine. */
667
+ static inline void ignore_value (int i )
668
+ {
669
+ (void ) i ;
670
+ }
671
+
663
672
int LinkTable_disk_save (LinkTable * linktbl , const char * dirn )
664
673
{
665
674
char * metadirn = path_append (META_DIR , dirn );
@@ -673,16 +682,20 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
673
682
FREE (path );
674
683
return -1 ;
675
684
}
676
- FREE (path );
677
685
678
- fwrite (& linktbl -> num , sizeof (int ), 1 , fp );
686
+ lprintf (debug , "linktbl->index_time: %d\n" , linktbl -> index_time );
687
+ if (fwrite (& linktbl -> num , sizeof (int ), 1 , fp ) != 1 ||
688
+ fwrite (& linktbl -> index_time , sizeof (time_t ), 1 , fp ) != 1 ) {
689
+ lprintf (error , "Failed to save the header of %s!\n" , path );
690
+ }
691
+ FREE (path );
679
692
for (int i = 0 ; i < linktbl -> num ; i ++ ) {
680
- fwrite (linktbl -> links [i ]-> linkname , sizeof (char ),
681
- MAX_FILENAME_LEN , fp );
682
- fwrite (linktbl -> links [i ]-> f_url , sizeof (char ), MAX_PATH_LEN , fp );
683
- fwrite (& linktbl -> links [i ]-> type , sizeof (LinkType ), 1 , fp );
684
- fwrite (& linktbl -> links [i ]-> content_length , sizeof (size_t ), 1 , fp );
685
- fwrite (& linktbl -> links [i ]-> time , sizeof (long ), 1 , fp );
693
+ ignore_value ( fwrite (linktbl -> links [i ]-> linkname , sizeof (char ),
694
+ MAX_FILENAME_LEN , fp )) ;
695
+ ignore_value ( fwrite (linktbl -> links [i ]-> f_url , sizeof (char ), MAX_PATH_LEN , fp ) );
696
+ ignore_value ( fwrite (& linktbl -> links [i ]-> type , sizeof (LinkType ), 1 , fp ) );
697
+ ignore_value ( fwrite (& linktbl -> links [i ]-> content_length , sizeof (size_t ), 1 , fp ) );
698
+ ignore_value ( fwrite (& linktbl -> links [i ]-> time , sizeof (long ), 1 , fp ) );
686
699
}
687
700
688
701
int res = 0 ;
@@ -701,14 +714,6 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
701
714
return res ;
702
715
}
703
716
704
- /* This is necessary to get the compiler on some platforms to stop
705
- complaining about the fact that we're not using the return value of
706
- fread, when we know we aren't and that's fine. */
707
- static inline void ignore_value (int i )
708
- {
709
- (void ) i ;
710
- }
711
-
712
717
LinkTable * LinkTable_disk_open (const char * dirn )
713
718
{
714
719
char * metadirn = path_append (META_DIR , dirn );
@@ -728,13 +733,16 @@ LinkTable *LinkTable_disk_open(const char *dirn)
728
733
}
729
734
730
735
LinkTable * linktbl = CALLOC (1 , sizeof (LinkTable ));
731
-
732
- if ( fread (& linktbl -> num , sizeof (int ), 1 , fp ) != 1 ) {
733
- lprintf (error , "Failed to read the first int of %s!\n" , path );
736
+ if ( fread ( & linktbl -> num , sizeof ( int ), 1 , fp ) != 1 ||
737
+ fread (& linktbl -> index_time , sizeof (time_t ), 1 , fp ) != 1 ) {
738
+ lprintf (error , "Failed to read the header of %s!\n" , path );
734
739
LinkTable_free (linktbl );
735
740
LinkTable_disk_delete (dirn );
741
+ FREE (path );
736
742
return NULL ;
737
743
}
744
+ lprintf (debug , "linktbl->index_time: %d\n" , linktbl -> index_time );
745
+
738
746
linktbl -> links = CALLOC (linktbl -> num , sizeof (Link * ));
739
747
for (int i = 0 ; i < linktbl -> num ; i ++ ) {
740
748
linktbl -> links [i ] = CALLOC (1 , sizeof (Link ));
@@ -749,16 +757,13 @@ LinkTable *LinkTable_disk_open(const char *dirn)
749
757
sizeof (size_t ), 1 , fp ));
750
758
ignore_value (fread (& linktbl -> links [i ]-> time , sizeof (long ), 1 , fp ));
751
759
if (feof (fp )) {
752
- /*
753
- * reached EOF
754
- */
755
- lprintf (error , "reached EOF!\n" );
760
+ lprintf (error , "Corrupted LinkTable!\n" );
756
761
LinkTable_free (linktbl );
757
762
LinkTable_disk_delete (dirn );
758
763
return NULL ;
759
764
}
760
765
if (ferror (fp )) {
761
- lprintf (error , "encountered ferror!\n" );
766
+ lprintf (error , "Encountered ferror!\n" );
762
767
LinkTable_free (linktbl );
763
768
LinkTable_disk_delete (dirn );
764
769
return NULL ;
@@ -769,6 +774,7 @@ LinkTable *LinkTable_disk_open(const char *dirn)
769
774
"cannot close the file pointer, %s\n" , strerror (errno ));
770
775
}
771
776
return linktbl ;
777
+ FREE (path );
772
778
}
773
779
774
780
LinkTable * path_to_Link_LinkTable_new (const char * path )
@@ -790,6 +796,9 @@ LinkTable *path_to_Link_LinkTable_new(const char *path)
790
796
time_t time_now = time (NULL );
791
797
if (time_now - next_table -> index_time > CONFIG .refresh_timeout ) {
792
798
/* refresh directory contents */
799
+ lprintf (info , "time_now: %d, index_time: %d\n" , time_now , next_table -> index_time )
800
+ lprintf (info , "diff: %d, limit: %d\n" , time_now - next_table -> index_time , CONFIG .refresh_timeout );
801
+ lprintf (info , "Refreshing LinkTable for %s\n" , path );
793
802
LinkTable_free (next_table );
794
803
next_table = NULL ;
795
804
if (link ) {
0 commit comments