@@ -574,6 +574,7 @@ LinkTable *LinkTable_new(const char *url)
574574{
575575 LinkTable * linktbl = LinkTable_alloc (url );
576576 linktbl -> index_time = time (NULL );
577+ lprintf (debug , "linktbl->index_time: %d\n" , linktbl -> index_time );
577578
578579 /*
579580 * start downloading the base URL
@@ -660,6 +661,14 @@ static void LinkTable_disk_delete(const char *dirn)
660661 FREE (metadirn );
661662}
662663
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+
663672int LinkTable_disk_save (LinkTable * linktbl , const char * dirn )
664673{
665674 char * metadirn = path_append (META_DIR , dirn );
@@ -673,16 +682,20 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
673682 FREE (path );
674683 return -1 ;
675684 }
676- FREE (path );
677685
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 );
679692 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 ) );
686699 }
687700
688701 int res = 0 ;
@@ -701,14 +714,6 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
701714 return res ;
702715}
703716
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-
712717LinkTable * LinkTable_disk_open (const char * dirn )
713718{
714719 char * metadirn = path_append (META_DIR , dirn );
@@ -728,13 +733,16 @@ LinkTable *LinkTable_disk_open(const char *dirn)
728733 }
729734
730735 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 );
734739 LinkTable_free (linktbl );
735740 LinkTable_disk_delete (dirn );
741+ FREE (path );
736742 return NULL ;
737743 }
744+ lprintf (debug , "linktbl->index_time: %d\n" , linktbl -> index_time );
745+
738746 linktbl -> links = CALLOC (linktbl -> num , sizeof (Link * ));
739747 for (int i = 0 ; i < linktbl -> num ; i ++ ) {
740748 linktbl -> links [i ] = CALLOC (1 , sizeof (Link ));
@@ -749,16 +757,13 @@ LinkTable *LinkTable_disk_open(const char *dirn)
749757 sizeof (size_t ), 1 , fp ));
750758 ignore_value (fread (& linktbl -> links [i ]-> time , sizeof (long ), 1 , fp ));
751759 if (feof (fp )) {
752- /*
753- * reached EOF
754- */
755- lprintf (error , "reached EOF!\n" );
760+ lprintf (error , "Corrupted LinkTable!\n" );
756761 LinkTable_free (linktbl );
757762 LinkTable_disk_delete (dirn );
758763 return NULL ;
759764 }
760765 if (ferror (fp )) {
761- lprintf (error , "encountered ferror!\n" );
766+ lprintf (error , "Encountered ferror!\n" );
762767 LinkTable_free (linktbl );
763768 LinkTable_disk_delete (dirn );
764769 return NULL ;
@@ -769,6 +774,7 @@ LinkTable *LinkTable_disk_open(const char *dirn)
769774 "cannot close the file pointer, %s\n" , strerror (errno ));
770775 }
771776 return linktbl ;
777+ FREE (path );
772778}
773779
774780LinkTable * path_to_Link_LinkTable_new (const char * path )
@@ -790,6 +796,9 @@ LinkTable *path_to_Link_LinkTable_new(const char *path)
790796 time_t time_now = time (NULL );
791797 if (time_now - next_table -> index_time > CONFIG .refresh_timeout ) {
792798 /* 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 );
793802 LinkTable_free (next_table );
794803 next_table = NULL ;
795804 if (link ) {
0 commit comments