updatehashdb 0.1.0

Update an index of the hashes of all files
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
411
412
413
414
415
416
417
/*
    updatehashdb – Update an index of the hashes of all files
    Copyright (C) 2023  Matthias Kaak

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

//! # Updatehashdb
//!
//! `updatehashdb` is part of the
//! [hashfindutils](https://codeberg.org/zvavybir/hashfindutils)
//! suite.  See there for more information about the hashfindutils in
//! general and about `updatehashdb` in particular.

#![feature(file_create_new)]
#![warn(
    clippy::all,
    clippy::pedantic,
    clippy::nursery,
    clippy::cargo_common_metadata
)]
// Anachronism
#![allow(clippy::non_ascii_literal)]
// More or less manual checked and documentation agrees with me that
// it's usually not needed.
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    clippy::cast_lossless
)]
// Explicitly decided against; I think `let _ = …` is better than
// `mem::drop(…)`. TODO: align my opinion and community's one with
// each other.
#![allow(let_underscore_drop)]

use std::{
    env::set_var,
    fs::{create_dir_all, read_to_string, remove_file, rename, File},
    io::{self, BufWriter, ErrorKind, Read, Write},
    os::{
        fd::{FromRawFd, IntoRawFd},
        unix::prelude::OsStrExt,
    },
    path::Path,
    process,
    thread::{self, sleep, JoinHandle},
    time::{Duration, Instant},
};

use anyhow::{anyhow, Context as AnyhowContext, Error};
use clap::Parser;
use libc::{c_int, fcntl, F_GETFL, F_SETFL, O_NONBLOCK};
use libhashfindutils::parser::UpdateDbConfig;
use ring::digest::{Context, SHA256};
use signal_hook::{
    consts::{SIGINT, SIGTERM},
    iterator::Signals,
    low_level::signal_name,
};
use walkdir::WalkDir;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli
{
    /// Only index all files once
    #[arg(short, long)]
    once: bool,
}

struct Writer
{
    sha256: Vec<BufWriter<File>>,
    ids: BufWriter<File>,
    next_id: u64,
    block_size: usize,
}

impl Writer
{
    fn new(config: &UpdateDbConfig) -> Result<Self, Error>
    {
        let mut sha256 = vec![];

        let mut ids = BufWriter::new(
            File::create(config.db_path.join("ids.new"))
                .context("Couldn't create the database master file")?,
        );

        ids.write_all(&[0, 0, 0, 0])
            .context("Couldn't declare database version")?;

        for i in 0..=255
        {
            sha256.push(BufWriter::new(
                File::create(config.db_path.join(format!("{i:03}.sha256.new")))
                    .with_context(|| format!("Couldn't create the {i}th hash file"))?,
            ));
        }

        Ok(Self {
            sha256,
            ids,
            next_id: 0,
            block_size: 4096,
        })
    }

    fn handle_file(&mut self, path: &Path) -> Result<(), Error>
    {
        let file = match File::open(path)
        {
            Ok(file) => file,
            Err(e) =>
            {
                println!("Error with opening file {path:?} to hash it: {e}");
                return Ok(());
            }
        };
        let mut file = match make_file_non_blocking(file)
        {
            Ok(file) => file,
            Err(e) =>
            {
                println!("Error with marking file {path:?} as non-blocking: {e}");
                return Ok(());
            }
        };

        let mut hash = Context::new(&SHA256);
        let mut buf = vec![0; self.block_size];

        let mut last_time: Option<Instant> = None;

        loop
        {
            match file.read(&mut buf)
            {
                Ok(0) => break,
                Ok(n) =>
                {
                    last_time = None;
                    hash.update(&buf[..n]);
                }
                Err(e) =>
                {
                    if e.kind() == ErrorKind::WouldBlock
                    {
                        if let Some(time) = &last_time
                        {
                            if time.elapsed().as_secs() > 5
                            {
                                println!(
                                    "Error with reading file {path:?} to hash it: It is blocking"
                                );
                                return Ok(());
                            }
                        }
                        else
                        {
                            last_time = Some(Instant::now());
                        }
                        sleep(Duration::from_millis(1));
                        continue;
                    }

                    println!("Error with reading file {path:?} to hash it: {e}");
                    return Ok(());
                }
            }
        }

        let hash = hash.finish();
        let hash = hash.as_ref();

        self.ids
            .write_all(&self.next_id.to_ne_bytes())
            .context("Couldn't write file ID to the database master file")?;
        self.ids
            .write_all(path.as_os_str().as_bytes())
            .with_context(|| {
                format!("Couldn't write the path to the database master file: {path:?}")
            })?;
        self.ids
            .write_all(&[0])
            .context("Couldn't write file name seperator to the database master file")?;

        let file = &mut self.sha256[hash[0] as usize];
        file.write_all(&self.next_id.to_ne_bytes())
            .with_context(|| format!("Couldn't write file id to {}th hash file", hash[0]))?;
        file.write_all(&hash[1..])
            .with_context(|| format!("Couldn't write hash to {} hash file", hash[0]))?;

        self.next_id += 1;

        Ok(())
    }
}

trait GetLast
{
    type MutOutput;

    fn last_mut(&mut self) -> &mut Self::MutOutput;
}

impl<T> GetLast for Vec<T>
{
    type MutOutput = T;

    fn last_mut(&mut self) -> &mut Self::MutOutput
    {
        let len = self.len();
        &mut self[len - 1]
    }
}

// This function makes a `File` non blocking.  This is only possible
// with `unsafe`, so special care has to be taken.
fn make_file_non_blocking(file: File) -> Result<File, Error>
{
    let fd: c_int = file.into_raw_fd();

    unsafe {
        // SAFETY: This is safe according to fcntl(2) and common
        // practice.
        let flags = fcntl(fd, F_GETFL, 0);
        if flags == -1
        {
            return Err(io::Error::last_os_error()).context("Couldn't get file flags");
        }
        let rv = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
        if rv == -1
        {
            return Err(io::Error::last_os_error()).context("Couldn't set file flags");
        }

        // SAFETY: This is safe since the file descriptor was created
        // from a `File` and so is valid and open.  This is the
        // condition that the documentation names.
        Ok(File::from_raw_fd(fd))
    }
}

fn get_lock(config: &UpdateDbConfig) -> Result<(), Error>
{
    let mut lock =
        File::create_new(config.db_path.join("lock")).context("Couldn't create new lock file")?;
    write!(lock, "{}", process::id()).context("Couldn't write pid to lock file")?;
    drop(lock);

    if read_to_string(config.db_path.join("lock"))
        .context("Couldn't read lock file to checked against race conditions")?
        != format!("{}", process::id())
    {
        return Err(anyhow!("Race condition in acquiring lock file"));
    }

    Ok(())
}

fn release_lock(config: &UpdateDbConfig) -> Result<(), Error>
{
    remove_file(config.db_path.join("lock")).context("Couldn't delete lock file")?;

    Ok(())
}

fn is_path_excluded(path: &Path, config: &UpdateDbConfig) -> bool
{
    config
        .exclude_paths
        .iter()
        .any(|excluded| path.starts_with(excluded))
}

fn traverse_file_system(
    fds: &mut Writer,
    config: &UpdateDbConfig,
    sig_thread: &JoinHandle<Result<i32, Error>>,
) -> Result<(), Error>
{
    macro_rules! try_ {
        ($val: expr) => {
            match $val
            {
                Ok(val) => val,
                Err(e) =>
                {
                    println!("Error traversing file system: {e}");
                    continue;
                }
            }
        };
    }

    for file in config.search_paths.iter().flat_map(|search_path| {
        WalkDir::new(search_path)
            .sort_by_file_name()
            .into_iter()
            .filter_entry(|entry| !is_path_excluded(entry.path(), config))
    })
    {
        if sig_thread.is_finished()
        {
            return Ok(());
        }

        let file = try_!(file);

        if try_!(file.metadata()).is_file()
        {
            fds.handle_file(file.path())
                .with_context(|| format!("Couldn't handle file: {:?}", file.path()))?;
        }
        thread::sleep(Duration::from_micros(1));
    }

    Ok(())
}

fn handle_sig_thread(
    sig_thread: JoinHandle<Result<i32, Error>>,
    config: &UpdateDbConfig,
) -> Result<(), Error>
{
    match sig_thread.join()
    {
        Ok(Ok(sig)) =>
        {
            eprintln!(
                "Stopping program due to signal: {}",
                signal_name(sig).map_or_else(
                    || format!("Unknown signal with ID {sig}"),
                    ToOwned::to_owned
                )
            );
            release_lock(config).context("Couldn't release the lock; please do this manually")?;

            Ok(())
        }
        Ok(Err(e)) => Err(e).context("Problems with capturing signals, stopping..."),
        Err(e) => Err(anyhow!("Problems with signal capturing thread: {e:?}")),
    }
}

fn main() -> Result<(), Error>
{
    let should_once = Cli::parse().once;

    set_var("RUST_BACKTRACE", "full");

    let config = UpdateDbConfig::new().context("Couldn't read the configuration")?;

    create_dir_all(&config.db_path).context("Couldn't create necessary directories")?;

    get_lock(&config).context("Couldn't acquire lock")?;

    let sig_thread = thread::spawn(|| {
        let mut sigs = Signals::new([SIGINT, SIGTERM]).context("Couldn't capture signals")?;
        sigs.forever()
            .next()
            .ok_or_else(|| anyhow!("Capturing of signals was wrongly stopped."))
    });

    let mut has_once = false;

    while !(should_once && has_once)
    {
        let mut fds = Writer::new(&config).context("Couldn't open all database files")?;

        if sig_thread.is_finished()
        {
            return handle_sig_thread(sig_thread, &config);
        }

        traverse_file_system(&mut fds, &config, &sig_thread)
            .context("Problem hashing all files")?;

        if sig_thread.is_finished()
        {
            return handle_sig_thread(sig_thread, &config);
        }

        drop(fds);
        for i in 0..=255
        {
            rename(
                config.db_path.join(format!("{i:03}.sha256.new")),
                config.db_path.join(format!("{i:03}.sha256")),
            )
            .with_context(|| format!("Couldn't update the {i}th hash file"))?;
        }
        rename(config.db_path.join("ids.new"), config.db_path.join("ids"))
            .context("Couldn't update the database master file")?;

        thread::sleep(Duration::from_secs(1));

        has_once = true;
    }

    release_lock(&config).context("Couldn't release lock")?;

    Ok(())
}