From e9787f2ba58f46e93af38d3b2c9d3306ac005b13 Mon Sep 17 00:00:00 2001 From: undefined Date: Sun, 28 Aug 2022 20:40:35 +0800 Subject: [PATCH] judge: use basename instead of parseFilename --- packages/hydrojudge/src/flow.ts | 14 ++++++----- packages/hydrojudge/src/hosts/builtin.ts | 6 ++--- packages/hydrojudge/src/judge/default.ts | 5 ++-- packages/hydrojudge/src/judge/interactive.ts | 11 ++++---- packages/hydrojudge/src/judge/interface.ts | 25 ------------------- .../hydrojudge/src/judge/submit_answer.ts | 25 +++++++++---------- packages/hydrojudge/src/tmpfs.ts | 2 ++ packages/hydrojudge/src/utils.ts | 7 +----- 8 files changed, 34 insertions(+), 61 deletions(-) diff --git a/packages/hydrojudge/src/flow.ts b/packages/hydrojudge/src/flow.ts index 0fa5a1b0b..aa58bdce8 100644 --- a/packages/hydrojudge/src/flow.ts +++ b/packages/hydrojudge/src/flow.ts @@ -14,7 +14,7 @@ interface Task { function judgeSubtask(subtask: NormalizedSubtask, sid: string, judgeCase: Task['judgeCase']) { return async (ctx: Context) => { - subtask.type = subtask.type || 'min'; + subtask.type ||= 'min'; const ctxSubtask = { subtask, status: 0, @@ -27,9 +27,9 @@ function judgeSubtask(subtask: NormalizedSubtask, sid: string, judgeCase: Task[' const runner = judgeCase(subtask.cases[cid], subtask.id.toString() ?? sid); cases.push(ctx.queue.add(async () => { if (ctx.errored - || (ctxSubtask.subtask.type === 'min' && ctxSubtask.score === 0) - || (ctxSubtask.subtask.type === 'max' && ctxSubtask.score === ctxSubtask.subtask.score) - || ((ctxSubtask.subtask.if || []).filter((i) => ctx.failed[i]).length) + || (subtask.type === 'min' && ctxSubtask.score === 0) + || (subtask.type === 'max' && ctxSubtask.score === subtask.score) + || (subtask.if || []).filter((i) => ctx.failed[i]).length ) { ctx.next({ case: { @@ -46,10 +46,12 @@ function judgeSubtask(subtask: NormalizedSubtask, sid: string, judgeCase: Task[' } else await runner(ctx, ctxSubtask, runner); })); } - await Promise.all(cases).catch((e) => { + try { + await Promise.all(cases); + } catch (e) { ctx.errored = true; throw e; - }); + } ctx.total_status = Math.max(ctx.total_status, ctxSubtask.status); return ctxSubtask.score; }; diff --git a/packages/hydrojudge/src/hosts/builtin.ts b/packages/hydrojudge/src/hosts/builtin.ts index 96e15f1e0..68b18292f 100644 --- a/packages/hydrojudge/src/hosts/builtin.ts +++ b/packages/hydrojudge/src/hosts/builtin.ts @@ -9,7 +9,6 @@ import { Logger } from 'hydrooj/src/logger'; import * as setting from 'hydrooj/src/model/setting'; import * as system from 'hydrooj/src/model/system'; import task from 'hydrooj/src/model/task'; -import * as monitor from 'hydrooj/src/service/monitor'; import storage from 'hydrooj/src/service/storage'; import { processTestdata } from '../cases'; import { getConfig } from '../config'; @@ -77,6 +76,7 @@ const session = { export async function postInit() { if (system.get('hydrojudge.disable')) return; - task.consume({ type: 'judge' }, (t) => (new JudgeTask(session, t)).handle().catch(logger.error)); - task.consume({ type: 'judge', priority: { $gt: -50 } }, (t) => (new JudgeTask(session, t)).handle().catch(logger.error)); + const handle = (t) => (new JudgeTask(session, t)).handle().catch(logger.error); + task.consume({ type: 'judge' }, handle); + task.consume({ type: 'judge', priority: { $gt: -50 } }, handle); } diff --git a/packages/hydrojudge/src/judge/default.ts b/packages/hydrojudge/src/judge/default.ts index a8ad2c57a..ca55dd038 100644 --- a/packages/hydrojudge/src/judge/default.ts +++ b/packages/hydrojudge/src/judge/default.ts @@ -1,3 +1,4 @@ +import { basename } from 'path'; import { STATUS } from '@hydrooj/utils/lib/status'; import { check, compileChecker } from '../check'; import compile from '../compile'; @@ -5,7 +6,7 @@ import { runFlow } from '../flow'; import { Logger } from '../log'; import { CmdFile, del, run } from '../sandbox'; import signals from '../signals'; -import { NormalizedCase, parseFilename } from '../utils'; +import { NormalizedCase } from '../utils'; import { Context, ContextSubTask } from './interface'; const Score = { @@ -136,7 +137,7 @@ export const judge = async (ctx: Context) => await runFlow(ctx, { { user_code: ctx.code, ...Object.fromEntries( - (ctx.config.judge_extra_files || []).map((i) => [parseFilename(i), { src: i }]), + (ctx.config.judge_extra_files || []).map((i) => [basename(i), { src: i }]), ), }, ), diff --git a/packages/hydrojudge/src/judge/interactive.ts b/packages/hydrojudge/src/judge/interactive.ts index 8289ac5e2..9af8dd2a8 100644 --- a/packages/hydrojudge/src/judge/interactive.ts +++ b/packages/hydrojudge/src/judge/interactive.ts @@ -1,12 +1,11 @@ +import { basename } from 'path'; import { STATUS } from '@hydrooj/utils/lib/status'; import compile from '../compile'; import { runFlow } from '../flow'; import { runPiped } from '../sandbox'; import signals from '../signals'; import { parse } from '../testlib'; -import { - findFileSync, NormalizedCase, parseFilename, -} from '../utils'; +import { findFileSync, NormalizedCase } from '../utils'; import { Context, ContextSubTask } from './interface'; const testlibSrc = findFileSync('@hydrooj/hydrojudge/vendor/testlib/testlib.h'); @@ -81,7 +80,7 @@ export const judge = async (ctx: Context) => await runFlow(ctx, { (() => { const copyIn = {}; for (const file of ctx.config.user_extra_files) { - copyIn[parseFilename(file)] = { src: file }; + copyIn[basename(file)] = { src: file }; } return compile(ctx.session.getLang(ctx.lang), ctx.code, copyIn, ctx.next); })(), @@ -91,10 +90,10 @@ export const judge = async (ctx: Context) => await runFlow(ctx, { user_code: ctx.code, }; for (const file of ctx.config.judge_extra_files) { - copyIn[parseFilename(file)] = { src: file }; + copyIn[basename(file)] = { src: file }; } return compile( - ctx.session.getLang(parseFilename(ctx.config.interactor).split('.')[1].replace('@', '.')), + ctx.session.getLang(basename(ctx.config.interactor).split('.')[1].replace('@', '.')), { src: ctx.config.interactor }, copyIn, ); diff --git a/packages/hydrojudge/src/judge/interface.ts b/packages/hydrojudge/src/judge/interface.ts index f48aa2321..939430d56 100644 --- a/packages/hydrojudge/src/judge/interface.ts +++ b/packages/hydrojudge/src/judge/interface.ts @@ -23,31 +23,6 @@ export interface RuntimeContext { executeUser?: Execute; } -export interface Config { - type: string; - subType?: string; - time: string; - memory: string; - - subtasks?: NormalizedSubtask[]; - count?: number; - checker_type?: string; - detail?: boolean; - filename?: string; - - judge_extra_files: string[]; - user_extra_files: string[]; - template?: Record; - - checker?: string; - validator?: string; - std?: string; - hack?: string; - interactor?: string; - - answers?: Record; -} - export interface ContextSubTask { subtask: NormalizedSubtask; score: number; diff --git a/packages/hydrojudge/src/judge/submit_answer.ts b/packages/hydrojudge/src/judge/submit_answer.ts index 006ca208a..1b1fb7a54 100644 --- a/packages/hydrojudge/src/judge/submit_answer.ts +++ b/packages/hydrojudge/src/judge/submit_answer.ts @@ -1,9 +1,10 @@ +import { basename } from 'path'; import { readFile } from 'fs-extra'; import { STATUS } from '@hydrooj/utils/lib/status'; import { check, compileChecker } from '../check'; import { runFlow } from '../flow'; import { del, run } from '../sandbox'; -import { NormalizedCase, parseFilename } from '../utils'; +import { NormalizedCase } from '../utils'; import { Context, ContextSubTask } from './interface'; const Score = { @@ -80,18 +81,16 @@ function judgeCase(c: NormalizedCase, sid: string) { export const judge = async (ctx: Context) => { await runFlow(ctx, { compile: async () => { - ctx.checker = await (() => { - const copyIn = { user_code: ctx.code }; - for (const file of ctx.config.judge_extra_files) { - copyIn[parseFilename(file)] = { src: file }; - } - return compileChecker( - ctx.session.getLang, - ctx.config.checker_type, - ctx.config.checker, - copyIn, - ); - })(); + const copyIn = { user_code: ctx.code }; + for (const file of ctx.config.judge_extra_files) { + copyIn[basename(file)] = { src: file }; + } + ctx.checker = await compileChecker( + ctx.session.getLang, + ctx.config.checker_type, + ctx.config.checker, + copyIn, + ); ctx.clean.push(ctx.checker.clean); }, judgeCase, diff --git a/packages/hydrojudge/src/tmpfs.ts b/packages/hydrojudge/src/tmpfs.ts index 4b7326527..2fe21cb7c 100644 --- a/packages/hydrojudge/src/tmpfs.ts +++ b/packages/hydrojudge/src/tmpfs.ts @@ -1,3 +1,4 @@ +import assert from 'assert'; import child from 'child_process'; import os from 'os'; import fs from 'fs-extra'; @@ -12,6 +13,7 @@ if (uid !== 0) log.warn('Not running by root. tmpfs disabled.'); export function mount(path: string, size = '32m') { fs.ensureDirSync(path); + assert(size.match(/^\d+[kmg]b?$/gi)); if (linux && uid === 0) child.execSync(`mount tmpfs ${path} -t tmpfs -o size=${size}`); } diff --git a/packages/hydrojudge/src/utils.ts b/packages/hydrojudge/src/utils.ts index 8c9de8b8a..c6c4993c2 100644 --- a/packages/hydrojudge/src/utils.ts +++ b/packages/hydrojudge/src/utils.ts @@ -8,11 +8,6 @@ const EMPTY_STR = /^[ \r\n\t]*$/i; export const cmd = parse; -export function parseFilename(filePath: string) { - const t = filePath.split('/'); - return t[t.length - 1]; -} - export namespace Lock { const data = {}; @@ -34,7 +29,7 @@ export function compilerText(stdout: string, stderr: string) { return ret.join('\n'); } -export function restrictFile(p: string) { +function restrictFile(p: string) { if (!p) return '/'; if (p[0] === '/') p = ''; return p.replace(/\.\./gmi, '');