4 use std
::io
::prelude
::*;
7 use crate::header
::{bytes2path, path2bytes, HeaderMode}
;
8 use crate::{other, EntryType, Header}
;
10 /// A structure for building archives
12 /// This structure has methods for building up an archive from scratch into any
14 pub struct Builder
<W
: Write
> {
21 impl<W
: Write
> Builder
<W
> {
22 /// Create a new archive builder with the underlying object as the
23 /// destination of all data written. The builder will use
24 /// `HeaderMode::Complete` by default.
25 pub fn new(obj
: W
) -> Builder
<W
> {
27 mode
: HeaderMode
::Complete
,
34 /// Changes the HeaderMode that will be used when reading fs Metadata for
35 /// methods that implicitly read metadata for an input Path. Notably, this
36 /// does _not_ apply to `append(Header)`.
37 pub fn mode(&mut self, mode
: HeaderMode
) {
41 /// Follow symlinks, archiving the contents of the file they point to rather
42 /// than adding a symlink to the archive. Defaults to true.
43 pub fn follow_symlinks(&mut self, follow
: bool
) {
47 /// Gets shared reference to the underlying object.
48 pub fn get_ref(&self) -> &W
{
49 self.obj
.as_ref().unwrap()
52 /// Gets mutable reference to the underlying object.
54 /// Note that care must be taken while writing to the underlying
55 /// object. But, e.g. `get_mut().flush()` is clamed to be safe and
56 /// useful in the situations when one needs to be ensured that
57 /// tar entry was flushed to the disk.
58 pub fn get_mut(&mut self) -> &mut W
{
59 self.obj
.as_mut().unwrap()
62 /// Unwrap this archive, returning the underlying object.
64 /// This function will finish writing the archive if the `finish` function
65 /// hasn't yet been called, returning any I/O error which happens during
67 pub fn into_inner(mut self) -> io
::Result
<W
> {
71 Ok(self.obj
.take().unwrap())
74 /// Adds a new entry to this archive.
76 /// This function will append the header specified, followed by contents of
77 /// the stream specified by `data`. To produce a valid archive the `size`
78 /// field of `header` must be the same as the length of the stream that's
79 /// being written. Additionally the checksum for the header should have been
80 /// set via the `set_cksum` method.
82 /// Note that this will not attempt to seek the archive to a valid position,
83 /// so if the archive is in the middle of a read or some other similar
84 /// operation then this may corrupt the archive.
86 /// Also note that after all entries have been written to an archive the
87 /// `finish` function needs to be called to finish writing the archive.
91 /// This function will return an error for any intermittent I/O error which
92 /// occurs when either reading or writing.
97 /// use tar::{Builder, Header};
99 /// let mut header = Header::new_gnu();
100 /// header.set_path("foo").unwrap();
101 /// header.set_size(4);
102 /// header.set_cksum();
104 /// let mut data: &[u8] = &[1, 2, 3, 4];
106 /// let mut ar = Builder::new(Vec::new());
107 /// ar.append(&header, data).unwrap();
108 /// let data = ar.into_inner().unwrap();
110 pub fn append
<R
: Read
>(&mut self, header
: &Header
, mut data
: R
) -> io
::Result
<()> {
111 append(self.get_mut(), header
, &mut data
)
114 /// Adds a new entry to this archive with the specified path.
116 /// This function will set the specified path in the given header, which may
117 /// require appending a GNU long-name extension entry to the archive first.
118 /// The checksum for the header will be automatically updated via the
119 /// `set_cksum` method after setting the path. No other metadata in the
120 /// header will be modified.
122 /// Then it will append the header, followed by contents of the stream
123 /// specified by `data`. To produce a valid archive the `size` field of
124 /// `header` must be the same as the length of the stream that's being
127 /// Note that this will not attempt to seek the archive to a valid position,
128 /// so if the archive is in the middle of a read or some other similar
129 /// operation then this may corrupt the archive.
131 /// Also note that after all entries have been written to an archive the
132 /// `finish` function needs to be called to finish writing the archive.
136 /// This function will return an error for any intermittent I/O error which
137 /// occurs when either reading or writing.
142 /// use tar::{Builder, Header};
144 /// let mut header = Header::new_gnu();
145 /// header.set_size(4);
146 /// header.set_cksum();
148 /// let mut data: &[u8] = &[1, 2, 3, 4];
150 /// let mut ar = Builder::new(Vec::new());
151 /// ar.append_data(&mut header, "really/long/path/to/foo", data).unwrap();
152 /// let data = ar.into_inner().unwrap();
154 pub fn append_data
<P
: AsRef
<Path
>, R
: Read
>(
159 ) -> io
::Result
<()> {
160 prepare_header_path(self.get_mut(), header
, path
.as_ref())?
;
162 self.append(&header
, data
)
165 /// Adds a file on the local filesystem to this archive.
167 /// This function will open the file specified by `path` and insert the file
168 /// into the archive with the appropriate metadata set, returning any I/O
169 /// error which occurs while writing. The path name for the file inside of
170 /// this archive will be the same as `path`, and it is required that the
171 /// path is a relative path.
173 /// Note that this will not attempt to seek the archive to a valid position,
174 /// so if the archive is in the middle of a read or some other similar
175 /// operation then this may corrupt the archive.
177 /// Also note that after all files have been written to an archive the
178 /// `finish` function needs to be called to finish writing the archive.
183 /// use tar::Builder;
185 /// let mut ar = Builder::new(Vec::new());
187 /// ar.append_path("foo/bar.txt").unwrap();
189 pub fn append_path
<P
: AsRef
<Path
>>(&mut self, path
: P
) -> io
::Result
<()> {
190 let mode
= self.mode
.clone();
191 let follow
= self.follow
;
192 append_path_with_name(self.get_mut(), path
.as_ref(), None
, mode
, follow
)
195 /// Adds a file on the local filesystem to this archive under another name.
197 /// This function will open the file specified by `path` and insert the file
198 /// into the archive as `name` with appropriate metadata set, returning any
199 /// I/O error which occurs while writing. The path name for the file inside
200 /// of this archive will be `name` and `path` is required to be a relative
203 /// Note that this will not attempt to seek the archive to a valid position,
204 /// so if the archive is in the middle of a read or some other similar
205 /// operation then this may corrupt the archive.
207 /// Also note that after all files have been written to an archive the
208 /// `finish` function needs to be called to finish writing the archive.
213 /// use tar::Builder;
215 /// let mut ar = Builder::new(Vec::new());
217 /// // Insert the local file "foo/bar.txt" in the archive but with the name
218 /// // "bar/foo.txt".
219 /// ar.append_path_with_name("foo/bar.txt", "bar/foo.txt").unwrap();
221 pub fn append_path_with_name
<P
: AsRef
<Path
>, N
: AsRef
<Path
>>(
225 ) -> io
::Result
<()> {
226 let mode
= self.mode
.clone();
227 let follow
= self.follow
;
228 append_path_with_name(
237 /// Adds a file to this archive with the given path as the name of the file
240 /// This will use the metadata of `file` to populate a `Header`, and it will
241 /// then append the file to the archive with the name `path`.
243 /// Note that this will not attempt to seek the archive to a valid position,
244 /// so if the archive is in the middle of a read or some other similar
245 /// operation then this may corrupt the archive.
247 /// Also note that after all files have been written to an archive the
248 /// `finish` function needs to be called to finish writing the archive.
253 /// use std::fs::File;
254 /// use tar::Builder;
256 /// let mut ar = Builder::new(Vec::new());
258 /// // Open the file at one location, but insert it into the archive with a
259 /// // different name.
260 /// let mut f = File::open("foo/bar/baz.txt").unwrap();
261 /// ar.append_file("bar/baz.txt", &mut f).unwrap();
263 pub fn append_file
<P
: AsRef
<Path
>>(&mut self, path
: P
, file
: &mut fs
::File
) -> io
::Result
<()> {
264 let mode
= self.mode
.clone();
265 append_file(self.get_mut(), path
.as_ref(), file
, mode
)
268 /// Adds a directory to this archive with the given path as the name of the
269 /// directory in the archive.
271 /// This will use `stat` to populate a `Header`, and it will then append the
272 /// directory to the archive with the name `path`.
274 /// Note that this will not attempt to seek the archive to a valid position,
275 /// so if the archive is in the middle of a read or some other similar
276 /// operation then this may corrupt the archive.
278 /// Also note that after all files have been written to an archive the
279 /// `finish` function needs to be called to finish writing the archive.
285 /// use tar::Builder;
287 /// let mut ar = Builder::new(Vec::new());
289 /// // Use the directory at one location, but insert it into the archive
290 /// // with a different name.
291 /// ar.append_dir("bardir", ".").unwrap();
293 pub fn append_dir
<P
, Q
>(&mut self, path
: P
, src_path
: Q
) -> io
::Result
<()>
298 let mode
= self.mode
.clone();
299 append_dir(self.get_mut(), path
.as_ref(), src_path
.as_ref(), mode
)
302 /// Adds a directory and all of its contents (recursively) to this archive
303 /// with the given path as the name of the directory in the archive.
305 /// Note that this will not attempt to seek the archive to a valid position,
306 /// so if the archive is in the middle of a read or some other similar
307 /// operation then this may corrupt the archive.
309 /// Also note that after all files have been written to an archive the
310 /// `finish` function needs to be called to finish writing the archive.
316 /// use tar::Builder;
318 /// let mut ar = Builder::new(Vec::new());
320 /// // Use the directory at one location, but insert it into the archive
321 /// // with a different name.
322 /// ar.append_dir_all("bardir", ".").unwrap();
324 pub fn append_dir_all
<P
, Q
>(&mut self, path
: P
, src_path
: Q
) -> io
::Result
<()>
329 let mode
= self.mode
.clone();
330 let follow
= self.follow
;
340 /// Finish writing this archive, emitting the termination sections.
342 /// This function should only be called when the archive has been written
343 /// entirely and if an I/O error happens the underlying object still needs
346 /// In most situations the `into_inner` method should be preferred.
347 pub fn finish(&mut self) -> io
::Result
<()> {
351 self.finished
= true;
352 self.get_mut().write_all(&[0; 1024])
356 fn append(mut dst
: &mut Write
, header
: &Header
, mut data
: &mut Read
) -> io
::Result
<()> {
357 dst
.write_all(header
.as_bytes())?
;
358 let len
= io
::copy(&mut data
, &mut dst
)?
;
360 // Pad with zeros if necessary.
362 let remaining
= 512 - (len
% 512);
364 dst
.write_all(&buf
[..remaining
as usize])?
;
370 fn append_path_with_name(
376 ) -> io
::Result
<()> {
377 let stat
= if follow
{
378 fs
::metadata(path
).map_err(|err
| {
381 format
!("{} when getting metadata for {}", err
, path
.display()),
385 fs
::symlink_metadata(path
).map_err(|err
| {
388 format
!("{} when getting metadata for {}", err
, path
.display()),
392 let ar_name
= name
.unwrap_or(path
);
394 append_fs(dst
, ar_name
, &stat
, &mut fs
::File
::open(path
)?
, mode
, None
)
395 } else if stat
.is_dir() {
396 append_fs(dst
, ar_name
, &stat
, &mut io
::empty(), mode
, None
)
397 } else if stat
.file_type().is_symlink() {
398 let link_name
= fs
::read_link(path
)?
;
408 Err(other(&format
!("{} has unknown file type", path
.display())))
417 ) -> io
::Result
<()> {
418 let stat
= file
.metadata()?
;
419 append_fs(dst
, path
, &stat
, file
, mode
, None
)
422 fn append_dir(dst
: &mut Write
, path
: &Path
, src_path
: &Path
, mode
: HeaderMode
) -> io
::Result
<()> {
423 let stat
= fs
::metadata(src_path
)?
;
424 append_fs(dst
, path
, &stat
, &mut io
::empty(), mode
, None
)
427 fn prepare_header(size
: u64, entry_type
: u8) -> Header
{
428 let mut header
= Header
::new_gnu();
429 let name
= b
"././@LongLink";
430 header
.as_gnu_mut().unwrap().name
[..name
.len()].clone_from_slice(&name
[..]);
431 header
.set_mode(0o644);
435 // + 1 to be compliant with GNU tar
436 header
.set_size(size
+ 1);
437 header
.set_entry_type(EntryType
::new(entry_type
));
442 fn prepare_header_path(dst
: &mut Write
, header
: &mut Header
, path
: &Path
) -> io
::Result
<()> {
443 // Try to encode the path directly in the header, but if it ends up not
444 // working (probably because it's too long) then try to use the GNU-specific
445 // long name extension by emitting an entry which indicates that it's the
447 if let Err(e
) = header
.set_path(path
) {
448 let data
= path2bytes(&path
)?
;
449 let max
= header
.as_old().name
.len();
450 // Since e isn't specific enough to let us know the path is indeed too
451 // long, verify it first before using the extension.
452 if data
.len() < max
{
455 let header2
= prepare_header(data
.len() as u64, b'L'
);
456 // null-terminated string
457 let mut data2
= data
.chain(io
::repeat(0).take(1));
458 append(dst
, &header2
, &mut data2
)?
;
459 // Truncate the path to store in the header we're about to emit to
460 // ensure we've got something at least mentioned.
461 let path
= bytes2path(Cow
::Borrowed(&data
[..max
]))?
;
462 header
.set_path(&path
)?
;
467 fn prepare_header_link(dst
: &mut Write
, header
: &mut Header
, link_name
: &Path
) -> io
::Result
<()> {
468 // Same as previous function but for linkname
469 if let Err(e
) = header
.set_link_name(&link_name
) {
470 let data
= path2bytes(&link_name
)?
;
471 if data
.len() < header
.as_old().linkname
.len() {
474 let header2
= prepare_header(data
.len() as u64, b'K'
);
475 let mut data2
= data
.chain(io
::repeat(0).take(1));
476 append(dst
, &header2
, &mut data2
)?
;
487 link_name
: Option
<&Path
>,
488 ) -> io
::Result
<()> {
489 let mut header
= Header
::new_gnu();
491 prepare_header_path(dst
, &mut header
, path
)?
;
492 header
.set_metadata_in_mode(meta
, mode
);
493 if let Some(link_name
) = link_name
{
494 prepare_header_link(dst
, &mut header
, link_name
)?
;
497 append(dst
, &header
, read
)
506 ) -> io
::Result
<()> {
507 let mut stack
= vec
![(src_path
.to_path_buf(), true, false)];
508 while let Some((src
, is_dir
, is_symlink
)) = stack
.pop() {
509 let dest
= path
.join(src
.strip_prefix(&src_path
).unwrap());
510 // In case of a symlink pointing to a directory, is_dir is false, but src.is_dir() will return true
511 if is_dir
|| (is_symlink
&& follow
&& src
.is_dir()) {
512 for entry
in fs
::read_dir(&src
)?
{
514 let file_type
= entry
.file_type()?
;
515 stack
.push((entry
.path(), file_type
.is_dir(), file_type
.is_symlink()));
517 if dest
!= Path
::new("") {
518 append_dir(dst
, &dest
, &src
, mode
)?
;
520 } else if !follow
&& is_symlink
{
521 let stat
= fs
::symlink_metadata(&src
)?
;
522 let link_name
= fs
::read_link(&src
)?
;
523 append_fs(dst
, &dest
, &stat
, &mut io
::empty(), mode
, Some(&link_name
))?
;
525 append_file(dst
, &dest
, &mut fs
::File
::open(src
)?
, mode
)?
;
531 impl<W
: Write
> Drop
for Builder
<W
> {
533 let _
= self.finish();