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
use crate::entities::{ImmutableFileName, ImmutableFileNumber};

use crate::digesters::ImmutableFileListingError::MissingImmutableFolder;
use digest::{Digest, Output};
use std::{
    cmp::Ordering,
    fs::File,
    io,
    num::ParseIntError,
    path::{Path, PathBuf},
};
use thiserror::Error;
use walkdir::WalkDir;

const IMMUTABLE_FILE_EXTENSIONS: [&str; 3] = ["chunk", "primary", "secondary"];

fn is_immutable(entry: &walkdir::DirEntry) -> bool {
    let is_file = entry.file_type().is_file();
    let extension = entry.path().extension().map(|e| e.to_string_lossy());

    is_file && extension.is_some_and(|e| IMMUTABLE_FILE_EXTENSIONS.contains(&e.as_ref()))
}

/// Walk the given path and return the first directory named "immutable" it finds
fn find_immutables_dir(path_to_walk: &Path) -> Option<PathBuf> {
    WalkDir::new(path_to_walk)
        .into_iter()
        .filter_entry(|e| e.file_type().is_dir())
        .filter_map(|e| e.ok())
        .find(|f| f.file_name() == "immutable")
        .map(|e| e.into_path())
}

/// Represent an immutable file in a Cardano node database directory
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ImmutableFile {
    /// The path to the immutable file
    pub path: PathBuf,

    /// The immutable file number
    pub number: ImmutableFileNumber,

    /// The filename
    pub filename: ImmutableFileName,
}

/// [ImmutableFile::new] related errors.
#[derive(Error, Debug)]
pub enum ImmutableFileCreationError {
    /// Raised when the immutable file stem extraction fails.
    #[error("Couldn't extract the file stem for '{path:?}'")]
    FileStemExtraction {
        /// Path for which file stem extraction failed.
        path: PathBuf,
    },

    /// Raised when the immutable file filename extraction fails.
    #[error("Couldn't extract the filename as string for '{path:?}'")]
    FileNameExtraction {
        /// Path for which filename extraction failed.
        path: PathBuf,
    },

    /// Raised when the immutable file number parsing, from the filename, fails.
    #[error("Error while parsing immutable file number")]
    FileNumberParsing(#[from] ParseIntError),
}

/// [ImmutableFile::list_completed_in_dir] related errors.
#[derive(Error, Debug)]
pub enum ImmutableFileListingError {
    /// Raised when the metadata of a file could not be read.
    #[error("metadata parsing failed")]
    MetadataParsing(#[from] io::Error),

    /// Raised when [ImmutableFile::new] fails.
    #[error("immutable file creation error")]
    ImmutableFileCreation(#[from] ImmutableFileCreationError),

    /// Raised when the "immutable" folder could not be found in a file structure.
    #[error("Couldn't find the 'immutable' folder in '{0:?}'")]
    MissingImmutableFolder(PathBuf),
}

impl ImmutableFile {
    /// ImmutableFile factory
    pub fn new(path: PathBuf) -> Result<ImmutableFile, ImmutableFileCreationError> {
        let filename = path
            .file_name()
            .ok_or(ImmutableFileCreationError::FileNameExtraction { path: path.clone() })?
            .to_str()
            .ok_or(ImmutableFileCreationError::FileNameExtraction { path: path.clone() })?
            .to_string();

        let filestem = path
            .file_stem()
            .ok_or(ImmutableFileCreationError::FileStemExtraction { path: path.clone() })?
            .to_str()
            .ok_or(ImmutableFileCreationError::FileNameExtraction { path: path.clone() })?;
        let immutable_file_number = filestem.parse::<ImmutableFileNumber>()?;

        Ok(Self {
            path,
            number: immutable_file_number,
            filename,
        })
    }

    /// ImmutableFile factory, TEST ONLY as it bypass the checks done by [ImmutableFile::new].
    #[cfg(test)]
    pub(crate) fn dummy(path: PathBuf, number: ImmutableFileNumber, filename: String) -> Self {
        Self {
            path,
            number,
            filename,
        }
    }

    /// Compute the hash of this immutable file.
    pub fn compute_raw_hash<D>(&self) -> Result<Output<D>, io::Error>
    where
        D: Digest + io::Write,
    {
        let mut hasher = D::new();
        let mut file = File::open(&self.path)?;
        io::copy(&mut file, &mut hasher)?;
        Ok(hasher.finalize())
    }

    /// List all [`ImmutableFile`] in a given directory.
    ///
    /// Important Note: It will skip the last chunk / primary / secondary trio since they're not yet
    /// complete.
    pub fn list_completed_in_dir(
        dir: &Path,
    ) -> Result<Vec<ImmutableFile>, ImmutableFileListingError> {
        let immutable_dir =
            find_immutables_dir(dir).ok_or(MissingImmutableFolder(dir.to_path_buf()))?;
        let mut files: Vec<ImmutableFile> = vec![];

        for path in WalkDir::new(immutable_dir)
            .min_depth(1)
            .max_depth(1)
            .into_iter()
            .filter_entry(is_immutable)
            .filter_map(|file| file.ok())
        {
            let immutable_file = ImmutableFile::new(path.into_path())?;
            files.push(immutable_file);
        }
        files.sort();

        match files.last() {
            // empty list
            None => Ok(files),
            // filter out the last immutable file(s)
            Some(last_file) => {
                let last_number = last_file.number;
                Ok(files
                    .into_iter()
                    .filter(|f| f.number < last_number)
                    .collect())
            }
        }
    }
}

impl PartialOrd for ImmutableFile {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ImmutableFile {
    fn cmp(&self, other: &Self) -> Ordering {
        self.number
            .cmp(&other.number)
            .then(self.path.cmp(&other.path))
    }
}

#[cfg(test)]
mod tests {
    use super::ImmutableFile;
    use crate::test_utils::TempDir;
    use std::fs::File;
    use std::io::prelude::*;
    use std::path::{Path, PathBuf};

    fn get_test_dir(subdir_name: &str) -> PathBuf {
        TempDir::create("immutable_file", subdir_name)
    }

    fn create_fake_files(parent_dir: &Path, child_filenames: &[&str]) {
        for filename in child_filenames {
            let file = parent_dir.join(Path::new(filename));
            let mut source_file = File::create(file).unwrap();
            write!(source_file, "This is a test file named '{filename}'").unwrap();
        }
    }

    fn extract_filenames(immutables: &[ImmutableFile]) -> Vec<String> {
        immutables
            .iter()
            .map(|i| i.path.file_name().unwrap().to_str().unwrap().to_owned())
            .collect()
    }

    #[test]
    fn list_immutable_file_fail_if_not_in_immutable_dir() {
        let target_dir = get_test_dir("list_immutable_file_fail_if_not_in_immutable_dir/invalid");
        let entries = vec![];
        create_fake_files(&target_dir, &entries);

        ImmutableFile::list_completed_in_dir(target_dir.parent().unwrap())
            .expect_err("ImmutableFile::list_in_dir should have Failed");
    }

    #[test]
    fn list_immutable_file_should_skip_last_number() {
        let target_dir = get_test_dir("list_immutable_file_should_skip_last_number/immutable");
        let entries = vec![
            "123.chunk",
            "123.primary",
            "123.secondary",
            "125.chunk",
            "125.primary",
            "125.secondary",
            "0124.chunk",
            "0124.primary",
            "0124.secondary",
            "223.chunk",
            "223.primary",
            "223.secondary",
            "0423.chunk",
            "0423.primary",
            "0423.secondary",
            "0424.chunk",
            "0424.primary",
            "0424.secondary",
            "21.chunk",
            "21.primary",
            "21.secondary",
        ];
        create_fake_files(&target_dir, &entries);
        let result = ImmutableFile::list_completed_in_dir(target_dir.parent().unwrap())
            .expect("ImmutableFile::list_in_dir Failed");

        assert_eq!(result.last().unwrap().number, 423);
        assert_eq!(
            result.len(),
            entries.len() - 3,
            "Expected to find {} files since The last (chunk, primary, secondary) trio is skipped, but found {}",
            entries.len() - 3,
            result.len(),
        );
    }

    #[test]
    fn list_immutable_file_should_works_in_a_empty_folder() {
        let target_dir =
            get_test_dir("list_immutable_file_should_works_even_in_a_empty_folder/immutable");
        let entries = vec![];
        create_fake_files(&target_dir, &entries);
        let result = ImmutableFile::list_completed_in_dir(target_dir.parent().unwrap())
            .expect("ImmutableFile::list_in_dir Failed");

        assert!(result.is_empty());
    }

    #[test]
    fn immutable_order_should_be_deterministic() {
        let target_dir = get_test_dir("immutable_order_should_be_deterministic/immutable");
        let entries = vec![
            "21.chunk",
            "21.primary",
            "21.secondary",
            "123.chunk",
            "123.primary",
            "123.secondary",
            "124.chunk",
            "124.primary",
            "124.secondary",
            "125.chunk",
            "125.primary",
            "125.secondary",
            "223.chunk",
            "223.primary",
            "223.secondary",
            "423.chunk",
            "423.primary",
            "423.secondary",
            "424.chunk",
            "424.primary",
            "424.secondary",
        ];
        create_fake_files(&target_dir, &entries);
        let immutables = ImmutableFile::list_completed_in_dir(target_dir.parent().unwrap())
            .expect("ImmutableFile::list_in_dir Failed");
        let immutables_names: Vec<String> = extract_filenames(&immutables);

        let expected: Vec<&str> = entries.into_iter().rev().skip(3).rev().collect();
        assert_eq!(expected, immutables_names);
    }

    #[test]
    fn list_immutable_file_should_work_with_non_immutable_files() {
        let target_dir =
            get_test_dir("list_immutable_file_should_work_with_non_immutable_files/immutable");
        let entries = vec![
            "123.chunk",
            "123.primary",
            "123.secondary",
            "124.chunk",
            "124.primary",
            "124.secondary",
            "README.md",
            "124.secondary.back",
        ];
        create_fake_files(&target_dir, &entries);
        let immutables = ImmutableFile::list_completed_in_dir(target_dir.parent().unwrap())
            .expect("ImmutableFile::list_in_dir Failed");
        let immutables_names: Vec<String> = extract_filenames(&immutables);

        let expected: Vec<&str> = entries.into_iter().rev().skip(5).rev().collect();
        assert_eq!(expected, immutables_names);
    }

    #[test]
    fn list_immutable_file_can_list_incomplete_trio() {
        let target_dir = get_test_dir("list_immutable_file_can_list_incomplete_trio/immutable");
        let entries = vec![
            "21.chunk",
            "21.primary",
            "21.secondary",
            "123.chunk",
            "123.secondary",
            "124.chunk",
            "124.primary",
            "125.primary",
            "125.secondary",
            "223.chunk",
            "224.primary",
            "225.secondary",
            "226.chunk",
        ];
        create_fake_files(&target_dir, &entries);
        let immutables = ImmutableFile::list_completed_in_dir(target_dir.parent().unwrap())
            .expect("ImmutableFile::list_in_dir Failed");
        let immutables_names: Vec<String> = extract_filenames(&immutables);

        let expected: Vec<&str> = entries.into_iter().rev().skip(1).rev().collect();
        assert_eq!(expected, immutables_names);
    }
}