Skip to content

Commit

Permalink
#1728 create LaunchAgents directory, if it does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
koekeishiya committed May 3, 2023
1 parent f4ad453 commit a8d62d6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Create `LaunchAgents` folder in `~/Library` when installing service file, if the directory does not already exist [#1728](https://github.com/koekeishiya/yabai/issues/1728)

## [5.0.4] - 2023-05-01
### Added
Expand Down
11 changes: 11 additions & 0 deletions src/misc/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ static inline bool file_exists(char *filename)
return true;
}

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 bool ensure_executable_permission(char *filename)
{
struct stat buffer;
Expand Down
24 changes: 24 additions & 0 deletions src/misc/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,34 @@ static void populate_plist(char *yabai_plist, int size)
snprintf(yabai_plist, size, _YABAI_PLIST, exe_path, path_env, user, user);
}

static inline void ensure_directory_exists(char *yabai_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(yabai_plist_path, '/');
*last_slash = '\0';

if (!directory_exists(yabai_plist_path)) {
mkdir(yabai_plist_path, 0755);
}

//
// NOTE(koekeishiya): Restore original filename.
//

*last_slash = '/';
}

static int service_install_internal(char *yabai_plist_path)
{
char yabai_plist[4096];
populate_plist(yabai_plist, sizeof(yabai_plist));
ensure_directory_exists(yabai_plist_path);

FILE *handle = fopen(yabai_plist_path, "w");
if (!handle) return 1;
Expand Down

0 comments on commit a8d62d6

Please sign in to comment.