Skip to content

Commit 7813487

Browse files
committed
improved error message, removed unnecessary locks
1 parent 14c4b3b commit 7813487

9 files changed

Lines changed: 265 additions & 303 deletions

File tree

src/cache.c

Lines changed: 165 additions & 210 deletions
Large diffs are not rendered by default.

src/link.c

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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

320324
static 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)
356360
void 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

365369
void LinkTable_print(LinkTable *linktbl)
@@ -439,13 +443,6 @@ LinkTable *LinkTable_alloc(const char *url)
439443

440444
LinkTable *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

533525
int 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

713705
Link *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
}

src/log.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "log.h"
22

33
#include "config.h"
4+
#include "util.h"
45

56
#include <stdarg.h>
67
#include <stdio.h>
@@ -15,7 +16,8 @@ int log_level_init()
1516
return DEFAULT_LOG_LEVEL;
1617
}
1718

18-
void log_printf(LogType type, const char *file, int line, const char *format, ...)
19+
void log_printf(LogType type, const char *file, const char *func, int line,
20+
const char *format, ...)
1921
{
2022
FILE *out = stderr;
2123
if (type & CONFIG.log_level) {
@@ -33,15 +35,12 @@ void log_printf(LogType type, const char *file, int line, const char *format, ..
3335
out = stdout;
3436
goto print_actual_message;
3537
break;
36-
case debug:
37-
fprintf(out, "Debug: ");
38-
break;
3938
default:
40-
fprintf(out, "Unknown (%x):", type);
39+
fprintf(out, "Debug (%x):", type);
4140
break;
4241
}
4342

44-
fprintf(out, "(%s:%d): ", file, line);
43+
fprintf(out, "(%s:%s:%d): ", file, func, line);
4544

4645
print_actual_message:
4746
/* A label can only be part of a statement, this is a statement. lol*/
@@ -50,5 +49,9 @@ void log_printf(LogType type, const char *file, int line, const char *format, ..
5049
va_start(args, format);
5150
vfprintf(out, format, args);
5251
va_end(args);
52+
53+
if (type == fatal) {
54+
exit_failure();
55+
}
5356
}
5457
}

src/log.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
* \brief Log types
66
*/
77
typedef enum {
8-
fatal = 1 << 0,
9-
error = 1 << 1,
10-
warning = 1 << 2,
11-
info = 1 << 3,
12-
debug = 1 << 4,
8+
fatal = 1 << 0,
9+
error = 1 << 1,
10+
warning = 1 << 2,
11+
info = 1 << 3,
12+
debug = 1 << 4,
13+
link_lock_debug = 1 << 5,
14+
cache_lock_debug = 1 << 6,
1315
} LogType;
1416

1517
/**
@@ -26,12 +28,13 @@ int log_level_init();
2628
* \brief Log printf
2729
* \details This is for printing nice log messages
2830
*/
29-
void log_printf(LogType type, const char *file, int line, const char *format, ...);
31+
void log_printf(LogType type, const char *file, const char *func, int line,
32+
const char *format, ...);
3033

3134
/**
3235
* \brief Log type printf
3336
* \details This macro automatically prints out the filename and line number
3437
*/
3538
#define lprintf(type, ...) \
36-
log_printf(type, __FILE__, __LINE__, __VA_ARGS__);
39+
log_printf(type, __FILE__, __func__, __LINE__, __VA_ARGS__);
3740
#endif

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void parse_config_file(char ***argv, int *argc)
136136
}
137137
}
138138
}
139-
free(full_path);
139+
FREE(full_path);
140140
}
141141

142142
static int

src/network.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl,
144144
/* clean up the handle, if we are querying the file size */
145145
if (transfer->type == FILESTAT) {
146146
curl_easy_cleanup(curl);
147-
free(transfer);
147+
FREE(transfer);
148148
}
149149
} else {
150150
lprintf(debug, "curl_process_msgs(): curl_msg->msg: %d\n",
@@ -160,7 +160,7 @@ int curl_multi_perform_once(void)
160160
{
161161
#ifdef NETWORK_LOCK_DEBUG
162162
lprintf(debug,
163-
"curl_multi_perform_once(): thread %lu: locking transfer_lock;\n",
163+
"curl_multi_perform_once(): thread %x: locking transfer_lock;\n",
164164
pthread_self());
165165
#endif
166166
PTHREAD_MUTEX_LOCK(&transfer_lock);
@@ -222,7 +222,7 @@ int curl_multi_perform_once(void)
222222
}
223223
#ifdef NETWORK_LOCK_DEBUG
224224
lprintf(debug,
225-
"curl_multi_perform_once(): thread %lu: unlocking transfer_lock;\n",
225+
"curl_multi_perform_once(): thread %x: unlocking transfer_lock;\n",
226226
pthread_self());
227227
#endif
228228
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
@@ -292,14 +292,14 @@ void transfer_blocking(CURL *curl)
292292
curl_easy_setopt(curl, CURLOPT_PRIVATE, &transfer);
293293
#ifdef NETWORK_LOCK_DEBUG
294294
lprintf(debug,
295-
"transfer_blocking(): thread %lu: locking transfer_lock;\n",
295+
"transfer_blocking(): thread %x: locking transfer_lock;\n",
296296
pthread_self());
297297
#endif
298298
PTHREAD_MUTEX_LOCK(&transfer_lock);
299299
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
300300
#ifdef NETWORK_LOCK_DEBUG
301301
lprintf(debug,
302-
"transfer_blocking(): thread %lu: unlocking transfer_lock;\n",
302+
"transfer_blocking(): thread %x: unlocking transfer_lock;\n",
303303
pthread_self());
304304
#endif
305305
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
@@ -319,14 +319,14 @@ void transfer_nonblocking(CURL *curl)
319319
{
320320
#ifdef NETWORK_LOCK_DEBUG
321321
lprintf(debug,
322-
"transfer_nonblocking(): thread %lu: locking transfer_lock;\n",
322+
"transfer_nonblocking(): thread %x: locking transfer_lock;\n",
323323
pthread_self());
324324
#endif
325325
PTHREAD_MUTEX_LOCK(&transfer_lock);
326326
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
327327
#ifdef NETWORK_LOCK_DEBUG
328328
lprintf(debug,
329-
"transfer_nonblocking(): thread %lu: unlocking transfer_lock;\n",
329+
"transfer_nonblocking(): thread %x: unlocking transfer_lock;\n",
330330
pthread_self());
331331
#endif
332332
PTHREAD_MUTEX_UNLOCK(&transfer_lock);

0 commit comments

Comments
 (0)