7
import { NodeProcess } from 'src/services/node-process';
8
import { TerminalUI } from 'src/services/terminal-ui';
9
import { getCompletionScript } from 'src/effects/shell-completions';
10
+import { atomicWriteFileString } from 'src/utils/atomic-write';
11
12
// ---------------------------------------------------------------------------
13
// Options
385
return { appendPathFiles, replacementsByTarget, loginOverrideWritten };
386
};
387
387
-/** Atomically replace a physical target file's contents, preserving its file mode. */
388
-const atomicWriteWithMode = (
388
+/**
389
+ * Replace an rc file's contents via the shared atomic-write helper, preserving
390
+ * the existing file's mode so a private rc stays private across the rewrite.
391
+ */
392
+const replaceFilePreservingMode = (
393
fs: FileSystem.FileSystem,
390
- writeTarget: string,
394
+ target: string,
395
contents: string
396
): Effect.Effect<void, PlatformError> =>
397
+ atomicWriteFileString({ fs, target, contents, preserveMode: true });
398
+
399
+/** Append each file's queued blocks in one atomic, mode-preserving rewrite per file. */
400
+const appendQueuedBlocks = (params: {
401
+ readonly blocksByFile: ReadonlyMap<string, ReadonlyArray<string>>;
402
+ readonly pathFileTargets: ReadonlyMap<string, string>;
403
+ readonly fs: FileSystem.FileSystem;
404
+ readonly path: Path.Path;
405
+ readonly homedir: string;
406
+ readonly report: (message: string) => Effect.Effect<void>;
407
+}): Effect.Effect<void, PlatformError> =>
408
Effect.gen(function* () {
394
- const existingTargetInfo = yield* fs.stat(writeTarget).pipe(Effect.option);
395
- const tmpPath = `${writeTarget}.composio-tmp`;
396
-
397
- yield* fs.writeFileString(tmpPath, contents);
398
- // Mirror install.sh's write_path_block: when the chmod or rename step
399
- // fails, remove the temp file before propagating the error so no
400
- // `.composio-tmp` litter is left next to the target.
401
- const promoteTmp = Effect.gen(function* () {
402
- if (Option.isSome(existingTargetInfo)) {
403
- yield* fs.chmod(tmpPath, existingTargetInfo.value.mode & 0o7777);
404
- }
405
- yield* fs.rename(tmpPath, writeTarget);
406
- });
407
- yield* promoteTmp.pipe(Effect.onError(() => fs.remove(tmpPath).pipe(Effect.ignore)));
409
+ const { fs, path } = params;
410
+ for (const [filePath, blocks] of params.blocksByFile.entries()) {
411
+ // Re-read instead of reusing the pre-check snapshot: two configured paths
412
+ // can alias the same physical file through symlinks, and this write must
413
+ // not discard a previous iteration's append.
414
+ const existingContents = yield* readMaybeMissingFile(filePath, fs);
415
+ // PATH files were already resolved by the caller; only the completion
416
+ // file needs a fresh resolve.
417
+ const writeTarget =
418
+ params.pathFileTargets.get(filePath) ?? (yield* resolveWriteTarget(filePath, fs));
419
+
420
+ yield* fs
421
+ .makeDirectory(path.dirname(writeTarget), { recursive: true })
422
+ .pipe(
423
+ Effect.catchAll(e =>
424
+ Effect.logDebug('Could not create parent directory (may already exist):', e)
425
+ )
426
+ );
427
+
428
+ const appendContent = '\n' + blocks.join('\n\n') + '\n';
429
+ yield* replaceFilePreservingMode(fs, writeTarget, existingContents + appendContent);
430
+ yield* params.report(`Updated ${tildify(filePath, params.homedir)}`);
431
+ }
432
});
433
434
/** Atomically rewrite each reconciled physical target, preserving its file mode. */
440
}): Effect.Effect<void, PlatformError> =>
441
Effect.gen(function* () {
442
for (const [writeTarget, replacement] of params.replacementsByTarget.entries()) {
419
- yield* atomicWriteWithMode(params.fs, writeTarget, replacement.contents);
443
+ yield* replaceFilePreservingMode(params.fs, writeTarget, replacement.contents);
444
yield* params.report(`Updated ${tildify(replacement.displayPath, params.homedir)}`);
445
}
446
});
551
Effect.catchAll(() => Effect.succeed(filePath))
552
);
553
530
-/**
531
- * Atomically replace `writeTarget` via a same-directory tmp file. The tmp copy
532
- * is created with the target's mode from the start so a private rc's contents
533
- * never sit in a default-mode, world-readable tmp file. open(2) masks the
534
- * requested mode with the process umask (only ever clearing bits), so a chmod
535
- * still follows to pin the exact mode.
536
- */
537
-const replaceFilePreservingMode = (
538
- writeTarget: string,
539
- contents: string,
540
- fs: FileSystem.FileSystem
541
-): Effect.Effect<void, PlatformError> =>
542
- Effect.gen(function* () {
543
- const existingTargetInfo = yield* fs.stat(writeTarget).pipe(Effect.option);
544
- const preservedMode = Option.map(existingTargetInfo, info => info.mode & 0o7777);
545
- const tmpPath = `${writeTarget}.composio-tmp`;
546
- yield* Option.match(preservedMode, {
547
- onNone: () => fs.writeFileString(tmpPath, contents),
548
- onSome: mode => fs.writeFileString(tmpPath, contents, { mode }),
549
- });
550
- // Mirror install.sh's write_path_block: when the chmod or rename step
551
- // fails, remove the temp file before propagating the error so no
552
- // `.composio-tmp` litter is left next to the target.
553
- const promoteTmp = Effect.gen(function* () {
554
- if (Option.isSome(preservedMode)) {
555
- yield* fs.chmod(tmpPath, preservedMode.value);
556
- }
557
- yield* fs.rename(tmpPath, writeTarget);
558
- });
559
- yield* promoteTmp.pipe(Effect.onError(() => fs.remove(tmpPath).pipe(Effect.ignore)));
560
- });
561
-
554
// ---------------------------------------------------------------------------
555
// Exported logic (reusable from install.sh post-install delegation)
556
// ---------------------------------------------------------------------------
763
report: success,
764
});
765
774
- if (blocksByFile.size > 0) {
775
- for (const [filePath, blocks] of blocksByFile.entries()) {
776
- // Re-read instead of reusing the pre-check snapshot: two configured paths
777
- // can alias the same physical file through symlinks, and this write must
778
- // not discard a previous iteration's append.
779
- const existingContents = yield* readMaybeMissingFile(filePath, fs);
780
- // PATH files were already resolved above; only the completion file needs a fresh resolve.
781
- const writeTarget =
782
- pathFileTargets.get(filePath) ?? (yield* resolveWriteTarget(filePath, fs));
783
-
784
- yield* fs
785
- .makeDirectory(path.dirname(writeTarget), { recursive: true })
786
- .pipe(
787
- Effect.catchAll(e =>
788
- Effect.logDebug('Could not create parent directory (may already exist):', e)
789
- )
790
- );
791
-
792
- const appendContent = '\n' + blocks.join('\n\n') + '\n';
793
- yield* replaceFilePreservingMode(writeTarget, existingContents + appendContent, fs);
794
-
795
- yield* success(`Updated ${tildify(filePath, os.homedir)}`);
796
- }
797
- }
766
+ yield* appendQueuedBlocks({
767
+ blocksByFile,
768
+ pathFileTargets,
769
+ fs,
770
+ path,
771
+ homedir: os.homedir,
772
+ report: success,
773
+ });
774
775
const changesWritten = blocksByFile.size > 0 || replacementsByTarget.size > 0;
776
if (!changesWritten) {