@@ -660,6 +660,14 @@ static void LinkTable_disk_delete(const char *dirn)
660660 FREE (metadirn );
661661}
662662
663+ /* This is necessary to get the compiler on some platforms to stop
664+ complaining about the fact that we're not using the return value of
665+ fread, when we know we aren't and that's fine. */
666+ static inline void ignore_value (int i )
667+ {
668+ (void ) i ;
669+ }
670+
663671int LinkTable_disk_save (LinkTable * linktbl , const char * dirn )
664672{
665673 char * metadirn = path_append (META_DIR , dirn );
@@ -673,16 +681,19 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
673681 FREE (path );
674682 return -1 ;
675683 }
676- FREE (path );
677684
678- fwrite (& linktbl -> num , sizeof (int ), 1 , fp );
685+ if (fwrite (& linktbl -> num , sizeof (int ), 1 , fp ) != 1 ||
686+ fwrite (& linktbl -> index_time , sizeof (time ), 1 , fp ) != 1 ) {
687+ lprintf (error , "Failed to save the header of %s!\n" , path );
688+ }
689+ FREE (path );
679690 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 );
691+ ignore_value ( fwrite (linktbl -> links [i ]-> linkname , sizeof (char ),
692+ MAX_FILENAME_LEN , fp )) ;
693+ ignore_value ( fwrite (linktbl -> links [i ]-> f_url , sizeof (char ), MAX_PATH_LEN , fp ) );
694+ ignore_value ( fwrite (& linktbl -> links [i ]-> type , sizeof (LinkType ), 1 , fp ) );
695+ ignore_value ( fwrite (& linktbl -> links [i ]-> content_length , sizeof (size_t ), 1 , fp ) );
696+ ignore_value ( fwrite (& linktbl -> links [i ]-> time , sizeof (long ), 1 , fp ) );
686697 }
687698
688699 int res = 0 ;
@@ -701,14 +712,6 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
701712 return res ;
702713}
703714
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-
712715LinkTable * LinkTable_disk_open (const char * dirn )
713716{
714717 char * metadirn = path_append (META_DIR , dirn );
@@ -729,10 +732,12 @@ LinkTable *LinkTable_disk_open(const char *dirn)
729732
730733 LinkTable * linktbl = CALLOC (1 , sizeof (LinkTable ));
731734
732- if (fread (& linktbl -> num , sizeof (int ), 1 , fp ) != 1 ) {
733- lprintf (error , "Failed to read the first int of %s!\n" , path );
735+ if (fread (& linktbl -> num , sizeof (int ), 1 , fp ) != 1 ||
736+ fread (& linktbl -> index_time , sizeof (time ), 1 , fp ) != 1 ) {
737+ lprintf (error , "Failed to read the header of %s!\n" , path );
734738 LinkTable_free (linktbl );
735739 LinkTable_disk_delete (dirn );
740+ FREE (path );
736741 return NULL ;
737742 }
738743 linktbl -> links = CALLOC (linktbl -> num , sizeof (Link * ));
@@ -749,16 +754,13 @@ LinkTable *LinkTable_disk_open(const char *dirn)
749754 sizeof (size_t ), 1 , fp ));
750755 ignore_value (fread (& linktbl -> links [i ]-> time , sizeof (long ), 1 , fp ));
751756 if (feof (fp )) {
752- /*
753- * reached EOF
754- */
755- lprintf (error , "reached EOF!\n" );
757+ lprintf (error , "Corrupted LinkTable!\n" );
756758 LinkTable_free (linktbl );
757759 LinkTable_disk_delete (dirn );
758760 return NULL ;
759761 }
760762 if (ferror (fp )) {
761- lprintf (error , "encountered ferror!\n" );
763+ lprintf (error , "Encountered ferror!\n" );
762764 LinkTable_free (linktbl );
763765 LinkTable_disk_delete (dirn );
764766 return NULL ;
@@ -769,6 +771,7 @@ LinkTable *LinkTable_disk_open(const char *dirn)
769771 "cannot close the file pointer, %s\n" , strerror (errno ));
770772 }
771773 return linktbl ;
774+ FREE (path );
772775}
773776
774777LinkTable * path_to_Link_LinkTable_new (const char * path )
@@ -790,6 +793,9 @@ LinkTable *path_to_Link_LinkTable_new(const char *path)
790793 time_t time_now = time (NULL );
791794 if (time_now - next_table -> index_time > CONFIG .refresh_timeout ) {
792795 /* refresh directory contents */
796+ lprintf (info , "%d, %d\n" , time_now , next_table -> index_time )
797+ lprintf (info , "%d, %d\n" , time_now - next_table -> index_time , CONFIG .refresh_timeout );
798+ lprintf (info , "Refreshing LinkTable for %s\n" , path );
793799 LinkTable_free (next_table );
794800 next_table = NULL ;
795801 if (link ) {
0 commit comments