From 3a93831e395b06c62d7eecd1fc8687928f015d84 Mon Sep 17 00:00:00 2001 From: Evan Boehs Date: Fri, 5 Jan 2024 10:36:26 -0500 Subject: [PATCH 1/3] Fix #1213 (create dir if needed for settings) --- src/config/edit.zig | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/config/edit.zig b/src/config/edit.zig index c673991df..a28cfc362 100644 --- a/src/config/edit.zig +++ b/src/config/edit.zig @@ -5,9 +5,23 @@ const internal_os = @import("../os/main.zig"); /// Open the configuration in the OS default editor according to the default /// paths the main config file could be in. pub fn open(alloc_gpa: Allocator) !void { + // default dir + const config_dir = try internal_os.xdg.config(alloc_gpa, .{ .subdir = "ghostty" }); // default location const config_path = try internal_os.xdg.config(alloc_gpa, .{ .subdir = "ghostty/config" }); - defer alloc_gpa.free(config_path); + + defer { + alloc_gpa.free(config_path); + alloc_gpa.free(config_dir); + } + + // Check if the directory exists, create it if it doesn't + _ = std.fs.makeDirAbsolute(config_dir) catch |err| { + switch (err) { + error.PathAlreadyExists => {}, + else => return err, + } + }; // Try to create file and go on if it already exists _ = std.fs.createFileAbsolute( From a62061aec6b59327b6d9e2e5a85acf2ae17e9293 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 5 Jan 2024 07:54:20 -0800 Subject: [PATCH 2/3] config: use dirname so we don't have to alloc a new dir for edit --- src/config/edit.zig | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/config/edit.zig b/src/config/edit.zig index a28cfc362..00c8df718 100644 --- a/src/config/edit.zig +++ b/src/config/edit.zig @@ -5,24 +5,22 @@ const internal_os = @import("../os/main.zig"); /// Open the configuration in the OS default editor according to the default /// paths the main config file could be in. pub fn open(alloc_gpa: Allocator) !void { - // default dir - const config_dir = try internal_os.xdg.config(alloc_gpa, .{ .subdir = "ghostty" }); // default location const config_path = try internal_os.xdg.config(alloc_gpa, .{ .subdir = "ghostty/config" }); + defer alloc_gpa.free(config_path); - defer { - alloc_gpa.free(config_path); - alloc_gpa.free(config_dir); + // Check if the directory exists, create it if it doesn't. This dirname + // call should always succeed but we still check for errors just in case, + // no harm if we skip this step the open may just fail. + if (std.fs.path.dirname(config_path)) |config_dir| { + _ = std.fs.makeDirAbsolute(config_dir) catch |err| { + switch (err) { + error.PathAlreadyExists => {}, + else => return err, + } + }; } - // Check if the directory exists, create it if it doesn't - _ = std.fs.makeDirAbsolute(config_dir) catch |err| { - switch (err) { - error.PathAlreadyExists => {}, - else => return err, - } - }; - // Try to create file and go on if it already exists _ = std.fs.createFileAbsolute( config_path, From cf18e23256b56b4b5bb154de2fc3bc2c3740270f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 5 Jan 2024 09:01:30 -0800 Subject: [PATCH 3/3] config: create the config dir recursively for edit --- src/config/edit.zig | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/config/edit.zig b/src/config/edit.zig index 00c8df718..38d9f2b7f 100644 --- a/src/config/edit.zig +++ b/src/config/edit.zig @@ -9,16 +9,9 @@ pub fn open(alloc_gpa: Allocator) !void { const config_path = try internal_os.xdg.config(alloc_gpa, .{ .subdir = "ghostty/config" }); defer alloc_gpa.free(config_path); - // Check if the directory exists, create it if it doesn't. This dirname - // call should always succeed but we still check for errors just in case, - // no harm if we skip this step the open may just fail. + // Create config directory recursively. if (std.fs.path.dirname(config_path)) |config_dir| { - _ = std.fs.makeDirAbsolute(config_dir) catch |err| { - switch (err) { - error.PathAlreadyExists => {}, - else => return err, - } - }; + try std.fs.cwd().makePath(config_dir); } // Try to create file and go on if it already exists