From d4d2b34e342233cbc5498328406c9ae873965547 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Sun, 16 Oct 2016 12:41:34 +0200 Subject: [PATCH 1/2] Entry::symlink(): Return Option<&str> libarchive may return NULL. --- src/archive.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/archive.rs b/src/archive.rs index 2a79870..9142252 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -161,10 +161,16 @@ 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())) }; + fn symlink(&self) -> Option<&str> { + let c_str: &CStr = unsafe { + let ptr = ffi::archive_entry_symlink(self.entry()); + if ptr.is_null() { + return None; + } + CStr::from_ptr(ptr) + }; let buf: &[u8] = c_str.to_bytes(); - str::from_utf8(buf).unwrap() + Some(str::from_utf8(buf).unwrap()) } fn set_filetype(&mut self, file_type: FileType) { From 3f723cf0064561f21f0cebbd534a75076e6dbcaa Mon Sep 17 00:00:00 2001 From: Yorhel Date: Sun, 16 Oct 2016 15:06:46 +0200 Subject: [PATCH 2/2] Add Entry::FileType::Unknown to represent value 0 archive_entry_filetype() can return 0. It does this for hard links in a GNU tar-created archive. --- src/archive.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/archive.rs b/src/archive.rs index 9142252..bf19950 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -101,6 +101,7 @@ pub enum FileType { NamedPipe, Mount, RegularFile, + Unknown, } pub trait Handle { @@ -134,6 +135,7 @@ pub trait Entry { ffi::AE_IFMT => FileType::Mount, ffi::AE_IFREG => FileType::RegularFile, ffi::AE_IFSOCK => FileType::Socket, + 0 => FileType::Unknown, code => unreachable!("undefined filetype: {}", code), } } @@ -184,6 +186,7 @@ pub trait Entry { FileType::Mount => ffi::AE_IFMT, FileType::RegularFile => ffi::AE_IFREG, FileType::Socket => ffi::AE_IFSOCK, + FileType::Unknown => 0, }; ffi::archive_entry_set_filetype(self.entry(), file_type); }