Skip to content

Commit 4186d11

Browse files
committed
Replace OpenOptions::new with File::options to be readable (#3059)
## Issue Addressed Closes #3049 This PR updates widely but this replace is safe as `File::options()` is equivelent to `OpenOptions::new()`. ref: https://doc.rust-lang.org/stable/src/std/fs.rs.html#378-380
1 parent cbda0a2 commit 4186d11

16 files changed

Lines changed: 40 additions & 41 deletions

File tree

beacon_node/beacon_chain/src/validator_pubkey_cache.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{BeaconChainTypes, BeaconStore};
33
use ssz::{Decode, DecodeError, Encode};
44
use std::collections::HashMap;
55
use std::convert::TryInto;
6-
use std::fs::{File, OpenOptions};
6+
use std::fs::File;
77
use std::io::{self, Read, Write};
88
use std::path::Path;
99
use store::{DBColumn, Error as StoreError, StoreItem};
@@ -255,7 +255,7 @@ impl From<Error> for BeaconChainError {
255255
impl ValidatorPubkeyCacheFile {
256256
/// Opens an existing file for reading and writing.
257257
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
258-
OpenOptions::new()
258+
File::options()
259259
.read(true)
260260
.write(true)
261261
.create(false)
@@ -453,7 +453,7 @@ mod test {
453453
let cache = ValidatorPubkeyCache::<T>::load_from_file(&path).expect("should open cache");
454454
drop(cache);
455455

456-
let mut file = OpenOptions::new()
456+
let mut file = File::options()
457457
.write(true)
458458
.append(true)
459459
.open(&path)

common/account_utils/src/validator_definitions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use regex::Regex;
1010
use serde_derive::{Deserialize, Serialize};
1111
use slog::{error, Logger};
1212
use std::collections::HashSet;
13-
use std::fs::{self, OpenOptions};
13+
use std::fs::{self, File};
1414
use std::io;
1515
use std::path::{Path, PathBuf};
1616
use types::{graffiti::GraffitiString, Address, PublicKey};
@@ -162,7 +162,7 @@ impl ValidatorDefinitions {
162162
/// Open an existing file, returning an error if the file does not exist.
163163
pub fn open<P: AsRef<Path>>(validators_dir: P) -> Result<Self, Error> {
164164
let config_path = validators_dir.as_ref().join(CONFIG_FILENAME);
165-
let file = OpenOptions::new()
165+
let file = File::options()
166166
.write(true)
167167
.read(true)
168168
.create_new(false)
@@ -219,7 +219,7 @@ impl ValidatorDefinitions {
219219
return None;
220220
}
221221

222-
let keystore_result = OpenOptions::new()
222+
let keystore_result = File::options()
223223
.read(true)
224224
.create(false)
225225
.open(&voting_keystore_path)

common/eth2_wallet_manager/src/filesystem.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use eth2_wallet::Error as WalletError;
44
use eth2_wallet::{Uuid, Wallet};
5-
use std::fs::{copy as copy_file, remove_file, OpenOptions};
5+
use std::fs::{copy as copy_file, remove_file, File};
66
use std::io;
77
use std::path::{Path, PathBuf};
88

@@ -27,7 +27,7 @@ pub fn read<P: AsRef<Path>>(wallet_dir: P, uuid: &Uuid) -> Result<Wallet, Error>
2727
if !json_path.exists() {
2828
Err(Error::WalletDoesNotExist(json_path))
2929
} else {
30-
OpenOptions::new()
30+
File::options()
3131
.read(true)
3232
.create(false)
3333
.open(json_path)
@@ -79,7 +79,7 @@ pub fn create<P: AsRef<Path>>(wallet_dir: P, wallet: &Wallet) -> Result<(), Erro
7979
if json_path.exists() {
8080
Err(Error::WalletAlreadyExists(json_path))
8181
} else {
82-
OpenOptions::new()
82+
File::options()
8383
.write(true)
8484
.create_new(true)
8585
.open(json_path)

common/eth2_wallet_manager/src/wallet_manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use eth2_wallet::{bip39::Mnemonic, Error as WalletError, Uuid, Wallet, WalletBui
66
use lockfile::LockfileError;
77
use std::collections::HashMap;
88
use std::ffi::OsString;
9-
use std::fs::{create_dir_all, read_dir, OpenOptions};
9+
use std::fs::{create_dir_all, read_dir, File};
1010
use std::io;
1111
use std::path::{Path, PathBuf};
1212

@@ -172,7 +172,7 @@ impl WalletManager {
172172
// Ignore any paths that don't parse as a UUID.
173173
if let Ok(uuid) = Uuid::parse_str(&file_name) {
174174
let wallet_path = f.path().join(format!("{}", uuid));
175-
let wallet = OpenOptions::new()
175+
let wallet = File::options()
176176
.read(true)
177177
.create(false)
178178
.open(wallet_path)

common/lockfile/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use fs2::FileExt;
2-
use std::fs::{self, File, OpenOptions};
2+
use std::fs::{self, File};
33
use std::io::{self, ErrorKind};
44
use std::path::{Path, PathBuf};
55

@@ -30,7 +30,7 @@ impl Lockfile {
3030
let file = if file_existed {
3131
File::open(&path)
3232
} else {
33-
OpenOptions::new()
33+
File::options()
3434
.read(true)
3535
.write(true)
3636
.create_new(true)

common/validator_dir/src/builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use deposit_contract::{encode_eth1_tx_data, Error as DepositError};
44
use eth2_keystore::{Error as KeystoreError, Keystore, KeystoreBuilder, PlainText};
55
use filesystem::create_with_600_perms;
66
use rand::{distributions::Alphanumeric, Rng};
7-
use std::fs::{create_dir_all, OpenOptions};
7+
use std::fs::{create_dir_all, File};
88
use std::io::{self, Write};
99
use std::path::{Path, PathBuf};
1010
use types::{ChainSpec, DepositData, Hash256, Keypair, Signature};
@@ -197,7 +197,7 @@ impl<'a> Builder<'a> {
197197
return Err(Error::DepositDataAlreadyExists(path));
198198
} else {
199199
let hex = format!("0x{}", hex::encode(&deposit_data));
200-
OpenOptions::new()
200+
File::options()
201201
.write(true)
202202
.read(true)
203203
.create(true)
@@ -214,7 +214,7 @@ impl<'a> Builder<'a> {
214214
if path.exists() {
215215
return Err(Error::DepositAmountAlreadyExists(path));
216216
} else {
217-
OpenOptions::new()
217+
File::options()
218218
.write(true)
219219
.read(true)
220220
.create(true)
@@ -267,7 +267,7 @@ fn write_keystore_to_file(path: PathBuf, keystore: &Keystore) -> Result<(), Erro
267267
if path.exists() {
268268
Err(Error::KeystoreAlreadyExists(path))
269269
} else {
270-
let file = OpenOptions::new()
270+
let file = File::options()
271271
.write(true)
272272
.read(true)
273273
.create_new(true)

common/validator_dir/src/validator_dir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use deposit_contract::decode_eth1_tx_data;
66
use derivative::Derivative;
77
use eth2_keystore::{Error as KeystoreError, Keystore, PlainText};
88
use lockfile::{Lockfile, LockfileError};
9-
use std::fs::{read, write, OpenOptions};
9+
use std::fs::{read, write, File};
1010
use std::io;
1111
use std::path::{Path, PathBuf};
1212
use tree_hash::TreeHash;
@@ -211,7 +211,7 @@ pub fn unlock_keypair<P: AsRef<Path>>(
211211
password_dir: P,
212212
) -> Result<Keypair, Error> {
213213
let keystore = Keystore::from_json_reader(
214-
&mut OpenOptions::new()
214+
&mut File::options()
215215
.read(true)
216216
.create(false)
217217
.open(keystore_path)
@@ -236,7 +236,7 @@ pub fn unlock_keypair_from_password_path(
236236
password_path: &Path,
237237
) -> Result<Keypair, Error> {
238238
let keystore = Keystore::from_json_reader(
239-
&mut OpenOptions::new()
239+
&mut File::options()
240240
.read(true)
241241
.create(false)
242242
.open(keystore_path)

consensus/types/src/chain_spec.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,14 +1163,13 @@ mod tests {
11631163
#[cfg(test)]
11641164
mod yaml_tests {
11651165
use super::*;
1166-
use std::fs::OpenOptions;
11671166
use tempfile::NamedTempFile;
11681167

11691168
#[test]
11701169
fn minimal_round_trip() {
11711170
// create temp file
11721171
let tmp_file = NamedTempFile::new().expect("failed to create temp file");
1173-
let writer = OpenOptions::new()
1172+
let writer = File::options()
11741173
.read(false)
11751174
.write(true)
11761175
.open(tmp_file.as_ref())
@@ -1181,7 +1180,7 @@ mod yaml_tests {
11811180
// write fresh minimal config to file
11821181
serde_yaml::to_writer(writer, &yamlconfig).expect("failed to write or serialize");
11831182

1184-
let reader = OpenOptions::new()
1183+
let reader = File::options()
11851184
.read(true)
11861185
.write(false)
11871186
.open(tmp_file.as_ref())
@@ -1194,7 +1193,7 @@ mod yaml_tests {
11941193
#[test]
11951194
fn mainnet_round_trip() {
11961195
let tmp_file = NamedTempFile::new().expect("failed to create temp file");
1197-
let writer = OpenOptions::new()
1196+
let writer = File::options()
11981197
.read(false)
11991198
.write(true)
12001199
.open(tmp_file.as_ref())
@@ -1203,7 +1202,7 @@ mod yaml_tests {
12031202
let yamlconfig = Config::from_chain_spec::<MainnetEthSpec>(&mainnet_spec);
12041203
serde_yaml::to_writer(writer, &yamlconfig).expect("failed to write or serialize");
12051204

1206-
let reader = OpenOptions::new()
1205+
let reader = File::options()
12071206
.read(true)
12081207
.write(false)
12091208
.open(tmp_file.as_ref())

consensus/types/src/config_and_preset.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ impl ConfigAndPreset {
9292
mod test {
9393
use super::*;
9494
use crate::MainnetEthSpec;
95-
use std::fs::OpenOptions;
95+
use std::fs::File;
9696
use tempfile::NamedTempFile;
9797

9898
#[test]
9999
fn extra_fields_round_trip() {
100100
let tmp_file = NamedTempFile::new().expect("failed to create temp file");
101-
let writer = OpenOptions::new()
101+
let writer = File::options()
102102
.read(false)
103103
.write(true)
104104
.open(tmp_file.as_ref())
@@ -116,7 +116,7 @@ mod test {
116116

117117
serde_yaml::to_writer(writer, &yamlconfig).expect("failed to write or serialize");
118118

119-
let reader = OpenOptions::new()
119+
let reader = File::options()
120120
.read(true)
121121
.write(false)
122122
.open(tmp_file.as_ref())

crypto/eth2_keystore/src/keystore.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use scrypt::{
2121
};
2222
use serde::{Deserialize, Serialize};
2323
use sha2::{Digest, Sha256};
24-
use std::fs::OpenOptions;
24+
use std::fs::File;
2525
use std::io::{Read, Write};
2626
use std::iter::FromIterator;
2727
use std::path::Path;
@@ -329,7 +329,7 @@ impl Keystore {
329329

330330
/// Instantiates `self` by reading a JSON file at `path`.
331331
pub fn from_json_file<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
332-
OpenOptions::new()
332+
File::options()
333333
.read(true)
334334
.write(false)
335335
.create(false)

0 commit comments

Comments
 (0)