fix(systemdmanager): migrate systemd file operations to smartfs and tighten TypeScript safety

This commit is contained in:
2026-05-01 15:24:08 +00:00
parent a8b6ecb1a6
commit ddb77fd37d
12 changed files with 2569 additions and 3270 deletions
+9 -9
View File
@@ -29,6 +29,7 @@ export class SmartDaemonSystemdManager {
public smartdaemonRef: SmartDaemon;
public smartshellInstance: plugins.smartshell.Smartshell;
public smartsystem: plugins.smartsystem.Smartsystem;
public smartFs: plugins.smartfs.SmartFs;
public shouldExecute: boolean = false;
public isRoot: boolean = false;
@@ -40,6 +41,7 @@ export class SmartDaemonSystemdManager {
executor: 'bash',
});
this.smartsystem = new plugins.smartsystem.Smartsystem();
this.smartFs = new plugins.smartfs.SmartFs(new plugins.smartfs.SmartFsProviderNode());
this.sudoPassword = sudoPasswordArg;
// Check if we're running as root
@@ -85,7 +87,7 @@ export class SmartDaemonSystemdManager {
try {
await this.smartshellInstance.exec(commandArg);
} catch (error) {
if (!this.isRoot && error.message && error.message.includes('authentication')) {
if (!this.isRoot && error instanceof Error && error.message.includes('authentication')) {
throw new Error('Sudo authentication failed. Please configure passwordless sudo or provide a sudo password.');
}
throw error;
@@ -102,12 +104,10 @@ export class SmartDaemonSystemdManager {
const smartfmInstance = new plugins.smartfm.Smartfm({
fmType: 'yaml',
});
const availableServices = await plugins.smartfile.fs.fileTreeToObject(
paths.systemdDir,
'smartdaemon_*.service'
);
const availableServices = await this.smartFs.directory(paths.systemdDir).filter('smartdaemon_*.service').list();
for (const serviceFile of availableServices) {
const data = smartfmInstance.parseFromComments('# ', serviceFile.contentBuffer.toString())
const serviceFileContent = await this.smartFs.file(serviceFile.path).encoding('utf8').read();
const data = smartfmInstance.parseFromComments('# ', serviceFileContent.toString())
.data as ISmartDaemonServiceConstructorOptions;
const service = await SmartDaemonService.createFromOptions(this.smartdaemonRef, data);
service.alreadyExists = true;
@@ -143,11 +143,11 @@ export class SmartDaemonSystemdManager {
if (this.isRoot) {
// Direct write when running as root
await plugins.smartfile.memory.toFs(content, targetPath);
await this.smartFs.file(targetPath).encoding('utf8').mode(0o644).write(content);
} else {
// Use sudo to write when not root
const tempPath = `/tmp/smartdaemon_${serviceArg.name}.service`;
await plugins.smartfile.memory.toFs(content, tempPath);
await this.smartFs.file(tempPath).encoding('utf8').mode(0o644).write(content);
await this.execute(`mv ${tempPath} ${targetPath}`); // execute() will add sudo
await this.execute(`chmod 644 ${targetPath}`); // execute() will add sudo
}
@@ -159,7 +159,7 @@ export class SmartDaemonSystemdManager {
const filePath = SmartDaemonSystemdManager.createFilePathFromServiceName(serviceArg.name);
if (this.isRoot) {
await plugins.smartfile.fs.remove(filePath);
await this.smartFs.file(filePath).delete();
} else {
await this.execute(`rm ${filePath}`); // execute() will add sudo
}