From 4b7f18baaaf6757cabd02c6470d7646dc773b4a0 Mon Sep 17 00:00:00 2001 From: undefined Date: Mon, 5 Oct 2020 20:58:30 +0800 Subject: [PATCH] judge: ignore tmpfs error --- packages/hydrojudge/package.json | 5 +--- packages/hydrojudge/src/sysinfo.ts | 48 ++++++++++++++++-------------- packages/hydrojudge/src/tmpfs.ts | 10 +++++-- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/packages/hydrojudge/package.json b/packages/hydrojudge/package.json index 640c8cc2e..7ff17a536 100644 --- a/packages/hydrojudge/package.json +++ b/packages/hydrojudge/package.json @@ -1,11 +1,8 @@ { "name": "@hydrooj/hydrojudge", - "version": "2.0.13", + "version": "2.0.14", "main": "package.json", "author": "masnn", - "os": [ - "linux" - ], "dependencies": { "axios": "^0.20.0", "bson": "^4.1.0", diff --git a/packages/hydrojudge/src/sysinfo.ts b/packages/hydrojudge/src/sysinfo.ts index 65d9115ac..0dc97495b 100644 --- a/packages/hydrojudge/src/sysinfo.ts +++ b/packages/hydrojudge/src/sysinfo.ts @@ -20,9 +20,10 @@ const cache: any = {}; async function stackSize() { let output = ''; - const context: any = { - lang: 'ccWithoutO2', - code: `#include + try { + const context: any = { + lang: 'ccWithoutO2', + code: `#include using namespace std; int i=1; int main(){ @@ -32,25 +33,28 @@ int main(){ if (i>256) return 0; main(); }`, - config: { - time: 3000, - memory: 256, - }, - stat: {}, - clean: [], - next: (data) => { - if (data.case) output = data.case.message; - }, - end: () => { }, - }; - context.tmpdir = path.resolve(tmpdir(), 'hydro', 'tmp', 'sysinfo'); - fs.ensureDirSync(context.tmpdir); - tmpfs.mount(context.tmpdir, '64m'); - await judge(context).catch((e) => console.error(e)); - // eslint-disable-next-line no-await-in-loop - for (const clean of context.clean) await clean().catch(); - tmpfs.umount(context.tmpdir); - fs.removeSync(context.tmpdir); + config: { + time: 3000, + memory: 256, + }, + stat: {}, + clean: [], + next: (data) => { + if (data.case) output = data.case.message; + }, + end: () => { }, + }; + context.tmpdir = path.resolve(tmpdir(), 'hydro', 'tmp', 'sysinfo'); + fs.ensureDirSync(context.tmpdir); + tmpfs.mount(context.tmpdir, '64m'); + await judge(context).catch((e) => console.error(e)); + // eslint-disable-next-line no-await-in-loop + for (const clean of context.clean) await clean().catch(); + tmpfs.umount(context.tmpdir); + fs.removeSync(context.tmpdir); + } catch (e) { + return -1; + } const a = output.split(' '); return parseInt(a[a.length - 2], 10); } diff --git a/packages/hydrojudge/src/tmpfs.ts b/packages/hydrojudge/src/tmpfs.ts index 3d5a26743..14d13cb6c 100644 --- a/packages/hydrojudge/src/tmpfs.ts +++ b/packages/hydrojudge/src/tmpfs.ts @@ -1,11 +1,17 @@ import child from 'child_process'; import fs from 'fs-extra'; +import os from 'os'; +import { Logger } from 'hydrooj/dist/logger'; + +const logger = new Logger('judge'); +const linux = os.platform() === 'linux'; +if (!linux) logger.warn('Not running on linux. tmpfs disabled.'); export function mount(path: string, size = '32m') { fs.ensureDirSync(path); - child.execSync(`mount tmpfs ${path} -t tmpfs -o size=${size}`); + if (linux) child.execSync(`mount tmpfs ${path} -t tmpfs -o size=${size}`); } export function umount(path: string) { - child.execSync(`umount ${path}`); + if (linux) child.execSync(`umount ${path}`); }