-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
treewide: refactor mkdir, write_file and read_file
mkdir, read_file write_file augment the error case with the path in the error case. This is useful as we otherwise don't know which location failed. This commit refactors the functions to take any argument that coerces into a Path - which is implemented by many types. mkdir now uses create_dir_all as it is not meant to fail if the directory already exists. Signed-off-by: Stefan Kerkmann <s.kerkmann@pengutronix.de>
- Loading branch information
Showing
5 changed files
with
48 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
|
||
use std::{ | ||
fs::{create_dir_all, read_to_string, write}, | ||
path::Path, | ||
}; | ||
|
||
use crate::Result; | ||
|
||
pub(crate) fn mkdir(dir: impl AsRef<Path>) -> Result<()> { | ||
create_dir_all(dir.as_ref()).map_err(|e| { | ||
format!( | ||
"Failed to create directory {}: {e}", | ||
dir.as_ref().to_string_lossy() | ||
) | ||
.into() | ||
}) | ||
} | ||
|
||
pub(crate) fn read_file(filename: impl AsRef<Path>) -> Result<String> { | ||
read_to_string(filename.as_ref()).map_err(|e| { | ||
format!( | ||
"Failed to read {}: {e}", | ||
filename.as_ref().to_string_lossy() | ||
) | ||
.into() | ||
}) | ||
} | ||
|
||
pub(crate) fn write_file<C: AsRef<[u8]>>(path: impl AsRef<Path>, content: C) -> Result<()> { | ||
write(&path, content).map_err(|e| { | ||
format!( | ||
"Failed to write to {}: {e}", | ||
path.as_ref().to_string_lossy() | ||
) | ||
.into() | ||
}) | ||
} |