/* * Copyright 2026 Flix Authors * * Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE.md file. */////// Effect for listing the contents of a directory, and its functions.///pubmod Fs.DirList {use Fs.DirListuse Fs.FileSystemuse Fs.FsLayer.{logPre, logPost}////// Effect for listing the contents of a directory.////// Use the `DirList.runWithFileSystem` handler.///pubeffDirList {////// Returns a list with the names of all files and directories in the given directory `f`.///deflist(f: String): Result[IoError, List[String]] }////// Handles the `DirList` effect of the given function `f`.////// In other words, re-interprets the `DirList` effect using the `IO` effect.///pubdefhandle(f: a -> b \ ef): a -> b \ (ef - DirList) + IO = x ->run { f(x) } withhandlerDirList {deflist(filename, k) = k(Fs.FsLayer.list(filename)) }////// Runs the `DirList` effect of the given function `f`.////// In other words, re-interprets the `DirList` effect using the `IO` effect.///@DefaultHandlerpubdefrunWithIO(f: Unit -> a \ ef): a \ (ef - DirList) + IO = handle(f)()////// Handles the `DirList` effect of the given function `f` using the `FileSystem` effect.///pubdefrunWithFileSystem(f: Unit -> a \ ef): a \ (ef - DirList) + FileSystem =run { f() } withhandlerDirList {deflist(filename, k) = k(Fs.FileSystem.list(filename)) }////// Middleware that intercepts the `DirList` effect, logging each operation/// and its result via the `Logger` effect.////// Successful operations are logged at `Debug` level; errors at `Warn`.///pubdefwithLogging(f: Unit -> a \ ef): a \ (ef - DirList) + { DirList, Logger } =run { f() } withhandlerDirList {deflist(filename, k) = {logPre("list", filename);letresult = DirList.list(filename);logPost("list", filename, ls -> "${List.length(ls)} entries", result);k(result) } }////// Middleware that intercepts the `DirList` effect, resolving all file paths/// relative to the given `baseDir` using Java's `Path.resolve` semantics.////// Absolute paths are passed through unchanged.///pubdefwithBaseDir(baseDir: String, f: Unit -> a \ ef): a \ (ef - DirList) + DirList =letr = p -> Fs.FsLayer.resolve(baseDir, p);run { f() } withhandlerDirList {deflist(filename, k) = k(DirList.list(r(filename))) }////// Middleware that intercepts the `DirList` effect, validating that all file paths/// resolve within the given `chrootDir`. Rejects paths that escape the chroot with/// a `PermissionDenied` error.///pubdefwithChroot(chrootDir: String, f: Unit -> a \ ef): a \ (ef - DirList) + DirList =letc = p -> Fs.FsLayer.chroot(chrootDir, p);run { f() } withhandlerDirList {deflist(filename, k) = k(Result.flatMap(p -> DirList.list(p), c(filename))) }////// Middleware that intercepts the `DirList` effect, validating that all file paths/// resolve within one of the given `allowedDirs`. Rejects paths outside the/// allow list with a `PermissionDenied` error.///pubdefwithAllowList(allowedDirs: Nel[String], f: Unit -> a \ ef): a \ (ef - DirList) + DirList =letc = p -> Fs.FsLayer.allowList(allowedDirs, p);run { f() } withhandlerDirList {deflist(filename, k) = k(Result.flatMap(p -> DirList.list(p), c(filename))) }////// Middleware that intercepts the `DirList` effect, validating that all file paths/// do not resolve within any of the given `deniedDirs`. Rejects paths inside a/// denied directory with a `PermissionDenied` error.///pubdefwithDenyList(deniedDirs: Nel[String], f: Unit -> a \ ef): a \ (ef - DirList) + DirList =letc = p -> Fs.FsLayer.denyList(deniedDirs, p);run { f() } withhandlerDirList {deflist(filename, k) = k(Result.flatMap(p -> DirList.list(p), c(filename))) }////// Middleware that intercepts the `DirList` effect, validating that all file paths/// match at least one of the given glob `patterns`. Rejects paths that do not match/// with a `PermissionDenied` error.///pubdefwithAllowGlob(patterns: Nel[String], f: Unit -> a \ ef): a \ (ef - DirList) + DirList =letc = p -> Fs.FsLayer.allowGlob(patterns, p);run { f() } withhandlerDirList {deflist(filename, k) = k(Result.flatMap(p -> DirList.list(p), c(filename))) }////// Middleware that intercepts the `DirList` effect, validating that all file paths/// do not match any of the given glob `patterns`. Rejects paths that match a denied/// pattern with a `PermissionDenied` error.///pubdefwithDenyGlob(patterns: Nel[String], f: Unit -> a \ ef): a \ (ef - DirList) + DirList =letc = p -> Fs.FsLayer.denyGlob(patterns, p);run { f() } withhandlerDirList {deflist(filename, k) = k(Result.flatMap(p -> DirList.list(p), c(filename))) }////// Middleware that intercepts the `DirList` effect, resolving all file paths/// to their real filesystem targets by following symlinks.///pubdefwithFollowLinks(f: Unit -> a \ ef): a \ (ef - DirList) + DirList =letresolve = p -> Fs.FsLayer.resolveRealPathOrSelf(p);run { f() } withhandlerDirList {deflist(filename, k) = k(DirList.list(resolve(filename))) }}