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
184/// alias for `join`
185#[inline]
186pub fn join2(path: &str, child: &str) -> Result<String, FileError> {
187    join(path, child)
188}
189
190pub fn join3(path1: &str, path2: &str, path3: &str) -> Result<String, FileError> {
191    join(
192        path1,
193        &join(path2, path3)?,
194    )
195}
196
197pub fn join4(path1: &str, path2: &str, path3: &str, path4: &str) -> Result<String, FileError> {
198    join(
199        &join(path1, path2)?,
200        &join(path3, path4)?,
201    )
202}
203
204pub fn join5(path1: &str, path2: &str, path3: &str, path4: &str, path5: &str) -> Result<String, FileError> {
205    join(
206        &join(path1, path2)?,
207        &join(path3, &join(path4, path5)?)?,
208    )
209}
210
211/// `a/b/c.d, e` -> `a/b/c.e`
212pub fn set_extension(path: &str, ext: &str) -> Result<String, FileError> {
213    let mut path_buf = PathBuf::from_str(path).unwrap();  // Infallible
214
215    if path_buf.set_extension(ext) {
216        match path_buf.to_str() {
217            Some(result) => Ok(result.to_string()),
218            None => Err(FileError::os_str_err(path_buf.into_os_string())),
219        }
220    } else {
221        // has no filename
222        Ok(path.to_string())
223    }
224}
225
226/// It returns `false` if `path` doesn't exist
227pub fn is_dir(path: &str) -> bool {
228    PathBuf::from_str(path).map(|path| path.is_dir()).unwrap_or(false)
229}
230
231/// It returns `false` if `path` doesn't exist
232pub fn is_symlink(path: &str) -> bool {
233    PathBuf::from_str(path).map(|path| path.is_symlink()).unwrap_or(false)
234}
235
236pub fn exists(path: &str) -> bool {
237    PathBuf::from_str(path).map(|path| path.exists()).unwrap_or(false)
238}
239
240/// `a/b/c.d` -> `a/b/`
241pub fn parent(path: &str) -> Result<String, FileError> {
242    let std_path = Path::new(path);
243
244    std_path.parent().map(
245        |p| p.to_string_lossy().to_string()
246    ).ok_or_else(
247        || FileError::unknown(
248            String::from("function `parent` died"),
249            Some(path.to_string()),
250        )
251    )
252}
253
254/// It's like `create_dir` but does not raise an error if `path` already exists
255pub fn try_create_dir(path: &str) -> Result<(), FileError> {
256    match fs::create_dir(path) {
257        Ok(()) => Ok(()),
258        Err(e) => match e.kind() {
259            io::ErrorKind::AlreadyExists => Ok(()),
260            _ => Err(FileError::from_std(e, path)),
261        },
262    }
263}
264
265pub fn create_dir(path: &str) -> Result<(), FileError> {
266    fs::create_dir(path).map_err(|e| FileError::from_std(e, path))
267}
268
269pub fn create_dir_all(path: &str) -> Result<(), FileError> {
270    fs::create_dir_all(path).map_err(|e| FileError::from_std(e, path))
271}
272
273pub fn rename(from: &str, to: &str) -> Result<(), FileError> {
274    fs::rename(from, to).map_err(|e| FileError::from_std(e, from))
275}
276
277pub fn copy_dir(src: &str, dst: &str) -> Result<(), FileError> {
278    create_dir_all(dst)?;
279
280    // TODO: how about links?
281    for e in read_dir(src, false)? {
282        let new_dst = join(dst, &basename(&e)?)?;
283
284        if is_dir(&e) {
285            create_dir_all(&new_dst)?;
286            copy_dir(&e, &new_dst)?;
287        }
288
289        else {
290            copy_file(&e, &new_dst)?;
291        }
292    }
293
294    Ok(())
295}
296
297/// It returns the total number of bytes copied.
298pub fn copy_file(src: &str, dst: &str) -> Result<u64, FileError> {
299    std::fs::copy(src, dst).map_err(|e| FileError::from_std(e, src))  // TODO: how about dst?
300}
301
302// it only returns the hash value of the modified time
303pub fn last_modified(path: &str) -> Result<u64, FileError> {
304    match fs::metadata(path) {
305        Ok(m) => match m.modified() {
306            Ok(m) => {
307                let mut hasher = hash_map::DefaultHasher::new();
308                m.hash(&mut hasher);
309                let hash = hasher.finish();
310
311                Ok(hash)
312            },
313            Err(e) => Err(FileError::from_std(e, path)),
314        },
315        Err(e) => Err(FileError::from_std(e, path)),
316    }
317}
318
319pub fn file_size(path: &str) -> Result<u64, FileError> {
320    match fs::metadata(path) {
321        Ok(m) => Ok(m.len()),
322        Err(e) => Err(FileError::from_std(e, path)),
323    }
324}
325
326pub fn read_dir(path: &str, sort: bool) -> Result<Vec<String>, FileError> {
327    match fs::read_dir(path) {
328        Err(e) => Err(FileError::from_std(e, path)),
329        Ok(entries) => {
330            let mut result = vec![];
331
332            for entry in entries {
333                match entry {
334                    Err(e) => {
335                        return Err(FileError::from_std(e, path));
336                    },
337                    Ok(e) => {
338                        if let Some(ee) = e.path().to_str() {
339                            result.push(ee.to_string());
340                        }
341                    },
342                }
343            }
344
345            if sort {
346                result.sort();
347            }
348
349            Ok(result)
350        }
351    }
352}
353
354pub fn remove_file(path: &str) -> Result<(), FileError> {
355    fs::remove_file(path).map_err(|e| FileError::from_std(e, path))
356}
357
358pub fn remove_dir(path: &str) -> Result<(), FileError> {
359    fs::remove_dir(path).map_err(|e| FileError::from_std(e, path))
360}
361
362pub fn remove_dir_all(path: &str) -> Result<(), FileError> {
363    fs::remove_dir_all(path).map_err(|e| FileError::from_std(e, path))
364}
365
366pub fn into_abs_path(path: &str) -> Result<String, FileError> {
367    let std_path = Path::new(path);
368
369    if std_path.is_absolute() {
370        Ok(path.to_string())
371    }
372
373    else {
374        Ok(join(
375            &current_dir()?,
376            path,
377        )?)
378    }
379}
380
381pub fn current_dir() -> Result<String, FileError> {
382    let cwd = std::env::current_dir().map_err(|e| FileError::from_std(e, "."))?;
383
384    match cwd.to_str() {
385        Some(cwd) => Ok(cwd.to_string()),
386        None => Err(FileError::os_str_err(cwd.into_os_string())),
387    }
388}
389
390pub fn diff(path: &str, base: &str) -> Result<String, FileError> {
391    match pathdiff::diff_paths(path, base) {
392        Some(path) => match path.to_str() {
393            Some(path) => Ok(path.to_string()),
394            None => Err(FileError::os_str_err(path.into_os_string())),
395        },
396        None => Err(FileError::cannot_diff_path(path.to_string(), base.to_string())),
397    }
398}
399
400/// It calcs diff and normalizes.
401pub fn get_relative_path(base: &str, path: &str) -> Result<String, FileError> {
402    // It has to normalize the output because `diff` behaves differently on windows and unix.
403    Ok(normalize(&diff(
404        // in order to calc diff, it needs a full path
405        &normalize(
406            &into_abs_path(path)?,
407        )?,
408        &normalize(
409            &into_abs_path(base)?,
410        )?,
411    )?)?)
412}
413
414pub fn normalize(path: &str) -> Result<String, FileError> {
415    let mut result = vec![];
416    let path = path.replace("\\", "/");
417
418    for component in path.split("/") {
419        match component {
420            c if c == "." => {},
421            c if c == ".." => if result.is_empty() {
422                result.push(c.to_string());
423            } else {
424                result.pop();
425            },
426            c => { result.push(c.to_string()); },
427        }
428    }
429
430    Ok(result.join("/"))
431}
432
433#[derive(Clone,  PartialEq)]
434pub struct FileError {
435    pub kind: FileErrorKind,
436    pub given_path: Option<String>,
437}
438
439impl FileError {
440    pub fn from_std(e: io::Error, given_path: &str) -> Self {
441        let kind = match e.kind() {
442            io::ErrorKind::NotFound => FileErrorKind::FileNotFound,
443            io::ErrorKind::PermissionDenied => FileErrorKind::PermissionDenied,
444            io::ErrorKind::AlreadyExists => FileErrorKind::AlreadyExists,
445            e => FileErrorKind::Unknown(format!("unknown error: {e:?}")),
446        };
447
448        FileError {
449            kind,
450            given_path: Some(given_path.to_string()),
451        }
452    }
453
454    pub(crate) fn os_str_err(os_str: OsString) -> Self {
455        FileError {
456            kind: FileErrorKind::OsStrErr(os_str),
457            given_path: None,
458        }
459    }
460
461    pub(crate) fn cannot_diff_path(path: String, base: String) -> Self {
462        FileError {
463            kind: FileErrorKind::CannotDiffPath(path.to_string(), base),
464            given_path: Some(path),
465        }
466    }
467
468    pub fn unknown(msg: String, path: Option<String>) -> Self {
469        FileError {
470            kind: FileErrorKind::Unknown(msg),
471            given_path: path,
472        }
473    }
474
475    pub fn render_error(&self) -> String {
476        let path = self.given_path.as_ref().map(|p| p.to_string()).unwrap_or(String::new());
477
478        match &self.kind {
479            FileErrorKind::FileNotFound => format!(
480                "file not found: `{path}`"
481            ),
482            FileErrorKind::PermissionDenied => format!(
483                "permission denied: `{path}`"
484            ),
485            FileErrorKind::AlreadyExists => format!(
486                "file already exists: `{path}`"
487            ),
488            FileErrorKind::CannotDiffPath(path, base) => format!(
489                "cannot calc diff: `{path}` and `{base}`"
490            ),
491            FileErrorKind::Unknown(msg) => format!(
492                "unknown file error: `{msg}`"
493            ),
494            FileErrorKind::OsStrErr(os_str) => format!(
495                "error converting os_str: `{os_str:?}`"
496            ),
497        }
498    }
499}
500
501impl fmt::Debug for FileError {
502    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
503        write!(fmt, "{}", self.render_error())
504    }
505}
506
507impl fmt::Display for FileError {
508    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
509        write!(fmt, "{}", self.render_error())
510    }
511}
512
513#[derive(Clone, Debug, PartialEq)]
514pub enum FileErrorKind {
515    FileNotFound,
516    PermissionDenied,
517    AlreadyExists,
518    CannotDiffPath(String, String),
519    Unknown(String),
520    OsStrErr(OsString),
521}