Move setPermissionsSync to util

This commit is contained in:
Cohee
2025-04-27 18:23:57 +03:00
parent 005a495e96
commit 31cc05ae46
2 changed files with 37 additions and 24 deletions

View File

@@ -7,7 +7,7 @@ import fetch from 'node-fetch';
import sanitize from 'sanitize-filename';
import { sync as writeFileAtomicSync } from 'write-file-atomic';
import { getConfigValue, color } from '../util.js';
import { getConfigValue, color, setPermissionsSync } from '../util.js';
import { write } from '../character-card-parser.js';
import { serverDirectory } from '../server-directory.js';
@@ -149,29 +149,6 @@ async function seedContentForUser(contentIndex, directories, forceCategories) {
}
fs.cpSync(contentPath, targetPath, { recursive: true, force: false });
function setPermissionsSync(targetPath_) {
function appendWritablePermission(filepath, stats) {
const currentMode = stats.mode;
const newMode = currentMode | 0o200;
if (newMode != currentMode) {
fs.chmodSync(filepath, newMode);
}
}
const stats = fs.statSync(targetPath_);
if (stats.isDirectory()) {
appendWritablePermission(targetPath_, stats);
const files = fs.readdirSync(targetPath_);
files.forEach((file) => {
setPermissionsSync(path.join(targetPath_, file));
});
} else {
appendWritablePermission(targetPath_, stats);
}
}
setPermissionsSync(targetPath);
console.info(`Content file ${contentItem.filename} copied to ${contentTarget}`);
anyContentAdded = true;

View File

@@ -1087,3 +1087,39 @@ export function mutateJsonString(jsonString, mutation) {
return jsonString;
}
}
/**
* Sets the permissions of a file or directory to be writable.
* @param {string} targetPath Path to the file or directory
*/
export function setPermissionsSync(targetPath) {
/**
* Appends writable permission to the file mode.
* @param {string} filePath Path to the file
* @param {fs.Stats} stats File stats
*/
function appendWritablePermission(filePath, stats) {
const currentMode = stats.mode;
const newMode = currentMode | 0o200;
if (newMode != currentMode) {
fs.chmodSync(filePath, newMode);
}
}
try {
const stats = fs.statSync(targetPath);
if (stats.isDirectory()) {
appendWritablePermission(targetPath, stats);
const files = fs.readdirSync(targetPath);
files.forEach((file) => {
setPermissionsSync(path.join(targetPath, file));
});
} else {
appendWritablePermission(targetPath, stats);
}
} catch (error) {
console.error(`Error setting write permissions for ${targetPath}:`, error);
}
}