]> git.proxmox.com Git - rustc.git/blob - src/librustc_llvm/archive_ro.rs
14a99026aac8ac18c8e06b377e68981ca534f52b
[rustc.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 libc;
14 use ArchiveRef;
15
16 use std::ffi::CString;
17 use std::mem;
18 use std::raw;
19
20 pub struct ArchiveRO {
21 ptr: ArchiveRef,
22 }
23
24 impl ArchiveRO {
25 /// Opens a static archive for read-only purposes. This is more optimized
26 /// than the `open` method because it uses LLVM's internal `Archive` class
27 /// rather than shelling out to `ar` for everything.
28 ///
29 /// If this archive is used with a mutable method, then an error will be
30 /// raised.
31 pub fn open(dst: &Path) -> Option<ArchiveRO> {
32 unsafe {
33 let s = CString::new(dst.as_vec()).unwrap();
34 let ar = ::LLVMRustOpenArchive(s.as_ptr());
35 if ar.is_null() {
36 None
37 } else {
38 Some(ArchiveRO { ptr: ar })
39 }
40 }
41 }
42
43 /// Reads a file in the archive
44 pub fn read<'a>(&'a self, file: &str) -> Option<&'a [u8]> {
45 unsafe {
46 let mut size = 0 as libc::size_t;
47 let file = CString::new(file).unwrap();
48 let ptr = ::LLVMRustArchiveReadSection(self.ptr, file.as_ptr(),
49 &mut size);
50 if ptr.is_null() {
51 None
52 } else {
53 Some(mem::transmute(raw::Slice {
54 data: ptr,
55 len: size as uint,
56 }))
57 }
58 }
59 }
60 }
61
62 impl Drop for ArchiveRO {
63 fn drop(&mut self) {
64 unsafe {
65 ::LLVMRustDestroyArchive(self.ptr);
66 }
67 }
68 }