Skip to content

Commit

Permalink
use a grin specific tmp dir instead of a system tmp dir (#2753) (#2772)
Browse files Browse the repository at this point in the history
* use a grin specific tmp dir instead of a system tmp dir

* rustfmt

* using create_new instead of create

* switch the txhashset sandbox from system tmp dir to grin specific tmp dir

* rustfmt

* adapter get_tmp_dir delegate to chain implementation

* simply the logic of protolc for get_tmpfile_pathname

* rustfmt

* fix the merge mistake
  • Loading branch information
garyyu authored and ignopeverell committed Apr 22, 2019
1 parent d7f940e commit 97e96e4
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 13 deletions.
43 changes: 36 additions & 7 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ use crate::util::secp::pedersen::{Commitment, RangeProof};
use crate::util::{Mutex, RwLock, StopState};
use grin_store::Error::NotFoundErr;
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::fs::{self, File};
use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -840,9 +839,39 @@ impl Chain {

/// Clean the temporary sandbox folder
pub fn clean_txhashset_sandbox(&self) {
let sandbox_dir = env::temp_dir();
txhashset::clean_txhashset_folder(&sandbox_dir);
txhashset::clean_header_folder(&sandbox_dir);
txhashset::clean_txhashset_folder(&self.get_tmp_dir());
txhashset::clean_header_folder(&self.get_tmp_dir());
}

/// Specific tmp dir.
/// Normally it's ~/.grin/main/tmp for mainnet
/// or ~/.grin/floo/tmp for floonet
pub fn get_tmp_dir(&self) -> PathBuf {
let mut tmp_dir = PathBuf::from(self.db_root.clone());
tmp_dir = tmp_dir
.parent()
.expect("fail to get parent of db_root dir")
.to_path_buf();
tmp_dir.push("tmp");
tmp_dir
}

/// Get a tmp file path in above specific tmp dir (create tmp dir if not exist)
/// Delete file if tmp file already exists
pub fn get_tmpfile_pathname(&self, tmpfile_name: String) -> PathBuf {
let mut tmp = self.get_tmp_dir();
if !tmp.exists() {
if let Err(e) = fs::create_dir(tmp.clone()) {
warn!("fail to create tmp folder on {:?}. err: {}", tmp, e);
}
}
tmp.push(tmpfile_name);
if tmp.exists() {
if let Err(e) = fs::remove_file(tmp.clone()) {
warn!("fail to clean existing tmp file: {:?}. err: {}", tmp, e);
}
}
tmp
}

/// Writes a reading view on a txhashset state that's been provided to us.
Expand All @@ -866,8 +895,8 @@ impl Chain {

let header = self.get_block_header(&h)?;

// Write txhashset to sandbox (in the os temporary directory)
let sandbox_dir = env::temp_dir();
// Write txhashset to sandbox (in the Grin specific tmp dir)
let sandbox_dir = self.get_tmp_dir();
txhashset::clean_txhashset_folder(&sandbox_dir);
txhashset::clean_header_folder(&sandbox_dir);
txhashset::zip_write(sandbox_dir.clone(), txhashset_data.try_clone()?, &header)?;
Expand Down
9 changes: 9 additions & 0 deletions p2p/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::util::{Mutex, RwLock};
use std::fmt;
use std::fs::File;
use std::net::{Shutdown, TcpStream};
use std::path::PathBuf;
use std::sync::Arc;

use crate::chain;
Expand Down Expand Up @@ -627,6 +628,14 @@ impl ChainAdapter for TrackingAdapter {
self.adapter
.txhashset_download_update(start_time, downloaded_size, total_size)
}

fn get_tmp_dir(&self) -> PathBuf {
self.adapter.get_tmp_dir()
}

fn get_tmpfile_pathname(&self, tmpfile_name: String) -> PathBuf {
self.adapter.get_tmpfile_pathname(tmpfile_name)
}
}

impl NetAdapter for TrackingAdapter {
Expand Down
9 changes: 9 additions & 0 deletions p2p/src/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use crate::util::RwLock;
use std::collections::HashMap;
use std::fs::File;
use std::path::PathBuf;
use std::sync::Arc;

use rand::{thread_rng, Rng};
Expand Down Expand Up @@ -618,6 +619,14 @@ impl ChainAdapter for Peers {
self.adapter
.txhashset_download_update(start_time, downloaded_size, total_size)
}

fn get_tmp_dir(&self) -> PathBuf {
self.adapter.get_tmp_dir()
}

fn get_tmpfile_pathname(&self, tmpfile_name: String) -> PathBuf {
self.adapter.get_tmpfile_pathname(tmpfile_name)
}
}

impl NetAdapter for Peers {
Expand Down
21 changes: 15 additions & 6 deletions p2p/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use rand::{thread_rng, Rng};
use std::cmp;
use std::env;
use std::fs::File;
use std::fs::{self, File, OpenOptions};
use std::io::{BufWriter, Write};
use std::sync::Arc;

Expand Down Expand Up @@ -288,10 +288,15 @@ impl MessageHandler for Protocol {
self.adapter
.txhashset_download_update(download_start_time, 0, sm_arch.bytes);

let mut tmp = env::temp_dir();
tmp.push("txhashset.zip");
let nonce: u32 = thread_rng().gen_range(0, 1_000_000);
let tmp = self.adapter.get_tmpfile_pathname(format!(
"txhashset-{}-{}.zip",
download_start_time.timestamp(),
nonce
));
let mut save_txhashset_to_file = |file| -> Result<(), Error> {
let mut tmp_zip = BufWriter::new(File::create(file)?);
let mut tmp_zip =
BufWriter::new(OpenOptions::new().write(true).create_new(true).open(file)?);
let total_size = sm_arch.bytes as usize;
let mut downloaded_size: usize = 0;
let mut request_size = cmp::min(48_000, total_size);
Expand Down Expand Up @@ -332,7 +337,7 @@ impl MessageHandler for Protocol {
tmp,
);

let tmp_zip = File::open(tmp)?;
let tmp_zip = File::open(tmp.clone())?;
let res = self
.adapter
.txhashset_write(sm_arch.hash, tmp_zip, &self.peer_info)?;
Expand All @@ -342,6 +347,10 @@ impl MessageHandler for Protocol {
sm_arch.hash, sm_arch.height, res
);

if let Err(e) = fs::remove_file(tmp.clone()) {
warn!("fail to remove tmp file: {:?}. err: {}", tmp, e);
}

Ok(None)
}

Expand Down
9 changes: 9 additions & 0 deletions p2p/src/serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::fs::File;
use std::net::{Shutdown, SocketAddr, TcpListener, TcpStream};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use std::{io, thread};
Expand Down Expand Up @@ -306,6 +307,14 @@ impl ChainAdapter for DummyAdapter {
) -> bool {
false
}

fn get_tmp_dir(&self) -> PathBuf {
unimplemented!()
}

fn get_tmpfile_pathname(&self, _tmpfile_name: String) -> PathBuf {
unimplemented!()
}
}

impl NetAdapter for DummyAdapter {
Expand Down
8 changes: 8 additions & 0 deletions p2p/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::convert::From;
use std::fs::File;
use std::io;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::path::PathBuf;

use std::sync::mpsc;
use std::sync::Arc;
Expand Down Expand Up @@ -558,6 +559,13 @@ pub trait ChainAdapter: Sync + Send {
txhashset_data: File,
peer_peer_info: &PeerInfo,
) -> Result<bool, chain::Error>;

/// Get the Grin specific tmp dir
fn get_tmp_dir(&self) -> PathBuf;

/// Get a tmp file path in above specific tmp dir (create tmp dir if not exist)
/// Delete file if tmp file already exists
fn get_tmpfile_pathname(&self, tmpfile_name: String) -> PathBuf;
}

/// Additional methods required by the protocol that don't need to be
Expand Down
9 changes: 9 additions & 0 deletions servers/src/common/adapters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use crate::util::RwLock;
use std::fs::File;
use std::path::PathBuf;
use std::sync::{Arc, Weak};
use std::thread;
use std::time::Instant;
Expand Down Expand Up @@ -423,6 +424,14 @@ impl p2p::ChainAdapter for NetToChainAdapter {
Ok(true)
}
}

fn get_tmp_dir(&self) -> PathBuf {
self.chain().get_tmp_dir()
}

fn get_tmpfile_pathname(&self, tmpfile_name: String) -> PathBuf {
self.chain().get_tmpfile_pathname(tmpfile_name)
}
}

impl NetToChainAdapter {
Expand Down

0 comments on commit 97e96e4

Please sign in to comment.