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

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