From fc8b870317b28f90f0be2a0b72ed5beeb4ceb176 Mon Sep 17 00:00:00 2001 From: Jamie Winsor Date: Mon, 21 Mar 2016 17:06:57 -0700 Subject: [PATCH] write prefix for hardlinks on Disk restore --- libraries/libarchive/src/archive.rs | 27 ++++++++++++++++++++++++++- libraries/libarchive/src/writer.rs | 6 +++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/libraries/libarchive/src/archive.rs b/libraries/libarchive/src/archive.rs index ff5a1dfb..2a798702 100644 --- a/libraries/libarchive/src/archive.rs +++ b/libraries/libarchive/src/archive.rs @@ -139,6 +139,18 @@ pub trait Entry { } } + fn hardlink(&self) -> Option<&str> { + let c_str: &CStr = unsafe { + let ptr = ffi::archive_entry_hardlink(self.entry()); + if ptr.is_null() { + return None; + } + CStr::from_ptr(ptr) + }; + let buf: &[u8] = c_str.to_bytes(); + Some(str::from_utf8(buf).unwrap()) + } + fn pathname(&self) -> &str { let c_str: &CStr = unsafe { CStr::from_ptr(ffi::archive_entry_pathname(self.entry())) }; let buf: &[u8] = c_str.to_bytes(); @@ -149,6 +161,12 @@ pub trait Entry { unsafe { ffi::archive_entry_size(self.entry()) } } + fn symlink(&self) -> &str { + let c_str: &CStr = unsafe { CStr::from_ptr(ffi::archive_entry_symlink(self.entry())) }; + let buf: &[u8] = c_str.to_bytes(); + str::from_utf8(buf).unwrap() + } + fn set_filetype(&mut self, file_type: FileType) { unsafe { let file_type = match file_type { @@ -165,7 +183,14 @@ pub trait Entry { } } - fn set_pathname(&mut self, path: PathBuf) { + fn set_link(&mut self, path: &PathBuf) { + unsafe { + let c_str = CString::new(path.to_str().unwrap()).unwrap(); + ffi::archive_entry_set_link(self.entry(), c_str.as_ptr()); + } + } + + fn set_pathname(&mut self, path: &PathBuf) { unsafe { let c_str = CString::new(path.to_str().unwrap()).unwrap(); ffi::archive_entry_set_pathname(self.entry(), c_str.as_ptr()); diff --git a/libraries/libarchive/src/writer.rs b/libraries/libarchive/src/writer.rs index 6b79b2fc..9218aa2f 100644 --- a/libraries/libarchive/src/writer.rs +++ b/libraries/libarchive/src/writer.rs @@ -113,7 +113,11 @@ impl Disk { if let Some(entry) = reader.next_header() { if let Some(pfx) = prefix { let path = Path::new(pfx).join(entry.pathname()); - entry.set_pathname(path); + entry.set_pathname(&path); + if entry.hardlink().is_some() { + let path = Path::new(pfx).join(entry.hardlink().unwrap()); + entry.set_link(&path); + } } match self.write_header(entry) { Ok(()) => (),