Skip to content

Commit 3bf724e

Browse files
author
Robert Gawdzik
committed
Refactored get_metadata_section to return a Result<T,~str>, added error messages. Closes #6615.
1 parent 33768c4 commit 3bf724e

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/librustc/metadata/loader.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -298,17 +298,17 @@ impl<'a> Context<'a> {
298298

299299
let lib = m.move_iter().next().unwrap();
300300
if slot.is_none() {
301-
info!("{} reading meatadata from: {}", flavor, lib.display());
301+
info!("{} reading metadata from: {}", flavor, lib.display());
302302
match get_metadata_section(self.os, &lib) {
303-
Some(blob) => {
303+
Ok(blob) => {
304304
if self.crate_matches(blob.as_slice()) {
305305
*slot = Some(blob);
306306
} else {
307307
info!("metadata mismatch");
308308
return None;
309309
}
310310
}
311-
None => {
311+
Err(_) => {
312312
info!("no metadata found");
313313
return None
314314
}
@@ -387,15 +387,18 @@ impl ArchiveMetadata {
387387
}
388388

389389
// Just a small wrapper to time how long reading metadata takes.
390-
fn get_metadata_section(os: Os, filename: &Path) -> Option<MetadataBlob> {
390+
fn get_metadata_section(os: Os, filename: &Path) -> Result<MetadataBlob, ~str> {
391391
let start = time::precise_time_ns();
392392
let ret = get_metadata_section_imp(os, filename);
393393
info!("reading {} => {}ms", filename.filename_display(),
394394
(time::precise_time_ns() - start) / 1000000);
395395
return ret;
396396
}
397397

398-
fn get_metadata_section_imp(os: Os, filename: &Path) -> Option<MetadataBlob> {
398+
fn get_metadata_section_imp(os: Os, filename: &Path) -> Result<MetadataBlob, ~str> {
399+
if !filename.exists() {
400+
return Err(format!("no such file: '{}'", filename.display()));
401+
}
399402
if filename.filename_str().unwrap().ends_with(".rlib") {
400403
// Use ArchiveRO for speed here, it's backed by LLVM and uses mmap
401404
// internally to read the file. We also avoid even using a memcpy by
@@ -404,19 +407,26 @@ fn get_metadata_section_imp(os: Os, filename: &Path) -> Option<MetadataBlob> {
404407
Some(ar) => ar,
405408
None => {
406409
debug!("llvm didn't like `{}`", filename.display());
407-
return None;
410+
return Err(format!("failed to read rlib metadata: '{}'",
411+
filename.display()));
408412
}
409413
};
410-
return ArchiveMetadata::new(archive).map(|ar| MetadataArchive(ar));
414+
return match ArchiveMetadata::new(archive).map(|ar| MetadataArchive(ar)) {
415+
None => return Err(format!("failed to read rlib metadata: '{}'",
416+
filename.display())),
417+
Some(blob) => return Ok(blob)
418+
}
411419
}
412420
unsafe {
413421
let mb = filename.with_c_str(|buf| {
414422
llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
415423
});
416-
if mb as int == 0 { return None }
424+
if mb as int == 0 {
425+
return Err(format!("error reading library: '{}'",filename.display()))
426+
}
417427
let of = match ObjectFile::new(mb) {
418428
Some(of) => of,
419-
_ => return None
429+
_ => return Err(format!("provided path not an object file: '{}'", filename.display()))
420430
};
421431
let si = mk_section_iter(of.llof);
422432
while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
@@ -426,30 +436,31 @@ fn get_metadata_section_imp(os: Os, filename: &Path) -> Option<MetadataBlob> {
426436
if read_meta_section_name(os) == name {
427437
let cbuf = llvm::LLVMGetSectionContents(si.llsi);
428438
let csz = llvm::LLVMGetSectionSize(si.llsi) as uint;
429-
let mut found = None;
439+
let mut found = Err(format!("metadata not found: '{}'", filename.display()));
430440
let cvbuf: *u8 = cast::transmute(cbuf);
431441
let vlen = encoder::metadata_encoding_version.len();
432442
debug!("checking {} bytes of metadata-version stamp",
433443
vlen);
434444
let minsz = cmp::min(vlen, csz);
435445
let version_ok = vec::raw::buf_as_slice(cvbuf, minsz,
436446
|buf0| buf0 == encoder::metadata_encoding_version);
437-
if !version_ok { return None; }
447+
if !version_ok { return Err(format!("incompatible metadata version found: '{}'",
448+
filename.display()));}
438449

439450
let cvbuf1 = cvbuf.offset(vlen as int);
440451
debug!("inflating {} bytes of compressed metadata",
441452
csz - vlen);
442453
vec::raw::buf_as_slice(cvbuf1, csz-vlen, |bytes| {
443454
let inflated = flate::inflate_bytes(bytes);
444-
found = Some(MetadataVec(inflated));
455+
found = Ok(MetadataVec(inflated));
445456
});
446-
if found.is_some() {
457+
if found.is_ok() {
447458
return found;
448459
}
449460
}
450461
llvm::LLVMMoveToNextSection(si.llsi);
451462
}
452-
return None;
463+
return Err(format!("metadata not found: '{}'", filename.display()));
453464
}
454465
}
455466

@@ -477,9 +488,9 @@ pub fn read_meta_section_name(os: Os) -> &'static str {
477488
pub fn list_file_metadata(os: Os, path: &Path,
478489
out: &mut io::Writer) -> io::IoResult<()> {
479490
match get_metadata_section(os, path) {
480-
Some(bytes) => decoder::list_crate_metadata(bytes.as_slice(), out),
481-
None => {
482-
write!(out, "could not find metadata in {}.\n", path.display())
491+
Ok(bytes) => decoder::list_crate_metadata(bytes.as_slice(), out),
492+
Err(msg) => {
493+
write!(out, "{}\n", msg)
483494
}
484495
}
485496
}

0 commit comments

Comments
 (0)