From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [RFC PATCH proxmox-backup 4/5] pxar/extract: add extract_sub_dir
Date: Mon, 21 Dec 2020 12:25:06 +0100 [thread overview]
Message-ID: <20201221112507.30450-4-d.csapak@proxmox.com> (raw)
In-Reply-To: <20201221112507.30450-1-d.csapak@proxmox.com>
to extract some subdirectory of a pxar into a given target
this will be used in the client
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
the code looks *very* similar to what we do in 'extract_archive' or
in the fuse extract method, but not quite... maybe there is a good
way to refactor that which i am not seeing?
| 122 ++++++++++++++++++++++++++++++++++++++++++++
src/pxar/mod.rs | 1 +
2 files changed, 123 insertions(+)
--git a/src/pxar/extract.rs b/src/pxar/extract.rs
index 77472f56..1f88c6e8 100644
--- a/src/pxar/extract.rs
+++ b/src/pxar/extract.rs
@@ -575,3 +575,125 @@ where
})
}
+
+pub async fn extract_sub_dir<T, DEST, PATH>(
+ destination: DEST,
+ mut decoder: Accessor<T>,
+ path: PATH,
+ verbose: bool,
+) -> Result<(), Error>
+where
+ T: Clone + pxar::accessor::ReadAt + Unpin + Send + Sync + 'static,
+ DEST: AsRef<Path>,
+ PATH: AsRef<Path>,
+{
+ let root = decoder.open_root().await?;
+
+ create_path(
+ &destination,
+ None,
+ Some(CreateOptions::new().perm(Mode::from_bits_truncate(0o700))),
+ )
+ .map_err(|err| format_err!("error creating directory {:?}: {}", destination.as_ref(), err))?;
+
+ let dir = Dir::open(
+ destination.as_ref(),
+ OFlag::O_DIRECTORY | OFlag::O_CLOEXEC,
+ Mode::empty(),
+ )
+ .map_err(|err| format_err!("unable to open target directory {:?}: {}", destination.as_ref(), err,))?;
+
+ let mut extractor = Extractor::new(
+ dir,
+ root.lookup_self().await?.entry().metadata().clone(),
+ false,
+ Flags::DEFAULT,
+ );
+
+ let file = root
+ .lookup(&path).await?
+ .ok_or(format_err!("error opening '{:?}'", path.as_ref()))?;
+
+ recurse_files_extractor(&mut extractor, &mut decoder, file, verbose).await
+}
+
+fn recurse_files_extractor<'a, T>(
+ extractor: &'a mut Extractor,
+ decoder: &'a mut Accessor<T>,
+ file: FileEntry<T>,
+ verbose: bool,
+) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'a>>
+where
+ T: Clone + pxar::accessor::ReadAt + Unpin + Send + Sync + 'static,
+{
+ use pxar::EntryKind;
+ Box::pin(async move {
+ let metadata = file.entry().metadata();
+ let file_name_os = file.file_name();
+
+ // safety check: a file entry in an archive must never contain slashes:
+ if file_name_os.as_bytes().contains(&b'/') {
+ bail!("archive file entry contains slashes, which is invalid and a security concern");
+ }
+
+ let file_name = CString::new(file_name_os.as_bytes())
+ .map_err(|_| format_err!("encountered file name with null-bytes"))?;
+
+ if verbose {
+ eprintln!("extracting: {}", file.path().display());
+ }
+
+ match file.kind() {
+ EntryKind::Directory => {
+ extractor
+ .enter_directory(file_name_os.to_owned(), metadata.clone(), true)
+ .map_err(|err| format_err!("error at entry {:?}: {}", file_name_os, err))?;
+
+ let dir = file.enter_directory().await?;
+ let mut readdir = dir.read_dir();
+ while let Some(entry) = readdir.next().await {
+ let entry = entry?.decode_entry().await?;
+ let filename = entry.path().to_path_buf();
+
+ // log errors and continue
+ if let Err(err) = recurse_files_extractor(extractor, decoder, entry, verbose).await {
+ eprintln!("error extracting {:?}: {}", filename.display(), err);
+ }
+ }
+ extractor.leave_directory()?;
+ }
+ EntryKind::Symlink(link) => {
+ extractor.extract_symlink(&file_name, metadata, link.as_ref())?;
+ }
+ EntryKind::Hardlink(link) => {
+ extractor.extract_hardlink(&file_name, link.as_os_str())?;
+ }
+ EntryKind::Device(dev) => {
+ if extractor.contains_flags(Flags::WITH_DEVICE_NODES) {
+ extractor.extract_device(&file_name, metadata, dev)?;
+ }
+ }
+ EntryKind::Fifo => {
+ if extractor.contains_flags(Flags::WITH_FIFOS) {
+ extractor.extract_special(&file_name, metadata, 0)?;
+ }
+ }
+ EntryKind::Socket => {
+ if extractor.contains_flags(Flags::WITH_SOCKETS) {
+ extractor.extract_special(&file_name, metadata, 0)?;
+ }
+ }
+ EntryKind::File { size, .. } => extractor.async_extract_file(
+ &file_name,
+ metadata,
+ *size,
+ &mut file.contents().await.map_err(|_| {
+ format_err!("found regular file entry without contents in archive")
+ })?,
+ ).await?,
+ EntryKind::GoodbyeTable => {}, // ignore
+ }
+ Ok(())
+ })
+}
+
diff --git a/src/pxar/mod.rs b/src/pxar/mod.rs
index ba47e220..d3c1ca16 100644
--- a/src/pxar/mod.rs
+++ b/src/pxar/mod.rs
@@ -62,6 +62,7 @@ pub use create::create_archive;
pub use extract::{
extract_archive,
create_zip,
+ extract_sub_dir,
};
/// The format requires to build sorted directory lookup tables in
--
2.20.1
next prev parent reply other threads:[~2020-12-21 11:25 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-21 11:25 [pbs-devel] [RFC PATCH proxmox-backup 1/5] api2/admin/datastore: refactor list_dir_content in catalog_reader Dominik Csapak
2020-12-21 11:25 ` [pbs-devel] [RFC PATCH proxmox-backup 2/5] api2/admin/datastore: accept "/" as path for root Dominik Csapak
2020-12-21 11:25 ` [pbs-devel] [RFC PATCH proxmox-backup 3/5] api2/admin/datastore: refactor create_zip into pxar/extract Dominik Csapak
2020-12-21 11:25 ` Dominik Csapak [this message]
2020-12-21 11:25 ` [pbs-devel] [RFC PATCH 5/5] proxmox-backup-client: add file-restore commands Dominik Csapak
2020-12-21 11:43 ` Dominik Csapak
2020-12-22 5:49 ` [pbs-devel] [RFC PATCH proxmox-backup 1/5] api2/admin/datastore: refactor list_dir_content in catalog_reader Dietmar Maurer
2020-12-22 7:52 ` Dominik Csapak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201221112507.30450-4-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.