]> git.proxmox.com Git - proxmox-backup.git/blob - src/backup/chunk_store.rs
fix chunk store file paths
[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 u256_to_hex(digest: &[u8; 32]) -> String {
24
25 let mut buf = Vec::<u8>::with_capacity(64);
26
27 for i in 0..32 {
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 u256_to_prefix(digest: &[u8; 32]) -> 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 insert_chunk(&mut self, chunk: &[u8]) -> Result<(bool, [u8; 32]), Error> {
155
156 self.hasher.reset();
157 self.hasher.input(chunk);
158
159 let mut digest = [0u8; 32];
160 self.hasher.result(&mut digest);
161 //println!("DIGEST {}", u256_to_hex(&digest));
162
163 let mut chunk_path = self.chunk_dir.clone();
164 let prefix = u256_to_prefix(&digest);
165 chunk_path.push(&prefix);
166 let digest_str = u256_to_hex(&digest);
167 chunk_path.push(&digest_str);
168
169 let lock = self.mutex.lock();
170
171 if let Ok(metadata) = std::fs::metadata(&chunk_path) {
172 if metadata.is_file() {
173 return Ok((true, digest));
174 } else {
175 bail!("Got unexpected file type for chunk {}", digest_str);
176 }
177 }
178
179 let mut chunk_dir = self.chunk_dir.clone();
180 chunk_dir.push(&prefix);
181
182 if let Err(_) = std::fs::create_dir(&chunk_dir) { /* ignore */ }
183
184 let mut tmp_path = chunk_path.clone();
185 tmp_path.set_extension("tmp");
186 let mut f = std::fs::File::create(&tmp_path)?;
187 f.write_all(chunk)?;
188
189 if let Err(err) = std::fs::rename(&tmp_path, &chunk_path) {
190 if let Err(_) = std::fs::remove_file(&tmp_path) { /* ignore */ }
191 bail!("Atomic rename failed for chunk {} - {}", digest_str, err);
192 }
193
194 println!("PATH {:?}", chunk_path);
195
196 drop(lock);
197
198 Ok((false, digest))
199 }
200
201 pub fn relative_path(&self, path: &Path) -> PathBuf {
202
203 let mut full_path = self.base.clone();
204 full_path.push(path);
205 full_path
206 }
207
208 }
209
210
211 #[test]
212 fn test_chunk_store1() {
213
214 if let Err(_e) = std::fs::remove_dir_all(".testdir") { /* ignore */ }
215
216 let chunk_store = ChunkStore::open(".testdir");
217 assert!(chunk_store.is_err());
218
219 let mut chunk_store = ChunkStore::create(".testdir").unwrap();
220 let (exists, _) = chunk_store.insert_chunk(&[0u8, 1u8]).unwrap();
221 assert!(!exists);
222
223 let (exists, _) = chunk_store.insert_chunk(&[0u8, 1u8]).unwrap();
224 assert!(exists);
225
226
227 let chunk_store = ChunkStore::create(".testdir");
228 assert!(chunk_store.is_err());
229
230
231 }