Skip to content

Commit 5d1d285

Browse files
committed
auto merge of #12758 : rgawdzik/rust/master, r=alexcrichton
Refactored get_metadata_section to return a Result<MetadataBlob,~str> instead of a Option<MetadataBlob>. This provides more clarity to the user through the debug output when using --ls. This is kind of a continuation of my original closed pull request 2 months ago (#11544), but I think the time-span constitutes a new pull request.
2 parents 5f64adc + 3bf724e commit 5d1d285

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
@@ -299,17 +299,17 @@ impl<'a> Context<'a> {
299299

300300
let lib = m.move_iter().next().unwrap();
301301
if slot.is_none() {
302-
info!("{} reading meatadata from: {}", flavor, lib.display());
302+
info!("{} reading metadata from: {}", flavor, lib.display());
303303
match get_metadata_section(self.os, &lib) {
304-
Some(blob) => {
304+
Ok(blob) => {
305305
if self.crate_matches(blob.as_slice()) {
306306
*slot = Some(blob);
307307
} else {
308308
info!("metadata mismatch");
309309
return None;
310310
}
311311
}
312-
None => {
312+
Err(_) => {
313313
info!("no metadata found");
314314
return None
315315
}
@@ -388,15 +388,18 @@ impl ArchiveMetadata {
388388
}
389389

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

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

440451
let cvbuf1 = cvbuf.offset(vlen as int);
441452
debug!("inflating {} bytes of compressed metadata",
442453
csz - vlen);
443454
vec::raw::buf_as_slice(cvbuf1, csz-vlen, |bytes| {
444455
let inflated = flate::inflate_bytes(bytes);
445-
found = Some(MetadataVec(inflated));
456+
found = Ok(MetadataVec(inflated));
446457
});
447-
if found.is_some() {
458+
if found.is_ok() {
448459
return found;
449460
}
450461
}
451462
llvm::LLVMMoveToNextSection(si.llsi);
452463
}
453-
return None;
464+
return Err(format!("metadata not found: '{}'", filename.display()));
454465
}
455466
}
456467

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

0 commit comments

Comments
 (0)