mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 18:27:38 +00:00
working_copy: get file metadata using open file descriptor
When we have just written a file or conflict, we can get metadata for it via the open file descriptor instead of using the path. That removes the risk of a race where the file got removed or replaced by another file type (at least on Unix).
This commit is contained in:
parent
e7aaf2f95f
commit
ecb86118e5
1 changed files with 6 additions and 6 deletions
|
@ -570,11 +570,11 @@ impl TreeState {
|
||||||
let mut contents = self.store.read_file(path, id)?;
|
let mut contents = self.store.read_file(path, id)?;
|
||||||
std::io::copy(&mut contents, &mut file).unwrap();
|
std::io::copy(&mut contents, &mut file).unwrap();
|
||||||
self.set_executable(disk_path, executable);
|
self.set_executable(disk_path, executable);
|
||||||
// Read the file state while we still have the file open. That way, know that
|
// Read the file state from the file descriptor. That way, know that the file
|
||||||
// the file exists, and the stat information is most likely accurate,
|
// exists and is of the expected type, and the stat information is most likely
|
||||||
// except for other processes modifying the file concurrently (The mtime is set
|
// accurate, except for other processes modifying the file concurrently (The
|
||||||
// at write time and won't change when we close the file.)
|
// mtime is set at write time and won't change when we close the file.)
|
||||||
let metadata = disk_path.symlink_metadata().unwrap();
|
let metadata = file.metadata().unwrap();
|
||||||
let mut file_state = file_state(&metadata);
|
let mut file_state = file_state(&metadata);
|
||||||
// Make sure the state we record is what we tried to set above. This is mostly
|
// Make sure the state we record is what we tried to set above. This is mostly
|
||||||
// for Windows, since the executable bit is not reflected in the file system
|
// for Windows, since the executable bit is not reflected in the file system
|
||||||
|
@ -624,7 +624,7 @@ impl TreeState {
|
||||||
materialize_conflict(self.store.as_ref(), path, &conflict, &mut file).unwrap();
|
materialize_conflict(self.store.as_ref(), path, &conflict, &mut file).unwrap();
|
||||||
// TODO: Set the executable bit correctly (when possible) and preserve that on
|
// TODO: Set the executable bit correctly (when possible) and preserve that on
|
||||||
// Windows like we do with the executable bit for regular files.
|
// Windows like we do with the executable bit for regular files.
|
||||||
let metadata = disk_path.symlink_metadata().unwrap();
|
let metadata = file.metadata().unwrap();
|
||||||
let mut result = file_state(&metadata);
|
let mut result = file_state(&metadata);
|
||||||
result.file_type = FileType::Conflict { id: id.clone() };
|
result.file_type = FileType::Conflict { id: id.clone() };
|
||||||
Ok(result)
|
Ok(result)
|
||||||
|
|
Loading…
Reference in a new issue