From cd9af4ff099c6b7ffa4f9f4b261cc16948245863 Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Wed, 3 May 2023 18:54:16 +0200 Subject: create LaunchAgents directory, if it does not exist --- src/service.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/service.h b/src/service.h index 01c5f75..2617d06 100644 --- a/src/service.h +++ b/src/service.h @@ -2,6 +2,7 @@ #define SERVICE_H #include +#include #define MAXLEN 512 @@ -94,10 +95,46 @@ static void populate_plist(char *skhd_plist, int size) snprintf(skhd_plist, size, _SKHD_PLIST, exe_path, path_env, user, user); } + +static inline bool directory_exists(char *filename) +{ + struct stat buffer; + + if (stat(filename, &buffer) != 0) { + return false; + } + + return S_ISDIR(buffer.st_mode); +} + +static inline void ensure_directory_exists(char *skhd_plist_path) +{ + // + // NOTE(koekeishiya): Temporarily remove filename. + // We know the filepath will contain a slash, as + // it is controlled by us, so don't bother checking + // the result.. + // + + char *last_slash = strrchr(skhd_plist_path, '/'); + *last_slash = '\0'; + + if (!directory_exists(skhd_plist_path)) { + mkdir(skhd_plist_path, 0755); + } + + // + // NOTE(koekeishiya): Restore original filename. + // + + *last_slash = '/'; +} + static int service_install_internal(char *skhd_plist_path) { char skhd_plist[4096]; populate_plist(skhd_plist, sizeof(skhd_plist)); + ensure_directory_exists(skhd_plist_path); FILE *handle = fopen(skhd_plist_path, "w"); if (!handle) return 1; -- cgit v1.2.3