1 (* Copyright (C
) 1999-2007 Henry Cejtin
, Matthew Fluet
, Suresh
2 * Jagannathan
, and Stephen Weeks
.
4 * MLton is released under a BSD
-style license
.
5 * See the file MLton
-LICENSE for details
.
11 structure FS
= OS
.FileSys
18 val layout
= Layout
.str
28 val cd
= Trace
.trace ("Dir.cd", layout
, Unit
.layout
) cd
32 fun isDir d
= FS
.isDir d
handle OS
.SysErr _
=> false
34 val doesExist
= File
.doesExist
41 Exn
.finally (th
, fn () => cd cur
)
44 fun fold (d
: t
, a
: 'a
, f
: string * 'a
-> 'a
): 'a
=
46 val stream
= FS
.openDir d
48 case FS
.readDir stream
of
50 | SOME s
=> loop (f (s
, a
))
52 Exn
.finally (fn () => loop a
, fn () => FS
.closeDir stream
)
56 fold (d
, ([], []), fn (x
, (dirs
, files
)) =>
58 val file
= OS
.Path
.joinDirFile
{ dir
=d
, file
=x
}
59 val file
= FS
.realPath file
64 then (x
:: dirs
, files
)
65 else (dirs
, x
:: files
)
75 (* loop removes everything
in the current directory
*)
77 fold (".", (), fn (s
, ()) =>
93 val d
= concat
[MLton
.TextIO.tempPrefix
"dir", Random
.alphaNumString
6]
96 Exn
.finally (fn () => inDir (d
, fn _
=> thunk ()),