refactor: mount smb by puttings credentials in temporary file (#396)

This commit is contained in:
Nico 2026-01-22 21:56:20 +01:00 committed by GitHub
parent 2ab37e6b67
commit 51d2ffad17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -40,16 +40,22 @@ const mount = async (config: BackendConfig, path: string) => {
const source = `//${config.server}/${config.share}`;
const { uid, gid } = os.userInfo();
const options = [`user=${config.username}`, `pass=${password}`, `port=${config.port}`, `uid=${uid}`, `gid=${gid}`];
const credentialsDir = await fs.mkdtemp(`${os.tmpdir()}/zerobyte-smb-`);
const credentialsPath = `${credentialsDir}/credentials`;
const credentialsLines = [`username=${config.username}`, `password=${password}`];
if (config.domain) {
credentialsLines.push(`domain=${config.domain}`);
}
await fs.writeFile(credentialsPath, credentialsLines.join("\n"), { mode: 0o600 });
const options = [`credentials=${credentialsPath}`, `port=${config.port}`, `uid=${uid}`, `gid=${gid}`];
if (config.vers && config.vers !== "auto") {
options.push(`vers=${config.vers}`);
}
if (config.domain) {
options.push(`domain=${config.domain}`);
}
if (config.readOnly) {
options.push("ro");
}
@ -60,11 +66,15 @@ const mount = async (config: BackendConfig, path: string) => {
logger.info(`Executing mount: mount ${args.join(" ")}`);
try {
await executeMount(args);
} catch (error) {
logger.warn(`Initial SMB mount failed, retrying with -i flag: ${toMessage(error)}`);
// Fallback with -i flag if the first mount fails using the mount helper
await executeMount(["-i", ...args]);
try {
await executeMount(args);
} catch (error) {
logger.warn(`Initial SMB mount failed, retrying with -i flag: ${toMessage(error)}`);
await executeMount(["-i", ...args]);
}
} finally {
await fs.rm(credentialsPath, { force: true });
await fs.rmdir(credentialsDir).catch(() => {});
}
logger.info(`SMB volume at ${path} mounted successfully.`);