tokio_tar/builder.rs
1use crate::{
2 header::{path2bytes, HeaderMode},
3 other, EntryType, Header,
4};
5use std::{fs::Metadata, path::Path, str};
6use tokio::{
7 fs,
8 io::{self, AsyncRead as Read, AsyncReadExt, AsyncWrite as Write, AsyncWriteExt},
9};
10
11/// A structure for building archives
12///
13/// This structure has methods for building up an archive from scratch into any
14/// arbitrary writer.
15pub struct Builder<W: Write + Unpin + Send> {
16 mode: HeaderMode,
17 follow: bool,
18 finished: bool,
19 obj: Option<W>,
20 cancellation: Option<tokio::sync::oneshot::Sender<W>>,
21}
22
23const TERMINATION: &[u8; 1024] = &[0; 1024];
24
25impl<W: Write + Unpin + Send + 'static> Builder<W> {
26 /// Create a new archive builder with the underlying object as the
27 /// destination of all data written. The builder will use
28 /// `HeaderMode::Complete` by default.
29 ///
30 /// On drop, would write [`TERMINATION`] into the end of the archive,
31 /// use `skip_termination` method to disable this.
32 pub fn new(obj: W) -> Builder<W> {
33 let (tx, rx) = tokio::sync::oneshot::channel::<W>();
34 tokio::spawn(async move {
35 if let Ok(mut w) = rx.await {
36 let _ = w.write_all(TERMINATION).await;
37 }
38 });
39 Builder {
40 mode: HeaderMode::Complete,
41 follow: true,
42 finished: false,
43 obj: Some(obj),
44 cancellation: Some(tx),
45 }
46 }
47}
48
49impl<W: Write + Unpin + Send> Builder<W> {
50 /// Create a new archive builder with the underlying object as the
51 /// destination of all data written. The builder will use
52 /// `HeaderMode::Complete` by default.
53 ///
54 /// The [`TERMINATION`] symbol would not be written to the archive in the end.
55 pub fn new_non_terminated(obj: W) -> Builder<W> {
56 Builder {
57 mode: HeaderMode::Complete,
58 follow: true,
59 finished: false,
60 obj: Some(obj),
61 cancellation: None,
62 }
63 }
64
65 /// Changes the HeaderMode that will be used when reading fs Metadata for
66 /// methods that implicitly read metadata for an input Path. Notably, this
67 /// does _not_ apply to `append(Header)`.
68 pub fn mode(&mut self, mode: HeaderMode) {
69 self.mode = mode;
70 }
71
72 /// Follow symlinks, archiving the contents of the file they point to rather
73 /// than adding a symlink to the archive. Defaults to true.
74 pub fn follow_symlinks(&mut self, follow: bool) {
75 self.follow = follow;
76 }
77
78 /// Skip writing final termination bytes into the archive.
79 pub fn skip_termination(&mut self) {
80 drop(self.cancellation.take());
81 }
82
83 /// Gets shared reference to the underlying object.
84 pub fn get_ref(&self) -> &W {
85 self.obj.as_ref().unwrap()
86 }
87
88 /// Gets mutable reference to the underlying object.
89 ///
90 /// Note that care must be taken while writing to the underlying
91 /// object. But, e.g. `get_mut().flush()` is claimed to be safe and
92 /// useful in the situations when one needs to be ensured that
93 /// tar entry was flushed to the disk.
94 pub fn get_mut(&mut self) -> &mut W {
95 self.obj.as_mut().unwrap()
96 }
97
98 /// Unwrap this archive, returning the underlying object.
99 ///
100 /// This function will finish writing the archive if the `finish` function
101 /// hasn't yet been called, returning any I/O error which happens during
102 /// that operation.
103 pub async fn into_inner(mut self) -> io::Result<W> {
104 if !self.finished {
105 self.finish().await?;
106 }
107 Ok(self.obj.take().unwrap())
108 }
109
110 /// Adds a new entry to this archive.
111 ///
112 /// This function will append the header specified, followed by contents of
113 /// the stream specified by `data`. To produce a valid archive the `size`
114 /// field of `header` must be the same as the length of the stream that's
115 /// being written. Additionally the checksum for the header should have been
116 /// set via the `set_cksum` method.
117 ///
118 /// Note that this will not attempt to seek the archive to a valid position,
119 /// so if the archive is in the middle of a read or some other similar
120 /// operation then this may corrupt the archive.
121 ///
122 /// Also note that after all entries have been written to an archive the
123 /// `finish` function needs to be called to finish writing the archive.
124 ///
125 /// # Errors
126 ///
127 /// This function will return an error for any intermittent I/O error which
128 /// occurs when either reading or writing.
129 ///
130 /// # Examples
131 ///
132 /// ```
133 /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { tokio::runtime::Runtime::new().unwrap().block_on(async {
134 /// #
135 /// use tokio_tar::{Builder, Header};
136 ///
137 /// let mut header = Header::new_gnu();
138 /// header.set_path("foo")?;
139 /// header.set_size(4);
140 /// header.set_cksum();
141 ///
142 /// let mut data: &[u8] = &[1, 2, 3, 4];
143 ///
144 /// let mut ar = Builder::new(Vec::new());
145 /// ar.append(&header, data).await?;
146 /// let data = ar.into_inner().await?;
147 /// #
148 /// # Ok(()) }) }
149 /// ```
150 pub async fn append<R: Read + Unpin>(
151 &mut self,
152 header: &Header,
153 mut data: R,
154 ) -> io::Result<()> {
155 append(self.get_mut(), header, &mut data).await?;
156
157 Ok(())
158 }
159
160 /// Adds a new entry to this archive with the specified path.
161 ///
162 /// This function will set the specified path in the given header, which may
163 /// require appending a GNU long-name extension entry to the archive first.
164 /// The checksum for the header will be automatically updated via the
165 /// `set_cksum` method after setting the path. No other metadata in the
166 /// header will be modified.
167 ///
168 /// Then it will append the header, followed by contents of the stream
169 /// specified by `data`. To produce a valid archive the `size` field of
170 /// `header` must be the same as the length of the stream that's being
171 /// written.
172 ///
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.
176 ///
177 /// Also note that after all entries have been written to an archive the
178 /// `finish` function needs to be called to finish writing the archive.
179 ///
180 /// # Errors
181 ///
182 /// This function will return an error for any intermittent I/O error which
183 /// occurs when either reading or writing.
184 ///
185 /// # Examples
186 ///
187 /// ```
188 /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { tokio::runtime::Runtime::new().unwrap().block_on(async {
189 /// #
190 /// use tokio_tar::{Builder, Header};
191 ///
192 /// let mut header = Header::new_gnu();
193 /// header.set_size(4);
194 /// header.set_cksum();
195 ///
196 /// let mut data: &[u8] = &[1, 2, 3, 4];
197 ///
198 /// let mut ar = Builder::new(Vec::new());
199 /// ar.append_data(&mut header, "really/long/path/to/foo", data).await?;
200 /// let data = ar.into_inner().await?;
201 /// #
202 /// # Ok(()) }) }
203 /// ```
204 pub async fn append_data<P: AsRef<Path>, R: Read + Unpin>(
205 &mut self,
206 header: &mut Header,
207 path: P,
208 data: R,
209 ) -> io::Result<()> {
210 prepare_header_path(self.get_mut(), header, path.as_ref()).await?;
211 header.set_cksum();
212 self.append(header, data).await?;
213
214 Ok(())
215 }
216
217 /// Adds a file on the local filesystem to this archive.
218 ///
219 /// This function will open the file specified by `path` and insert the file
220 /// into the archive with the appropriate metadata set, returning any I/O
221 /// error which occurs while writing. The path name for the file inside of
222 /// this archive will be the same as `path`, and it is required that the
223 /// path is a relative path.
224 ///
225 /// Note that this will not attempt to seek the archive to a valid position,
226 /// so if the archive is in the middle of a read or some other similar
227 /// operation then this may corrupt the archive.
228 ///
229 /// Also note that after all files have been written to an archive the
230 /// `finish` function needs to be called to finish writing the archive.
231 ///
232 /// # Examples
233 ///
234 /// ```no_run
235 /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { tokio::runtime::Runtime::new().unwrap().block_on(async {
236 /// #
237 /// use tokio_tar::Builder;
238 ///
239 /// let mut ar = Builder::new(Vec::new());
240 ///
241 /// ar.append_path("foo/bar.txt").await?;
242 /// #
243 /// # Ok(()) }) }
244 /// ```
245 pub async fn append_path<P: AsRef<Path>>(&mut self, path: P) -> io::Result<()> {
246 let mode = self.mode;
247 let follow = self.follow;
248 append_path_with_name(self.get_mut(), path.as_ref(), None, mode, follow).await?;
249 Ok(())
250 }
251
252 /// Adds a file on the local filesystem to this archive under another name.
253 ///
254 /// This function will open the file specified by `path` and insert the file
255 /// into the archive as `name` with appropriate metadata set, returning any
256 /// I/O error which occurs while writing. The path name for the file inside
257 /// of this archive will be `name` is required to be a relative path.
258 ///
259 /// Note that this will not attempt to seek the archive to a valid position,
260 /// so if the archive is in the middle of a read or some other similar
261 /// operation then this may corrupt the archive.
262 ///
263 /// Also note that after all files have been written to an archive the
264 /// `finish` function needs to be called to finish writing the archive.
265 ///
266 /// # Examples
267 ///
268 /// ```no_run
269 /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { tokio::runtime::Runtime::new().unwrap().block_on(async {
270 /// #
271 /// use tokio_tar::Builder;
272 ///
273 /// let mut ar = Builder::new(Vec::new());
274 ///
275 /// // Insert the local file "foo/bar.txt" in the archive but with the name
276 /// // "bar/foo.txt".
277 /// ar.append_path_with_name("foo/bar.txt", "bar/foo.txt").await?;
278 /// #
279 /// # Ok(()) }) }
280 /// ```
281 pub async fn append_path_with_name<P: AsRef<Path>, N: AsRef<Path>>(
282 &mut self,
283 path: P,
284 name: N,
285 ) -> io::Result<()> {
286 let mode = self.mode;
287 let follow = self.follow;
288 append_path_with_name(
289 self.get_mut(),
290 path.as_ref(),
291 Some(name.as_ref()),
292 mode,
293 follow,
294 )
295 .await?;
296 Ok(())
297 }
298
299 /// Adds a file to this archive with the given path as the name of the file
300 /// in the archive.
301 ///
302 /// This will use the metadata of `file` to populate a `Header`, and it will
303 /// then append the file to the archive with the name `path`.
304 ///
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.
308 ///
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.
311 ///
312 /// # Examples
313 ///
314 /// ```no_run
315 /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { tokio::runtime::Runtime::new().unwrap().block_on(async {
316 /// #
317 /// use tokio::fs::File;
318 /// use tokio_tar::Builder;
319 ///
320 /// let mut ar = Builder::new(Vec::new());
321 ///
322 /// // Open the file at one location, but insert it into the archive with a
323 /// // different name.
324 /// let mut f = File::open("foo/bar/baz.txt").await?;
325 /// ar.append_file("bar/baz.txt", &mut f).await?;
326 /// #
327 /// # Ok(()) }) }
328 /// ```
329 pub async fn append_file<P: AsRef<Path>>(
330 &mut self,
331 path: P,
332 file: &mut fs::File,
333 ) -> io::Result<()> {
334 let mode = self.mode;
335 append_file(self.get_mut(), path.as_ref(), file, mode).await?;
336 Ok(())
337 }
338
339 /// Adds a directory to this archive with the given path as the name of the
340 /// directory in the archive.
341 ///
342 /// This will use `stat` to populate a `Header`, and it will then append the
343 /// directory to the archive with the name `path`.
344 ///
345 /// Note that this will not attempt to seek the archive to a valid position,
346 /// so if the archive is in the middle of a read or some other similar
347 /// operation then this may corrupt the archive.
348 ///
349 /// Also note that after all files have been written to an archive the
350 /// `finish` function needs to be called to finish writing the archive.
351 ///
352 /// # Examples
353 ///
354 /// ```
355 /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { tokio::runtime::Runtime::new().unwrap().block_on(async {
356 /// #
357 /// use tokio::fs;
358 /// use tokio_tar::Builder;
359 ///
360 /// let mut ar = Builder::new(Vec::new());
361 ///
362 /// // Use the directory at one location, but insert it into the archive
363 /// // with a different name.
364 /// ar.append_dir("bardir", ".").await?;
365 /// #
366 /// # Ok(()) }) }
367 /// ```
368 pub async fn append_dir<P, Q>(&mut self, path: P, src_path: Q) -> io::Result<()>
369 where
370 P: AsRef<Path>,
371 Q: AsRef<Path>,
372 {
373 let mode = self.mode;
374 append_dir(self.get_mut(), path.as_ref(), src_path.as_ref(), mode).await?;
375 Ok(())
376 }
377
378 /// Adds a directory and all of its contents (recursively) to this archive
379 /// with the given path as the name of the directory in the archive.
380 ///
381 /// Note that this will not attempt to seek the archive to a valid position,
382 /// so if the archive is in the middle of a read or some other similar
383 /// operation then this may corrupt the archive.
384 ///
385 /// Also note that after all files have been written to an archive the
386 /// `finish` function needs to be called to finish writing the archive.
387 ///
388 /// # Examples
389 ///
390 /// ```
391 /// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { tokio::runtime::Runtime::new().unwrap().block_on(async {
392 /// #
393 /// use tokio::fs;
394 /// use tokio_tar::Builder;
395 ///
396 /// let mut ar = Builder::new(Vec::new());
397 ///
398 /// let td = tempfile::tempdir()?;
399 ///
400 /// // Use the directory at one location, but insert it into the archive
401 /// // with a different name.
402 /// ar.append_dir_all("bardir", td.path()).await?;
403 /// #
404 /// # Ok(()) }) }
405 /// ```
406 pub async fn append_dir_all<P, Q>(&mut self, path: P, src_path: Q) -> io::Result<()>
407 where
408 P: AsRef<Path>,
409 Q: AsRef<Path>,
410 {
411 let mode = self.mode;
412 let follow = self.follow;
413 append_dir_all(
414 self.get_mut(),
415 path.as_ref(),
416 src_path.as_ref(),
417 mode,
418 follow,
419 )
420 .await?;
421 Ok(())
422 }
423
424 /// Finish writing this archive, emitting the termination sections.
425 ///
426 /// This function should only be called when the archive has been written
427 /// entirely and if an I/O error happens the underlying object still needs
428 /// to be acquired.
429 ///
430 /// In most situations the `into_inner` method should be preferred.
431 pub async fn finish(&mut self) -> io::Result<()> {
432 if self.finished {
433 return Ok(());
434 }
435 self.finished = true;
436 self.get_mut().write_all(&[0; 1024]).await?;
437 Ok(())
438 }
439}
440
441async fn append<Dst: Write + Unpin + ?Sized, Data: Read + Unpin + ?Sized>(
442 mut dst: &mut Dst,
443 header: &Header,
444 mut data: &mut Data,
445) -> io::Result<()> {
446 dst.write_all(header.as_bytes()).await?;
447 let len = io::copy(&mut data, &mut dst).await?;
448
449 // Pad with zeros if necessary.
450 let buf = [0; 512];
451 let remaining = 512 - (len % 512);
452 if remaining < 512 {
453 dst.write_all(&buf[..remaining as usize]).await?;
454 }
455
456 Ok(())
457}
458
459async fn append_path_with_name<Dst: Write + Unpin + ?Sized>(
460 dst: &mut Dst,
461 path: &Path,
462 name: Option<&Path>,
463 mode: HeaderMode,
464 follow: bool,
465) -> io::Result<()> {
466 let stat = if follow {
467 fs::metadata(path).await.map_err(|err| {
468 io::Error::new(
469 err.kind(),
470 format!("{} when getting metadata for {}", err, path.display()),
471 )
472 })?
473 } else {
474 fs::symlink_metadata(path).await.map_err(|err| {
475 io::Error::new(
476 err.kind(),
477 format!("{} when getting metadata for {}", err, path.display()),
478 )
479 })?
480 };
481 let ar_name = name.unwrap_or(path);
482 if stat.is_file() {
483 append_fs(
484 dst,
485 ar_name,
486 &stat,
487 &mut fs::File::open(path).await?,
488 mode,
489 None,
490 )
491 .await?;
492 Ok(())
493 } else if stat.is_dir() {
494 append_fs(dst, ar_name, &stat, &mut io::empty(), mode, None).await?;
495 Ok(())
496 } else if stat.file_type().is_symlink() {
497 let link_name = fs::read_link(path).await?;
498 append_fs(
499 dst,
500 ar_name,
501 &stat,
502 &mut io::empty(),
503 mode,
504 Some(&link_name),
505 )
506 .await?;
507 Ok(())
508 } else {
509 #[cfg(unix)]
510 {
511 append_special(dst, path, &stat, mode).await
512 }
513 #[cfg(not(unix))]
514 {
515 Err(other(&format!("{} has unknown file type", path.display())))
516 }
517 }
518}
519
520#[cfg(unix)]
521async fn append_special<Dst: Write + Unpin + ?Sized>(
522 dst: &mut Dst,
523 path: &Path,
524 stat: &Metadata,
525 mode: HeaderMode,
526) -> io::Result<()> {
527 use ::std::os::unix::fs::{FileTypeExt, MetadataExt};
528
529 let file_type = stat.file_type();
530 let entry_type;
531 if file_type.is_socket() {
532 // sockets can't be archived
533 return Err(other(&format!(
534 "{}: socket can not be archived",
535 path.display()
536 )));
537 } else if file_type.is_fifo() {
538 entry_type = EntryType::Fifo;
539 } else if file_type.is_char_device() {
540 entry_type = EntryType::Char;
541 } else if file_type.is_block_device() {
542 entry_type = EntryType::Block;
543 } else {
544 return Err(other(&format!("{} has unknown file type", path.display())));
545 }
546
547 let mut header = Header::new_gnu();
548 header.set_metadata_in_mode(stat, mode);
549 prepare_header_path(dst, &mut header, path).await?;
550
551 header.set_entry_type(entry_type);
552 let dev_id = stat.rdev();
553 let dev_major = ((dev_id >> 32) & 0xffff_f000) | ((dev_id >> 8) & 0x0000_0fff);
554 let dev_minor = ((dev_id >> 12) & 0xffff_ff00) | ((dev_id) & 0x0000_00ff);
555 header.set_device_major(dev_major as u32)?;
556 header.set_device_minor(dev_minor as u32)?;
557
558 header.set_cksum();
559 dst.write_all(header.as_bytes()).await?;
560
561 Ok(())
562}
563
564async fn append_file<Dst: Write + Unpin + ?Sized>(
565 dst: &mut Dst,
566 path: &Path,
567 file: &mut fs::File,
568 mode: HeaderMode,
569) -> io::Result<()> {
570 let stat = file.metadata().await?;
571 append_fs(dst, path, &stat, file, mode, None).await?;
572 Ok(())
573}
574
575async fn append_dir<Dst: Write + Unpin + ?Sized>(
576 dst: &mut Dst,
577 path: &Path,
578 src_path: &Path,
579 mode: HeaderMode,
580) -> io::Result<()> {
581 let stat = fs::metadata(src_path).await?;
582 append_fs(dst, path, &stat, &mut io::empty(), mode, None).await?;
583 Ok(())
584}
585
586fn prepare_header(size: u64, entry_type: EntryType) -> Header {
587 let mut header = Header::new_gnu();
588 let name = b"././@LongLink";
589 header.as_gnu_mut().unwrap().name[..name.len()].clone_from_slice(&name[..]);
590 header.set_mode(0o644);
591 header.set_uid(0);
592 header.set_gid(0);
593 header.set_mtime(0);
594 // + 1 to be compliant with GNU tar
595 header.set_size(size + 1);
596 header.set_entry_type(entry_type);
597 header.set_cksum();
598 header
599}
600
601async fn prepare_header_path<Dst: Write + Unpin + ?Sized>(
602 dst: &mut Dst,
603 header: &mut Header,
604 path: &Path,
605) -> io::Result<()> {
606 // Try to encode the path directly in the header, but if it ends up not
607 // working (probably because it's too long) then try to use the GNU-specific
608 // long name extension by emitting an entry which indicates that it's the
609 // filename.
610 if let Err(e) = header.set_path(path) {
611 let data = path2bytes(path)?;
612 let max = header.as_old().name.len();
613 // Since e isn't specific enough to let us know the path is indeed too
614 // long, verify it first before using the extension.
615 if data.len() < max {
616 return Err(e);
617 }
618 let header2 = prepare_header(data.len() as u64, EntryType::GNULongName);
619 // null-terminated string
620 let mut data2 = data.chain(io::repeat(0).take(1));
621 append(dst, &header2, &mut data2).await?;
622
623 // Truncate the path to store in the header we're about to emit to
624 // ensure we've got something at least mentioned. Note that we use
625 // `str`-encoding to be compatible with Windows, but in general the
626 // entry in the header itself shouldn't matter too much since extraction
627 // doesn't look at it.
628 let truncated = match str::from_utf8(&data[..max]) {
629 Ok(s) => s,
630 Err(e) => str::from_utf8(&data[..e.valid_up_to()]).unwrap(),
631 };
632 header.set_truncated_path_for_gnu_header(truncated)?;
633 }
634 Ok(())
635}
636
637async fn prepare_header_link<Dst: Write + Unpin + ?Sized>(
638 dst: &mut Dst,
639 header: &mut Header,
640 link_name: &Path,
641) -> io::Result<()> {
642 // Same as previous function but for linkname
643 if let Err(e) = header.set_link_name(link_name) {
644 let data = path2bytes(link_name)?;
645 if data.len() < header.as_old().linkname.len() {
646 return Err(e);
647 }
648 let header2 = prepare_header(data.len() as u64, EntryType::GNULongLink);
649 let mut data2 = data.chain(io::repeat(0).take(1));
650 append(dst, &header2, &mut data2).await?;
651 }
652 Ok(())
653}
654
655async fn append_fs<Dst: Write + Unpin + ?Sized, R: Read + Unpin + ?Sized>(
656 dst: &mut Dst,
657 path: &Path,
658 meta: &Metadata,
659 read: &mut R,
660 mode: HeaderMode,
661 link_name: Option<&Path>,
662) -> io::Result<()> {
663 let mut header = Header::new_gnu();
664
665 prepare_header_path(dst, &mut header, path).await?;
666 header.set_metadata_in_mode(meta, mode);
667 if let Some(link_name) = link_name {
668 prepare_header_link(dst, &mut header, link_name).await?;
669 }
670 header.set_cksum();
671 append(dst, &header, read).await?;
672
673 Ok(())
674}
675
676async fn append_dir_all<Dst: Write + Unpin + ?Sized>(
677 dst: &mut Dst,
678 path: &Path,
679 src_path: &Path,
680 mode: HeaderMode,
681 follow: bool,
682) -> io::Result<()> {
683 let mut stack = vec![(src_path.to_path_buf(), true, false)];
684 while let Some((src, is_dir, is_symlink)) = stack.pop() {
685 let dest = path.join(src.strip_prefix(src_path).unwrap());
686
687 // In case of a symlink pointing to a directory, is_dir is false, but src.is_dir() will return true
688 if is_dir || (is_symlink && follow && src.is_dir()) {
689 let mut entries = fs::read_dir(&src).await?;
690 while let Some(entry) = entries.next_entry().await.transpose() {
691 let entry = entry?;
692 let file_type = entry.file_type().await?;
693 stack.push((entry.path(), file_type.is_dir(), file_type.is_symlink()));
694 }
695 if dest != Path::new("") {
696 append_dir(dst, &dest, &src, mode).await?;
697 }
698 } else if !follow && is_symlink {
699 let stat = fs::symlink_metadata(&src).await?;
700 let link_name = fs::read_link(&src).await?;
701 append_fs(dst, &dest, &stat, &mut io::empty(), mode, Some(&link_name)).await?;
702 } else {
703 #[cfg(unix)]
704 {
705 let stat = fs::metadata(&src).await?;
706 if !stat.is_file() {
707 append_special(dst, &dest, &stat, mode).await?;
708 continue;
709 }
710 }
711 append_file(dst, &dest, &mut fs::File::open(src).await?, mode).await?;
712 }
713 }
714 Ok(())
715}
716
717impl<W: Write + Unpin + Send> Drop for Builder<W> {
718 fn drop(&mut self) {
719 // TODO: proper async cancellation
720 if !self.finished {
721 if let Some(cancellation) = self.cancellation.take() {
722 cancellation.send(self.obj.take().unwrap()).ok();
723 }
724 }
725 }
726}