This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexandre Stein
committed
Feb 13, 2021
1 parent
6abcfe2
commit 69d6b78
Showing
4 changed files
with
128 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ Cargo.lock | |
**/*.rs.bk | ||
|
||
# End of https://www.gitignore.io/api/rust | ||
|
||
fs-scan_output.csv |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::fs::OpenOptions; | ||
use std::io::BufRead; | ||
use std::io::BufReader; | ||
use std::io::Write; | ||
|
||
use super::objects; | ||
|
||
static OUTPUT_FILE: &'static str = "fs-scan_output.csv"; | ||
static FILE_FIRST_LINE: &'static str = "Path,Files,Directories,4K,4K_16K,16K_64K,64K_128K,128K_256K,256K_512K,512K_1M,1M_10M,10M_100M,100M_1G,1G"; | ||
|
||
pub fn save(res: &objects::Result) { | ||
match check_file() { | ||
Err(s) => { | ||
println!("ERROR on check: {}", s); | ||
return; | ||
} | ||
Ok(s) => println!("SUCCESS on check: {}", s), | ||
} | ||
|
||
let mut file = OpenOptions::new() | ||
.write(true) | ||
.append(true) | ||
.open(&OUTPUT_FILE) | ||
.unwrap(); | ||
|
||
if let Err(_) = writeln!(file, "{}", res.result_to_string()) { | ||
return; | ||
} | ||
} | ||
|
||
fn check_file() -> Result<String, String> { | ||
// Open the file | ||
let mut file = match OpenOptions::new() | ||
.read(true) | ||
.write(true) | ||
.open(&OUTPUT_FILE) | ||
{ | ||
Err(_) => { | ||
// File not opened | ||
// Try to create it | ||
let file = match OpenOptions::new() | ||
.write(true) | ||
.read(true) | ||
.append(true) | ||
.create(true) | ||
.open(&OUTPUT_FILE) | ||
{ | ||
Err(_) => return Err("can't open/create new file".to_string()), | ||
Ok(file) => file, | ||
}; | ||
file | ||
} | ||
Ok(file) => file, | ||
}; | ||
|
||
match file.metadata() { | ||
Ok(m) => { | ||
if m.len() == 0 { | ||
if let Err(_) = writeln!(&mut file, "{}", &String::from(FILE_FIRST_LINE)) { | ||
return Err("can't write first line".to_string()); | ||
} | ||
return Ok("new file created and first line added successfully".to_string()); | ||
} | ||
} | ||
Err(_) => return Err("Can't get meta".to_string()), | ||
} | ||
|
||
// Check the first line is valid | ||
// | ||
// Read the content | ||
let file_content = BufReader::new(&file); | ||
for line in file_content.lines() { | ||
// let l = line.unwrap(); | ||
let l = match line { | ||
Ok(l) => l, | ||
Err(e) => return Err(format!("can't read line: {}", e)), | ||
}; | ||
if l == FILE_FIRST_LINE { | ||
break; | ||
} | ||
return Err(format!("Not the same line: content {}", l)); | ||
} | ||
|
||
Ok("first line valid, can add the new report".to_string()) | ||
} |
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
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