diff options
author | Aylur <[email protected]> | 2024-12-30 17:28:12 +0100 |
---|---|---|
committer | Aylur <[email protected]> | 2024-12-30 17:28:12 +0100 |
commit | ce9ba0d07b44c52700fef44ea6c9cbad33c3fe44 (patch) | |
tree | eb06a4b7225656cd6e15fc8c4e525d838b26dae6 /lib | |
parent | 998b57d645b75c144d0e591cc7888ea3d9b59a03 (diff) |
feat: auto create parent dirs on write_file
Diffstat (limited to 'lib')
-rw-r--r-- | lib/astal/io/file.vala | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/astal/io/file.vala b/lib/astal/io/file.vala index 57b6dc0..e8e8a8b 100644 --- a/lib/astal/io/file.vala +++ b/lib/astal/io/file.vala @@ -26,6 +26,11 @@ public async string read_file_async(string path) throws Error { */ public void write_file(string path, string content) { try { + var dir = Path.get_dirname(path); + if (!FileUtils.test(dir, FileTest.IS_DIR)) { + File.new_for_path(dir).make_directory_with_parents(null); + } + FileUtils.set_contents(path, content); } catch (Error error) { critical(error.message); @@ -36,6 +41,11 @@ public void write_file(string path, string content) { * Write content to a file asynchronously. */ public async void write_file_async(string path, string content) throws Error { + var dir = Path.get_dirname(path); + if (!FileUtils.test(dir, FileTest.IS_DIR)) { + File.new_for_path(dir).make_directory_with_parents(null); + } + yield File.new_for_path(path).replace_contents_async( content.data, null, |