cxx_build/gen/
fs.rs

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
#![allow(dead_code)]

use std::error::Error as StdError;
use std::fmt::{self, Display};
use std::io::{self, Read};
use std::path::{Path, PathBuf};

pub(crate) type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
pub(crate) struct Error {
    source: Option<io::Error>,
    message: String,
}

impl Error {
    pub(crate) fn kind(&self) -> io::ErrorKind {
        match &self.source {
            Some(io_error) => io_error.kind(),
            None => io::ErrorKind::Other,
        }
    }
}

impl Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        let source = self.source.as_ref()?;
        Some(source)
    }
}

macro_rules! err {
    ($io_error:expr, $fmt:expr $(, $path:expr)* $(,)?) => {
        Err(Error {
            source: Option::from($io_error),
            message: format!($fmt $(, $path.display())*),
        })
    }
}

pub(crate) fn copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<u64> {
    let from = from.as_ref();
    let to = to.as_ref();
    match std::fs::copy(from, to) {
        Ok(n) => Ok(n),
        Err(e) => err!(e, "Failed to copy `{}` -> `{}`", from, to),
    }
}

pub(crate) fn create_dir_all(path: impl AsRef<Path>) -> Result<()> {
    let path = path.as_ref();
    match std::fs::create_dir_all(path) {
        Ok(()) => Ok(()),
        Err(e) => err!(e, "Failed to create directory `{}`", path),
    }
}

pub(crate) fn current_dir() -> Result<PathBuf> {
    match std::env::current_dir() {
        Ok(dir) => Ok(dir),
        Err(e) => err!(e, "Failed to determine current directory"),
    }
}

pub(crate) fn exists(path: impl AsRef<Path>) -> bool {
    let path = path.as_ref();
    // If path is a symlink, this returns true, regardless of whether the
    // symlink points to a path that exists.
    std::fs::symlink_metadata(path).is_ok()
}

pub(crate) fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
    let path = path.as_ref();
    match std::fs::read(path) {
        Ok(string) => Ok(string),
        Err(e) => err!(e, "Failed to read file `{}`", path),
    }
}

pub(crate) fn read_stdin() -> Result<Vec<u8>> {
    let mut bytes = Vec::new();
    match io::stdin().read_to_end(&mut bytes) {
        Ok(_len) => Ok(bytes),
        Err(e) => err!(e, "Failed to read input from stdin"),
    }
}

pub(crate) fn remove_file(path: impl AsRef<Path>) -> Result<()> {
    let path = path.as_ref();
    match std::fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(e) => err!(e, "Failed to remove file `{}`", path),
    }
}

pub(crate) fn remove_dir(path: impl AsRef<Path>) -> Result<()> {
    let path = path.as_ref();
    match std::fs::remove_dir(path) {
        Ok(()) => Ok(()),
        Err(e) => err!(e, "Failed to remove directory `{}`", path),
    }
}

fn symlink<'a>(
    original: &'a Path,
    link: &'a Path,
    fun: fn(&'a Path, &'a Path) -> io::Result<()>,
) -> Result<()> {
    match fun(original, link) {
        Ok(()) => Ok(()),
        Err(e) => err!(
            e,
            "Failed to create symlink `{}` pointing to `{}`",
            link,
            original,
        ),
    }
}

pub(crate) fn symlink_fail(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
    err!(
        None,
        "Failed to create symlink `{}` pointing to `{}`",
        link.as_ref(),
        original.as_ref(),
    )
}

#[cfg(unix)]
#[allow(unused_imports)]
pub(crate) use self::symlink_file as symlink_dir;

#[cfg(not(any(unix, windows)))]
#[allow(unused_imports)]
pub(crate) use self::symlink_fail as symlink_dir;

#[cfg(unix)]
pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
    symlink(original.as_ref(), link.as_ref(), std::os::unix::fs::symlink)
}

#[cfg(windows)]
pub(crate) fn symlink_file(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
    symlink(
        original.as_ref(),
        link.as_ref(),
        std::os::windows::fs::symlink_file,
    )
}

#[cfg(windows)]
pub(crate) fn symlink_dir(original: impl AsRef<Path>, link: impl AsRef<Path>) -> Result<()> {
    symlink(
        original.as_ref(),
        link.as_ref(),
        std::os::windows::fs::symlink_dir,
    )
}

pub(crate) fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
    let path = path.as_ref();
    match std::fs::write(path, contents) {
        Ok(()) => Ok(()),
        Err(e) => err!(e, "Failed to write file `{}`", path),
    }
}