@@ -571,31 +571,14 @@ LinkTable *LinkTable_alloc(const char *url)
571571
572572LinkTable * LinkTable_new (const char * url )
573573{
574- LinkTable * linktbl = LinkTable_alloc (url );
575- linktbl -> index_time = time (NULL );
576- lprintf (debug , "linktbl->index_time: %d\n" , linktbl -> index_time );
577-
578- /*
579- * start downloading the base URL
580- */
581- TransferStruct ts = Link_download_full (linktbl -> links [0 ]);
582- if (ts .curr_size == 0 ) {
583- LinkTable_free (linktbl );
584- return NULL ;
585- }
586-
587- /*
588- * Otherwise parsed the received data
589- */
590- GumboOutput * output = gumbo_parse (ts .data );
591- HTML_to_LinkTable (url , output -> root , linktbl );
592- gumbo_destroy_output (& kGumboDefaultOptions , output );
593- FREE (ts .data );
594-
595- int skip_fill = 0 ;
596574 char * unescaped_path ;
597575 unescaped_path =
598576 curl_easy_unescape (NULL , url + ROOT_LINK_OFFSET , 0 , NULL );
577+ LinkTable * linktbl = NULL ;
578+
579+ /*
580+ * Attempt to load the LinkTable from the disk.
581+ */
599582 if (CACHE_SYSTEM_INIT ) {
600583 CacheDir_create (unescaped_path );
601584 LinkTable * disk_linktbl ;
@@ -613,57 +596,57 @@ LinkTable *LinkTable_new(const char *url)
613596 time_now - disk_linktbl -> index_time ,
614597 CONFIG .refresh_timeout );
615598 LinkTable_free (disk_linktbl );
616- }
617- /*
618- * Check if there are inconsistencies for the on-disk LinkTable
619- */
620-
621- if (disk_linktbl -> num == linktbl -> num ) {
622- LinkTable_free (linktbl );
623- linktbl = disk_linktbl ;
624- skip_fill = 1 ;
625599 } else {
626- lprintf (info ,
627- "disk_linktbl->num: %d, linktbl->num: %d\n" ,
628- disk_linktbl -> num , linktbl -> num );
629- lprintf (info , "Refreshing LinkTable for %s\n" ,
630- url + ROOT_LINK_OFFSET );
631- LinkTable_free (disk_linktbl );
600+ linktbl = disk_linktbl ;
632601 }
633602 }
634603 }
635604
636- if (!skip_fill ) {
605+ /*
606+ * Download a new LinkTable because we didn't manange to load it from the
607+ * disk
608+ */
609+ if (!linktbl ) {
610+ LinkTable * linktbl = LinkTable_alloc (url );
611+ linktbl -> index_time = time (NULL );
612+ lprintf (debug , "linktbl->index_time: %d\n" , linktbl -> index_time );
613+
637614 /*
638- * Fill in the link table
639- */
640- LinkTable_fill (linktbl );
641- } else {
615+ * start downloading the base URL
616+ */
617+ TransferStruct ts = Link_download_full (linktbl -> links [0 ]);
618+ if (ts .curr_size == 0 ) {
619+ LinkTable_free (linktbl );
620+ return NULL ;
621+ }
622+
642623 /*
643- * Fill in the holes in the link table
644- */
645- LinkTable_invalid_reset (linktbl );
646- LinkTable_uninitialised_fill (linktbl );
647- }
624+ * Otherwise parsed the received data
625+ */
626+ GumboOutput * output = gumbo_parse (ts .data );
627+ HTML_to_LinkTable (url , output -> root , linktbl );
628+ gumbo_destroy_output (& kGumboDefaultOptions , output );
629+ FREE (ts .data );
648630
649- /*
650- * Save the link table
651- */
652- if (CACHE_SYSTEM_INIT ) {
653- if (LinkTable_disk_save (linktbl , unescaped_path )) {
654- lprintf (error , "Failed to save the LinkTable!\n" );
631+ LinkTable_fill (linktbl );
632+
633+ /*
634+ * Save the link table
635+ */
636+ if (CACHE_SYSTEM_INIT ) {
637+ if (LinkTable_disk_save (linktbl , unescaped_path )) {
638+ lprintf (error , "Failed to save the LinkTable!\n" );
639+ }
655640 }
656641 }
657- free (unescaped_path );
658642
659643#ifdef DEBUG
660644 static int i = 0 ;
661645 lprintf (debug , "!!!!Calling LinkTable_new for the %d time!!!!\n" , i );
662646 i ++ ;
663647#endif
664-
648+ free ( unescaped_path );
665649 LinkTable_print (linktbl );
666-
667650 return linktbl ;
668651}
669652
@@ -791,8 +774,9 @@ LinkTable *LinkTable_disk_open(const char *dirn)
791774 lprintf (error ,
792775 "cannot close the file pointer, %s\n" , strerror (errno ));
793776 }
794- return linktbl ;
777+
795778 FREE (path );
779+ return linktbl ;
796780}
797781
798782LinkTable * path_to_Link_LinkTable_new (const char * path )
0 commit comments