Skip to content

Commit

Permalink
Close file immediately after writing to it
Browse files Browse the repository at this point in the history
This ensures the file fully exists when we try to realpath it
  • Loading branch information
Beyley committed Aug 22, 2023
1 parent 8245cc7 commit 63a09c6
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ pub fn getConfig(allocator: std.mem.Allocator) !Self {
if (err == std.fs.File.OpenError.FileNotFound) {
const default_config = Self{};

//Create the new config file
var file = try (config_dir orelse cwd).createFile(config_filename, .{});
defer file.close();

var buffered_writer = std.io.bufferedWriter(file.writer());
//Serialize our config as ini into the file
try zini.stringify(buffered_writer.writer(), default_config);
try buffered_writer.flush();

//Close before realpath so that windows is happy
file.close();

//Get the full path of the config
var full_path = try (config_dir orelse cwd).realpathAlloc(allocator, config_filename);
defer allocator.free(full_path);
Expand All @@ -47,7 +51,7 @@ pub fn getConfig(allocator: std.mem.Allocator) !Self {
var msg = std.ArrayList(u8).init(allocator);
defer msg.deinit();
//Write out the message
try std.fmt.format(msg.writer(), "Config created at {s}!\nWould you like to open the config in your default text editor?\x00", .{full_path});
try std.fmt.format(msg.writer(), "Config created at {s}!\nWould you like to open the config for editing?\x00", .{full_path});

//Display the message to the user
if (c.boxerShow(
Expand Down

0 comments on commit 63a09c6

Please sign in to comment.