Skip to content

Commit acaa419

Browse files
committed
refactor: extract create_parent_dirs into common utility
- Moved `create_storage_dir` logic to `src/common.c` as `create_parent_dirs` and declared it in `include/common.h` for reuse. - Updated `wpc_lightdm_helper.c` and `config.c` to use the shared utility. - Fixed unsafe string mutation by duplicating the input path in `create_parent_dirs`. - Added `common.c` to build system (`Makefile` and `nob.c`). - Corrected logic in `should_use_imagemagick7`: treat "0" as using IM6. - Ensured `Monitor.wallpaper` is nulled before loading new GUI images. - Fixed reversed ImageMagick pixel format conditional in `wallpaper.c`.
1 parent d182be9 commit acaa419

File tree

8 files changed

+57
-25
lines changed

8 files changed

+57
-25
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ endif
3333
ifeq ($(WPC_HELPER), 1)
3434
WPC_SRCS += $(SRC_DIR)/lightdm.c
3535
WPC_CFLAGS += -DWPC_ENABLE_HELPER
36-
HELPER_SRCS := $(SRC_DIR)/wpc_lightdm_helper.c
36+
HELPER_SRCS := $(SRC_DIR)/wpc_lightdm_helper.c $(SRC_DIR)/common.c
3737
endif
3838

3939
WPC_OBJS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(WPC_SRCS))
@@ -78,7 +78,7 @@ endif
7878
iwyu:
7979
@for file in $(WPC_SRCS) $(HELPER_SRCS); do \
8080
echo "Running iwyu on $$file..."; \
81-
include-what-you-use -Xiwyu --transitive_includes_only -std=c23 $(WPC_CFLAGS) $(WPC_LDFLAGS) -I$(INCLUDE_DIR) $$file; \
81+
include-what-you-use -Xiwyu --transitive_includes_only -std=gnu11 $(WPC_CFLAGS) $(WPC_LDFLAGS) -I$(INCLUDE_DIR) $$file; \
8282
done
8383

8484
clean:

include/common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <sys/stat.h>
2+
#include <sys/types.h>
3+
#include <unistd.h>
4+
5+
extern int create_parent_dirs(const char *file_path, mode_t mode);

nob.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ void build_object(Nob_Cmd *cmd, LibFlagsDa *main_flags,
3131
}
3232

3333
nob_cc_flags(cmd);
34+
if (enable_dev_tooling)
35+
nob_cmd_append(cmd, "-g", "-fsanitize=address",
36+
"-fno-omit-frame-pointer");
37+
3438
nob_cmd_append(cmd, lib);
3539
uint nmain_flags = main_flags->count;
3640
uint ncommon_flags = common_flags->count;
@@ -151,7 +155,8 @@ Nob_File_Paths build_source_files(Nob_Cmd *cmd, const char *target,
151155
continue;
152156
}
153157
} else if (strcmp(target, "wpc_lightdm_helper") == 0) {
154-
if (strcmp(object, "wpc_lightdm_helper.o") == 0) {
158+
if (strcmp(object, "wpc_lightdm_helper.o") == 0 ||
159+
strcmp(object, "common.o") == 0) {
155160
nob_da_append(&objects, object_place);
156161
} else {
157162
continue;
@@ -205,7 +210,7 @@ int build_target(Nob_Cmd *cmd, const char *target, Nob_File_Paths objects,
205210
void should_use_imagemagick7(Nob_Cmd *cmd) {
206211
char *imagemagick_version_env = getenv("WPC_IMAGEMAGICK_7");
207212
if (imagemagick_version_env != NULL) {
208-
use_imagemagick7 = strcmp(imagemagick_version_env, "0") != 0;
213+
use_imagemagick7 = strcmp(imagemagick_version_env, "0") == 0;
209214
goto log;
210215
}
211216

@@ -227,11 +232,10 @@ void should_use_imagemagick7(Nob_Cmd *cmd) {
227232
free(buffer);
228233
cmd->count = 0;
229234
log:
230-
char imagemagick_version = use_imagemagick7 ? '7' : '6';
231235
nob_log(NOB_INFO,
232236
"Chosen ImageMagick version: %c based on environment or system "
233237
"detection",
234-
imagemagick_version);
238+
use_imagemagick7 ? '7' : '6');
235239
return;
236240
}
237241

src/common.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "common.h"
2+
#include <assert.h>
3+
#include <cjson/cJSON.h>
4+
#include <dirent.h>
5+
#include <errno.h>
6+
#include <fcntl.h>
7+
#include <glib.h>
8+
#include <libgen.h>
9+
#include <string.h>
10+
11+
extern int create_parent_dirs(const char *file_path, mode_t mode) {
12+
assert(file_path && *file_path);
13+
char *path_copy = strdup(file_path);
14+
if (!path_copy) return -1;
15+
for (char *p = strchr(path_copy + 1, '/'); p; p = strchr(p + 1, '/')) {
16+
*p = '\0';
17+
if (mkdir(path_copy, mode) == -1) {
18+
if (errno != EEXIST) {
19+
*p = '/';
20+
return -1;
21+
}
22+
}
23+
*p = '/';
24+
}
25+
free(path_copy);
26+
return 0;
27+
}

src/config.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdlib.h>
55
#include <string.h>
66

7+
#include "common.h"
78
#include "config.h"
89

910
#define CONFIG_FILE ".config/wpc/settings.json"
@@ -344,7 +345,9 @@ extern Config *load_config() {
344345
}
345346

346347
extern void dump_config(Config *config) {
347-
FILE *file = fopen(get_config_file(), "w");
348+
char *filename = get_config_file();
349+
create_parent_dirs(filename, 0770);
350+
FILE *file = fopen(filename, "w");
348351
if (file == NULL) {
349352
perror("Error opening configuration file");
350353
exit(1);

src/gui.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ static void show_images_src_dir(GtkApplication *app) {
137137
WallpaperArray *mon_wrap = g_object_get_data(G_OBJECT(app), "monitors");
138138
Monitor *monitors = (Monitor *)mon_wrap->data;
139139
gushort monitor_id;
140+
141+
for (monitor_id = 0; monitor_id < mon_wrap->amount_used; monitor_id++) {
142+
Monitor *monitor = &monitors[monitor_id];
143+
monitor->wallpaper = NULL;
144+
}
145+
140146
if (wallpapers) {
141147
gtk_flow_box_set_sort_func(GTK_FLOW_BOX(flowbox), NULL, NULL, NULL);
142148
gushort i;
@@ -366,6 +372,7 @@ static void storage_dir_chosen(GObject *source_object, GAsyncResult *res,
366372
g_warning("Error code: %d Error: %s", err->code, err->message);
367373
return;
368374
}
375+
369376
new_src_dir = g_file_get_path(dir);
370377

371378
update_source_directory(config, new_src_dir);

src/wallpaper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Atom wmDeleteWindow;
1616
XContext xid_context = 0;
1717

1818
#ifdef WPC_IMAGEMAGICK_7
19-
const char *pixel_format = "RGBA";
20-
#else
2119
const char *pixel_format = "BGRA";
20+
#else
21+
const char *pixel_format = "RGBA";
2222
#endif
2323

2424
static void set_bg_for_monitor(const gchar *wallpaper_path,

src/wpc_lightdm_helper.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#define _GNU_SOURCE
22
#define _FILE_OFFSET_BITS 64
33

4+
#include "common.h"
45
#include <assert.h>
56
#include <cjson/cJSON.h>
67
#include <dirent.h>
@@ -90,21 +91,6 @@ extern int lightdm_parse_config(char ***config_ptr, int *lines_ptr) {
9091
return 0;
9192
}
9293

93-
static int create_storage_dir(const char *file_path, mode_t mode) {
94-
assert(file_path && *file_path);
95-
for (char *p = strchr(file_path + 1, '/'); p; p = strchr(p + 1, '/')) {
96-
*p = '\0';
97-
if (mkdir(file_path, mode) == -1) {
98-
if (errno != EEXIST) {
99-
*p = '/';
100-
return -1;
101-
}
102-
}
103-
*p = '/';
104-
}
105-
return 0;
106-
}
107-
10894
static void copy_file(const char *src, const char *dst) {
10995
int fd_in, fd_out;
11096
off_t len, ret;
@@ -176,7 +162,7 @@ static int set_background(const char *scaled_wallpaper_path,
176162
if (dir) {
177163
closedir(dir);
178164
} else if (errno == ENOENT) {
179-
if (create_storage_dir(dst_wallpaper_path, 0775) != 0) {
165+
if (create_parent_dirs(dst_wallpaper_path, 0775) != 0) {
180166
fprintf(stderr, "Failed to create storage directory");
181167
return 1;
182168
}

0 commit comments

Comments
 (0)