8000 Tell which path an I/O error failed on by dominion525 · Pull Request #177 · dominion525/jetdb · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions crates/jetdb-cli/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,15 @@ fn csv_options(args: &ExportArgs) -> CsvOptions {
}

/// Write the file `name` of the attachment that holds `number` in a row to
/// `dir/<number>/<name>`, and give that path under `dir`.
/// `dir/<number>/<name>`, and give that path under `dir`. An I/O error names
/// the path it failed on.
fn write_attachment(dir: &Path, number: i32, name: &str, data: &[u8]) -> io::Result<String> {
let with_path =
|path: &Path, e: io::Error| io::Error::new(e.kind(), format!("{}: {e}", path.display()));
let folder = dir.join(number.to_string());
fs::create_dir_all(&folder)?;
fs::write(folder.join(name), data)?;
fs::create_dir_all(&folder).map_err(|e| with_path(&folder, e))?;
let file = folder.join(name);
fs::write(&file, data).map_err(|e| with_path(&file, e))?;
Ok(format!("{number}/{name}"))
}

Expand Down
21 changes: 21 additions & 0 deletions crates/jetdb-cli/tests/cli_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,27 @@ fn export_attachments_to_a_directory() {
std::fs::remove_dir_all(&dir).unwrap();
}

#[test]
fn export_attachments_names_the_directory_it_cannot_create() {
let path = skip_if_missing!("V2007/complexDataTestV2007.accdb");
// A directory under a regular file cannot be created.
let file = std::env::temp_dir().join(format!("jetdb-export-not-a-dir-{}", std::process::id()));
std::fs::write(&file, "").unwrap();
let dir = file.join("attachments");
let output = jetdb_bin()
.args(["export", "--attachments", dir.to_str().unwrap()])
.args([path.to_str().unwrap(), "Table1"])
.output()
.expect("failed to run jetdb");
std::fs::remove_file(&file).unwrap();
assert!(!output.status.success());
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains(&format!("jetdb: I/O error: {}", dir.join("2").display())),
"stderr should name the directory, got: {stderr}"
);
}

// ---------------------------------------------------------------------------
// System tables keep all columns
// ---------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions crates/jetdb-cli/tests/cli_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn tables_nonexistent_file() {
assert!(!output.status.success());
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("jetdb:"),
"stderr should contain 'jetdb:' prefix, got: {stderr}"
stderr.contains("jetdb: I/O error: /nonexistent/path/to/file.mdb: "),
"stderr should name the file, got: {stderr}"
);
}
14 changes: 12 additions & 2 deletions crates/jetdb/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,15 @@ impl PageReader {
}

/// Internal: open a file and decrypt header without checking for Agile Encryption.
///
/// An I/O error in opening the file names the path, which
/// [`std::io::Error`] does not.
fn open_raw(path: impl AsRef<Path>) -> Result<Self, FileError> {
let file = File::open(path.as_ref())?;
let file_size = file.metadata()?.len();
let path = path.as_ref();
let with_path =
|e: std::io::Error| std::io::Error::new(e.kind(), format!("{}: {e}", path.display()));
let file = File::open(path).map_err(with_path)?;
let file_size = file.metadata().map_err(with_path)?.len();
Self::open_raw_from_source(Box::new(file), file_size)
}

Expand Down Expand Up @@ -908,6 +914,10 @@ mod tests {
fn open_nonexistent_file() {
let err = PageReader::open("/nonexistent/path/to/file.mdb").unwrap_err();
assert!(matches!(err, FileError::Io(_)));
assert!(
err.to_string().contains("/nonexistent/path/to/file.mdb"),
"the error should name the path, got: {err}"
);
}

#[test]
Expand Down
Loading
0