Created
October 30, 2017 03:55
-
-
Save FreeMasen/cc96994fff0c14a22516bca83a33ae34 to your computer and use it in GitHub Desktop.
Looking for help porting this nodejs script to rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::process::{Command, Stdio}; | |
use std::path::{Path, PathBuf}; | |
use std::fs::{metadata, Metadata}; | |
use std::io::prelude::*; | |
use std::io::{BufReader, BufWriter}; | |
use std::string::String; | |
#[macro_use] | |
extern crate clap; | |
use clap::App; | |
fn main() { | |
let yaml = load_yaml!("cli.yml"); | |
let matches = App::from_yaml(yaml).get_matches(); | |
let in_file = matches.value_of("infile").unwrap_or_else(|| panic!("input is required")); | |
let out_file = matches.value_of("outfile").unwrap_or_else(|| panic!("output is required")); | |
let bs = matches.value_of("blocksize").unwrap_or("1m"); | |
let target_size = get_in_size(in_file.to_string().clone()); | |
println!("infile is {} bytes", target_size); | |
let mut dd = std::process::Command::new("dd") | |
.args(&[ | |
format!("if={}", in_file), | |
format!("of={}", out_file), | |
format!("bs={}", bs), | |
]) | |
.stderr(Stdio::inherit()) | |
.stdout(Stdio::piped()) | |
.stdin(Stdio::piped()) | |
.spawn() | |
.expect("Error from dd"); | |
println!("pre-loop"); | |
let mut i = 0; | |
loop { | |
if let Some(ref mut stdout) = dd.stdout { | |
let lines = BufReader::new(stdout).lines().enumerate().take(10); | |
for (counter, line) in lines { | |
println!("{}, {:?}", counter, line); | |
} | |
} | |
println!("loop"); | |
let stdo = dd.stdout.as_mut().expect("Couldn't take stdout as mut"); | |
println!("took stdo as mut"); | |
let stdi = dd.stdin.as_mut().expect("Couldn't take stdin as mut"); | |
println!("took stdi as mut"); | |
let mut stdin = BufWriter::new(stdi); | |
println!("converted stdin to BufWriter"); | |
let mut stdout = BufReader::new(stdo); | |
println!("Converted stdin to BufReader"); | |
stdin.write(b"SIGINFO").unwrap(); | |
println!("wrote SIGINFO to stdin"); | |
let mut line = String::new(); | |
println!("attempted to read stdout to string"); | |
let _ = stdout.read_line(&mut line); | |
println!("line: {}", line); | |
//I would like to be parsing dd SIGINFO here | |
} | |
dd.wait().expect("dd error"); | |
} | |
fn get_in_size(path: String) -> i32 { | |
let path = PathBuf::from(path); | |
let md = metadata(path).expect("Unable to get metadata for infile"); | |
md.len() as i32 | |
} | |
fn calculate_progress(target: i32, bytes_transfered: String) -> i32 { | |
let progress = bytes_transfered.parse::<i32>().unwrap(); | |
progress / target | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cp = require('child_process') | |
const path = require('path'); | |
const fs = require('fs'); | |
const rng = require('crypto').randomBytes | |
const prog = require('progress') | |
function dd(infile, outfile, bytesize) { | |
var inFileName = path.basename(infile); | |
var outfilename = path.basename(outfile) | |
var bar | |
var currentBytes | |
fs.stat(infile, (err, stat) => { | |
if (err) return console.error('Unable to get infile stats', err.message) | |
console.log(`moving \n\t${infile}`) | |
console.log(`to \n\t${outfile}`) | |
var inFileSize = stat.size | |
bar = new prog('Progress [:bar] :percent :current :total', | |
{ | |
total: inFileSize, | |
complete: '‡', | |
incomplete: ' ' | |
}) | |
var dd = cp.spawn('dd', [`if=${infile}`, `of=${outfile}`, `bs=${bytesize || '1m'}`]) | |
var interval = setInterval(() => { | |
if (bar.complete) { | |
clearInterval(interval) | |
console.log('Finishing up') | |
} else { | |
dd.kill('SIGINFO') | |
} | |
}, 100) | |
dd.addListener('exit', (code, sig) => { | |
if (code == 0) { | |
bar.tick(bar.total - bar.curr) | |
console.log('Complete') | |
process.exit() | |
} else { | |
console.log(`Exit with code ${code}: ${sig}`) | |
process.exit() | |
} | |
}) | |
dd.stderr.on('data', (data) => { | |
if (typeof data != 'string') data = data.toString() | |
var status = parse(data); | |
var update | |
if (status) { | |
update = status - currentBytes | |
currentBytes = status | |
if (!bar.complete) bar.tick(update) | |
} | |
}) | |
}) | |
} | |
function parse(text) { | |
var lines = text.split('\n') | |
var line = lines[2] | |
if (!line) { | |
line = lines[0] | |
} | |
var words = line.split(' ') | |
return Number.parseInt(words[0]) | |
} | |
// function report(prog, total) { | |
// var counter = 0 | |
// while (prog > 1024) { | |
// prog = Math.floor(prog / 1024) | |
// total = Math.floor(total / 1024) | |
// counter ++ | |
// } | |
// var postfix | |
// switch (counter) { | |
// case 0: | |
// postfix = 'bytes' | |
// break; | |
// case 1: | |
// postfix = 'kb' | |
// break; | |
// case 2: | |
// postfix = 'mb' | |
// break; | |
// case 3: | |
// postfix = 'gb' | |
// break; | |
// default: | |
// postfix = '?' | |
// } | |
// console.log(`${prog}${postfix}/${total}${postfix}`) | |
// } | |
var ifile; | |
var ofile; | |
var bs; | |
if (process.argv[2]) { | |
ifile = process.argv[2] | |
} | |
if (process.argv[3]) { | |
ofile = process.argv[3] | |
} | |
if (process.argv[4]) { | |
bs = process.argv[4] | |
} | |
dd(ifile, ofile, bs) | |
function create() { | |
for (var i = 0; i < 100000; i++) { | |
fs.appendFileSync('new', rng(512).toString('hex') + '\n') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment