Skip to content

Commit

Permalink
feat: check file size
Browse files Browse the repository at this point in the history
  • Loading branch information
xfoxfu committed Nov 14, 2023
1 parent b3f3871 commit d934e67
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
5 changes: 3 additions & 2 deletions checker.cfg.json
Expand Up @@ -19,6 +19,7 @@
"regex": "^variance\\\\variance\\.(cpp|c|pas)$"
}
],
"start_time": "2023-10-18T15:00:00Z",
"end_time": "2023-10-18T16:00:00Z"
"start_time": "2023-10-19T23:00:00+09:00",
"end_time": "2023-10-20T00:00:00+09:00",
"size_limit_kb": 100
}
29 changes: 23 additions & 6 deletions src/config.rs
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::fs::{DirEntry, FileType, Metadata};
use std::path::PathBuf;

use chrono::{DateTime, Utc};
use regex::Regex;
Expand All @@ -10,12 +11,26 @@ pub struct Problem {
pub name: String,
#[serde(with = "regex_sd")]
pub regex: Regex,
#[serde(default)]
pub existing_files: Vec<String>,
#[serde(default)]
pub existing_files_date: HashMap<String, DateTime<Utc>>,
#[serde(skip)]
pub existing_files: Vec<FileEntry>,
}

#[derive(Debug)]
pub struct FileEntry {
pub path: PathBuf,
pub file_type: FileType,
pub metadata: Metadata,
}

impl FileEntry {
pub fn from(entry: &DirEntry) -> Result<Self, std::io::Error> {
Ok(Self {
path: entry.path(),
file_type: entry.file_type()?,
metadata: entry.metadata()?,
})
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Contestant {
/// 选手文件夹父路径
Expand All @@ -28,6 +43,8 @@ pub struct Contestant {
pub start_time: DateTime<Utc>,
/// 考试结束时间
pub end_time: DateTime<Utc>,
/// 文件大小限制
pub size_limit_kb: u64,
}

mod regex_sd {
Expand All @@ -48,7 +65,7 @@ mod regex_sd {
{
let s = String::deserialize(deserializer)?;
#[cfg(unix)]
let s = s.replace(r#"\\"#, "/");
let s = s.replace(r"\\", "/");
Regex::from_str(&s).map_err(serde::de::Error::custom)
}
}
31 changes: 17 additions & 14 deletions src/main.rs
Expand Up @@ -4,8 +4,8 @@ mod config;
mod render;

use anyhow::Result;
use chrono::{FixedOffset, Offset};
use config::Contestant;
use chrono::{DateTime, FixedOffset, Utc};
use config::{Contestant, FileEntry};
use std::fs::{read_dir, File};
use std::io::Read;
use std::path::Path;
Expand Down Expand Up @@ -111,13 +111,7 @@ fn build_message(messages: &mut Vec<(String, Color)>) -> Result<()> {
.regex
.is_match(dir2.path().strip_prefix(&user_directory)?.to_str().unwrap())
{
let filepath = dir2.path().to_str().unwrap().to_string();
prob.existing_files.push(filepath.clone());
if let Ok(meta) = dir2.metadata() {
if let Ok(modi) = meta.modified() {
prob.existing_files_date.insert(filepath, modi.into());
}
}
prob.existing_files.push(FileEntry::from(&dir2)?);
}
}
}
Expand All @@ -128,7 +122,8 @@ fn build_message(messages: &mut Vec<(String, Color)>) -> Result<()> {
if prob.existing_files.is_empty() {
messages.push((format!(" 未找到源代码文件."), Color::Black));
} else if prob.existing_files.len() == 1 {
let filename = &prob.existing_files[0];
let file_entry = &prob.existing_files[0];
let filename = file_entry.path.to_str().unwrap_or("");
let f = Path::new(filename).strip_prefix(&user_directory)?;
messages.push((
format!(
Expand All @@ -138,10 +133,11 @@ fn build_message(messages: &mut Vec<(String, Color)>) -> Result<()> {
),
Color::Black,
));
if let Some(mod_date) = &prob.existing_files_date.get(filename) {
if let Ok(mod_date) = file_entry.metadata.modified() {
let mod_date: DateTime<Utc> = mod_date.into();
let date_shanghai =
mod_date.with_timezone(&FixedOffset::east_opt(8 * 3600).unwrap());
if mod_date >= &&cfg.start_time && mod_date <= &&cfg.end_time {
if mod_date >= cfg.start_time && mod_date <= cfg.end_time {
messages.push((
format!(" 修改日期有效 {}.", date_shanghai),
Color::Black,
Expand All @@ -155,10 +151,17 @@ fn build_message(messages: &mut Vec<(String, Color)>) -> Result<()> {
} else {
messages.push((format!(" 文件没有修改日期记录."), Color::Yellow));
}
let size = file_entry.metadata.len();
if size > cfg.size_limit_kb * 1024 {
messages.push((
format!(" 文件大小 {}KiB 超出限制.", size / 1024),
Color::Red,
));
}
} else {
messages.push((format!(" 找到多个源代码文件:"), Color::Red));
for file in prob.existing_files.iter() {
messages.push((format!(" {}", file), Color::Red));
messages.push((format!(" {}", file.path.display()), Color::Red));
}
}
}
Expand Down Expand Up @@ -193,7 +196,7 @@ fn build_message(messages: &mut Vec<(String, Color)>) -> Result<()> {
for prob in cfg.problems.iter() {
let f = prob.existing_files.first();
let real_hash = if let Some(f) = f {
result_into_ok_or_err(try_crc32(f))
result_into_ok_or_err(try_crc32(&f.path))
} else {
"文件不存在".to_string()
};
Expand Down

0 comments on commit d934e67

Please sign in to comment.