Skip to content

Commit

Permalink
Wire up modes
Browse files Browse the repository at this point in the history
Prior to this commit, modes did nothing since they were not populated.
After this commit, they are populated in the index, and that information
is used to specify modes on the created files.

Closes #13.
  • Loading branch information
peterwaller-arm committed Jan 28, 2023
1 parent 0848d16 commit 1875c28
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/packidx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ pub struct FileHandle {
}

#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct FileMetadata {}
pub struct FileMetadata {
pub mode: u32,
}

impl Eq for FileHandle {}

Expand Down
17 changes: 13 additions & 4 deletions src/repo/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
fs::{self, Permissions},
io,
io::{BufReader, Read, Write},
os::unix::prelude::PermissionsExt,
path::{Path, PathBuf},
};
use std::{fmt::Display, str::FromStr};
Expand Down Expand Up @@ -549,7 +550,7 @@ fn assign_to_frames(
offset: local_offset, // Replace global offset -> local offset
size: entry.object_metadata.size,
},
None,
entry.file_metadata,
);
frames[frame_index].push(local_entry);
}
Expand Down Expand Up @@ -662,9 +663,17 @@ fn extract_files(
path_buf.clear();
path_buf.push(&output_dir);
path_buf.push(&entry.path);
stats.write_time += measure_ok(|| write_object(&buf[..], &path_buf, None))?
.0
.as_secs_f64();
stats.write_time += measure_ok(|| {
write_object(
&buf[..],
&path_buf,
entry
.file_metadata
.map(|fm| Permissions::from_mode(fm.mode)),
)
})?
.0
.as_secs_f64();
}

Ok(())
Expand Down
23 changes: 17 additions & 6 deletions src/repo/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use super::{constants::*, pack::IdError};

use std::{
collections::{HashMap, HashSet},
fs, io,
fs::{self, File, Permissions},
io,
io::{Read, Write},
os::unix::prelude::PermissionsExt,
path::{Path, PathBuf},
str::FromStr,
sync::atomic::{AtomicBool, Ordering},
Expand All @@ -29,7 +31,7 @@ use super::fs::{
};
use super::pack::{write_skippable_frame, Pack, PackFrame, PackHeader, PackId, SnapshotId};
use super::remote;
use crate::packidx::{FileEntry, ObjectChecksum, PackError, PackIndex};
use crate::packidx::{FileEntry, FileMetadata, ObjectChecksum, PackError, PackIndex};
use crate::progress::ProgressReporter;
use crate::{
batch,
Expand Down Expand Up @@ -581,7 +583,11 @@ impl Repository {
let threads = num_cpus::get();

let pack_entries = run_in_parallel(threads, files.into_iter(), |file_path| {
let buf = fs::read(&file_path)?;
let mut fd = File::open(&file_path)?;
let mut buf = vec![];
fd.read_to_end(&mut buf)?;
let mode = fd.metadata()?.permissions().mode();
drop(fd);
let mut checksum = [0u8; 20];
let mut hasher = Sha1::new();
hasher.input(&buf);
Expand All @@ -595,7 +601,7 @@ impl Repository {
offset: LOOSE_OBJECT_OFFSET,
size: buf.len() as u64,
},
None,
Some(FileMetadata { mode }),
))
})
.into_iter()
Expand Down Expand Up @@ -1020,6 +1026,11 @@ impl Repository {
),
)
})?;

entry
.file_metadata
.map(|md| Permissions::from_mode(md.mode))
.map_or(Ok(()), |p| fs::set_permissions(&dest_path, p))?;
}

if verify {
Expand Down Expand Up @@ -1068,11 +1079,11 @@ impl Repository {
// but not, for example, the offset of the object in the pack file.
let from_lookup: HashMap<_, _> = from_entries
.iter()
.map(|e| ((&e.path, &e.checksum), e))
.map(|e| ((&e.path, &e.checksum, &e.file_metadata), e))
.collect();
let to_lookup: HashMap<_, _> = to_entries
.iter()
.map(|e| ((&e.path, &e.checksum), e))
.map(|e| ((&e.path, &e.checksum, &e.file_metadata), e))
.collect();

let mut added = vec![];
Expand Down

0 comments on commit 1875c28

Please sign in to comment.