Skip to content

Commit 460cdc8

Browse files
yujakejadlen
authored andcommitted
op_store: attach path context to read/write errors
This will probably help debug weird problem like jj-vcs#6287. File names are a bit redundant for ReadObject errors (which include ObjectId), but that should be okay.
1 parent ef478da commit 460cdc8

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

lib/src/simple_op_store.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ impl OpStore for SimpleOpStore {
152152
}
153153

154154
let path = self.views_dir().join(id.hex());
155-
let buf = fs::read(path).map_err(|err| io_to_read_error(err, id))?;
155+
let buf = fs::read(&path)
156+
.context(&path)
157+
.map_err(|err| io_to_read_error(err, id))?;
156158

157159
let proto = crate::protos::op_store::View::decode(&*buf)
158160
.map_err(|err| to_read_error(err.into(), id))?;
@@ -161,18 +163,22 @@ impl OpStore for SimpleOpStore {
161163

162164
fn write_view(&self, view: &View) -> OpStoreResult<ViewId> {
163165
let dir = self.views_dir();
164-
let temp_file =
165-
NamedTempFile::new_in(&dir).map_err(|err| io_to_write_error(err, "view"))?;
166+
let temp_file = NamedTempFile::new_in(&dir)
167+
.context(&dir)
168+
.map_err(|err| io_to_write_error(err, "view"))?;
166169

167170
let proto = view_to_proto(view);
168171
temp_file
169172
.as_file()
170173
.write_all(&proto.encode_to_vec())
174+
.context(temp_file.path())
171175
.map_err(|err| io_to_write_error(err, "view"))?;
172176

173177
let id = ViewId::new(blake2b_hash(view).to_vec());
174178

175-
persist_content_addressed_temp_file(temp_file, dir.join(id.hex()))
179+
let new_path = dir.join(id.hex());
180+
persist_content_addressed_temp_file(temp_file, &new_path)
181+
.context(&new_path)
176182
.map_err(|err| io_to_write_error(err, "view"))?;
177183
Ok(id)
178184
}
@@ -183,7 +189,9 @@ impl OpStore for SimpleOpStore {
183189
}
184190

185191
let path = self.operations_dir().join(id.hex());
186-
let buf = fs::read(path).map_err(|err| io_to_read_error(err, id))?;
192+
let buf = fs::read(&path)
193+
.context(&path)
194+
.map_err(|err| io_to_read_error(err, id))?;
187195

188196
let proto = crate::protos::op_store::Operation::decode(&*buf)
189197
.map_err(|err| to_read_error(err.into(), id))?;
@@ -200,18 +208,22 @@ impl OpStore for SimpleOpStore {
200208
fn write_operation(&self, operation: &Operation) -> OpStoreResult<OperationId> {
201209
assert!(!operation.parents.is_empty());
202210
let dir = self.operations_dir();
203-
let temp_file =
204-
NamedTempFile::new_in(&dir).map_err(|err| io_to_write_error(err, "operation"))?;
211+
let temp_file = NamedTempFile::new_in(&dir)
212+
.context(&dir)
213+
.map_err(|err| io_to_write_error(err, "operation"))?;
205214

206215
let proto = operation_to_proto(operation);
207216
temp_file
208217
.as_file()
209218
.write_all(&proto.encode_to_vec())
219+
.context(temp_file.path())
210220
.map_err(|err| io_to_write_error(err, "operation"))?;
211221

212222
let id = OperationId::new(blake2b_hash(operation).to_vec());
213223

214-
persist_content_addressed_temp_file(temp_file, dir.join(id.hex()))
224+
let new_path = dir.join(id.hex());
225+
persist_content_addressed_temp_file(temp_file, &new_path)
226+
.context(&new_path)
215227
.map_err(|err| io_to_write_error(err, "operation"))?;
216228
Ok(id)
217229
}
@@ -345,8 +357,8 @@ impl OpStore for SimpleOpStore {
345357
}
346358
}
347359

348-
fn io_to_read_error(err: std::io::Error, id: &impl ObjectId) -> OpStoreError {
349-
if err.kind() == ErrorKind::NotFound {
360+
fn io_to_read_error(err: PathError, id: &impl ObjectId) -> OpStoreError {
361+
if err.error.kind() == ErrorKind::NotFound {
350362
OpStoreError::ObjectNotFound {
351363
object_type: id.object_type(),
352364
hash: id.hex(),
@@ -368,7 +380,7 @@ fn to_read_error(
368380
}
369381
}
370382

371-
fn io_to_write_error(err: std::io::Error, object_type: &'static str) -> OpStoreError {
383+
fn io_to_write_error(err: PathError, object_type: &'static str) -> OpStoreError {
372384
OpStoreError::WriteObject {
373385
object_type,
374386
source: Box::new(err),

0 commit comments

Comments
 (0)