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 457 - (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 367 exception Format (* if something is seriously wrong with a pickle *)
11 :     exception BadAnchor of string (* if anchor cannot be resolved *)
12 :    
13 : blume 270 type context
14 : blume 265 type t
15 : blume 305 type ord_key = t
16 : blume 265
17 : blume 366 val newEra : unit -> unit
18 : blume 265 val revalidateCwd : unit -> unit
19 :    
20 : blume 270 val cwdContext: unit -> context
21 : blume 353 val sameDirContext: t -> context
22 : blume 270
23 : blume 354 val osstring : t -> string
24 :     val descr : t -> string
25 : blume 265 val compare : t * t -> order
26 : blume 353 val contextOf : t -> context
27 :     val specOf : t -> string
28 : blume 272 val contextName : context -> string
29 : blume 265
30 : blume 352 (* Replace the anchor context in the path argument with the
31 :     * given context. Returns NONE if there was no anchor context. *)
32 : blume 354 val reAnchoredName : t * string -> string option
33 : blume 352
34 : blume 270 val native : { context: context, spec: string } -> t
35 : blume 318 val standard : PathConfig.mode -> { context: context, spec: string } -> t
36 : blume 268
37 : blume 457 val fromDescr : PathConfig.mode -> string -> t
38 :    
39 : blume 353 (* the second path argument is the path of the group spec that
40 :     * pickling is based upon. *)
41 : blume 340 val pickle : (bool -> unit) -> t * t -> string list
42 : blume 367 val unpickle : PathConfig.mode -> string list * t -> t
43 : blume 305
44 : blume 274 val tstamp : t -> TStamp.t
45 : blume 265 end
46 :    
47 :     structure AbsPath :> ABSPATH = struct
48 :    
49 :     structure P = OS.Path
50 :     structure F = OS.FileSys
51 : blume 340 val impossible = GenericVC.ErrorMsg.impossible
52 : blume 265
53 : blume 367 exception Format
54 :     exception BadAnchor of string
55 :    
56 : blume 265 (* unique file id that can handle absent files *)
57 :     datatype id =
58 :     PRESENT of F.file_id
59 :     | ABSENT of string
60 :    
61 :     (* comparison of unique file ids *)
62 :     fun compareId (PRESENT fid, PRESENT fid') = F.compare (fid, fid')
63 :     | compareId (ABSENT _, PRESENT _) = LESS
64 :     | compareId (PRESENT _, ABSENT _) = GREATER
65 :     | compareId (ABSENT s, ABSENT s') = String.compare (s, s')
66 :    
67 : blume 374 (* To maximize our chances of recognizing eqivalent path names to
68 :     * non-existing files, we use F.fullPath to expand the largest
69 :     * possible prefix of the path. *)
70 :     fun expandPath f = let
71 :     fun loop { dir, file } =
72 :     P.concat (F.fullPath dir, file)
73 :     handle _ => let
74 :     val { dir = dir', file = file' } = P.splitDirFile dir
75 :     in
76 :     loop { dir = dir', file = P.concat (file', file) }
77 :     end
78 :     in
79 :     (* An initial call to splitDirFile is ok because we already know
80 :     * that the complete path does not refer to an existing file. *)
81 :     loop (P.splitDirFile f)
82 :     end
83 : blume 265
84 : blume 374 fun getId f = (PRESENT (F.fileId f) handle _ => ABSENT (expandPath f))
85 :    
86 : blume 265 type elaboration = { stamp : unit ref,
87 :     name : string,
88 :     id : id option ref }
89 : blume 270
90 : blume 265 (* When a relative name is to be looked up wrt. CUR:
91 :     * - if the cwd hasn't changed since, then use relative path
92 :     * - if the cwd has changed, then make absolute path using name
93 :     * If we come back to the original dir, then ideally we should
94 :     * re-validate the stamp, but that would require having a cwd
95 :     * history -- and, thus, is probably not worth the effort.
96 :     *)
97 :    
98 :     type cwdinfo = { stamp: unit ref, name: string, id: id }
99 : blume 270
100 :     datatype context =
101 : blume 353 THEN_CWD of cwdinfo
102 : blume 265 | CONFIG_ANCHOR of { fetch: unit -> string,
103 : blume 304 cache: elaboration option ref,
104 :     config_name: string }
105 : blume 353 | DIR_OF of t
106 : blume 457 | ROOT of string (* carries volume *)
107 : blume 270
108 : blume 324 and t =
109 : blume 270 PATH of { context: context,
110 : blume 265 spec: string,
111 :     cache: elaboration option ref }
112 :    
113 : blume 305 type ord_key = t
114 :    
115 : blume 265 local
116 :     val cwdInfoCache : cwdinfo option ref = ref NONE
117 :     fun cwdInfo () =
118 :     case !cwdInfoCache of
119 :     SOME i => i
120 :     | NONE => let
121 :     val stamp = ref ()
122 :     val name = F.getDir ()
123 :     val id = PRESENT (F.fileId name)
124 :     val i = { stamp = stamp, name = name, id = id }
125 :     in
126 :     cwdInfoCache := SOME i;
127 :     i
128 :     end
129 :     val cwdStamp = #stamp o cwdInfo
130 :     val cwdName = #name o cwdInfo
131 :     val cwdId = #id o cwdInfo
132 :     in
133 :     (* start a new era (i.e., invalidate all previous elaborations) *)
134 : blume 369 val newEra = Era.newEra
135 : blume 265
136 :     (* make sure the cwd is consistent *)
137 :     fun revalidateCwd () =
138 :     case !cwdInfoCache of
139 :     NONE => ignore (cwdInfo ())
140 :     | SOME { name, id, ... } => let
141 :     val name' = F.getDir ()
142 :     val id' = PRESENT (F.fileId name')
143 :     in
144 :     if compareId (id, id') <> EQUAL then
145 :     (newEra ();
146 :     cwdInfoCache := SOME { stamp = ref (),
147 :     name = name', id = id' })
148 :     else ()
149 :     end
150 :    
151 : blume 270 fun cwdContext () =
152 : blume 366 (revalidateCwd ();
153 :     THEN_CWD { stamp = cwdStamp (),
154 :     name = cwdName (),
155 :     id = cwdId () })
156 : blume 270
157 : blume 353 fun sameDirContext p = DIR_OF p
158 : blume 270
159 :     fun mkElab (cache, name) = let
160 :     val e : elaboration =
161 : blume 369 { stamp = Era.thisEra (), name = name, id = ref NONE }
162 : blume 265 in
163 : blume 270 cache := SOME e; e
164 :     end
165 :    
166 :     fun validElab NONE = NONE
167 :     | validElab (SOME (e as { stamp, name, id })) =
168 : blume 369 if Era.isThisEra stamp then SOME e else NONE
169 : blume 270
170 : blume 457 fun rootName vol = P.toString { isAbs = true, arcs = [], vol = vol }
171 :     val rootId = let
172 :     val m = ref (StringMap.empty: id option ref StringMap.map)
173 :     in
174 :     fn vol =>
175 :     (case StringMap.find (!m, vol) of
176 :     NONE => let
177 :     val idr = ref NONE
178 :     in
179 :     m := StringMap.insert (!m, vol, idr);
180 :     idr
181 :     end
182 :     | SOME idr => idr)
183 :     end
184 : blume 352
185 : blume 270 fun elabContext c =
186 :     case c of
187 : blume 353 THEN_CWD { stamp, name, id } =>
188 : blume 369 { stamp = Era.thisEra (), id = ref (SOME id),
189 : blume 353 name = if stamp = cwdStamp () orelse name = cwdName ()
190 : blume 270 then P.currentArc else name }
191 : blume 304 | CONFIG_ANCHOR { fetch, cache, config_name } =>
192 : blume 270 (case validElab (!cache) of
193 :     SOME e => e
194 :     | NONE => mkElab (cache, fetch ()))
195 : blume 353 | DIR_OF p => let
196 :     val { name, stamp, ... } = elab p
197 :     in
198 :     { name = P.dir name, stamp = stamp, id = ref NONE }
199 :     end
200 : blume 457 | ROOT vol => { stamp = Era.thisEra (),
201 :     name = rootName vol,
202 :     id = rootId vol }
203 : blume 265
204 : blume 305 and elab (PATH { context, spec, cache }) =
205 : blume 353 case validElab (!cache) of
206 :     SOME e => e
207 : blume 357 | NONE => mkElab (cache,
208 : blume 373 P.concat (#name (elabContext context), spec))
209 : blume 270
210 : blume 265 (* get the file id (calls elab, so don't cache externally!) *)
211 :     fun id p = let
212 :     val { id, name, ... } = elab p
213 :     in
214 :     case !id of
215 :     NONE => let
216 :     val i = getId name
217 :     in
218 :     id := SOME i; i
219 :     end
220 :     | SOME i => i
221 :     end
222 :    
223 :     (* get the name as a string (calls elab, so don't cache externally!) *)
224 : blume 354 fun osstring p = #name (elab p)
225 : blume 265
226 : blume 272 (* get the context back *)
227 : blume 353 fun contextOf (PATH { context = c, ... }) = c
228 : blume 272 fun contextName c = #name (elabContext c)
229 :    
230 :     (* get the spec back *)
231 : blume 353 fun specOf (PATH { spec = s, ... }) = s
232 : blume 272
233 : blume 265 (* compare pathnames efficiently *)
234 :     fun compare (p1, p2) = compareId (id p1, id p2)
235 :    
236 : blume 305 fun fresh (context, spec) =
237 :     PATH { context = context, spec = spec, cache = ref NONE }
238 : blume 265
239 : blume 268 (* make an abstract path from a native string *)
240 : blume 352 fun native { spec, context } = let
241 :     val { isAbs, vol, arcs } = P.fromString spec
242 : blume 457 val relSpec = P.toString { isAbs = false, vol = "", arcs = arcs }
243 : blume 352 in
244 : blume 457 if isAbs then fresh (ROOT vol, relSpec)
245 : blume 352 else fresh (context, relSpec)
246 :     end
247 : blume 268
248 : blume 265 (* make an abstract path from a standard string *)
249 : blume 318 fun standard mode { spec, context } = let
250 : blume 265 fun delim #"/" = true
251 :     | delim #"\\" = true (* accept DOS-style, too *)
252 :     | delim _ = false
253 :    
254 : blume 353 fun transl ".." = P.parentArc
255 :     | transl "." = P.currentArc
256 : blume 265 | transl arc = arc
257 :    
258 : blume 352 fun mk (arcs, context) =
259 : blume 268 fresh (context,
260 : blume 352 P.toString { isAbs = false, vol = "",
261 : blume 305 arcs = map transl arcs })
262 : blume 265 in
263 :     case String.fields delim spec of
264 : blume 353 [""] => impossible "AbsPath.standard: zero-length name"
265 : blume 457 | "" :: arcs => mk (arcs, ROOT "")
266 : blume 353 | [] => impossible "AbsPath.standard: no fields"
267 : blume 304 | arcs as (arc1 :: _) =>
268 : blume 318 (case PathConfig.configAnchor mode arc1 of
269 : blume 352 NONE => mk (arcs, context)
270 : blume 265 | SOME fetch => let
271 :     val anchorcontext =
272 :     CONFIG_ANCHOR { fetch = fetch,
273 : blume 304 cache = ref NONE,
274 :     config_name = arc1 }
275 : blume 265 in
276 : blume 352 mk (arcs, anchorcontext)
277 : blume 265 end)
278 :     end
279 : blume 268
280 : blume 305 (* make a pickle-string *)
281 : blume 353 fun pickle warn (path, gpath) = let
282 :     fun p_p (PATH { spec, context, ... }) =
283 :     spec :: p_c context
284 : blume 457 and p_c (ROOT vol) = (warn true; [vol, "r"])
285 : blume 353 | p_c (THEN_CWD _) = impossible "AbsPath.pickle: THEN_CWD"
286 :     | p_c (CONFIG_ANCHOR { config_name = n, ... }) = [n, "a"]
287 :     | p_c (DIR_OF p) =
288 :     if compare (p, gpath) = EQUAL then (warn false; ["c"])
289 :     else p_p p
290 : blume 305 in
291 : blume 340 p_p path
292 : blume 305 end
293 :    
294 : blume 353 fun unpickle mode (l, gpath) = let
295 :     fun u_p (s :: l) =
296 : blume 367 PATH { spec = s, context = u_c l, cache = ref NONE }
297 :     | u_p [] = raise Format
298 : blume 457 and u_c [vol, "r"] = ROOT vol
299 : blume 367 | u_c ["c"] = DIR_OF gpath
300 : blume 353 | u_c [n, "a"] =
301 : blume 318 (case PathConfig.configAnchor mode n of
302 : blume 367 NONE => raise BadAnchor n
303 : blume 353 | SOME fetch =>
304 : blume 367 CONFIG_ANCHOR { config_name = n,
305 :     fetch = fetch,
306 :     cache = ref NONE })
307 :     | u_c l = DIR_OF (u_p l)
308 : blume 305 in
309 : blume 353 u_p l
310 : blume 305 end
311 :    
312 : blume 354 fun tstamp p = TStamp.fmodTime (osstring p)
313 : blume 268
314 : blume 354 fun descr (PATH { spec, context, ... }) = let
315 :     fun dir (x, l) =
316 : blume 357 case P.dir x of
317 : blume 354 "" => l
318 :     | d => d :: "/" :: l
319 :     fun d_c (CONFIG_ANCHOR { config_name = n, ... }, l) =
320 :     "$" :: n :: "/" :: l
321 :     | d_c (DIR_OF (PATH { spec, context, ... }), l) =
322 :     d_c (context, dir (spec, l))
323 :     | d_c (THEN_CWD _, l) = "./" :: l
324 : blume 457 | d_c (ROOT "", l) = "/" :: l
325 :     | d_c (ROOT vol, l) = "%" :: vol :: "/" :: l
326 : blume 274 in
327 : blume 354 concat (d_c (context, [spec]))
328 : blume 274 end
329 : blume 275
330 : blume 457 fun fromDescr mode "" = fresh (cwdContext (), P.currentArc)
331 :     | fromDescr mode d = let
332 :     val l = size d
333 :     fun split n =
334 :     if n >= l then
335 :     (String.substring (d, 1, l), P.currentArc)
336 :     else if String.sub (d, n) = #"/" then
337 :     (String.substring (d, 1, n - 1),
338 :     String.extract (d, n + 1, NONE))
339 :     else split (n + 1)
340 :     in
341 :     case String.sub (d, 0) of
342 :     #"$" => let
343 :     val (a, s) = split 1
344 :     in
345 :     case PathConfig.configAnchor mode a of
346 :     NONE => raise BadAnchor a
347 :     | SOME fetch =>
348 :     fresh (CONFIG_ANCHOR { config_name = a,
349 :     fetch = fetch,
350 :     cache = ref NONE },
351 :     s)
352 :     end
353 :     | #"/" => fresh (ROOT "", String.extract (d, 1, NONE))
354 :     | #"." => fresh (cwdContext (), String.extract (d, 2, NONE))
355 :     | #"%" => let
356 :     val (v, s) = split 1
357 :     in
358 :     fresh (ROOT v, s)
359 :     end
360 :     | _ => fresh (cwdContext (), d)
361 :     end
362 :    
363 : blume 354 fun reAnchoredName (p, dirstring) = let
364 :     fun path (PATH { context, spec, ... }) = let
365 :     fun mk c = P.concat (c, spec)
366 :     in
367 :     Option.map mk (ctxt context)
368 :     end
369 :     and ctxt (CONFIG_ANCHOR { config_name = n, ... }) =
370 :     SOME (P.concat (dirstring, n))
371 :     | ctxt (DIR_OF p) = Option.map P.dir (path p)
372 : blume 457 | ctxt (THEN_CWD _) = NONE
373 :     | ctxt (ROOT _) = NONE
374 : blume 275 in
375 : blume 373 path p
376 : blume 275 end
377 : blume 265 end
378 :     end

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