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

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