Skip to content

Commit f74789f

Browse files
committed
Standardize File errors
1 parent d3d56be commit f74789f

File tree

5 files changed

+46
-38
lines changed

5 files changed

+46
-38
lines changed

src/combiner.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::fs;
1211
use std::io::{Read, Write};
1312
use std::path::Path;
1413
use flate2::read::GzDecoder;
@@ -63,12 +62,10 @@ impl Combiner {
6362
create_dir_all(&package_dir)?;
6463

6564
// Merge each installer into the work directory of the new installer
66-
let components = fs::File::create(package_dir.join("components"))
67-
.chain_err(|| "failed to create a new components file")?;
65+
let components = create_new_file(package_dir.join("components"))?;
6866
for input_tarball in self.input_tarballs.split(',').map(str::trim).filter(|s| !s.is_empty()) {
6967
// Extract the input tarballs
70-
fs::File::open(&input_tarball)
71-
.and_then(GzDecoder::new)
68+
GzDecoder::new(open_file(&input_tarball)?)
7269
.and_then(|tar| Archive::new(tar).unpack(&self.work_dir))
7370
.chain_err(|| format!("unable to extract '{}' into '{}'",
7471
&input_tarball, self.work_dir))?;
@@ -79,17 +76,17 @@ impl Combiner {
7976

8077
// Verify the version number
8178
let mut version = String::new();
82-
fs::File::open(pkg_dir.join("rust-installer-version"))
83-
.and_then(|mut file| file.read_to_string(&mut version))
79+
open_file(pkg_dir.join("rust-installer-version"))
80+
.and_then(|mut file| file.read_to_string(&mut version).map_err(Error::from))
8481
.chain_err(|| format!("failed to read version in '{}'", input_tarball))?;
8582
if version.trim().parse() != Ok(::RUST_INSTALLER_VERSION) {
8683
bail!("incorrect installer version in {}", input_tarball);
8784
}
8885

8986
// Move components to the new combined installer
9087
let mut pkg_components = String::new();
91-
fs::File::open(pkg_dir.join("components"))
92-
.and_then(|mut file| file.read_to_string(&mut pkg_components))
88+
open_file(pkg_dir.join("components"))
89+
.and_then(|mut file| file.read_to_string(&mut pkg_components).map_err(Error::from))
9390
.chain_err(|| format!("failed to read components in '{}'", input_tarball))?;
9491
for component in pkg_components.split_whitespace() {
9592
// All we need to do is move the component directory
@@ -104,8 +101,8 @@ impl Combiner {
104101
drop(components);
105102

106103
// Write the installer version
107-
fs::File::create(package_dir.join("rust-installer-version"))
108-
.and_then(|file| writeln!(&file, "{}", ::RUST_INSTALLER_VERSION))
104+
let version = package_dir.join("rust-installer-version");
105+
writeln!(create_new_file(version)?, "{}", ::RUST_INSTALLER_VERSION)
109106
.chain_err(|| "failed to write new installer version")?;
110107

111108
// Copy the overlay

src/generator.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::fs;
1211
use std::io::Write;
1312
use std::path::Path;
1413

@@ -71,14 +70,14 @@ impl Generator {
7170
copy_and_manifest(self.image_dir.as_ref(), &component_dir, &self.bulk_dirs)?;
7271

7372
// Write the component name
74-
fs::File::create(package_dir.join("components"))
75-
.and_then(|file| writeln!(&file, "{}", self.component_name))
73+
let components = package_dir.join("components");
74+
writeln!(create_new_file(components)?, "{}", self.component_name)
7675
.chain_err(|| "failed to write the component file")?;
7776

7877
// Write the installer version (only used by combine-installers.sh)
79-
fs::File::create(package_dir.join("rust-installer-version"))
80-
.and_then(|file| writeln!(&file, "{}", ::RUST_INSTALLER_VERSION))
81-
.chain_err(|| "failed to write the installer version")?;
78+
let version = package_dir.join("rust-installer-version");
79+
writeln!(create_new_file(version)?, "{}", ::RUST_INSTALLER_VERSION)
80+
.chain_err(|| "failed to write new installer version")?;
8281

8382
// Copy the overlay
8483
if !self.non_installed_overlay.is_empty() {
@@ -110,7 +109,7 @@ impl Generator {
110109

111110
/// Copies the `src` directory recursively to `dst`, writing `manifest.in` too.
112111
fn copy_and_manifest(src: &Path, dst: &Path, bulk_dirs: &str) -> Result<()> {
113-
let manifest = fs::File::create(dst.join("manifest.in"))?;
112+
let manifest = create_new_file(dst.join("manifest.in"))?;
114113
let bulk_dirs: Vec<_> = bulk_dirs.split(',')
115114
.filter(|s| !s.is_empty())
116115
.map(Path::new).collect();

src/scripter.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::fs;
1211
use std::io::Write;
1312

14-
// Needed to set the script mode to executable.
15-
#[cfg(unix)]
16-
use std::os::unix::fs::OpenOptionsExt;
17-
// FIXME: what about Windows? Are default ACLs executable?
18-
1913
use errors::*;
14+
use util::*;
2015

2116
const TEMPLATE: &'static str = include_str!("../install-template.sh");
2217

@@ -59,11 +54,8 @@ impl Scripter {
5954
.replace("%%TEMPLATE_LEGACY_MANIFEST_DIRS%%", &sh_quote(&self.legacy_manifest_dirs))
6055
.replace("%%TEMPLATE_RUST_INSTALLER_VERSION%%", &sh_quote(&::RUST_INSTALLER_VERSION));
6156

62-
let mut options = fs::OpenOptions::new();
63-
options.write(true).create_new(true);
64-
#[cfg(unix)] options.mode(0o755);
65-
options.open(&self.output_script)
66-
.and_then(|mut output| output.write_all(script.as_ref()))
57+
create_new_executable(&self.output_script)?
58+
.write_all(script.as_ref())
6759
.chain_err(|| format!("failed to write output script '{}'", self.output_script))
6860
}
6961
}

src/tarballer.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::fs;
1211
use std::io::{self, Write};
1312
use std::path::Path;
1413

@@ -56,14 +55,10 @@ impl Tarballer {
5655
files.sort_by(|a, b| a.bytes().rev().cmp(b.bytes().rev()));
5756

5857
// Prepare the .tar.gz file
59-
let output = fs::File::create(&tar_gz)
60-
.chain_err(|| "failed to create .tar.gz file")?;
61-
let gz = GzEncoder::new(output, flate2::Compression::Best);
58+
let gz = GzEncoder::new(create_new_file(tar_gz)?, flate2::Compression::Best);
6259

6360
// Prepare the .tar.xz file
64-
let output = fs::File::create(&tar_xz)
65-
.chain_err(|| "failed to create .tar.xz file")?;
66-
let xz = XzEncoder::new(output, 9);
61+
let xz = XzEncoder::new(create_new_file(tar_xz)?, 9);
6762

6863
// Write the tar into both encoded files. We write all directories
6964
// first, so files may be directly created. (see rustup.rs#1092)
@@ -75,8 +70,7 @@ impl Tarballer {
7570
}
7671
for path in files {
7772
let src = Path::new(&self.work_dir).join(&path);
78-
fs::File::open(&src)
79-
.and_then(|mut file| builder.append_file(&path, &mut file))
73+
builder.append_file(&path, &mut open_file(&src)?)
8074
.chain_err(|| format!("failed to tar file '{}'", src.display()))?;
8175
}
8276
let Tee(gz, xz) = builder.into_inner()

src/util.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ use std::fs;
1313
use std::path::Path;
1414
use walkdir::WalkDir;
1515

16+
// Needed to set the script mode to executable.
17+
#[cfg(unix)]
18+
use std::os::unix::fs::OpenOptionsExt;
19+
// FIXME: what about Windows? Are default ACLs executable?
20+
1621
use errors::*;
1722

1823
/// Convert a `&Path` to a UTF-8 `&str`
@@ -41,6 +46,27 @@ pub fn create_dir_all<P: AsRef<Path>>(path: P) -> Result<()> {
4146
.chain_err(|| format!("failed to create dir '{}'", path.as_ref().display()))
4247
}
4348

49+
/// Wrap `fs::OpenOptions::create_new().open()` as executable, with a nicer error message
50+
pub fn create_new_executable<P: AsRef<Path>>(path: P) -> Result<fs::File> {
51+
let mut options = fs::OpenOptions::new();
52+
options.write(true).create_new(true);
53+
#[cfg(unix)] options.mode(0o755);
54+
options.open(&path)
55+
.chain_err(|| format!("failed to create file '{}'", path.as_ref().display()))
56+
}
57+
58+
/// Wrap `fs::OpenOptions::create_new().open()`, with a nicer error message
59+
pub fn create_new_file<P: AsRef<Path>>(path: P) -> Result<fs::File> {
60+
fs::OpenOptions::new().write(true).create_new(true).open(&path)
61+
.chain_err(|| format!("failed to create file '{}'", path.as_ref().display()))
62+
}
63+
64+
/// Wrap `fs::File::open()` with a nicer error message
65+
pub fn open_file<P: AsRef<Path>>(path: P) -> Result<fs::File> {
66+
fs::File::open(&path)
67+
.chain_err(|| format!("failed to open file '{}'", path.as_ref().display()))
68+
}
69+
4470
/// Wrap `remove_dir_all` with a nicer error message
4571
pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> Result<()> {
4672
::remove_dir_all::remove_dir_all(path.as_ref())

0 commit comments

Comments
 (0)