sailfork / t3code-github / packages/ shared/ src/ symlink.ts

import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
import * as PlatformError from "effect/PlatformError";

const MAX_SYMLINK_HOPS = 40;

const isNotFound = (error: PlatformError.PlatformError) => error.reason._tag === "NotFound";

const isNotASymlink = (error: PlatformError.PlatformError) =>
  isNotFound(error) ||
  (error.cause instanceof Error && "code" in error.cause && error.cause.code === "EINVAL");

/**
 * Atomic writers rename onto this path so a linked file keeps its link. Fails
 * on a cycle, an overly long chain, or an unreadable link rather than handing
 * back a link that a rename would replace.
 */
export const resolveSymlinkTarget = Effect.fn("symlink.resolveSymlinkTarget")(function* (
  filePath: string,
) {
  const fs = yield* FileSystem.FileSystem;
  const path = yield* Path.Path;
  let current = path.resolve(filePath);
  // The terminal file needs a probe after the last permitted link is followed.
  for (let hop = 0; hop <= MAX_SYMLINK_HOPS; hop++) {
    const link = yield* fs.readLink(current).pipe(
      Effect.asSome,
      Effect.catchIf(isNotASymlink, () => Effect.succeedNone),
    );
    if (Option.isNone(link)) {
      return current;
    }
    // A relative target is relative to where the link really lives, which
    // differs from its lexical parent when that parent is itself a symlink.
    const linkDirectory = yield* fs
      .realPath(path.dirname(current))
      .pipe(Effect.catchIf(isNotFound, () => Effect.succeed(path.dirname(current))));
    current = path.resolve(linkDirectory, link.value);
  }
  return yield* PlatformError.systemError({
    _tag: "Unknown",
    module: "FileSystem",
    method: "readLink",
    description: "Too many levels of symbolic links",
    pathOrDescriptor: filePath,
  });
});