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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! Provides extension traits for [`NormalEntry`].
use super::private;
use libpna::{EntryBuilder, NormalEntry, WriteOptions};
use std::{fs, io, path::Path};
/// [`NormalEntry`] filesystem extension methods.
pub trait EntryFsExt: private::Sealed {
/// Creates an entry from the given path.
///
/// # Errors
///
/// Returns an error if an I/O error occurs while creating the entry.
fn from_path<P: AsRef<Path>>(path: P) -> io::Result<Self>
where
Self: Sized;
/// Creates an entry from the given path with options.
///
/// # Errors
///
/// Returns an error if an I/O error occurs while creating the entry.
fn from_path_with<P: AsRef<Path>>(path: P, options: WriteOptions) -> io::Result<Self>
where
Self: Sized;
/// Creates an entry from the given path without following symlinks.
///
/// # Errors
///
/// Returns an error if an I/O error occurs while creating the entry.
fn from_path_symlink<P: AsRef<Path>>(path: P) -> io::Result<Self>
where
Self: Sized;
/// Creates an entry from the given path with options, without following symlinks.
///
/// # Errors
///
/// Returns an error if an I/O error occurs while creating the entry.
fn from_path_symlink_with<P: AsRef<Path>>(path: P, options: WriteOptions) -> io::Result<Self>
where
Self: Sized;
}
impl EntryFsExt for NormalEntry {
/// Creates an entry from the given path.
///
/// The path may refer to a regular file or a directory. For files, the
/// file contents are read and embedded into the resulting entry using
/// default [`WriteOptions`] (equivalent to
/// `WriteOptions::builder().build()`). For directories, an empty directory
/// entry is created. Symlinks are followed (uses [`std::fs::metadata`]). If
/// the intention is to archive a symbolic link itself, use
/// [`EntryBuilder::new_symlink`].
///
/// # Examples
///
/// ```no_run
/// use pna::NormalEntry;
/// use pna::prelude::*;
///
/// # fn main() -> std::io::Result<()> {
/// NormalEntry::from_path("path/to/file")?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns an error if an I/O error occurs while creating the entry.
#[inline]
fn from_path<P: AsRef<Path>>(path: P) -> io::Result<Self> {
Self::from_path_with(path.as_ref(), WriteOptions::builder().build())
}
/// Creates an entry from the given path with options.
///
/// Behaves like [`EntryFsExt::from_path`], but uses the provided
/// [`WriteOptions`] when constructing a file entry.
///
/// # Examples
///
/// ```no_run
/// use pna::prelude::*;
/// use pna::{NormalEntry, WriteOptions};
///
/// # fn main() -> std::io::Result<()> {
/// NormalEntry::from_path_with("path/to/file", WriteOptions::store())?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns an error if an I/O error occurs while creating the entry.
#[inline]
fn from_path_with<P: AsRef<Path>>(path: P, options: WriteOptions) -> io::Result<Self>
where
Self: Sized,
{
let path = path.as_ref();
let meta = fs::metadata(path)?;
let name = path.try_into().map_err(io::Error::other)?;
if meta.is_file() {
let mut file = fs::File::open(path)?;
let mut builder = EntryBuilder::new_file(name, options)?;
io::copy(&mut file, &mut builder)?;
builder.build()
} else {
let builder = EntryBuilder::new_dir(name);
builder.build()
}
}
/// Creates an entry from the given path without following symlinks.
///
/// This behaves like [`EntryFsExt::from_path`], but uses
/// [`std::fs::symlink_metadata`] to avoid following symbolic links.
/// When `path` is a symbolic link, a symbolic-link entry is created using
/// [`EntryBuilder::new_symlink`], with the link target captured via
/// [`std::fs::read_link`]. For regular files and directories, behavior is
/// identical to [`EntryFsExt::from_path`].
///
/// # Examples
///
/// ```no_run
/// use pna::NormalEntry;
/// use pna::prelude::*;
///
/// # fn main() -> std::io::Result<()> {
/// NormalEntry::from_path_symlink("path/to/file")?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns an error if an I/O error occurs while creating the entry.
#[inline]
fn from_path_symlink<P: AsRef<Path>>(path: P) -> io::Result<Self>
where
Self: Sized,
{
Self::from_path_symlink_with(path, WriteOptions::builder().build())
}
/// Creates an entry from the given path with options, without following symlinks.
///
/// Behaves like [`EntryFsExt::from_path_with`], but uses
/// [`std::fs::symlink_metadata`] and creates a symbolic-link entry for
/// symlinks instead of following them.
///
/// # Examples
///
/// ```no_run
/// use pna::prelude::*;
/// use pna::{NormalEntry, WriteOptions};
///
/// # fn main() -> std::io::Result<()> {
/// NormalEntry::from_path_symlink_with("path/to/file", WriteOptions::store())?;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns an error if an I/O error occurs while creating the entry.
#[inline]
fn from_path_symlink_with<P: AsRef<Path>>(path: P, options: WriteOptions) -> io::Result<Self>
where
Self: Sized,
{
let path = path.as_ref();
let meta = fs::symlink_metadata(path)?;
let name = path.try_into().map_err(io::Error::other)?;
if meta.file_type().is_symlink() {
let target = fs::read_link(path)?;
let reference = target.as_path().try_into().map_err(io::Error::other)?;
let mut builder = EntryBuilder::new_symlink(name, reference)?;
#[cfg(windows)]
let ltp = {
use std::os::windows::fs::FileTypeExt;
let ft = meta.file_type();
if ft.is_symlink_dir() {
libpna::LinkTargetType::Directory
} else if ft.is_symlink_file() {
libpna::LinkTargetType::File
} else {
libpna::LinkTargetType::Unknown
}
};
#[cfg(not(windows))]
let ltp = match fs::metadata(path) {
Ok(m) if m.is_dir() => libpna::LinkTargetType::Directory,
Ok(m) if m.is_file() => libpna::LinkTargetType::File,
Ok(_) => libpna::LinkTargetType::Unknown,
Err(e) if e.kind() == io::ErrorKind::NotFound => libpna::LinkTargetType::Unknown,
Err(e) => return Err(e),
};
builder.link_target_type(ltp);
builder.build()
} else if meta.is_file() {
let mut file = fs::File::open(path)?;
let mut builder = EntryBuilder::new_file(name, options)?;
io::copy(&mut file, &mut builder)?;
builder.build()
} else {
let builder = EntryBuilder::new_dir(name);
builder.build()
}
}
}