@@ -39,7 +39,7 @@ LinkTable *LinkSystem_init(const char *raw_url)
3939 }
4040
4141 if (pthread_mutex_init (& link_lock , NULL ) != 0 ) {
42- lprintf (debug ,
42+ lprintf (fatal ,
4343 "link_system_init(): link_lock initialisation failed!\n" );
4444 exit_failure ();
4545 }
@@ -68,7 +68,7 @@ LinkTable *LinkSystem_init(const char *raw_url)
6868 ROOT_LINK_TBL = sonic_LinkTable_new_id3 (0 , "0" );
6969 }
7070 }
71- free (url );
71+ FREE (url );
7272 return ROOT_LINK_TBL ;
7373}
7474
@@ -305,16 +305,20 @@ static void LinkTable_uninitialised_fill(LinkTable *linktbl)
305305 int i = 0 ;
306306 int j = 0 ;
307307 while ( (i = curl_multi_perform_once ()) ) {
308- if (j ) {
309- erase_string (stderr , STATUS_LEN , s );
308+ if (CONFIG .log_level & debug ) {
309+ if (j ) {
310+ erase_string (stderr , STATUS_LEN , s );
311+ }
312+ snprintf (s , STATUS_LEN , "%d / %d" , n - i , n );
313+ fprintf (stderr , "%s" , s );
314+ j ++ ;
310315 }
311- snprintf (s , STATUS_LEN , "%d / %d" , n - i , n );
312- fprintf (stderr , "%s" , s );
313- j ++ ;
314316 }
315317 } while (u );
316- erase_string (stderr , STATUS_LEN , s );
317- fprintf (stderr , "Done!\n" );
318+ if (CONFIG .log_level & debug ) {
319+ erase_string (stderr , STATUS_LEN , s );
320+ fprintf (stderr , "Done!\n" );
321+ }
318322}
319323
320324static void LinkTable_fill (LinkTable * linktbl )
@@ -325,7 +329,7 @@ static void LinkTable_fill(LinkTable *linktbl)
325329 char * url ;
326330 url = path_append (head_link -> f_url , this_link -> linkname );
327331 strncpy (this_link -> f_url , url , MAX_PATH_LEN );
328- free (url );
332+ FREE (url );
329333 char * unescaped_linkname ;
330334 CURL * c = curl_easy_init ();
331335 unescaped_linkname = curl_easy_unescape (c , this_link -> linkname ,
@@ -356,10 +360,10 @@ static void LinkTable_invalid_reset(LinkTable *linktbl)
356360void LinkTable_free (LinkTable * linktbl )
357361{
358362 for (int i = 0 ; i < linktbl -> num ; i ++ ) {
359- free (linktbl -> links [i ]);
363+ FREE (linktbl -> links [i ]);
360364 }
361- free (linktbl -> links );
362- free (linktbl );
365+ FREE (linktbl -> links );
366+ FREE (linktbl );
363367}
364368
365369void LinkTable_print (LinkTable * linktbl )
@@ -439,13 +443,6 @@ LinkTable *LinkTable_alloc(const char *url)
439443
440444LinkTable * LinkTable_new (const char * url )
441445{
442- #ifdef LINK_LOCK_DEBUG
443- lprintf (debug ,
444- "LinkTable_new(): thread %lu: locking link_lock;\n" ,
445- pthread_self ());
446- #endif
447- PTHREAD_MUTEX_LOCK (& link_lock );
448-
449446 LinkTable * linktbl = LinkTable_alloc (url );
450447
451448 /* start downloading the base URL */
@@ -459,7 +456,7 @@ LinkTable *LinkTable_new(const char *url)
459456 GumboOutput * output = gumbo_parse (buf .data );
460457 HTML_to_LinkTable (output -> root , linktbl );
461458 gumbo_destroy_output (& kGumboDefaultOptions , output );
462- free (buf .data );
459+ FREE (buf .data );
463460
464461 int skip_fill = 0 ;
465462 char * unescaped_path ;
@@ -504,12 +501,7 @@ LinkTable *LinkTable_new(const char *url)
504501 curl_easy_cleanup (c );
505502
506503 LinkTable_print (linktbl );
507- #ifdef LINK_LOCK_DEBUG
508- lprintf (debug ,
509- "LinkTable_new(): thread %lu: unlocking link_lock;\n" ,
510- pthread_self ());
511- #endif
512- PTHREAD_MUTEX_UNLOCK (& link_lock );
504+
513505 return linktbl ;
514506}
515507
@@ -526,8 +518,8 @@ static void LinkTable_disk_delete(const char *dirn)
526518 lprintf (debug , "LinkTable_disk_delete(): unlink(%s): %s\n" , path ,
527519 strerror (errno ));
528520 }
529- free (path );
530- free (metadirn );
521+ FREE (path );
522+ FREE (metadirn );
531523}
532524
533525int LinkTable_disk_save (LinkTable * linktbl , const char * dirn )
@@ -540,15 +532,15 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
540532 path = path_append (metadirn , "/.LinkTable" );
541533 }
542534 FILE * fp = fopen (path , "w" );
543- free (metadirn );
535+ FREE (metadirn );
544536
545537 if (!fp ) {
546538 lprintf (debug , "LinkTable_disk_save(): fopen(%s): %s\n" , path ,
547539 strerror (errno ));
548- free (path );
540+ FREE (path );
549541 return -1 ;
550542 }
551- free (path );
543+ FREE (path );
552544
553545 fwrite (& linktbl -> num , sizeof (int ), 1 , fp );
554546 for (int i = 0 ; i < linktbl -> num ; i ++ ) {
@@ -586,10 +578,10 @@ LinkTable *LinkTable_disk_open(const char *dirn)
586578 path = path_append (metadirn , "/.LinkTable" );
587579 }
588580 FILE * fp = fopen (path , "r" );
589- free (metadirn );
581+ FREE (metadirn );
590582
591583 if (!fp ) {
592- free (path );
584+ FREE (path );
593585 return NULL ;
594586 }
595587
@@ -712,13 +704,23 @@ static Link *path_to_Link_recursive(char *path, LinkTable *linktbl)
712704
713705Link * path_to_Link (const char * path )
714706{
707+ lprintf (link_lock_debug ,
708+ "path_to_Link(): thread %x: locking link_lock;\n" ,
709+ pthread_self ());
710+
711+ PTHREAD_MUTEX_LOCK (& link_lock );
715712 char * new_path = strndup (path , MAX_PATH_LEN );
716713 if (!new_path ) {
717714 lprintf (debug , "path_to_Link(): cannot allocate memory\n" );
718715 exit_failure ();
719716 }
720717 Link * link = path_to_Link_recursive (new_path , ROOT_LINK_TBL );
721- free (new_path );
718+ FREE (new_path );
719+
720+ lprintf (link_lock_debug ,
721+ "path_to_Link(): thread %x: unlocking link_lock;\n" ,
722+ pthread_self ());
723+ PTHREAD_MUTEX_UNLOCK (& link_lock );
722724 return link ;
723725}
724726
@@ -749,15 +751,6 @@ long path_download(const char *path, char *output_buf, size_t size,
749751 curl_easy_setopt (curl , CURLOPT_WRITEDATA , (void * )& buf );
750752 curl_easy_setopt (curl , CURLOPT_RANGE , range_str );
751753
752- #ifdef LINK_LOCK_DEBUG
753- lprintf (debug ,
754- "path_download(): thread %lu: locking and unlocking link_lock;\n" ,
755- pthread_self ());
756- #endif
757-
758- PTHREAD_MUTEX_LOCK (& link_lock );
759- PTHREAD_MUTEX_UNLOCK (& link_lock );
760-
761754 DataStruct header ;
762755 header .size = 0 ;
763756 header .data = NULL ;
@@ -774,7 +767,7 @@ range requests\n");
774767 }
775768 }
776769
777- free (header .data );
770+ FREE (header .data );
778771
779772 long http_resp ;
780773 curl_easy_getinfo (curl , CURLINFO_RESPONSE_CODE , & http_resp );
@@ -798,7 +791,7 @@ range requests\n");
798791
799792 memmove (output_buf , buf .data , recv );
800793 curl_easy_cleanup (curl );
801- free (buf .data );
794+ FREE (buf .data );
802795
803796 return recv ;
804797}
0 commit comments