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 340 - (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 340 val pickle : (bool -> unit) -> t * t -> string list
30 :     val unpickle : PathConfig.mode -> string list * t -> 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 : blume 275
40 :     (* The open?Out functions automagically create any necessary directories
41 :     * and announce this activity via their string consumer argument. *)
42 :     val openTextIn : t -> TextIO.instream
43 : blume 297 val openTextOut : t -> TextIO.outstream
44 : blume 275 val openBinIn : t -> BinIO.instream
45 : blume 297 val openBinOut : t -> BinIO.outstream
46 : blume 265 end
47 :    
48 :     structure AbsPath :> ABSPATH = struct
49 :    
50 :     structure P = OS.Path
51 :     structure F = OS.FileSys
52 : blume 340 val impossible = GenericVC.ErrorMsg.impossible
53 : blume 265
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 : blume 340 fun pickle warn (path, gdir) = let
241 :     val warned = ref false
242 :     fun warn_once abs =
243 :     if !warned then () else (warned := true; warn abs)
244 :     fun check_abs spec =
245 :     if OS.Path.isAbsolute spec then warn_once true else ()
246 :     fun p_p (p as PATH { context, spec, ... }) =
247 :     if compare (p, gdir) = EQUAL then (warn_once false; ["r"])
248 :     else (check_abs spec; spec :: p_c context)
249 :     and p_c (CONFIG_ANCHOR { config_name = n, ... }) = [n, "a"]
250 :     | p_c (RELATIVE p) = p_p p
251 :     | p_c _ = impossible "AbsPath.pickle"
252 : blume 305 in
253 : blume 340 p_p path
254 : blume 305 end
255 :    
256 : blume 340 fun unpickle mode (l, gdir) = let
257 : blume 305 exception Format
258 : blume 340 fun u_p ["r"] = gdir
259 :     | u_p (h :: t) =
260 : blume 305 PATH { context = u_c t, spec = h, cache = ref NONE }
261 :     | u_p [] = raise Format
262 : blume 340 and u_c [n, "a"] =
263 : blume 318 (case PathConfig.configAnchor mode n of
264 : blume 305 NONE => raise Format
265 :     | SOME fetch => CONFIG_ANCHOR { fetch = fetch,
266 :     cache = ref NONE,
267 :     config_name = n })
268 : blume 340 | u_c l = RELATIVE (u_p l)
269 : blume 305 in
270 :     SOME (u_p l) handle Format => NONE
271 :     end
272 :    
273 : blume 269 (* . and .. are not permitted as file parameter *)
274 : blume 305 fun joinDirFile { dir = PATH { context, spec, ... }, file } =
275 : blume 269 if file = P.currentArc orelse file = P.parentArc then
276 : blume 340 impossible "AbsPath.joinDirFile: . or .."
277 : blume 305 else fresh (context, P.joinDirFile { dir = spec, file = file })
278 : blume 268
279 : blume 269 (* splitDirFile never walks past a context.
280 :     * Moreover, it is an error to split something that ends in "..". *)
281 : blume 305 fun splitDirFile (PATH { context, spec, ... }) = let
282 : blume 270 fun loop "" =
283 : blume 340 impossible "AbsPath.splitDirFile: tried to split a context"
284 : blume 270 | loop spec = let
285 :     val { dir, file } = P.splitDirFile spec
286 :     in
287 :     if file = P.currentArc then loop dir
288 :     else if file = P.parentArc then
289 : blume 340 impossible "AbsPath.splitDirFile: <path>/.."
290 : blume 270 else (dir, file)
291 :     end
292 :     val (dir, file) = loop spec
293 :     val dir = if dir = "" then P.currentArc else dir
294 :     in
295 : blume 305 { dir = fresh (context, dir), file = file }
296 : blume 270 end
297 : blume 268
298 :     val dir = #dir o splitDirFile
299 :     val file = #file o splitDirFile
300 : blume 274
301 :     fun fileExists n = F.access (n, []) handle _ => false
302 :     fun fileModTime n = F.modTime n handle _ => Time.zeroTime
303 :    
304 :     val exists = fileExists o name
305 :    
306 : blume 330 fun tstamp p = let
307 : blume 274 val n = name p
308 :     in
309 : blume 330 if fileExists n then TStamp.TSTAMP (fileModTime n)
310 :     else TStamp.NOTSTAMP
311 : blume 274 end
312 : blume 275
313 : blume 297 fun openOut fileopener ap = let
314 : blume 275 val p = name ap
315 :     fun generic (maker, pmaker, p) =
316 :     maker p
317 :     handle exn => let
318 :     val { dir, ... } = P.splitDirFile p
319 :     in
320 :     if dir = "" orelse fileExists dir then raise exn
321 :     else (pmaker dir; maker p)
322 :     end
323 :     fun makedirs dir = generic (F.mkDir, makedirs, dir)
324 :     fun advertisemakedirs dir =
325 : blume 310 (Say.vsay ["[creating directory ", dir, " ...]\n"];
326 : blume 275 makedirs dir)
327 :     in
328 :     generic (fileopener, advertisemakedirs, p)
329 :     end
330 :    
331 :     val openTextIn = TextIO.openIn o name
332 :     val openBinIn = BinIO.openIn o name
333 :     val openTextOut = openOut TextIO.openOut
334 :     val openBinOut = openOut BinIO.openOut
335 : blume 265 end
336 :     end

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