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 304 - (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 : blume 277 val newEra : unit -> unit
15 : blume 265
16 : blume 270 val cwdContext: unit -> context
17 : blume 304 val configContext: (unit -> string) * string -> context
18 : blume 270 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 304 val pickle : t -> string
26 :     val pickleSpec : t -> string
27 : blume 265
28 : blume 270 val native : { context: context, spec: string } -> t
29 :     val standard : { context: context, spec: string } -> t
30 : blume 304 val unpickle : string -> t
31 :     val unpickleSpec : { context: context, pickledSpec: string } -> t
32 : blume 268
33 :     val joinDirFile : { dir: t, file: string } -> t
34 :     val splitDirFile : t -> { dir: t, file: string }
35 :     val dir : t -> t
36 :     val file : t -> string
37 : blume 274
38 :     val exists : t -> bool
39 :     val tstamp : t -> TStamp.t
40 :     val stabletstamp : t -> TStamp.t
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 :    
55 :     (* unique file id that can handle absent files *)
56 :     datatype id =
57 :     PRESENT of F.file_id
58 :     | ABSENT of string
59 :    
60 :     (* comparison of unique file ids *)
61 :     fun compareId (PRESENT fid, PRESENT fid') = F.compare (fid, fid')
62 :     | compareId (ABSENT _, PRESENT _) = LESS
63 :     | compareId (PRESENT _, ABSENT _) = GREATER
64 :     | compareId (ABSENT s, ABSENT s') = String.compare (s, s')
65 :    
66 : blume 270 fun getId f = (PRESENT (F.fileId f) handle _ => ABSENT f)
67 : blume 265
68 :     type elaboration = { stamp : unit ref,
69 :     name : string,
70 :     id : id option ref }
71 : blume 270
72 : blume 265 (* When a relative name is to be looked up wrt. CUR:
73 :     * - if the cwd hasn't changed since, then use relative path
74 :     * - if the cwd has changed, then make absolute path using name
75 :     * If we come back to the original dir, then ideally we should
76 :     * re-validate the stamp, but that would require having a cwd
77 :     * history -- and, thus, is probably not worth the effort.
78 :     *)
79 :    
80 :     type cwdinfo = { stamp: unit ref, name: string, id: id }
81 : blume 270
82 :     datatype context =
83 : blume 265 CUR of cwdinfo
84 :     | CONFIG_ANCHOR of { fetch: unit -> string,
85 : blume 304 cache: elaboration option ref,
86 :     config_name: string }
87 : blume 270 | RELATIVE of t
88 :    
89 :     and t =
90 :     PATH of { context: context,
91 : blume 265 spec: string,
92 : blume 304 native: bool,
93 : blume 265 cache: elaboration option ref }
94 :    
95 :     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 304 fun configContext (f, n) =
137 :     CONFIG_ANCHOR { fetch = f, cache = ref NONE, config_name = n }
138 : blume 270
139 :     fun relativeContext p = RELATIVE p
140 :    
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 304 and elab (PATH { context, spec, cache, native }) =
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 304 (* make a pickle-string *)
202 :     fun pickle (PATH { context, spec, cache, native }) = Dummy.f ()
203 :     fun pickleSpec (PATH _) = Dummy.f ()
204 :     fun unpickle s = Dummy.f ()
205 :     fun unpickleSpec { context, pickledSpec } = Dummy.f ()
206 :    
207 : blume 265 (* compare pathnames efficiently *)
208 :     fun compare (p1, p2) = compareId (id p1, id p2)
209 :    
210 : blume 304 fun fresh (context, spec, native) =
211 :     PATH { context = context, spec = spec, cache = ref NONE,
212 :     native = native }
213 : blume 265
214 : blume 268 (* make an abstract path from a native string *)
215 : blume 304 fun native { spec, context } = fresh (context, spec, true)
216 : blume 268
217 : blume 265 (* make an abstract path from a standard string *)
218 :     fun standard { spec, context } = let
219 :     fun delim #"/" = true
220 :     | delim #"\\" = true (* accept DOS-style, too *)
221 :     | delim _ = false
222 :    
223 :     fun transl ".." = OS.Path.parentArc
224 :     | transl "." = OS.Path.currentArc
225 :     | transl arc = arc
226 :    
227 :     fun mk (isAbs, arcs, context) =
228 : blume 268 fresh (context,
229 :     P.toString { isAbs = isAbs, vol = "",
230 : blume 304 arcs = map transl arcs },
231 :     false)
232 : blume 265 in
233 :     case String.fields delim spec of
234 :     "" :: arcs => mk (true, arcs, context)
235 : blume 286 | [] => mk (false, [], context) (* shouldn't happen *)
236 :     | [arc] => mk (false, [arc], context)
237 : blume 304 | arcs as (arc1 :: _) =>
238 : blume 265 (case PathConfig.configAnchor arc1 of
239 :     NONE => mk (false, arcs, context)
240 :     | SOME fetch => let
241 :     val anchorcontext =
242 :     CONFIG_ANCHOR { fetch = fetch,
243 : blume 304 cache = ref NONE,
244 :     config_name = arc1 }
245 : blume 265 in
246 : blume 304 mk (false, arcs, anchorcontext)
247 : blume 265 end)
248 :     end
249 : blume 268
250 : blume 269 (* . and .. are not permitted as file parameter *)
251 : blume 304 fun joinDirFile { dir = PATH { context, spec, native, ... }, file } =
252 : blume 269 if file = P.currentArc orelse file = P.parentArc then
253 :     raise Fail "AbsPath.joinDirFile: . or .."
254 : blume 304 else fresh (context, P.joinDirFile { dir = spec, file = file },
255 :     native)
256 : blume 268
257 : blume 269 (* splitDirFile never walks past a context.
258 :     * Moreover, it is an error to split something that ends in "..". *)
259 : blume 304 fun splitDirFile (PATH { context, spec, native, ... }) = let
260 : blume 270 fun loop "" =
261 :     raise Fail "AbsPath.splitDirFile: tried to split a context"
262 :     | loop spec = let
263 :     val { dir, file } = P.splitDirFile spec
264 :     in
265 :     if file = P.currentArc then loop dir
266 :     else if file = P.parentArc then
267 :     raise Fail "AbsPath.splitDirFile: <path>/.."
268 :     else (dir, file)
269 :     end
270 :     val (dir, file) = loop spec
271 :     val dir = if dir = "" then P.currentArc else dir
272 :     in
273 : blume 304 { dir = fresh (context, dir, native), file = file }
274 : blume 270 end
275 : blume 268
276 :     val dir = #dir o splitDirFile
277 :     val file = #file o splitDirFile
278 : blume 274
279 :     fun fileExists n = F.access (n, []) handle _ => false
280 :     fun fileModTime n = F.modTime n handle _ => Time.zeroTime
281 :    
282 :     val exists = fileExists o name
283 :    
284 :     fun tstamp0 TS p = let
285 :     val n = name p
286 :     in
287 :     if fileExists n then TS (fileModTime n) else TStamp.NOTSTAMP
288 :     end
289 :     val tstamp = tstamp0 TStamp.TSTAMP
290 :     val stabletstamp = tstamp0 TStamp.STABLETSTAMP
291 : blume 275
292 : blume 297 fun openOut fileopener ap = let
293 : blume 275 val p = name ap
294 :     fun generic (maker, pmaker, p) =
295 :     maker p
296 :     handle exn => let
297 :     val { dir, ... } = P.splitDirFile p
298 :     in
299 :     if dir = "" orelse fileExists dir then raise exn
300 :     else (pmaker dir; maker p)
301 :     end
302 :     fun makedirs dir = generic (F.mkDir, makedirs, dir)
303 :     fun advertisemakedirs dir =
304 : blume 297 (Say.vsay (concat ["[creating directory ", dir, " ...]\n"]);
305 : blume 275 makedirs dir)
306 :     in
307 :     generic (fileopener, advertisemakedirs, p)
308 :     end
309 :    
310 :     val openTextIn = TextIO.openIn o name
311 :     val openBinIn = BinIO.openIn o name
312 :     val openTextOut = openOut TextIO.openOut
313 :     val openBinOut = openOut BinIO.openOut
314 : blume 265 end
315 :     end

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