| e0918b88 Andy McCall |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use anyhow::{bail, Context, Result}; |
| 6 |
use flow_core::Hash; |
| 7 |
use oxy_core::hash::ChangeHash; |
| 8 |
use oxy_core::objects::NodeKind; |
| 9 |
use oxy_core::repo::Repo; |
| 10 |
use oxy_core::state::FileContent; |
| 11 |
use std::path::Path; |
| 12 |
|
| 13 |
pub type Files = Vec<(String, Hash)>; |
| 14 |
|
| 15 |
|
| 16 |
pub fn export_state(repo: &Repo, heads: &[ChangeHash], dir: &Path) -> Result<Files> { |
| 17 |
let snapshot = repo.load_state(heads).context("load the state")?.materialize(); |
| 18 |
let mut files = Vec::new(); |
| 19 |
for (path, f) in &snapshot.files { |
| 20 |
if path.split('/').any(|p| p.is_empty() || p == "." || p == ".." || p == ".oxy") { |
| 21 |
continue; |
| 22 |
} |
| 23 |
let target = dir.join(path); |
| 24 |
match (&f.kind, &f.content) { |
| 25 |
(NodeKind::Dir, _) | (_, FileContent::Dir) => std::fs::create_dir_all(&target)?, |
| 26 |
(NodeKind::Subrepo, _) => {} |
| 27 |
(kind, content) => { |
| 28 |
let bytes = match content { |
| 29 |
FileContent::Text { bytes, .. } => bytes.clone(), |
| 30 |
FileContent::Opaque { blobs } => match blobs.first() { |
| 31 |
Some(b) => repo.read_blob(b)?, |
| 32 |
None => Vec::new(), |
| 33 |
}, |
| 34 |
FileContent::Dir => unreachable!(), |
| 35 |
}; |
| 36 |
if let Some(parent) = target.parent() { |
| 37 |
std::fs::create_dir_all(parent)?; |
| 38 |
} |
| 39 |
if *kind == NodeKind::Symlink { |
| 40 |
let to = String::from_utf8_lossy(&bytes).into_owned(); |
| 41 |
std::os::unix::fs::symlink(&to, &target).with_context(|| format!("symlink {path}"))?; |
| 42 |
} else { |
| 43 |
std::fs::write(&target, &bytes).with_context(|| format!("write {path}"))?; |
| 44 |
if f.mode.exec { |
| 45 |
use std::os::unix::fs::PermissionsExt; |
| 46 |
std::fs::set_permissions(&target, std::fs::Permissions::from_mode(0o755))?; |
| 47 |
} |
| 48 |
} |
| 49 |
files.push((path.clone(), *blake3::hash(&bytes).as_bytes())); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
Ok(files) |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
pub fn copy_tree(src: &Path, dir: &Path) -> Result<Files> { |
| 59 |
let mut files = Vec::new(); |
| 60 |
let walk = ignore::WalkBuilder::new(src) |
| 61 |
.hidden(false) |
| 62 |
.git_ignore(true) |
| 63 |
.require_git(false) |
| 64 |
.add_custom_ignore_filename(".oxyignore") |
| 65 |
.filter_entry(|e| e.file_name() != ".oxy" && e.file_name() != ".git") |
| 66 |
.build(); |
| 67 |
for entry in walk { |
| 68 |
let entry = entry?; |
| 69 |
let rel = entry.path().strip_prefix(src)?; |
| 70 |
if rel.as_os_str().is_empty() { |
| 71 |
continue; |
| 72 |
} |
| 73 |
let rel_s = rel.to_string_lossy().replace('\\', "/"); |
| 74 |
let target = dir.join(rel); |
| 75 |
let ft = entry.file_type().context("file type")?; |
| 76 |
if ft.is_dir() { |
| 77 |
std::fs::create_dir_all(&target)?; |
| 78 |
} else if ft.is_symlink() { |
| 79 |
let to = std::fs::read_link(entry.path())?; |
| 80 |
std::os::unix::fs::symlink(&to, &target)?; |
| 81 |
files.push((rel_s, *blake3::hash(to.to_string_lossy().as_bytes()).as_bytes())); |
| 82 |
} else if ft.is_file() { |
| 83 |
std::fs::copy(entry.path(), &target)?; |
| 84 |
files.push((rel_s, *blake3::hash(&std::fs::read(entry.path())?).as_bytes())); |
| 85 |
} |
| 86 |
} |
| 87 |
Ok(files) |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
pub fn image(dir: &Path, out: &Path) -> Result<()> { |
| 93 |
let used: u64 = ignore::WalkBuilder::new(dir) |
| 94 |
.hidden(false) |
| 95 |
.ignore(false) |
| 96 |
.git_ignore(false) |
| 97 |
.build() |
| 98 |
.flatten() |
| 99 |
.filter_map(|e| e.metadata().ok()) |
| 100 |
.map(|m| m.len().max(4096)) |
| 101 |
.sum(); |
| 102 |
let mib = used * 12 / 10 / (1 << 20) + 512; |
| 103 |
let (uid, gid) = unsafe { (libc::getuid(), libc::getgid()) }; |
| 104 |
let status = std::process::Command::new("mkfs.ext4") |
| 105 |
.args(["-q", "-F", "-L", "workspace", "-E"]) |
| 106 |
.arg(format!("root_owner={uid}:{gid}")) |
| 107 |
.arg("-d") |
| 108 |
.arg(dir) |
| 109 |
.arg(out) |
| 110 |
.arg(format!("{mib}M")) |
| 111 |
.status() |
| 112 |
.context("run mkfs.ext4 (e2fsprogs 1.43 or later)")?; |
| 113 |
if !status.success() { |
| 114 |
bail!("mkfs.ext4 failed for the workspace image"); |
| 115 |
} |
| 116 |
Ok(()) |
| 117 |
} |