source: project/wiki/eggref/4/vfs @ 32241

Last change on this file since 32241 was 32241, checked in by felix winkelmann, 6 years ago

removed some call/cc.org links

File size: 6.3 KB
Line 
1[[toc:]]
2[[tags: egg]]
3
4== vfs
5
6=== Introduction
7
8[[tinyclos|TinyCLOS]] wrappers for some file operations, which are:
9
10 open-input-file
11 open-output-file
12 file-exists?
13 delete-file
14 rename-file
15
16When this extension is used, all of the above procedures invoke
17user-defined generic functions implementing the actual file system
18operations. Since nearly all file operations (with the exception of
19those invoking operations from the [[/man/3/Unit posix|posix]] library unit)
20most procedures working on files can take advantage of this facility.
21
22To distinguish between different file-systems, a pathname may be
23preceded by an URI scheme of the form "{{<scheme>://}}".  If no scheme
24is given the current ''default file system'' is used.
25
26=== Requirements
27
28[[tinyclos]], [[regex-case]]
29
30
31=== Documentation
32
33==== class vfs:file-system
34
35<class><vfs:file-system></class>
36
37Represents an object that can be accessed via file-system operations.
38
39==== vfs:open-input-file
40==== vfs:open-output-file
41
42<method>(vfs:open-file FILESYSTEM PATHNAME OUTPUT? MODES)</method>
43<method>(vfs:open-input-file FILESYSTEM PATHNAME MODES)</method>
44<method>(vfs:open-output-file FILESYSTEM PATHNAME MODES)</method>
45
46Called for {{FILESYSTEM}} (an instance of {{<vfs:file-system>}}) when a file
47should be opened for reading or writing. {{PATHNAME}} is a string representing
48the path to the desired file.
49{{MODES}} is a list of one or more keywords specifying extra attributes
50for the file to be opened which are:
51
52 #:append
53 #:binary
54 #:text
55
56See the [[/man/3|The User's Manual]] for a description
57of these modes. If the {{vfs:open-file}} method is not implemented for
58a given file system, then either {{vfs:open-input-file}} or
59{{vfs:open-output-file}} are called, depending on the boolean
60{{OUTPUT?}}.
61
62All of these generic functions should return a port object.
63
64==== vfs:file-exists?
65
66<method>(vfs:file-exists? FILESYSTEM PATHNAME)</method>
67
68Called for {{file-exists?}} when the given pathname refers to {{FILESYSTEM}}.
69Should return true when the designated file exists or {{#f}} if not.
70
71==== vfs:delete-file
72
73<method>(vfs:delete-file FILESYSTEM PATHNAME)</method>
74
75Called for {{delete-file!}} and should delete the entity represented by
76{{PATHNAME}} in the given filesystem.
77
78==== vfs:rename-file
79
80<method>(vfs:rename-file FILESYSTEM OLDPATHNAME NEWPATHNAME)</method>
81
82Called for {{rename-file}}.
83
84==== vfs:register-file-system
85
86<procedure>(vfs:register-file-system SCHEME FILESYSTEM)</procedure>
87
88Registers a filesystem prefix named {{SCHEME}} (a string) for
89{{FILESYSTEM}}. Any of the above mentioned file operations that refer to
90a pathname prefixed with {{<SCHEME>://}} will invoke the appropriate
91method implemented for {{FILESYSTEM}} or a default method that signals
92an error. The actual path passed to the file operation methods will
93receive the pathname with the prefix removed.
94
95==== vfs:unregister-file-system
96
97<procedure>(vfs:unregister-file-system SCHEME)</procedure>
98
99Un-registers a filesystem prefix.
100
101==== class vfs:local-file-system
102
103<class><vfs:local-file-system></class>
104
105A subclass of {{<vfs:file-system>}} representing the default local,
106native fileystem. Implements all methods and does the usual stuff.
107
108==== vfs:current-file-system
109
110<parameter>vfs:current-file-system</parameter>
111
112Holds the current file system, which is used if a pathname has no
113filesystem prefix.
114
115=== Examples
116
117<enscript highlight=scheme>
118; hash-fs: a simple hash-table based file system
119
120(use vfs tinyclos)
121
122
123(define-class <hash-file-system> (<vfs:file-system>) (table))
124
125(define-method (vfs:open-input-file (fs <hash-file-system>) name modes)
126  (open-input-string
127   (hash-table-ref
128    (slot-ref fs 'table) name
129    (cut error 'open-input-file "file not found" name fs)) ) )
130
131(define-method (vfs:open-output-file (fs <hash-file-system>) name modes)
132  (let ((o (open-output-string))
133        (t (slot-ref fs 'table)))
134    (when (memq #:append modes)
135      (display (hash-table-ref/default t name "") o) )
136    (make-output-port
137     (cut display <> o)
138     (cut hash-table-set! t name (get-output-string o)) ) ) )
139
140(define-method (vfs:file-exists? (fs <hash-file-system>) name)
141  (and (hash-table-exists? (slot-ref fs 'table) name)
142       name) )
143
144(define-method (vfs:delete-file (fs <hash-file-system>) name)
145  (hash-table-delete! (slot-ref fs 'table) name) )
146
147(define-method (vfs:rename-file (fs <hash-file-system>) old new)
148  (let* ((t (slot-ref fs 'table))
149         (x (hash-table-ref
150             t old
151             (cut error 'rename-file "file not found" old fs)) ) )
152    (hash-table-delete! t old)
153    (hash-table-set! t new x) ) )
154
155(define-method (initialize (fs <hash-file-system>) initargs)
156  (set! (slot-ref fs 'table) (make-hash-table string=?)) )
157
158(vfs:register-file-system "hash" (make <hash-file-system>))
159</enscript>
160
161=== Author
162
163[[/users/felix winkelmann|felix winkelmann]]
164
165=== License
166
167 Copyright (c) 2007, Felix L. Winkelmann
168 All rights reserved.
169
170 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
171 conditions are met:
172
173   Redistributions of source code must retain the above copyright notice, this list of conditions and the following
174     disclaimer.
175   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
176     disclaimer in the documentation and/or other materials provided with the distribution.
177   Neither the name of the author nor the names of its contributors may be used to endorse or promote
178     products derived from this software without specific prior written permission.
179
180 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
181 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
182 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
183 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
184 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
185 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
186 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
187 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
188 POSSIBILITY OF SUCH DAMAGE.
189
190
191=== Version History
192
193; 0.1 : Initial version
Note: See TracBrowser for help on using the repository browser.