Compare commits

..

3 Commits

Author SHA1 Message Date
copilot-swe-agent[bot] df7f13c6bf Optimize writeFileWithLock to avoid redundant chmod calls
- Track if file is newly created to skip redundant chmod
- Only call chmod for existing files that need permission changes
- Improves performance and reduces unnecessary system calls

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
2025-11-07 16:20:30 +00:00
copilot-swe-agent[bot] 62831835a5 Fix SSH config file permissions race condition
- Modified writeFileWithLock to create files with correct permissions immediately
- Changed string mode values to proper octal numbers (0o600, 0o400)
- This eliminates the race condition where files existed with wrong permissions

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
2025-11-07 16:18:49 +00:00
copilot-swe-agent[bot] 8998b4078f Initial plan 2025-11-07 16:07:38 +00:00
9 changed files with 13139 additions and 10014 deletions
+3 -3
View File
@@ -3,7 +3,7 @@ import { Container } from 'typedi';
import { Logger } from 'winston';
import SubscriptionService from '../services/subscription';
import { celebrate, Joi } from 'celebrate';
import { parseExpression } from 'cron-parser';
import cron_parser from 'cron-parser';
const route = Router();
export default (app: Router) => {
@@ -60,7 +60,7 @@ export default (app: Router) => {
try {
if (
!req.body.schedule ||
parseExpression(req.body.schedule).hasNext()
cron_parser.parseExpression(req.body.schedule).hasNext()
) {
const subscriptionService = Container.get(SubscriptionService);
const data = await subscriptionService.create(req.body);
@@ -193,7 +193,7 @@ export default (app: Router) => {
if (
!req.body.schedule ||
typeof req.body.schedule === 'object' ||
parseExpression(req.body.schedule).hasNext()
cron_parser.parseExpression(req.body.schedule).hasNext()
) {
const subscriptionService = Container.get(SubscriptionService);
const data = await subscriptionService.update(req.body);
+4 -4
View File
@@ -1,6 +1,6 @@
import path from 'path';
import fs from 'fs/promises';
import chokidar, { FSWatcher } from 'chokidar';
import chokidar from 'chokidar';
import config from '../config/index';
import { fileExist, promiseExec, rmPath } from '../config/util';
@@ -55,9 +55,9 @@ export default async (src: string = 'deps') => {
const watcher = chokidar.watch(source, {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
}) as any;
});
watcher
.on('add', (_path: string) => linkToNodeModule(src))
.on('change', (_path: string) => linkToNodeModule(src));
.on('add', (path) => linkToNodeModule(src))
.on('change', (path) => linkToNodeModule(src));
};
+2 -2
View File
@@ -4,7 +4,7 @@ import config from '../config';
import { Crontab, CrontabModel, CrontabStatus } from '../data/cron';
import { exec, execSync } from 'child_process';
import fs from 'fs/promises';
import { parseExpression } from 'cron-parser';
import cron_parser from 'cron-parser';
import {
getFileContentByName,
fileExist,
@@ -670,7 +670,7 @@ export default class CronService {
if (
command &&
schedule &&
parseExpression(schedule).hasNext()
cron_parser.parseExpression(schedule).hasNext()
) {
const name = namePrefix + '_' + index;
+4 -4
View File
@@ -26,13 +26,13 @@ export default class SshKeyService {
if (_exist) {
config = await fs.readFile(this.sshConfigFilePath, { encoding: 'utf-8' });
} else {
await writeFileWithLock(this.sshConfigFilePath, '', { mode: '600' });
await writeFileWithLock(this.sshConfigFilePath, '', { mode: 0o600 });
}
if (!config.includes(this.sshConfigHeader)) {
await writeFileWithLock(
this.sshConfigFilePath,
`${this.sshConfigHeader}\n\n${config}`,
{ mode: '600' },
{ mode: 0o600 },
);
}
}
@@ -46,7 +46,7 @@ export default class SshKeyService {
path.join(this.sshPath, alias),
`${key}${os.EOL}`,
{
mode: '400',
mode: 0o400,
},
);
} catch (error) {
@@ -83,7 +83,7 @@ export default class SshKeyService {
config,
{
encoding: 'utf8',
mode: '600',
mode: 0o600,
},
);
}
+8 -3
View File
@@ -19,9 +19,13 @@ export async function writeFileWithLock(
if (typeof options === 'string') {
options = { encoding: options };
}
let isNewFile = false;
if (!(await fileExist(filePath))) {
const fileHandle = await open(filePath, 'w');
fileHandle.close();
// Create the file with the specified mode if provided, otherwise use default
const fileMode = options?.mode || 0o666;
const fileHandle = await open(filePath, 'w', fileMode);
await fileHandle.close();
isNewFile = true;
}
const lockfilePath = getUniqueLockPath(filePath);
@@ -35,7 +39,8 @@ export async function writeFileWithLock(
lockfilePath,
});
await writeFile(filePath, content, { encoding: 'utf8', ...options });
if (options?.mode) {
// Only chmod if the file already existed (not just created with the correct mode)
if (!isNewFile && options?.mode) {
await chmod(filePath, options.mode);
}
await release();
+2 -2
View File
@@ -1,5 +1,5 @@
import { Joi } from 'celebrate';
import { parseExpression } from 'cron-parser';
import cron_parser from 'cron-parser';
import { ScheduleType } from '../interface/schedule';
const validateSchedule = (value: string, helpers: any) => {
@@ -11,7 +11,7 @@ const validateSchedule = (value: string, helpers: any) => {
}
try {
if (parseExpression(value).hasNext()) {
if (cron_parser.parseExpression(value).hasNext()) {
return value;
}
} catch (e) {
+13112 -9992
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -12,7 +12,7 @@ import {
} from 'antd';
import { request } from '@/utils/http';
import config from '@/utils/config';
import { parseExpression } from 'cron-parser';
import cron_parser from 'cron-parser';
import isNil from 'lodash/isNil';
const { Option } = Select;
@@ -381,7 +381,7 @@ const SubscriptionModal = ({
if (
scheduleType === 'interval' ||
!value ||
parseExpression(value).hasNext()
cron_parser.parseExpression(value).hasNext()
) {
return Promise.resolve();
} else {
+2 -2
View File
@@ -1,6 +1,6 @@
import intl from 'react-intl-universal';
import { LANG_MAP, LOG_END_SYMBOL } from './const';
import { parseExpression } from 'cron-parser';
import cron_parser from 'cron-parser';
import { ICrontab } from '@/pages/crontab/type';
export default function browserType() {
@@ -333,7 +333,7 @@ export function getCommandScript(
export function parseCrontab(schedule: string): Date | null {
try {
const time = parseExpression(schedule);
const time = cron_parser.parseExpression(schedule);
if (time) {
return time.next().toDate();
}