httm 0.49.9

A CLI tool for viewing snapshot file versions on ZFS and btrfs datasets
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//       ___           ___           ___           ___
//      /\__\         /\  \         /\  \         /\__\
//     /:/  /         \:\  \        \:\  \       /::|  |
//    /:/__/           \:\  \        \:\  \     /:|:|  |
//   /::\  \ ___       /::\  \       /::\  \   /:/|:|__|__
//  /:/\:\  /\__\     /:/\:\__\     /:/\:\__\ /:/ |::::\__\
//  \/__\:\/:/  /    /:/  \/__/    /:/  \/__/ \/__/~~/:/  /
//       \::/  /    /:/  /        /:/  /            /:/  /
//       /:/  /     \/__/         \/__/            /:/  /
//      /:/  /                                    /:/  /
//      \/__/                                     \/__/
//
// Copyright (c) 2023, Robert Swinford <robert.swinford<...at...>gmail.com>
//
// For the full copyright and license information, please view the LICENSE file
// that was distributed with this source code.

// this module is a re-implementation of the diff_copy() method, as used by the lms crate,
// which served as a basis as to how to implement.
//
// see original: https://github.com/wchang22/LuminS/blob/9efedd6f20c74aa75261e51ac1c95ee883f7e65b/src/lumins/file_ops.rs#L63
//
// though I am fairly certain this implementation is fair use, I've reproduced his license,
// as of 3/30/2023, verbatim below:

// Copyright (c) 2019 Wesley Chang

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::config::generate::{ExecMode, InteractiveMode};
use crate::library::file_ops::is_same_file_contents;
use crate::library::results::{HttmError, HttmResult};
use crate::zfs::run_command::RunZFSCommand;
use crate::{GLOBAL_CONFIG, IN_BUFFER_SIZE};
use indicatif::{ProgressBar, ProgressStyle};
use std::borrow::Cow;
use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader, BufWriter, ErrorKind, Seek, SeekFrom, Write};
use std::os::fd::{AsFd, BorrowedFd};
use std::path::Path;
use std::sync::LazyLock;
use std::sync::atomic::AtomicBool;

static IS_CLONE_COMPATIBLE: LazyLock<AtomicBool> = LazyLock::new(|| {
    let Ok(zfs_cmd) = RunZFSCommand::new() else {
        return AtomicBool::new(false);
    };

    match zfs_cmd.version() {
        Err(_) => return AtomicBool::new(false),
        Ok(stdout)
            if stdout.contains("zfs-2.2.0")
                || stdout.contains("zfs-kmod-2.2.0")
                || stdout.contains("zfs-2.2.1")
                || stdout.contains("zfs-kmod-2.2.1")
                || stdout.contains("zfs-2.2-")
                || stdout.contains("zfs-kmod-2.2-") =>
        {
            return AtomicBool::new(false);
        }
        Ok(_) => return AtomicBool::new(true),
    }
});

enum DstFileState {
    Exists,
    DoesNotExist,
}

impl DstFileState {
    fn exists(dst_file: &File) -> Self {
        if dst_file.metadata().is_ok() {
            DstFileState::Exists
        } else {
            DstFileState::DoesNotExist
        }
    }
}

pub struct HttmCopy;

impl HttmCopy {
    pub fn new(src: &Path, dst: &Path) -> HttmResult<()> {
        // create source file reader
        let src_file = std::fs::OpenOptions::new().read(true).open(src)?;
        let src_len = src.symlink_metadata()?.len();

        let mut dst_file = OpenOptions::new()
            .write(true)
            .read(true)
            .create(true)
            .open(dst)?;

        dst_file.set_len(src_len)?;

        let file_name = src.file_name().unwrap_or_default().to_string_lossy();

        let opt_bar = Self::opt_bar(file_name, src_len)?;

        if !GLOBAL_CONFIG.opt_no_clones
            && IS_CLONE_COMPATIBLE.load(std::sync::atomic::Ordering::Relaxed)
        {
            match CloneCopy::new(&src_file, &mut dst_file, opt_bar.as_ref()) {
                Ok(_) => {
                    if GLOBAL_CONFIG.opt_debug {
                        eprintln!("DEBUG: copy_file_range call successful.");
                    }

                    return Ok(());
                }
                Err(err) => {
                    IS_CLONE_COMPATIBLE.store(false, std::sync::atomic::Ordering::Relaxed);
                    if GLOBAL_CONFIG.opt_debug {
                        if GLOBAL_CONFIG.opt_debug {
                            eprintln!(
                                "DEBUG: copy_file_range call unsuccessful for the following reason: \"{:?}\".\n
                                DEBUG: Retrying a conventional diff copy.",
                                err
                            );
                        }
                    }
                }
            }
        }

        DiffCopy::new(&src_file, &mut dst_file, opt_bar.as_ref())?;

        if GLOBAL_CONFIG.opt_debug {
            eprintln!("DEBUG: Write to file completed.  Confirmation initiated.");
            Self::confirm(src, dst)?;
        }

        Ok(())
    }

    fn opt_bar(file_name: Cow<str>, len: u64) -> HttmResult<Option<ProgressBar>> {
        match GLOBAL_CONFIG.exec_mode {
            ExecMode::Interactive(InteractiveMode::Restore(_)) => {
                let bar = ProgressBar::new(len).with_style(ProgressStyle::with_template(
                    "[{decimal_total_bytes}] {bar:40.cyan/blue} {msg}",
                )?);

                bar.set_message(file_name.to_string());
                Ok(Some(bar))
            }
            _ if len.gt(&1_000_000_000) => {
                let bar = ProgressBar::new(len).with_style(ProgressStyle::with_template(
                    "[{decimal_total_bytes}] {bar:40.cyan/blue} {msg}",
                )?);

                bar.set_message(file_name.to_string());
                Ok(Some(bar))
            }
            _ => Ok(None),
        }
    }

    pub fn confirm(src: &Path, dst: &Path) -> HttmResult<()> {
        if is_same_file_contents(src, dst) {
            Ok(())
        } else {
            let description = format!(
                "Copy failed.  File contents of {} and {} are NOT the same.",
                src.display(),
                dst.display()
            );

            HttmError::from(description).into()
        }
    }
}

pub struct CloneCopy;

impl CloneCopy {
    fn new(src_file: &File, dst_file: &mut File, opt_bar: Option<&ProgressBar>) -> HttmResult<()> {
        let src_len = src_file.metadata()?.len();

        let src_fd = src_file.as_fd();
        let dst_fd = dst_file.as_fd();

        if let Err(err) = Self::copy_file_range(src_fd, dst_fd, src_len, opt_bar) {
            // IS_CLONE_COMPATIBLE.store(false, std::sync::atomic::Ordering::Relaxed);
            let description =
                format!("DEBUG: copy_file_range call unsuccessful for the following reason");
            return HttmError::with_source(description, err.as_ref()).into();
        }

        // re docs, both a flush and a sync seem to be required re consistency
        dst_file.flush()?;
        dst_file.sync_data()?;

        Ok(())
    }

    #[allow(unreachable_code, unused_variables)]
    #[inline]
    fn copy_file_range(
        src_file_fd: BorrowedFd,
        dst_file_fd: BorrowedFd,
        len: u64,
        opt_bar: Option<&ProgressBar>,
    ) -> HttmResult<()> {
        #[cfg(any(target_os = "linux", target_os = "freebsd"))]
        {
            let mut amt_written = 0u64;
            let mut remainder = len as usize;

            while remainder > 0 {
                let mut off_src = amt_written as i64;
                let mut off_dst = off_src.clone();

                match nix::fcntl::copy_file_range(
                    src_file_fd,
                    Some(&mut off_src),
                    dst_file_fd,
                    Some(&mut off_dst),
                    remainder,
                ) {
                    // a return of zero for a non-zero len argument
                    // indicates that the offset for infd is at or beyond EOF.
                    Ok(bytes_written) if bytes_written == 0 && remainder != 0 => break,
                    Ok(bytes_written) => {
                        amt_written += bytes_written as u64;
                        remainder = len.saturating_sub(amt_written) as usize;

                        if let Some(ref bar) = opt_bar {
                            bar.inc(bytes_written as u64)
                        }

                        if amt_written > len {
                            return Err(
                                HttmError::new("Amount written larger than file len.").into()
                            );
                        }
                    }
                    Err(err) => match err {
                        nix::errno::Errno::ENOSYS => {
                            return HttmError::new(
                                "Operating system does not support copy_file_ranges.",
                            )
                            .into();
                        }
                        _ => {
                            if GLOBAL_CONFIG.opt_debug {
                                eprintln!(
                                    "DEBUG: copy_file_range call failed for the following reason: {}\nDEBUG: Falling back to default diff copy behavior.",
                                    err
                                );
                            }

                            return Err(Box::new(err));
                        }
                    },
                }
            }

            if let Some(ref bar) = opt_bar {
                bar.finish_and_clear()
            }

            return Ok(());
        }

        #[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
        HttmError::new("Operating system does not support copy_file_ranges.").into()
    }
}

pub struct DiffCopy;

impl DiffCopy {
    fn new(src_file: &File, dst_file: &mut File, opt_bar: Option<&ProgressBar>) -> HttmResult<()> {
        Self::write_no_cow(&src_file, &dst_file, opt_bar)?;

        // re docs, both a flush and a sync seem to be required re consistency
        dst_file.flush()?;
        dst_file.sync_data()?;

        Ok(())
    }

    #[inline]
    fn write_no_cow(
        src_file: &File,
        dst_file: &File,
        opt_bar: Option<&ProgressBar>,
    ) -> HttmResult<()> {
        // create destination file writer and maybe reader
        // only include dst file reader if the dst file exists
        // otherwise we just write to that location
        let mut src_reader = BufReader::with_capacity(IN_BUFFER_SIZE, src_file);
        let mut dst_reader = BufReader::with_capacity(IN_BUFFER_SIZE, dst_file);
        let mut dst_writer = BufWriter::with_capacity(IN_BUFFER_SIZE, dst_file);

        let dst_exists = DstFileState::exists(dst_file);

        // cur pos - byte offset in file,
        let mut cur_pos = 0u64;

        loop {
            match src_reader.fill_buf() {
                Ok(src_read) => {
                    // read (size of buffer amt) from src, and dst if it exists
                    let src_amt_read = src_read.len();

                    if src_amt_read == 0 {
                        break;
                    }

                    match dst_exists {
                        DstFileState::DoesNotExist => {
                            Self::write_to_offset(&mut dst_writer, src_read, cur_pos)?;
                        }
                        DstFileState::Exists => {
                            // read same amt from dst file, if it exists, to compare
                            match dst_reader.fill_buf() {
                                Ok(dst_read) => {
                                    if !Self::is_same_bytes(src_read, dst_read) {
                                        Self::write_to_offset(&mut dst_writer, src_read, cur_pos)?
                                    }

                                    let dst_amt_read = dst_read.len();
                                    dst_reader.consume(dst_amt_read);
                                }
                                Err(err) => match err.kind() {
                                    ErrorKind::Interrupted => continue,
                                    ErrorKind::UnexpectedEof => {
                                        break;
                                    }
                                    _ => return Err(err.into()),
                                },
                            }
                        }
                    };

                    if let Some(ref bar) = opt_bar {
                        bar.inc(src_amt_read as u64)
                    }

                    cur_pos += src_amt_read as u64;

                    src_reader.consume(src_amt_read);
                }
                Err(err) => match err.kind() {
                    ErrorKind::Interrupted => continue,
                    ErrorKind::UnexpectedEof => {
                        break;
                    }
                    _ => return Err(err.into()),
                },
            };
        }

        if let Some(ref bar) = opt_bar {
            bar.finish_and_clear();
        }

        Ok(())
    }

    #[inline]
    fn is_same_bytes(a_bytes: &[u8], b_bytes: &[u8]) -> bool {
        let (a_hash, b_hash): (u64, u64) =
            rayon::join(|| Self::hash(a_bytes), || Self::hash(b_bytes));

        a_hash == b_hash
    }

    #[inline]
    fn hash(bytes: &[u8]) -> u64 {
        use foldhash::quality::FixedState;
        use std::hash::{BuildHasher, Hasher};

        let s = FixedState::default();
        let mut hash = s.build_hasher();

        hash.write(bytes);
        hash.finish()
    }

    #[inline]
    fn write_to_offset(
        dst_writer: &mut BufWriter<&File>,
        src_read: &[u8],
        cur_pos: u64,
    ) -> HttmResult<()> {
        // seek to current byte offset in dst writer
        dst_writer.seek(SeekFrom::Start(cur_pos))?;
        dst_writer.write_all(src_read)?;

        Ok(())
    }
}