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
// This file is part of Gear.
//
// Copyright (C) 2023-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// 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/>.

//! Filesystem functions that avoid file changes
//! so we can avoid unnecessary build script launches.
//! because cargo looks for `mtime` metadata file parameter

use anyhow::Result;
use gmeta::MetadataRepr;
use std::{fs, io::ErrorKind, path::Path};

pub(crate) fn copy_if_newer(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<bool> {
    let from = from.as_ref();
    let to = to.as_ref();

    if check_if_newer(from, to)? {
        fs::copy(from, to)?;
        Ok(true)
    } else {
        Ok(false)
    }
}

pub(crate) fn check_if_newer(left: impl AsRef<Path>, right: impl AsRef<Path>) -> Result<bool> {
    let right_metadata = fs::metadata(right);
    if let Err(io_error) = right_metadata.as_ref() {
        if io_error.kind() == ErrorKind::NotFound {
            return Ok(true);
        }
    }
    let right_metadata = right_metadata.unwrap();
    let left_metadata = fs::metadata(left)?;
    Ok(left_metadata.modified()? > right_metadata.modified()?)
}

fn check_changed(path: &Path, contents: &[u8]) -> Result<bool> {
    // file does not exist
    let Ok(metadata) = fs::metadata(path) else {
        return Ok(true);
    };

    if metadata.len() != contents.len() as u64 {
        return Ok(true);
    }

    let old_contents = fs::read(path)?;
    Ok(old_contents != contents)
}

pub(crate) fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> {
    let path = path.as_ref();
    let contents = contents.as_ref();

    if check_changed(path, contents)? {
        fs::write(path, contents)?;
    }

    Ok(())
}

fn check_metadata_changed(path: &Path, metadata: &MetadataRepr) -> Result<bool> {
    if !path.exists() {
        return Ok(true);
    }

    let old_metadata = fs::read(path)?;
    let Ok(old_metadata) = MetadataRepr::from_hex(old_metadata) else {
        return Ok(true);
    };

    Ok(old_metadata != *metadata)
}

pub(crate) fn write_metadata<P: AsRef<Path>>(path: P, metadata: &MetadataRepr) -> Result<()> {
    let path = path.as_ref();

    if check_metadata_changed(path, metadata)? {
        fs::write(path, metadata.hex())?;
    }

    Ok(())
}