1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A wrapper around LLVM's archive (.a) code
15 use std::ffi::CString;
21 pub struct ArchiveRO {
26 archive: &'a ArchiveRO,
27 ptr: ::ArchiveIteratorRef,
30 pub struct Child<'a> {
31 ptr: ::ArchiveChildRef,
32 _data: marker::PhantomData<&'a ArchiveRO>,
36 /// Opens a static archive for read-only purposes. This is more optimized
37 /// than the `open` method because it uses LLVM's internal `Archive` class
38 /// rather than shelling out to `ar` for everything.
40 /// If this archive is used with a mutable method, then an error will be
42 pub fn open(dst: &Path) -> Result<ArchiveRO, String> {
44 let s = path2cstr(dst);
45 let ar = ::LLVMRustOpenArchive(s.as_ptr());
47 Err(::last_error().unwrap_or("failed to open archive".to_string()))
49 Ok(ArchiveRO { ptr: ar })
54 fn path2cstr(p: &Path) -> CString {
55 use std::os::unix::prelude::*;
57 let p: &OsStr = p.as_ref();
58 CString::new(p.as_bytes()).unwrap()
61 fn path2cstr(p: &Path) -> CString {
62 CString::new(p.to_str().unwrap()).unwrap()
66 pub fn raw(&self) -> ArchiveRef {
70 pub fn iter(&self) -> Iter {
73 ptr: ::LLVMRustArchiveIteratorNew(self.ptr),
80 impl Drop for ArchiveRO {
83 ::LLVMRustDestroyArchive(self.ptr);
88 impl<'a> Iterator for Iter<'a> {
89 type Item = Result<Child<'a>, String>;
91 fn next(&mut self) -> Option<Result<Child<'a>, String>> {
92 let ptr = unsafe { ::LLVMRustArchiveIteratorNext(self.ptr) };
94 ::last_error().map(Err)
98 _data: marker::PhantomData,
104 impl<'a> Drop for Iter<'a> {
107 ::LLVMRustArchiveIteratorFree(self.ptr);
113 pub fn name(&self) -> Option<&'a str> {
115 let mut name_len = 0;
116 let name_ptr = ::LLVMRustArchiveChildName(self.ptr, &mut name_len);
117 if name_ptr.is_null() {
120 let name = slice::from_raw_parts(name_ptr as *const u8, name_len as usize);
121 str::from_utf8(name).ok().map(|s| s.trim())
126 pub fn data(&self) -> &'a [u8] {
128 let mut data_len = 0;
129 let data_ptr = ::LLVMRustArchiveChildData(self.ptr, &mut data_len);
130 if data_ptr.is_null() {
131 panic!("failed to read data from archive child");
133 slice::from_raw_parts(data_ptr as *const u8, data_len as usize)
137 pub fn raw(&self) -> ::ArchiveChildRef {
142 impl<'a> Drop for Child<'a> {
145 ::LLVMRustArchiveChildFree(self.ptr);