]> git.lizzy.rs Git - rust.git/blob - src/librustc_llvm/archive_ro.rs
Rollup merge of #44989 - QuietMisdreavus:what-is-your-quest, r=GuillaumeGomez
[rust.git] / src / librustc_llvm / archive_ro.rs
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.
4 //
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.
10
11 //! A wrapper around LLVM's archive (.a) code
12
13 use ArchiveRef;
14
15 use std::ffi::CString;
16 use std::marker;
17 use std::path::Path;
18 use std::slice;
19 use std::str;
20
21 pub struct ArchiveRO {
22     ptr: ArchiveRef,
23 }
24
25 pub struct Iter<'a> {
26     archive: &'a ArchiveRO,
27     ptr: ::ArchiveIteratorRef,
28 }
29
30 pub struct Child<'a> {
31     ptr: ::ArchiveChildRef,
32     _data: marker::PhantomData<&'a ArchiveRO>,
33 }
34
35 impl 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.
39     ///
40     /// If this archive is used with a mutable method, then an error will be
41     /// raised.
42     pub fn open(dst: &Path) -> Result<ArchiveRO, String> {
43         return unsafe {
44             let s = path2cstr(dst);
45             let ar = ::LLVMRustOpenArchive(s.as_ptr());
46             if ar.is_null() {
47                 Err(::last_error().unwrap_or("failed to open archive".to_string()))
48             } else {
49                 Ok(ArchiveRO { ptr: ar })
50             }
51         };
52
53         #[cfg(unix)]
54         fn path2cstr(p: &Path) -> CString {
55             use std::os::unix::prelude::*;
56             use std::ffi::OsStr;
57             let p: &OsStr = p.as_ref();
58             CString::new(p.as_bytes()).unwrap()
59         }
60         #[cfg(windows)]
61         fn path2cstr(p: &Path) -> CString {
62             CString::new(p.to_str().unwrap()).unwrap()
63         }
64     }
65
66     pub fn raw(&self) -> ArchiveRef {
67         self.ptr
68     }
69
70     pub fn iter(&self) -> Iter {
71         unsafe {
72             Iter {
73                 ptr: ::LLVMRustArchiveIteratorNew(self.ptr),
74                 archive: self,
75             }
76         }
77     }
78 }
79
80 impl Drop for ArchiveRO {
81     fn drop(&mut self) {
82         unsafe {
83             ::LLVMRustDestroyArchive(self.ptr);
84         }
85     }
86 }
87
88 impl<'a> Iterator for Iter<'a> {
89     type Item = Result<Child<'a>, String>;
90
91     fn next(&mut self) -> Option<Result<Child<'a>, String>> {
92         let ptr = unsafe { ::LLVMRustArchiveIteratorNext(self.ptr) };
93         if ptr.is_null() {
94             ::last_error().map(Err)
95         } else {
96             Some(Ok(Child {
97                 ptr,
98                 _data: marker::PhantomData,
99             }))
100         }
101     }
102 }
103
104 impl<'a> Drop for Iter<'a> {
105     fn drop(&mut self) {
106         unsafe {
107             ::LLVMRustArchiveIteratorFree(self.ptr);
108         }
109     }
110 }
111
112 impl<'a> Child<'a> {
113     pub fn name(&self) -> Option<&'a str> {
114         unsafe {
115             let mut name_len = 0;
116             let name_ptr = ::LLVMRustArchiveChildName(self.ptr, &mut name_len);
117             if name_ptr.is_null() {
118                 None
119             } else {
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())
122             }
123         }
124     }
125
126     pub fn data(&self) -> &'a [u8] {
127         unsafe {
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");
132             }
133             slice::from_raw_parts(data_ptr as *const u8, data_len as usize)
134         }
135     }
136
137     pub fn raw(&self) -> ::ArchiveChildRef {
138         self.ptr
139     }
140 }
141
142 impl<'a> Drop for Child<'a> {
143     fn drop(&mut self) {
144         unsafe {
145             ::LLVMRustArchiveChildFree(self.ptr);
146         }
147     }
148 }