Skip to content
Open
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
62 changes: 40 additions & 22 deletions crates/data/src/dbf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,21 @@ pub(crate) fn read_cpg(path: &std::path::Path, ldid: u8, notes: &mut crate::loss
cpg_result(try_read_cpg(path), ldid, notes)
}

/// As [`read_cpg`], for the `.cpg` a Shapefile's [`crate::sidecar::Sidecars`] found (`None` when
/// it has none): that one file, so the reader takes the spelling publish and save write back.
/// Anything there but a regular file (a folder, a named pipe) is said as unreadable.
pub(crate) fn read_cpg_at(cpg: Option<&std::path::Path>, ldid: u8, notes: &mut crate::loss::Notes) -> crate::Result<Option<Vec<u8>>> {
let Some(cpg) = cpg else { return Ok(None) };
let read = match read_one_cpg(cpg) {
Ok(Some(bytes)) => Ok(Some(bytes)),
Ok(None) => Err(std::io::Error::other("not a regular file")),
// Gone since its folder was listed: there is none.
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e),
};
cpg_result(read, ldid, notes)
}

/// An optional declaration can fall back after an I/O failure, but Cancel must end the read.
fn cpg_result(result: std::io::Result<Option<Vec<u8>>>, ldid: u8, notes: &mut crate::loss::Notes) -> crate::Result<Option<Vec<u8>>> {
match result.map_err(crate::DataError::from) {
Expand All @@ -561,34 +576,23 @@ fn cpg_result(result: std::io::Result<Option<Vec<u8>>>, ldid: u8, notes: &mut cr
}
}

/// Every ASCII case of the `.cpg` extension, the conventional spellings first: tried in turn,
/// without scanning a potentially large directory. Non-file spellings are skipped; the first
/// regular file wins.
pub const CPG_SPELLINGS: [&str; 8] = ["cpg", "CPG", "Cpg", "cPg", "cpG", "CPg", "CpG", "cPG"];
/// Every ASCII case of the `.cpg` extension, lower case first, then in byte order, the order a
/// Shapefile's parts are taken in ([`crate::sidecar::case_spellings`]): tried in turn, without
/// scanning a potentially large directory. Non-file spellings are skipped; the first regular file
/// wins.
pub const CPG_SPELLINGS: [&str; 8] = ["cpg", "CPG", "CPg", "CpG", "Cpg", "cPG", "cPg", "cpG"];

/// Every ASCII case of a Shapefile attribute table's extension, in reader preference order.
pub const DBF_SPELLINGS: [&str; 8] = ["dbf", "DBF", "Dbf", "dBf", "dbF", "DBf", "DbF", "dBF"];
/// Every ASCII case of a Shapefile attribute table's extension, in reader preference order (as
/// [`CPG_SPELLINGS`]).
pub const DBF_SPELLINGS: [&str; 8] = ["dbf", "DBF", "DBf", "DbF", "Dbf", "dBF", "dBf", "dbF"];

/// Skips special-file spellings, retaining an unusable-declaration notice if none is regular.
fn try_read_cpg(path: &std::path::Path) -> std::io::Result<Option<Vec<u8>>> {
use std::io::Read;
let mut non_file = false;
for ext in CPG_SPELLINGS {
let candidate = path.with_extension(ext);
match open_regular_file(&candidate) {
Ok(None) => {
non_file = true;
continue;
}
Ok(Some(file)) => {
crate::provenance::opened(&candidate, &file);
let mut bytes = Vec::new();
(&file).take(MAX_CPG_BYTES as u64 + 1).read_to_end(&mut bytes)?;
if bytes.len() <= MAX_CPG_BYTES {
crate::provenance::buffer(&path.with_extension(ext), &file, &bytes);
}
return Ok(Some(bytes));
}
match read_one_cpg(&path.with_extension(ext)) {
Ok(None) => non_file = true,
Ok(Some(bytes)) => return Ok(Some(bytes)),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(e),
}
Expand All @@ -599,6 +603,20 @@ fn try_read_cpg(path: &std::path::Path) -> std::io::Result<Option<Vec<u8>>> {
Ok(None)
}

/// The `.cpg` at `cpg` when it is a regular file (`None` when it is something else, never opened
/// for reading), its bytes recorded as a source input.
fn read_one_cpg(cpg: &std::path::Path) -> std::io::Result<Option<Vec<u8>>> {
use std::io::Read;
let Some(file) = open_regular_file(cpg)? else { return Ok(None) };
crate::provenance::opened(cpg, &file);
let mut bytes = Vec::new();
(&file).take(MAX_CPG_BYTES as u64 + 1).read_to_end(&mut bytes)?;
if bytes.len() <= MAX_CPG_BYTES {
crate::provenance::buffer(cpg, &file, &bytes);
}
Ok(Some(bytes))
}

/// Reads the first present code-page spelling without recording a provenance input.
fn read_declaration_cpg(path: &std::path::Path) -> std::io::Result<Option<Vec<u8>>> {
use std::io::{Error, ErrorKind, Read};
Expand Down
Loading
Loading