-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathfs_helpers.rs
More file actions
211 lines (190 loc) · 7.04 KB
/
Copy pathfs_helpers.rs
File metadata and controls
211 lines (190 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use std::os::unix::fs::{MetadataExt, PermissionsExt};
use std::path::Path;
use std::time::UNIX_EPOCH;
use fuser::{FileAttr, FileType};
use crate::fs::{BranchFs, BLOCK_SIZE};
use crate::storage;
impl BranchFs {
pub(crate) fn resolve(&self, path: &str) -> Option<std::path::PathBuf> {
self.manager
.resolve_path(&self.get_branch_name(), path)
.ok()?
}
/// Resolve a path within a specific branch (not the root's current branch).
pub(crate) fn resolve_for_branch(
&self,
branch: &str,
path: &str,
) -> Option<std::path::PathBuf> {
self.manager.resolve_path(branch, path).ok()?
}
pub(crate) fn get_delta_path(&self, rel_path: &str) -> std::path::PathBuf {
self.manager
.with_branch(&self.get_branch_name(), |b| Ok(b.delta_path(rel_path)))
.unwrap()
}
pub(crate) fn get_delta_path_for_branch(
&self,
branch: &str,
rel_path: &str,
) -> std::path::PathBuf {
self.manager
.with_branch(branch, |b| Ok(b.delta_path(rel_path)))
.unwrap()
}
pub(crate) fn ensure_cow(&self, rel_path: &str) -> std::io::Result<std::path::PathBuf> {
self.ensure_cow_for_branch(&self.get_branch_name(), rel_path)
}
pub(crate) fn ensure_cow_for_branch(
&self,
branch: &str,
rel_path: &str,
) -> std::io::Result<std::path::PathBuf> {
let delta = self.get_delta_path_for_branch(branch, rel_path);
if delta.symlink_metadata().is_err() {
if let Some(src) = self.resolve_for_branch(branch, rel_path) {
if let Ok(meta) = src.symlink_metadata() {
if meta.file_type().is_symlink() || meta.file_type().is_file() {
let src_size = meta.len();
self.manager
.quota
.check(src_size)
.map_err(std::io::Error::from_raw_os_error)?;
storage::copy_entry(&src, &delta)
.map_err(|e| std::io::Error::other(e.to_string()))?;
self.manager.quota.add(src_size);
}
}
}
}
storage::ensure_parent_dirs(&delta).map_err(|e| std::io::Error::other(e.to_string()))?;
Ok(delta)
}
pub(crate) fn make_attr(&self, ino: u64, path: &Path) -> Option<FileAttr> {
let meta = std::fs::symlink_metadata(path).ok()?;
let kind = if meta.is_dir() {
FileType::Directory
} else if meta.is_symlink() {
FileType::Symlink
} else {
FileType::RegularFile
};
Some(FileAttr {
ino,
size: meta.len(),
blocks: meta.len().div_ceil(BLOCK_SIZE as u64),
atime: meta.accessed().unwrap_or(UNIX_EPOCH),
mtime: meta.modified().unwrap_or(UNIX_EPOCH),
ctime: UNIX_EPOCH,
crtime: UNIX_EPOCH,
kind,
perm: meta.permissions().mode() as u16,
nlink: meta.nlink() as u32,
uid: meta.uid(),
gid: meta.gid(),
rdev: 0,
blksize: BLOCK_SIZE,
flags: 0,
})
}
/// Return a synthetic directory FileAttr.
pub(crate) fn synthetic_dir_attr(&self, ino: u64) -> FileAttr {
FileAttr {
ino,
size: 0,
blocks: 0,
atime: UNIX_EPOCH,
mtime: UNIX_EPOCH,
ctime: UNIX_EPOCH,
crtime: UNIX_EPOCH,
kind: FileType::Directory,
perm: 0o755,
nlink: 2,
uid: self.uid.load(std::sync::atomic::Ordering::Relaxed),
gid: self.gid.load(std::sync::atomic::Ordering::Relaxed),
rdev: 0,
blksize: BLOCK_SIZE,
flags: 0,
}
}
/// Return a synthetic ctl-file FileAttr.
pub(crate) fn ctl_file_attr(&self, ino: u64) -> FileAttr {
let size = crate::platform::ctl_file_size(ino);
FileAttr {
ino,
size,
blocks: 0,
atime: UNIX_EPOCH,
mtime: UNIX_EPOCH,
ctime: UNIX_EPOCH,
crtime: UNIX_EPOCH,
kind: FileType::RegularFile,
perm: 0o600,
nlink: 1,
uid: self.uid.load(std::sync::atomic::Ordering::Relaxed),
gid: self.gid.load(std::sync::atomic::Ordering::Relaxed),
rdev: 0,
blksize: BLOCK_SIZE,
flags: 0,
}
}
/// Collect readdir entries for a directory resolved via a specific branch.
///
/// Collects candidate names from the branch delta plus its frozen inherited
/// snapshot, then resolves each via `resolve_path` to respect tombstones and
/// determine the correct file type.
///
/// `inode_prefix` controls how child inode paths are formed:
/// - `"/@branch"` for branch subtrees (produces `/@branch/child`)
/// - `""` for root-level paths (produces `/child`)
pub(crate) fn collect_readdir_entries(
&self,
branch: &str,
rel_path: &str,
ino: u64,
inode_prefix: &str,
) -> Vec<(u64, FileType, String)> {
let mut entries = vec![
(ino, FileType::Directory, ".".to_string()),
(ino, FileType::Directory, "..".to_string()),
];
// Collect all candidate names from the branch delta + inherited view.
let mut candidates: Vec<String> = match self.manager.collect_dir_names(branch, rel_path) {
Ok(names) => names.into_iter().collect(),
Err(_) => return entries,
};
// Sort for deterministic readdir ordering across paged calls.
candidates.sort();
// For each candidate, resolve via the branch chain to check visibility
// (handles tombstones) and determine the actual file type.
for name in candidates {
let child_rel = if rel_path == "/" {
format!("/{}", name)
} else {
format!("{}/{}", rel_path, name)
};
// resolve_path handles tombstone filtering — returns None for deleted files.
let resolved = match self.resolve_for_branch(branch, &child_rel) {
Some(p) => p,
None => continue,
};
let inode_path = format!("{}{}", inode_prefix, child_rel);
let ft = resolved.symlink_metadata();
let is_symlink = ft
.as_ref()
.map(|m| m.file_type().is_symlink())
.unwrap_or(false);
let is_dir = !is_symlink && ft.as_ref().map(|m| m.is_dir()).unwrap_or(false);
let child_ino = self.inodes.get_or_create(&inode_path, is_dir);
let kind = if is_symlink {
FileType::Symlink
} else if is_dir {
FileType::Directory
} else {
FileType::RegularFile
};
entries.push((child_ino, kind, name));
}
entries
}
}