1pub mod composite;
2use crate::error::archive::GetArchivePathError;
3use crate::error::fs::{
4 CanonicalizePathError, CopyFileError, CreateDirAllError, NoParentPathError, ReadDirError,
5 ReadFileError, ReadMetadataError, ReadPermissionsError, ReadToStringError,
6 RemoveDirectoryAndContentsError, RemoveDirectoryError, RemoveFileError, RenameError,
7 SetPermissionsError, SetPermissionsReadWriteError, UnpackingArchiveError, WriteFileError,
8};
9use std::fs::{Metadata, Permissions, ReadDir};
10use std::path::{Path, PathBuf};
11
12pub fn canonicalize(path: &Path) -> Result<PathBuf, CanonicalizePathError> {
13 dunce::canonicalize(path).map_err(|source| CanonicalizePathError {
14 path: path.to_path_buf(),
15 source,
16 })
17}
18
19pub fn copy(from: &Path, to: &Path) -> Result<u64, CopyFileError> {
20 std::fs::copy(from, to).map_err(|source| CopyFileError {
21 from: from.to_path_buf(),
22 to: to.to_path_buf(),
23 source,
24 })
25}
26
27pub fn create_dir_all(path: &Path) -> Result<(), CreateDirAllError> {
28 std::fs::create_dir_all(path).map_err(|source| CreateDirAllError {
29 path: path.to_path_buf(),
30 source,
31 })
32}
33
34pub fn get_archive_path(
35 archive: &tar::Entry<flate2::read::GzDecoder<&'static [u8]>>,
36) -> Result<PathBuf, GetArchivePathError> {
37 let path = archive.path().map_err(GetArchivePathError)?;
38 Ok(path.to_path_buf())
39}
40
41pub fn metadata(path: &Path) -> Result<Metadata, ReadMetadataError> {
42 std::fs::metadata(path).map_err(|source| ReadMetadataError {
43 path: path.to_path_buf(),
44 source,
45 })
46}
47
48pub fn parent(path: &Path) -> Result<PathBuf, NoParentPathError> {
49 match path.parent() {
50 None => Err(NoParentPathError(path.to_path_buf())),
51 Some(parent) => Ok(parent.to_path_buf()),
52 }
53}
54
55pub fn read(path: &Path) -> Result<Vec<u8>, ReadFileError> {
56 std::fs::read(path).map_err(|source| ReadFileError {
57 path: path.to_path_buf(),
58 source,
59 })
60}
61
62pub fn read_to_string(path: &Path) -> Result<String, ReadToStringError> {
63 std::fs::read_to_string(path).map_err(|source| ReadToStringError {
64 path: path.to_path_buf(),
65 source,
66 })
67}
68
69pub fn read_dir(path: &Path) -> Result<ReadDir, ReadDirError> {
70 path.read_dir().map_err(|source| ReadDirError {
71 path: path.to_path_buf(),
72 source,
73 })
74}
75
76pub fn rename(from: &Path, to: &Path) -> Result<(), RenameError> {
77 std::fs::rename(from, to).map_err(|source| RenameError {
78 from: from.to_path_buf(),
79 to: to.to_path_buf(),
80 source,
81 })
82}
83
84pub fn read_permissions(path: &Path) -> Result<Permissions, ReadPermissionsError> {
85 std::fs::metadata(path)
86 .map_err(|source| ReadPermissionsError {
87 path: path.to_path_buf(),
88 source,
89 })
90 .map(|x| x.permissions())
91}
92
93pub fn remove_dir(path: &Path) -> Result<(), RemoveDirectoryError> {
94 std::fs::remove_dir(path).map_err(|source| RemoveDirectoryError {
95 path: path.to_path_buf(),
96 source,
97 })
98}
99
100pub fn remove_dir_all(path: &Path) -> Result<(), RemoveDirectoryAndContentsError> {
101 std::fs::remove_dir_all(path).map_err(|source| RemoveDirectoryAndContentsError {
102 path: path.to_path_buf(),
103 source,
104 })
105}
106
107pub fn remove_file(path: &Path) -> Result<(), RemoveFileError> {
108 std::fs::remove_file(path).map_err(|source| RemoveFileError {
109 path: path.to_path_buf(),
110 source,
111 })
112}
113
114pub fn set_permissions(path: &Path, permissions: Permissions) -> Result<(), SetPermissionsError> {
115 std::fs::set_permissions(path, permissions).map_err(|source| SetPermissionsError {
116 path: path.to_path_buf(),
117 source,
118 })
119}
120
121#[cfg_attr(not(unix), allow(unused_variables))]
122pub fn set_permissions_readwrite(path: &Path) -> Result<(), SetPermissionsReadWriteError> {
123 #[cfg(unix)]
124 {
125 use std::os::unix::fs::PermissionsExt;
126 let mut permissions = read_permissions(path)?;
127 permissions.set_mode(permissions.mode() | 0o600);
128 set_permissions(path, permissions)?;
129 }
130 Ok(())
131}
132
133pub fn tar_unpack_in<P: AsRef<Path>>(
134 path: P,
135 tar: &mut tar::Entry<flate2::read::GzDecoder<&'static [u8]>>,
136) -> Result<(), UnpackingArchiveError> {
137 tar.unpack_in(&path)
138 .map_err(|source| UnpackingArchiveError {
139 path: path.as_ref().to_path_buf(),
140 source,
141 })?;
142 Ok(())
143}
144
145pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<(), WriteFileError> {
146 std::fs::write(path.as_ref(), contents).map_err(|source| WriteFileError {
147 path: path.as_ref().to_path_buf(),
148 source,
149 })
150}