Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use a grin specific tmp dir instead of a system tmp dir #2753

Merged
merged 9 commits into from
Apr 16, 2019
Merged
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 @@ -850,9 +849,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 @@ -876,8 +905,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::conn;
Expand Down Expand Up @@ -597,6 +598,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 @@ -586,6 +587,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.addr);
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 @@ -278,6 +279,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 @@ -506,6 +507,13 @@ pub trait ChainAdapter: Sync + Send {
/// read as a zip file, unzipped and the resulting state files should be
/// rewound to the provided indexes.
fn txhashset_write(&self, h: Hash, txhashset_data: File, peer_addr: PeerAddr) -> bool;

/// 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 @@ -374,6 +375,14 @@ impl p2p::ChainAdapter for NetToChainAdapter {
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