Skip to content

Commit b5d629d

Browse files
max size
1 parent a05781c commit b5d629d

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

command/src/commands/command.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub fn help() -> String{
3232
cat View file only read mv Move file's path
3333
python Run code in python tar -zxvf: Compression
3434
html Open html file tar -xvf: Decompression
35+
pd Check your password sudo Root
3536
exit Exit this process\0\x1B[0m\n"
3637
);
3738

@@ -300,18 +301,26 @@ pub fn cat(file: &str) -> Result<(usize,String),Error>{
300301
if file.is_empty(){
301302
return Ok(empty_file());
302303
}
303-
let f = fs::File::open(Path::new(file));
304304
let mut buffer = String::new();
305-
let _ = f.unwrap().read_to_string(&mut buffer);
305+
let file_size = max_size_file(file);
306+
match file_size{
307+
Ok(_) =>{
308+
let f = fs::File::open(Path::new(file));
309+
let _ = f.unwrap().read_to_string(&mut buffer);
310+
},
311+
Err(err) =>{
312+
return Ok((ERR_CODE,err.to_string()));
313+
}
314+
}
306315
Ok((STATUE_CODE,buffer))
307316
}
308317

309318

310319
use crate::commands::download::{download_package, find_package};
311320
use crate::priority::get_priority;
312-
use crate::set::set::file_create_time;
321+
use crate::set::set::{file_create_time, max_size_file};
313322
use crate::run::run;
314-
use crate::state_code::{empty_dir, empty_file, missing_pattern, STATUE_CODE};
323+
use crate::state_code::{empty_dir, empty_file, missing_pattern, ERR_CODE, STATUE_CODE};
315324
use super::download::update;
316325
use crate::root::SessionContext;
317326

command/src/set/set.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,46 @@ pub fn error_log(err: String){
134134
return;
135135
}
136136
// error.log -> $HOME/.Tiks/error.log -> [time]: [Error]
137+
}
138+
139+
pub const MB:f64 = 1024.0*1024.0;
140+
141+
use std::fs::metadata;
142+
use std::io;
143+
144+
fn get_file_size(file:&str) -> f64{
145+
let mut size = 0;
146+
if let Ok(s) = metadata(file){
147+
size = s.len();
148+
}
149+
150+
size as f64
151+
}
152+
153+
type MB = f64;
154+
155+
fn byte_to_mb(size: f64) -> MB{
156+
let new_size = size/MB;
157+
new_size
158+
}
159+
160+
pub fn max_size_file(path: &str) -> Result<(),&'static str>{
161+
let size = get_file_size(path);
162+
let mb = byte_to_mb(size); // MB
163+
164+
if mb > 128_f64{
165+
loop {
166+
let mut choice = String::new();
167+
println!("This file is more than 256MB, Do you want to read? [y/n]");
168+
io::stdout().flush().unwrap();
169+
io::stdin().read_line(&mut choice).unwrap();
170+
if choice.as_str() == "y"{
171+
break;
172+
}else{
173+
return Err("Canel read");
174+
}
175+
}
176+
}
177+
178+
Ok(())
137179
}

command/src/state_code.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub const STATUE_CODE:usize=0;
2+
pub const ERR_CODE:usize=109;
23

34

45
pub fn missing_pattern() -> (usize, String) {

0 commit comments

Comments
 (0)