ragit_fs/
lib.rs

1#![allow(dead_code)]
2
3mod log;
4
5pub use crate::log::{initialize_log_file, set_log_file_path, write_log};
6
7use std::collections::hash_map;
8use std::ffi::OsString;
9use std::fmt;
10use std::fs::{self, File, OpenOptions};
11use std::hash::{Hash, Hasher};
12use std::io::{self, Read, Seek, SeekFrom, Write};
13use std::path::{Path, PathBuf};
14use std::str::FromStr;
15
16/// ```nohighlight
17///       File Already Exists    File Does not Exist
18///
19///     AA       Append                  Dies
20///    AoC       Append                 Create
21///    CoT      Truncate                Create
22///     AC        Dies                  Create
23/// ```
24///
25/// `Atomic` is like `CreateOrTruncate`, but it tries to be more atomic.
26/// It first creates a tmp file with a different name, then renames the tmp file.
27/// If it fails, it might leave a tmp file. But you'll never have a partially
28/// written file.
29#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
30pub enum WriteMode {
31    AlwaysAppend,
32    AppendOrCreate,
33    CreateOrTruncate,
34    AlwaysCreate,
35    Atomic,
36}
37
38impl From<WriteMode> for OpenOptions {
39    fn from(m: WriteMode) -> OpenOptions {
40        let mut result = OpenOptions::new();
41
42        match m {
43            WriteMode::AlwaysAppend => { result.append(true); },
44            WriteMode::AppendOrCreate => { result.append(true).create(true); },
45            WriteMode::CreateOrTruncate | WriteMode::Atomic => { result.write(true).truncate(true).create(true); },
46            WriteMode::AlwaysCreate => { result.write(true).create_new(true); },
47        }
48
49        result
50    }
51}
52
53/// It never reads more than `to - from` bytes.
54/// If it fails to read from `from`, that's an error.
55/// If it fails to read to `to`, that's not an error.
56pub fn read_bytes_offset(path: &str, from: u64, to: u64) -> Result<Vec<u8>, FileError> {
57    assert!(to >= from);
58
59    match File::open(path) {
60        Err(e) => Err(FileError::from_std(e, path)),
61        Ok(mut f) => match f.seek(SeekFrom::Start(from)) {
62            Err(e) => Err(FileError::from_std(e, path)),
63            Ok(_) => {
64                let mut handle = f.take(to - from);
65                let mut buffer = Vec::with_capacity((to - from) as usize);
66
67                if let Err(e) = handle.read_to_end(&mut buffer) {
68                    return Err(FileError::from_std(e, path));
69                }
70
71                Ok(buffer)
72            },
73        },
74    }
75}
76
77pub fn read_bytes(path: &str) -> Result<Vec<u8>, FileError> {
78    fs::read(path).map_err(|e| FileError::from_std(e, path))
79}
80
81pub fn read_string(path: &str) -> Result<String, FileError> {
82    let mut s = String::new();
83
84    match File::open(path) {
85        Err(e) => Err(FileError::from_std(e, path)),
86        Ok(mut f) => match f.read_to_string(&mut s) {
87            Ok(_) => Ok(s),
88            Err(e) => Err(FileError::from_std(e, path)),
89        }
90    }
91}
92
93pub fn write_bytes(path: &str, bytes: &[u8], write_mode: WriteMode) -> Result<(), FileError> {
94    let option: OpenOptions = write_mode.into();
95
96    if let WriteMode::Atomic = write_mode {
97        // it has to create a unique name in extreme cases (e.g. 1k processes trying to write the same file)
98        // I cannot come up with better idea than this
99        let tmp_path = format!("{path}_tmp__{:x}", rand::random::<u64>());
100
101        match option.open(&tmp_path) {
102            Ok(mut f) => match f.write_all(bytes) {
103                Ok(_) => match rename(&tmp_path, path) {
104                    Ok(_) => Ok(()),
105                    Err(e) => {
106                        remove_file(&tmp_path)?;
107                        Err(e)
108                    },
109                },
110                Err(e) => {
111                    remove_file(&tmp_path)?;
112                    Err(FileError::from_std(e, path))
113                },
114            },
115            Err(e) => Err(FileError::from_std(e, path)),
116        }
117    } else {
118        match option.open(path) {
119            Ok(mut f) => match f.write_all(bytes) {
120                Ok(_) => Ok(()),
121                Err(e) => Err(FileError::from_std(e, path)),
122            },
123            Err(e) => Err(FileError::from_std(e, path)),
124        }
125    }
126}
127
128pub fn write_string(path: &str, s: &str, write_mode: WriteMode) -> Result<(), FileError> {
129    write_bytes(path, s.as_bytes(), write_mode)
130}
131
132/// `a/b/c.d` -> `c`
133pub fn file_name(path: &str) -> Result<String, FileError> {
134    let path_buf = PathBuf::from_str(path).unwrap();  // it's infallible
135
136    match path_buf.file_stem() {
137        None => Ok(String::new()),
138        Some(s) => match s.to_str() {
139            Some(ext) => Ok(ext.to_string()),
140            None => Err(FileError::os_str_err(s.to_os_string())),
141        }
142    }
143}
144
145/// `a/b/c.d` -> `d`
146pub fn extension(path: &str) -> Result<Option<String>, FileError> {
147    let path_buf = PathBuf::from_str(path).unwrap();  // it's infallible
148
149    match path_buf.extension() {
150        None => Ok(None),
151        Some(s) => match s.to_str() {
152            Some(ext) => Ok(Some(ext.to_string())),
153            None => Err(FileError::os_str_err(s.to_os_string())),
154        }
155    }
156}
157
158/// `a/b/c.d` -> `c.d`
159pub fn basename(path: &str) -> Result<String, FileError> {
160    let path_buf = PathBuf::from_str(path).unwrap();  // it's infallible
161
162    match path_buf.file_name() {
163        None => Ok(String::new()),  // when the path terminates in `..`
164        Some(s) => match s.to_str() {
165            Some(ext) => Ok(ext.to_string()),
166            None => Err(FileError::os_str_err(s.to_os_string())),
167        }
168    }
169}
170
171/// `a/b/`, `c.d` -> `a/b/c.d`
172pub fn join(path: &str, child: &str) -> Result<String, FileError> {
173    let mut path_buf = PathBuf::from_str(path).unwrap();  // Infallible
174    let child = PathBuf::from_str(child).unwrap();  // Infallible
175
176    path_buf.push(child);
177
178    match path_buf.to_str() {
179        Some(result) => Ok(result.to_string()),
180        None => Err(FileError::os_str_err(path_buf.into_os_string())),
181    }
182}
183
184pub fn join3(path1: &str, path2: &str, path3: &str) -> Result<String, FileError> {
185    join(
186        path1,
187        &join(path2, path3)?,
188    )
189}
190
191pub fn join4(path1: &str, path2: &str, path3: &str, path4: &str) -> Result<String, FileError> {
192    join(
193        &join(path1, path2)?,
194        &join(path3, path4)?,
195    )
196}
197
198/// `a/b/c.d, e` -> `a/b/c.e`
199pub fn set_extension(path: &str, ext: &str) -> Result<String, FileError> {
200    let mut path_buf = PathBuf::from_str(path).unwrap();  // Infallible
201
202    if path_buf.set_extension(ext) {
203        match path_buf.to_str() {
204            Some(result) => Ok(result.to_string()),
205            None => Err(FileError::os_str_err(path_buf.into_os_string())),
206        }
207    } else {
208        // has no filename
209        Ok(path.to_string())
210    }
211}
212
213/// It returns `false` if `path` doesn't exist
214pub fn is_dir(path: &str) -> bool {
215    PathBuf::from_str(path).map(|path| path.is_dir()).unwrap_or(false)
216}
217
218/// It returns `false` if `path` doesn't exist
219pub fn is_symlink(path: &str) -> bool {
220    PathBuf::from_str(path).map(|path| path.is_symlink()).unwrap_or(false)
221}
222
223pub fn exists(path: &str) -> bool {
224    PathBuf::from_str(path).map(|path| path.exists()).unwrap_or(false)
225}
226
227/// `a/b/c.d` -> `a/b/`
228pub fn parent(path: &str) -> Result<String, FileError> {
229    let std_path = Path::new(path);
230
231    std_path.parent().map(
232        |p| p.to_string_lossy().to_string()
233    ).ok_or_else(
234        || FileError::unknown(
235            String::from("function `parent` died"),
236            Some(path.to_string()),
237        )
238    )
239}
240
241/// It's like `create_dir` but does not raise an error if `path` already exists
242pub fn try_create_dir(path: &str) -> Result<(), FileError> {
243    match fs::create_dir(path) {
244        Ok(()) => Ok(()),
245        Err(e) => match e.kind() {
246            io::ErrorKind::AlreadyExists => Ok(()),
247            _ => Err(FileError::from_std(e, path)),
248        },
249    }
250}
251
252pub fn create_dir(path: &str) -> Result<(), FileError> {
253    fs::create_dir(path).map_err(|e| FileError::from_std(e, path))
254}
255
256pub fn create_dir_all(path: &str) -> Result<(), FileError> {
257    fs::create_dir_all(path).map_err(|e| FileError::from_std(e, path))
258}
259
260pub fn rename(from: &str, to: &str) -> Result<(), FileError> {
261    fs::rename(from, to).map_err(|e| FileError::from_std(e, from))
262}
263
264pub fn copy_dir(src: &str, dst: &str) -> Result<(), FileError> {
265    create_dir_all(dst)?;
266
267    // TODO: how about links?
268    for e in read_dir(src, false)? {
269        let new_dst = join(dst, &basename(&e)?)?;
270
271        if is_dir(&e) {
272            create_dir_all(&new_dst)?;
273            copy_dir(&e, &new_dst)?;
274        }
275
276        else {
277            copy_file(&e, &new_dst)?;
278        }
279    }
280
281    Ok(())
282}
283
284/// It returns the total number of bytes copied.
285pub fn copy_file(src: &str, dst: &str) -> Result<u64, FileError> {
286    std::fs::copy(src, dst).map_err(|e| FileError::from_std(e, src))  // TODO: how about dst?
287}
288
289// it only returns the hash value of the modified time
290pub fn last_modified(path: &str) -> Result<u64, FileError> {
291    match fs::metadata(path) {
292        Ok(m) => match m.modified() {
293            Ok(m) => {
294                let mut hasher = hash_map::DefaultHasher::new();
295                m.hash(&mut hasher);
296                let hash = hasher.finish();
297
298                Ok(hash)
299            },
300            Err(e) => Err(FileError::from_std(e, path)),
301        },
302        Err(e) => Err(FileError::from_std(e, path)),
303    }
304}
305
306pub fn file_size(path: &str) -> Result<u64, FileError> {
307    match fs::metadata(path) {
308        Ok(m) => Ok(m.len()),
309        Err(e) => Err(FileError::from_std(e, path)),
310    }
311}
312
313pub fn read_dir(path: &str, sort: bool) -> Result<Vec<String>, FileError> {
314    match fs::read_dir(path) {
315        Err(e) => Err(FileError::from_std(e, path)),
316        Ok(entries) => {
317            let mut result = vec![];
318
319            for entry in entries {
320                match entry {
321                    Err(e) => {
322                        return Err(FileError::from_std(e, path));
323                    },
324                    Ok(e) => {
325                        if let Some(ee) = e.path().to_str() {
326                            result.push(ee.to_string());
327                        }
328                    },
329                }
330            }
331
332            if sort {
333                result.sort();
334            }
335
336            Ok(result)
337        }
338    }
339}
340
341pub fn remove_file(path: &str) -> Result<(), FileError> {
342    fs::remove_file(path).map_err(|e| FileError::from_std(e, path))
343}
344
345pub fn remove_dir(path: &str) -> Result<(), FileError> {
346    fs::remove_dir(path).map_err(|e| FileError::from_std(e, path))
347}
348
349pub fn remove_dir_all(path: &str) -> Result<(), FileError> {
350    fs::remove_dir_all(path).map_err(|e| FileError::from_std(e, path))
351}
352
353pub fn into_abs_path(path: &str) -> Result<String, FileError> {
354    let std_path = Path::new(path);
355
356    if std_path.is_absolute() {
357        Ok(path.to_string())
358    }
359
360    else {
361        Ok(join(
362            &current_dir()?,
363            path,
364        )?)
365    }
366}
367
368pub fn current_dir() -> Result<String, FileError> {
369    let cwd = std::env::current_dir().map_err(|e| FileError::from_std(e, "."))?;
370
371    match cwd.to_str() {
372        Some(cwd) => Ok(cwd.to_string()),
373        None => Err(FileError::os_str_err(cwd.into_os_string())),
374    }
375}
376
377pub fn diff(path: &str, base: &str) -> Result<String, FileError> {
378    match pathdiff::diff_paths(path, base) {
379        Some(path) => match path.to_str() {
380            Some(path) => Ok(path.to_string()),
381            None => Err(FileError::os_str_err(path.into_os_string())),
382        },
383        None => Err(FileError::cannot_diff_path(path.to_string(), base.to_string())),
384    }
385}
386
387/// It calcs diff and normalizes.
388pub fn get_relative_path(base: &str, path: &str) -> Result<String, FileError> {
389    // It has to normalize the output because `diff` behaves differently on windows and unix.
390    Ok(normalize(&diff(
391        // in order to calc diff, it needs a full path
392        &normalize(
393            &into_abs_path(path)?,
394        )?,
395        &normalize(
396            &into_abs_path(base)?,
397        )?,
398    )?)?)
399}
400
401pub fn normalize(path: &str) -> Result<String, FileError> {
402    let mut result = vec![];
403    let path = path.replace("\\", "/");
404
405    for component in path.split("/") {
406        match component {
407            c if c == "." => {},
408            c if c == ".." => if result.is_empty() {
409                result.push(c.to_string());
410            } else {
411                result.pop();
412            },
413            c => { result.push(c.to_string()); },
414        }
415    }
416
417    Ok(result.join("/"))
418}
419
420#[derive(Clone,  PartialEq)]
421pub struct FileError {
422    pub kind: FileErrorKind,
423    pub given_path: Option<String>,
424}
425
426impl FileError {
427    pub fn from_std(e: io::Error, given_path: &str) -> Self {
428        let kind = match e.kind() {
429            io::ErrorKind::NotFound => FileErrorKind::FileNotFound,
430            io::ErrorKind::PermissionDenied => FileErrorKind::PermissionDenied,
431            io::ErrorKind::AlreadyExists => FileErrorKind::AlreadyExists,
432            e => FileErrorKind::Unknown(format!("unknown error: {e:?}")),
433        };
434
435        FileError {
436            kind,
437            given_path: Some(given_path.to_string()),
438        }
439    }
440
441    pub(crate) fn os_str_err(os_str: OsString) -> Self {
442        FileError {
443            kind: FileErrorKind::OsStrErr(os_str),
444            given_path: None,
445        }
446    }
447
448    pub(crate) fn cannot_diff_path(path: String, base: String) -> Self {
449        FileError {
450            kind: FileErrorKind::CannotDiffPath(path.to_string(), base),
451            given_path: Some(path),
452        }
453    }
454
455    pub fn unknown(msg: String, path: Option<String>) -> Self {
456        FileError {
457            kind: FileErrorKind::Unknown(msg),
458            given_path: path,
459        }
460    }
461
462    pub fn render_error(&self) -> String {
463        let path = self.given_path.as_ref().map(|p| p.to_string()).unwrap_or(String::new());
464
465        match &self.kind {
466            FileErrorKind::FileNotFound => format!(
467                "file not found: `{path}`"
468            ),
469            FileErrorKind::PermissionDenied => format!(
470                "permission denied: `{path}`"
471            ),
472            FileErrorKind::AlreadyExists => format!(
473                "file already exists: `{path}`"
474            ),
475            FileErrorKind::CannotDiffPath(path, base) => format!(
476                "cannot calc diff: `{path}` and `{base}`"
477            ),
478            FileErrorKind::Unknown(msg) => format!(
479                "unknown file error: `{msg}`"
480            ),
481            FileErrorKind::OsStrErr(os_str) => format!(
482                "error converting os_str: `{os_str:?}`"
483            ),
484        }
485    }
486}
487
488impl fmt::Debug for FileError {
489    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
490        write!(fmt, "{}", self.render_error())
491    }
492}
493
494impl fmt::Display for FileError {
495    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
496        write!(fmt, "{}", self.render_error())
497    }
498}
499
500#[derive(Clone, Debug, PartialEq)]
501pub enum FileErrorKind {
502    FileNotFound,
503    PermissionDenied,
504    AlreadyExists,
505    CannotDiffPath(String, String),
506    Unknown(String),
507    OsStrErr(OsString),
508}