mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 10:07:28 +00:00
working_copy: extract function for getting mtime
We had a few lines of duplicated code for this, so I moved it into a function and added better error handling.
This commit is contained in:
parent
23f9e6479f
commit
9feb786a51
1 changed files with 15 additions and 7 deletions
|
@ -14,7 +14,6 @@
|
||||||
|
|
||||||
use std::cell::{RefCell, RefMut};
|
use std::cell::{RefCell, RefMut};
|
||||||
use std::collections::{BTreeMap, HashSet};
|
use std::collections::{BTreeMap, HashSet};
|
||||||
use std::convert::TryInto;
|
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::{File, Metadata, OpenOptions};
|
use std::fs::{File, Metadata, OpenOptions};
|
||||||
|
@ -154,10 +153,21 @@ fn create_parent_dirs(disk_path: &Path) {
|
||||||
.unwrap_or_else(|_| panic!("failed to create parent directories for {:?}", &disk_path));
|
.unwrap_or_else(|_| panic!("failed to create parent directories for {:?}", &disk_path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn mtime_from_metadata(metadata: &Metadata) -> MillisSinceEpoch {
|
||||||
|
let time = metadata
|
||||||
|
.modified()
|
||||||
|
.expect("File mtime not supported on this platform?");
|
||||||
|
let since_epoch = time
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.expect("mtime before unix epoch");
|
||||||
|
|
||||||
|
MillisSinceEpoch(
|
||||||
|
u64::try_from(since_epoch.as_millis()).expect("mtime billions of years into the future"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fn file_state(metadata: &Metadata) -> FileState {
|
fn file_state(metadata: &Metadata) -> FileState {
|
||||||
let time = metadata.modified().unwrap();
|
let mtime = mtime_from_metadata(metadata);
|
||||||
let since_epoch = time.duration_since(UNIX_EPOCH).unwrap();
|
|
||||||
let mtime = MillisSinceEpoch(since_epoch.as_millis().try_into().unwrap());
|
|
||||||
let size = metadata.len();
|
let size = metadata.len();
|
||||||
let metadata_file_type = metadata.file_type();
|
let metadata_file_type = metadata.file_type();
|
||||||
let file_type = if metadata_file_type.is_dir() {
|
let file_type = if metadata_file_type.is_dir() {
|
||||||
|
@ -279,9 +289,7 @@ impl TreeState {
|
||||||
|
|
||||||
fn update_own_mtime(&mut self) {
|
fn update_own_mtime(&mut self) {
|
||||||
if let Ok(metadata) = self.state_path.join("tree_state").symlink_metadata() {
|
if let Ok(metadata) = self.state_path.join("tree_state").symlink_metadata() {
|
||||||
let time = metadata.modified().unwrap();
|
self.own_mtime = mtime_from_metadata(&metadata);
|
||||||
let since_epoch = time.duration_since(UNIX_EPOCH).unwrap();
|
|
||||||
self.own_mtime = MillisSinceEpoch(since_epoch.as_millis().try_into().unwrap());
|
|
||||||
} else {
|
} else {
|
||||||
self.own_mtime = MillisSinceEpoch(0);
|
self.own_mtime = MillisSinceEpoch(0);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue