Skip to content

Commit

Permalink
More details on fs errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Keats committed Dec 14, 2020
1 parent 2c681f3 commit e58c2d6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
3 changes: 2 additions & 1 deletion components/rendering/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,8 @@ pub fn markdown_to_html(content: &str, context: &RenderContext) -> Result<Render
{
let mut escaped = String::new();
// write_str can fail but here there are no reasons it should (afaik?)
cmark::escape::escape_href(&mut escaped, &link).expect("Could not write to buffer");
cmark::escape::escape_href(&mut escaped, &link)
.expect("Could not write to buffer");
Event::Html(
context
.config
Expand Down
43 changes: 35 additions & 8 deletions components/utils/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub fn is_path_in_directory(parent: &Path, path: &Path) -> Result<bool> {

/// Create a file with the content given
pub fn create_file(path: &Path, content: &str) -> Result<()> {
let mut file =
File::create(&path).map_err(|e| Error::chain(format!("Failed to create {:?}", path), e))?;
let mut file = File::create(&path)
.map_err(|e| Error::chain(format!("Failed to create file {}", path.display()), e))?;
file.write_all(content.as_bytes())?;
Ok(())
}
Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn read_file(path: &Path) -> Result<String> {

/// Return the content of a file, with error handling added.
/// The default error message is overwritten by the message given.
/// That means it is allocation 2 strings, oh well
/// That means it is allocating 2 strings, oh well
pub fn read_file_with_error(path: &Path, message: &str) -> Result<String> {
let res = read_file(&path);
if res.is_ok() {
Expand Down Expand Up @@ -101,7 +101,9 @@ pub fn copy_file(src: &Path, dest: &PathBuf, base_path: &PathBuf, hard_link: boo
let target_path = dest.join(relative_path);

if let Some(parent_directory) = target_path.parent() {
create_dir_all(parent_directory)?;
create_dir_all(parent_directory).map_err(|e| {
Error::chain(format!("Was not able to create folder {}", parent_directory.display()), e)
})?;
}

copy_file_if_needed(src, &target_path, hard_link)
Expand All @@ -113,7 +115,9 @@ pub fn copy_file(src: &Path, dest: &PathBuf, base_path: &PathBuf, hard_link: boo
/// 3. Its filesize is identical to that of the src file.
pub fn copy_file_if_needed(src: &Path, dest: &PathBuf, hard_link: bool) -> Result<()> {
if let Some(parent_directory) = dest.parent() {
create_dir_all(parent_directory)?;
create_dir_all(parent_directory).map_err(|e| {
Error::chain(format!("Was not able to create folder {}", parent_directory.display()), e)
})?;
}

if hard_link {
Expand All @@ -125,11 +129,25 @@ pub fn copy_file_if_needed(src: &Path, dest: &PathBuf, hard_link: bool) -> Resul
let target_metadata = metadata(&dest)?;
let target_mtime = FileTime::from_last_modification_time(&target_metadata);
if !(src_mtime == target_mtime && src_metadata.len() == target_metadata.len()) {
copy(src, &dest)?;
copy(src, &dest).map_err(|e| {
Error::chain(
format!(
"Was not able to copy file {} to {}",
src.display(),
dest.display()
),
e,
)
})?;
set_file_mtime(&dest, src_mtime)?;
}
} else {
copy(src, &dest)?;
copy(src, &dest).map_err(|e| {
Error::chain(
format!("Was not able to copy file {} to {}", src.display(), dest.display()),
e,
)
})?;
set_file_mtime(&dest, src_mtime)?;
}
}
Expand All @@ -146,7 +164,16 @@ pub fn copy_directory(src: &PathBuf, dest: &PathBuf, hard_link: bool) -> Result<
create_directory(&target_path)?;
}
} else {
copy_file(entry.path(), dest, src, hard_link)?;
copy_file(entry.path(), dest, src, hard_link).map_err(|e| {
Error::chain(
format!(
"Was not able to copy file {} to {}",
entry.path().display(),
dest.display()
),
e,
)
})?;
}
}
Ok(())
Expand Down

0 comments on commit e58c2d6

Please sign in to comment.