GCC Code Coverage Report


src/
File: src/service/service_utils.c
Date: 2026-03-31 17:48:22
Lines:
37/59
62.7%
Functions:
3/3
100.0%
Branches:
25/52
48.1%

Line Branch Exec Source
1 #include <stddef.h>
2 #include <service/service_internals.h>
3
4 28 extern int gittor_service_get_port(GError** error) {
5 // Evalute the port file path
6
2/2
✓ Branch 1 taken 28 times.
✓ Branch 4 taken 28 times.
28 gchar* dir = g_build_filename(g_get_user_config_dir(), "gittor", NULL);
7
1/1
✓ Branch 1 taken 28 times.
28 gchar* path = g_build_filename(dir, "service_port", NULL);
8
9 // Attempt to read the contents
10 28 gchar* content = NULL;
11 28 gsize length = 0;
12
1/1
✓ Branch 1 taken 28 times.
28 g_file_get_contents(path, &content, &length, error);
13
14 // Error checking
15
3/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 27 times.
28 if (error && *error) {
16
1/1
✓ Branch 1 taken 1 times.
1 g_free(content);
17
1/1
✓ Branch 1 taken 1 times.
1 g_free(dir);
18
1/1
✓ Branch 1 taken 1 times.
1 g_free(path);
19 28 return -1;
20 }
21
22
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (content == NULL) {
23 g_set_error(error, g_quark_from_static_string("get-port"), 1,
24 "Error Reading %s: content null", path);
25 g_free(content);
26 g_free(dir);
27 g_free(path);
28 return -1;
29 }
30
31
2/2
✓ Branch 1 taken 27 times.
✓ Branch 4 taken 27 times.
27 g_strstrip(content);
32
33
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (content[0] == '\0') {
34 g_set_error(error, g_quark_from_static_string("get-port"), 2,
35 "Error Reading %s: content empty", path);
36 g_free(content);
37 g_free(dir);
38 g_free(path);
39 return -1;
40 }
41
42 // Attempt to parse the contents
43 27 gchar* endptr = NULL;
44
1/1
✓ Branch 1 taken 27 times.
27 int port = (int)g_ascii_strtoll(content, &endptr, 10);
45
46 // Error checking
47
2/4
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
27 if (endptr == content || *endptr != '\0') {
48 g_set_error(error, g_quark_from_static_string("get-port"), 3,
49 "Error Reading %s: content non-integer", path);
50 g_free(content);
51 g_free(dir);
52 g_free(path);
53 return -1;
54 }
55
56 // Return the port
57
1/1
✓ Branch 1 taken 27 times.
27 g_free(content);
58
1/1
✓ Branch 1 taken 27 times.
27 g_free(dir);
59
1/1
✓ Branch 1 taken 27 times.
27 g_free(path);
60 27 return port;
61 }
62
63 6 extern void gittor_service_set_port(int port, GError** error) {
64 // Evalute the port file path
65 6 gchar* dir = g_build_filename(g_get_user_config_dir(), "gittor", NULL);
66
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (!g_file_test(dir, G_FILE_TEST_IS_DIR) &&
67 g_mkdir_with_parents(dir, 0700)) {
68 g_set_error(error, g_quark_from_static_string("set-port"), 1,
69 "Error creating configuration directory '%s'", dir);
70 return;
71 }
72 6 gchar* path = g_build_filename(dir, "service_port", NULL);
73
74 // Generate the port string
75 6 gchar* content = g_strdup_printf("%d\n", port);
76
77 // Write the contents to the file
78 6 g_file_set_contents(path, content, -1, error);
79
80 6 g_free(dir);
81 6 g_free(path);
82 6 g_free(content);
83 }
84
85 1 extern int bind_port_in_range(GSocketAddress** addr,
86 const char* ip,
87 GSocket* sock,
88 int start,
89 int end) {
90 1 GError* error = NULL;
91
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 for (int port = start; port <= end; port++) {
92
1/1
✓ Branch 1 taken 1 times.
1 *addr = g_inet_socket_address_new_from_string(ip, port);
93
94
1/1
✓ Branch 1 taken 1 times.
1 g_socket_bind(sock, *addr, true, &error);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (error) {
96 g_object_unref(*addr);
97 g_clear_error(&error);
98 continue;
99 }
100 1 return port;
101 }
102 return -1;
103 }
104