Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/cm/paths/abspath.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/paths/abspath.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 275 - (view) (download)

1 : blume 265 (*
2 : blume 267 * Operations over abstract path names.
3 : blume 265 *
4 : blume 267 * Copyright (c) 1999 by Lucent Technologies, Bell Laboratories
5 : blume 265 *
6 : blume 267 * Author: Matthias Blume (blume@cs.princeton.edu)
7 : blume 265 *)
8 :     signature ABSPATH = sig
9 :    
10 : blume 270 type context
11 : blume 265 type t
12 :    
13 :     val revalidateCwd : unit -> unit
14 :    
15 : blume 270 val cwdContext: unit -> context
16 :     val configContext: (unit -> string) -> context
17 :     val relativeContext: t -> context
18 :    
19 : blume 265 val name : t -> string
20 :     val compare : t * t -> order
21 : blume 272 val context : t -> context
22 :     val spec : t -> string
23 :     val contextName : context -> string
24 : blume 265
25 : blume 270 val native : { context: context, spec: string } -> t
26 :     val standard : { context: context, spec: string } -> t
27 : blume 268
28 :     val joinDirFile : { dir: t, file: string } -> t
29 :     val splitDirFile : t -> { dir: t, file: string }
30 :     val dir : t -> t
31 :     val file : t -> string
32 : blume 274
33 :     val exists : t -> bool
34 :     val tstamp : t -> TStamp.t
35 :     val stabletstamp : t -> TStamp.t
36 : blume 275
37 :     (* The open?Out functions automagically create any necessary directories
38 :     * and announce this activity via their string consumer argument. *)
39 :     val openTextIn : t -> TextIO.instream
40 :     val openTextOut : (string -> unit) -> t -> TextIO.outstream
41 :     val openBinIn : t -> BinIO.instream
42 :     val openBinOut : (string -> unit) -> t -> BinIO.outstream
43 : blume 265 end
44 :    
45 :     structure AbsPath :> ABSPATH = struct
46 :    
47 :     structure P = OS.Path
48 :     structure F = OS.FileSys
49 :    
50 :     (* unique file id that can handle absent files *)
51 :     datatype id =
52 :     PRESENT of F.file_id
53 :     | ABSENT of string
54 :    
55 :     (* comparison of unique file ids *)
56 :     fun compareId (PRESENT fid, PRESENT fid') = F.compare (fid, fid')
57 :     | compareId (ABSENT _, PRESENT _) = LESS
58 :     | compareId (PRESENT _, ABSENT _) = GREATER
59 :     | compareId (ABSENT s, ABSENT s') = String.compare (s, s')
60 :    
61 : blume 270 fun getId f = (PRESENT (F.fileId f) handle _ => ABSENT f)
62 : blume 265
63 :     type elaboration = { stamp : unit ref,
64 :     name : string,
65 :     id : id option ref }
66 : blume 270
67 : blume 265 (* When a relative name is to be looked up wrt. CUR:
68 :     * - if the cwd hasn't changed since, then use relative path
69 :     * - if the cwd has changed, then make absolute path using name
70 :     * If we come back to the original dir, then ideally we should
71 :     * re-validate the stamp, but that would require having a cwd
72 :     * history -- and, thus, is probably not worth the effort.
73 :     *)
74 :    
75 :     type cwdinfo = { stamp: unit ref, name: string, id: id }
76 : blume 270
77 :     datatype context =
78 : blume 265 CUR of cwdinfo
79 :     | CONFIG_ANCHOR of { fetch: unit -> string,
80 :     cache: elaboration option ref }
81 : blume 270 | RELATIVE of t
82 :    
83 :     and t =
84 :     PATH of { context: context,
85 : blume 265 spec: string,
86 :     cache: elaboration option ref }
87 :    
88 :     local
89 :     val elabStamp = ref (ref ())
90 :     val cwdInfoCache : cwdinfo option ref = ref NONE
91 :     fun cwdInfo () =
92 :     case !cwdInfoCache of
93 :     SOME i => i
94 :     | NONE => let
95 :     val stamp = ref ()
96 :     val name = F.getDir ()
97 :     val id = PRESENT (F.fileId name)
98 :     val i = { stamp = stamp, name = name, id = id }
99 :     in
100 :     cwdInfoCache := SOME i;
101 :     i
102 :     end
103 :     val cwdStamp = #stamp o cwdInfo
104 :     val cwdName = #name o cwdInfo
105 :     val cwdId = #id o cwdInfo
106 :     fun invalidateCwdInfo () = cwdInfoCache := NONE
107 :     in
108 :     (* start a new era (i.e., invalidate all previous elaborations) *)
109 :     fun newEra () = elabStamp := ref ()
110 :    
111 :     (* make sure the cwd is consistent *)
112 :     fun revalidateCwd () =
113 :     case !cwdInfoCache of
114 :     NONE => ignore (cwdInfo ())
115 :     | SOME { name, id, ... } => let
116 :     val name' = F.getDir ()
117 :     val id' = PRESENT (F.fileId name')
118 :     in
119 :     if compareId (id, id') <> EQUAL then
120 :     (newEra ();
121 :     cwdInfoCache := SOME { stamp = ref (),
122 :     name = name', id = id' })
123 :     else ()
124 :     end
125 :    
126 : blume 270 fun cwdContext () =
127 :     CUR { stamp = cwdStamp (), name = cwdName (), id = cwdId () }
128 :    
129 :     fun configContext fetch =
130 :     CONFIG_ANCHOR { fetch = fetch, cache = ref NONE }
131 :    
132 :     fun relativeContext p = RELATIVE p
133 :    
134 :     fun mkElab (cache, name) = let
135 :     val e : elaboration =
136 :     { stamp = !elabStamp, name = name, id = ref NONE }
137 : blume 265 in
138 : blume 270 cache := SOME e; e
139 :     end
140 :    
141 :     fun validElab NONE = NONE
142 :     | validElab (SOME (e as { stamp, name, id })) =
143 :     if stamp = !elabStamp then SOME e else NONE
144 :    
145 :     fun elabContext c =
146 :     case c of
147 : blume 265 CUR { stamp, name, id } =>
148 :     { stamp = !elabStamp, id = ref (SOME id),
149 : blume 270 name = if stamp = cwdStamp () orelse
150 :     name = cwdName ()
151 :     then P.currentArc else name }
152 :     | CONFIG_ANCHOR { fetch, cache } =>
153 :     (case validElab (!cache) of
154 :     SOME e => e
155 :     | NONE => mkElab (cache, fetch ()))
156 :     | RELATIVE p => elab p
157 : blume 265
158 : blume 270 and elab (PATH { context, spec, cache }) =
159 :     (case validElab (!cache) of
160 :     SOME e => e
161 :     | NONE => let
162 :     val name =
163 :     if P.isAbsolute spec then spec
164 :     else P.mkCanonical
165 :     (P.concat (#name (elabContext context),
166 :     spec))
167 :     in
168 :     mkElab (cache, name)
169 :     end)
170 :    
171 : blume 265 (* get the file id (calls elab, so don't cache externally!) *)
172 :     fun id p = let
173 :     val { id, name, ... } = elab p
174 :     in
175 :     case !id of
176 :     NONE => let
177 :     val i = getId name
178 :     in
179 :     id := SOME i; i
180 :     end
181 :     | SOME i => i
182 :     end
183 :    
184 :     (* get the name as a string (calls elab, so don't cache externally!) *)
185 :     fun name p = #name (elab p)
186 :    
187 : blume 272 (* get the context back *)
188 :     fun context (PATH { context = c, ... }) = c
189 :     fun contextName c = #name (elabContext c)
190 :    
191 :     (* get the spec back *)
192 :     fun spec (PATH { spec = s, ... }) = s
193 :    
194 : blume 265 (* compare pathnames efficiently *)
195 :     fun compare (p1, p2) = compareId (id p1, id p2)
196 :    
197 : blume 268 fun fresh (context, spec) =
198 : blume 270 PATH { context = context, spec = spec, cache = ref NONE }
199 : blume 265
200 : blume 268 (* make an abstract path from a native string *)
201 :     fun native { spec, context } = fresh (context, spec)
202 :    
203 : blume 265 (* make an abstract path from a standard string *)
204 :     fun standard { spec, context } = let
205 :     fun delim #"/" = true
206 :     | delim #"\\" = true (* accept DOS-style, too *)
207 :     | delim _ = false
208 :    
209 :     fun transl ".." = OS.Path.parentArc
210 :     | transl "." = OS.Path.currentArc
211 :     | transl arc = arc
212 :    
213 :     fun mk (isAbs, arcs, context) =
214 : blume 268 fresh (context,
215 :     P.toString { isAbs = isAbs, vol = "",
216 :     arcs = map transl arcs })
217 : blume 265 in
218 :     case String.fields delim spec of
219 :     "" :: arcs => mk (true, arcs, context)
220 :     | [] => mk (false, [], context)
221 :     | arcs as (arc1 :: arcn) =>
222 :     (case PathConfig.configAnchor arc1 of
223 :     NONE => mk (false, arcs, context)
224 :     | SOME fetch => let
225 :     val anchorcontext =
226 :     CONFIG_ANCHOR { fetch = fetch,
227 :     cache = ref NONE }
228 :     in
229 :     mk (false, arcn, anchorcontext)
230 :     end)
231 :     end
232 : blume 268
233 : blume 269 (* . and .. are not permitted as file parameter *)
234 : blume 270 fun joinDirFile { dir = PATH { context, spec, ... }, file } =
235 : blume 269 if file = P.currentArc orelse file = P.parentArc then
236 :     raise Fail "AbsPath.joinDirFile: . or .."
237 : blume 270 else fresh (context, P.joinDirFile { dir = spec, file = file })
238 : blume 268
239 : blume 269 (* splitDirFile never walks past a context.
240 :     * Moreover, it is an error to split something that ends in "..". *)
241 : blume 270 fun splitDirFile (PATH { context, spec, ... }) = let
242 :     fun loop "" =
243 :     raise Fail "AbsPath.splitDirFile: tried to split a context"
244 :     | loop spec = let
245 :     val { dir, file } = P.splitDirFile spec
246 :     in
247 :     if file = P.currentArc then loop dir
248 :     else if file = P.parentArc then
249 :     raise Fail "AbsPath.splitDirFile: <path>/.."
250 :     else (dir, file)
251 :     end
252 :     val (dir, file) = loop spec
253 :     val dir = if dir = "" then P.currentArc else dir
254 :     in
255 :     { dir = fresh (context, dir), file = file }
256 :     end
257 : blume 268
258 :     val dir = #dir o splitDirFile
259 :     val file = #file o splitDirFile
260 : blume 274
261 :     fun fileExists n = F.access (n, []) handle _ => false
262 :     fun fileModTime n = F.modTime n handle _ => Time.zeroTime
263 :    
264 :     val exists = fileExists o name
265 :    
266 :     fun tstamp0 TS p = let
267 :     val n = name p
268 :     in
269 :     if fileExists n then TS (fileModTime n) else TStamp.NOTSTAMP
270 :     end
271 :     val tstamp = tstamp0 TStamp.TSTAMP
272 :     val stabletstamp = tstamp0 TStamp.STABLETSTAMP
273 : blume 275
274 :     fun openOut fileopener (say: string -> unit) ap = let
275 :     val p = name ap
276 :     fun generic (maker, pmaker, p) =
277 :     maker p
278 :     handle exn => let
279 :     val { dir, ... } = P.splitDirFile p
280 :     in
281 :     if dir = "" orelse fileExists dir then raise exn
282 :     else (pmaker dir; maker p)
283 :     end
284 :     fun makedirs dir = generic (F.mkDir, makedirs, dir)
285 :     fun advertisemakedirs dir =
286 :     (say (concat ["[creating directory ", dir, " ...]\n"]);
287 :     makedirs dir)
288 :     in
289 :     generic (fileopener, advertisemakedirs, p)
290 :     end
291 :    
292 :     val openTextIn = TextIO.openIn o name
293 :     val openBinIn = BinIO.openIn o name
294 :     val openTextOut = openOut TextIO.openOut
295 :     val openBinOut = openOut BinIO.openOut
296 : blume 265 end
297 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0