]> git.proxmox.com Git - proxmox-backup.git/blob - src/backup/chunk_store.rs
impl sweep_used_chunks, first try
[proxmox-backup.git] / src / backup / chunk_store.rs
1 use failure::*;
2 use std::path::{Path, PathBuf};
3 use std::io::Write;
4
5 use crypto::digest::Digest;
6 use crypto::sha2::Sha512Trunc256;
7 use std::sync::Mutex;
8
9 use std::fs::{File, OpenOptions};
10 use nix::fcntl::{flock, FlockArg};
11 use std::os::unix::io::AsRawFd;
12
13 pub struct ChunkStore {
14 base: PathBuf,
15 chunk_dir: PathBuf,
16 hasher: Sha512Trunc256,
17 mutex: Mutex<bool>,
18 lockfile: File,
19 }
20
21 const HEX_CHARS: &'static [u8; 16] = b"0123456789abcdef";
22
23 pub fn digest_to_hex(digest: &[u8]) -> String {
24
25 let mut buf = Vec::<u8>::with_capacity(digest.len()*2);
26
27 for i in 0..digest.len() {
28 buf.push(HEX_CHARS[(digest[i] >> 4) as usize]);
29 buf.push(HEX_CHARS[(digest[i] & 0xf) as usize]);
30 }
31
32 unsafe { String::from_utf8_unchecked(buf) }
33 }
34
35 fn digest_to_prefix(digest: &[u8]) -> PathBuf {
36
37 let mut buf = Vec::<u8>::with_capacity(3+1+2+1);
38
39 buf.push(HEX_CHARS[(digest[0] as usize) >> 4]);
40 buf.push(HEX_CHARS[(digest[0] as usize) &0xf]);
41 buf.push(HEX_CHARS[(digest[1] as usize) >> 4]);
42 buf.push('/' as u8);
43
44 buf.push(HEX_CHARS[(digest[1] as usize) & 0xf]);
45 buf.push(HEX_CHARS[(digest[2] as usize) >> 4]);
46 buf.push('/' as u8);
47
48 let path = unsafe { String::from_utf8_unchecked(buf)};
49
50 path.into()
51 }
52
53 fn lock_file<P: AsRef<Path>>(filename: P, timeout: usize) -> Result<File, Error> {
54
55 let path = filename.as_ref();
56 let lockfile = match OpenOptions::new()
57 .create(true)
58 .append(true)
59 .open(path) {
60 Ok(file) => file,
61 Err(err) => bail!("Unable to open lock {:?} - {}",
62 path, err),
63 };
64
65 let fd = lockfile.as_raw_fd();
66
67 let now = std::time::SystemTime::now();
68 let mut print_msg = true;
69 loop {
70 match flock(fd, FlockArg::LockExclusiveNonblock) {
71 Ok(_) => break,
72 Err(_) => {
73 if print_msg {
74 print_msg = false;
75 eprintln!("trying to aquire lock...");
76 }
77 }
78 }
79
80 match now.elapsed() {
81 Ok(elapsed) => {
82 if elapsed.as_secs() >= (timeout as u64) {
83 bail!("unable to aquire lock {:?} - got timeout", path);
84 }
85 }
86 Err(err) => {
87 bail!("unable to aquire lock {:?} - clock problems - {}", path, err);
88 }
89 }
90 std::thread::sleep(std::time::Duration::from_millis(100));
91 }
92 Ok(lockfile)
93 }
94
95 impl ChunkStore {
96
97 fn chunk_dir<P: AsRef<Path>>(path: P) -> PathBuf {
98
99 let mut chunk_dir: PathBuf = PathBuf::from(path.as_ref());
100 chunk_dir.push(".chunks");
101
102 chunk_dir
103 }
104
105 pub fn create<P: Into<PathBuf>>(path: P) -> Result<Self, Error> {
106
107 let base: PathBuf = path.into();
108 let chunk_dir = Self::chunk_dir(&base);
109
110 if let Err(err) = std::fs::create_dir(&base) {
111 bail!("unable to create chunk store {:?} - {}", base, err);
112 }
113
114 if let Err(err) = std::fs::create_dir(&chunk_dir) {
115 bail!("unable to create chunk store subdir {:?} - {}", chunk_dir, err);
116 }
117
118 // create 4096 subdir
119 for i in 0..4096 {
120 let mut l1path = chunk_dir.clone();
121 l1path.push(format!("{:03x}",i));
122 if let Err(err) = std::fs::create_dir(&l1path) {
123 bail!("unable to create chunk subdir {:?} - {}", l1path, err);
124 }
125 }
126
127 Self::open(base)
128 }
129
130 pub fn open<P: Into<PathBuf>>(path: P) -> Result<Self, Error> {
131
132 let base: PathBuf = path.into();
133 let chunk_dir = Self::chunk_dir(&base);
134
135 if let Err(err) = std::fs::metadata(&chunk_dir) {
136 bail!("unable to open chunk store {:?} - {}", chunk_dir, err);
137 }
138
139 let mut lockfile_path = base.clone();
140 lockfile_path.push(".lock");
141
142 // make sure only one process/thread/task can use it
143 let lockfile = lock_file(lockfile_path, 10)?;
144
145 Ok(ChunkStore {
146 base,
147 chunk_dir,
148 hasher: Sha512Trunc256::new(),
149 lockfile,
150 mutex: Mutex::new(false)
151 })
152 }
153
154 pub fn touch_chunk(&mut self, digest:&[u8]) -> Result<(), Error> {
155
156 // fixme: nix::sys::stat::utimensat
157 let mut chunk_path = self.chunk_dir.clone();
158 let prefix = digest_to_prefix(&digest);
159 chunk_path.push(&prefix);
160 let digest_str = digest_to_hex(&digest);
161 chunk_path.push(&digest_str);
162
163 std::fs::metadata(&chunk_path)?;
164 Ok(())
165 }
166
167 fn sweep_old_files(&self, dir: &Path) {
168
169 let mut handle = match nix::dir::Dir::open(
170 dir, nix::fcntl::OFlag::O_RDONLY, nix::sys::stat::Mode::empty()) {
171 Ok(h) => h,
172 Err(_) => return,
173 };
174
175 let rawfd = handle.as_raw_fd();
176
177 let now = unsafe { libc::time(std::ptr::null_mut()) };
178
179 for entry in handle.iter() {
180 match entry {
181 Ok(entry) => {
182 if let Some(file_type) = entry.file_type() {
183 if file_type == nix::dir::Type::File {
184 let filename = entry.file_name();
185 if let Ok(stat) = nix::sys::stat::fstatat(rawfd, filename, nix::fcntl::AtFlags::AT_SYMLINK_NOFOLLOW) {
186 let age = now - stat.st_atime;
187 println!("FOUND {} {:?}", age/(3600*24), filename);
188 if age/(3600*24) >= 2 {
189 println!("UNLINK {} {:?}", age/(3600*24), filename);
190 unsafe { libc::unlinkat(rawfd, filename.as_ptr(), 0); }
191 }
192 }
193 }
194 }
195 }
196 Err(_) => {
197 // fixme ??
198 }
199 }
200 }
201
202 }
203
204 pub fn sweep_used_chunks(&mut self) -> Result<(), Error> {
205
206 for i in 0..4096 {
207 let mut l1path = self.chunk_dir.clone();
208 l1path.push(format!("{:03x}", i));
209 for j in 0..256 {
210 let mut l2path = l1path.clone();
211 l2path.push(format!("{:02x}", j));
212 self.sweep_old_files(&l2path);
213 }
214 }
215
216 Ok(())
217 }
218
219 pub fn insert_chunk(&mut self, chunk: &[u8]) -> Result<(bool, [u8; 32]), Error> {
220
221 self.hasher.reset();
222 self.hasher.input(chunk);
223
224 let mut digest = [0u8; 32];
225 self.hasher.result(&mut digest);
226 //println!("DIGEST {}", digest_to_hex(&digest));
227
228 let mut chunk_path = self.chunk_dir.clone();
229 let prefix = digest_to_prefix(&digest);
230 chunk_path.push(&prefix);
231 let digest_str = digest_to_hex(&digest);
232 chunk_path.push(&digest_str);
233
234 let lock = self.mutex.lock();
235
236 if let Ok(metadata) = std::fs::metadata(&chunk_path) {
237 if metadata.is_file() {
238 return Ok((true, digest));
239 } else {
240 bail!("Got unexpected file type for chunk {}", digest_str);
241 }
242 }
243
244 let mut chunk_dir = self.chunk_dir.clone();
245 chunk_dir.push(&prefix);
246
247 if let Err(_) = std::fs::create_dir(&chunk_dir) { /* ignore */ }
248
249 let mut tmp_path = chunk_path.clone();
250 tmp_path.set_extension("tmp");
251 let mut f = std::fs::File::create(&tmp_path)?;
252 f.write_all(chunk)?;
253
254 if let Err(err) = std::fs::rename(&tmp_path, &chunk_path) {
255 if let Err(_) = std::fs::remove_file(&tmp_path) { /* ignore */ }
256 bail!("Atomic rename failed for chunk {} - {}", digest_str, err);
257 }
258
259 println!("PATH {:?}", chunk_path);
260
261 drop(lock);
262
263 Ok((false, digest))
264 }
265
266 pub fn relative_path(&self, path: &Path) -> PathBuf {
267
268 let mut full_path = self.base.clone();
269 full_path.push(path);
270 full_path
271 }
272
273 pub fn base_path(&self) -> PathBuf {
274 self.base.clone()
275 }
276
277 }
278
279
280 #[test]
281 fn test_chunk_store1() {
282
283 if let Err(_e) = std::fs::remove_dir_all(".testdir") { /* ignore */ }
284
285 let chunk_store = ChunkStore::open(".testdir");
286 assert!(chunk_store.is_err());
287
288 let mut chunk_store = ChunkStore::create(".testdir").unwrap();
289 let (exists, _) = chunk_store.insert_chunk(&[0u8, 1u8]).unwrap();
290 assert!(!exists);
291
292 let (exists, _) = chunk_store.insert_chunk(&[0u8, 1u8]).unwrap();
293 assert!(exists);
294
295
296 let chunk_store = ChunkStore::create(".testdir");
297 assert!(chunk_store.is_err());
298
299
300 }