Diff

Differences From Artifact [662da588b0]:

To Artifact [20e6e11465]:


1
2
3
4
5

6
7
8
9


10
11

12
13
14
15

16
17
18
19
20
21
22


23
24
25

26
27
28
29
30
31

32
33
34

35
36
37
38
39
40

41
42
43

44
45
46
47
48
49
50
51

52
53

54
55
56
57
58

59
60

61
62
63
64
65
66
67

68
69
70
71
72


73
74
75
76
77
78


79
80
81
82

83
84
85
86
87
88
89
90
91
92


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

111
112
113
114
115
116
117

118
119
120
121
122
123


124
125
126
127
128
129
130
1
2
3
4

5
6
7


8
9
10

11
12
13
14

15
16
17
18
19
20


21
22
23
24

25
26
27
28
29
30

31
32
33

34
35
36
37
38
39

40
41
42

43
44
45
46
47
48
49
50

51
52

53
54
55
56
57

58
59

60
61
62
63
64
65
66

67
68
69
70


71
72
73
74
75
76


77
78
79
80
81

82
83
84
85
86
87
88
89
90


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

110
111
112
113
114
115
116

117
118
119
120
121


122
123
124
125
126
127
128
129
130




-
+


-
-
+
+

-
+



-
+





-
-
+
+


-
+





-
+


-
+





-
+


-
+







-
+

-
+




-
+

-
+






-
+



-
-
+
+




-
-
+
+



-
+








-
-
+
+

















-
+






-
+




-
-
+
+







#! /usr/bin/env tcl

package require vfs

namespace eval ::vfs::kitdll {}
namespace eval ::vfs::cvfs {}

# Convience functions
proc ::vfs::kitdll::Mount {hashkey local} {
	vfs::filesystem mount $local [list ::vfs::kitdll::vfshandler $hashkey]
proc ::vfs::cvfs::Mount {hashkey local} {
	vfs::filesystem mount $local [list ::vfs::cvfs::vfshandler $hashkey]
	catch {
		vfs::RegisterMount $local [list ::vfs::kitdll::Unmount]
		vfs::RegisterMount $local [list ::vfs::cvfs::Unmount]
	}
}

proc ::vfs::kitdll::Unmount {local} {
proc ::vfs::cvfs::Unmount {local} {
	vfs::filesystem unmount $local
}

# Implementation
## I/O Handlers (pass to appropriate hashkey)
namespace eval ::vfs::kitdll::data {}
proc ::vfs::kitdll::data::getChildren args {
namespace eval ::vfs::cvfs::data {}
proc ::vfs::cvfs::data::getChildren args {
	set hashkey [lindex $args 0]

	set cmd "::vfs::kitdll::data::${hashkey}::getChildren"
	set cmd "::vfs::cvfs::data::${hashkey}::getChildren"
	set cmd [linsert $args 0 $cmd]

	eval $cmd
}

proc ::vfs::kitdll::data::getMetadata args {
proc ::vfs::cvfs::data::getMetadata args {
	set hashkey [lindex $args 0]

	set cmd "::vfs::kitdll::data::${hashkey}::getMetadata"
	set cmd "::vfs::cvfs::data::${hashkey}::getMetadata"
	set cmd [linsert $args 0 $cmd]

	eval $cmd
}

proc ::vfs::kitdll::data::getData args {
proc ::vfs::cvfs::data::getData args {
	set hashkey [lindex $args 0]

	set cmd "::vfs::kitdll::data::${hashkey}::getData"
	set cmd "::vfs::cvfs::data::${hashkey}::getData"
	set cmd [linsert $args 0 $cmd]

	eval $cmd
}

## VFS and Chan I/O
### Dispatchers
proc ::vfs::kitdll::vfshandler {hashkey subcmd args} {
proc ::vfs::cvfs::vfshandler {hashkey subcmd args} {
	set cmd $args
	set cmd [linsert $cmd 0 "::vfs::kitdll::vfsop_${subcmd}" $hashkey]
	set cmd [linsert $cmd 0 "::vfs::cvfs::vfsop_${subcmd}" $hashkey]

	return [eval $cmd]
}

proc ::vfs::kitdll::chanhandler {hashkey subcmd args} {
proc ::vfs::cvfs::chanhandler {hashkey subcmd args} {
	set cmd $args
	set cmd [linsert $cmd 0 "::vfs::kitdll::chanop_${subcmd}" $hashkey]
	set cmd [linsert $cmd 0 "::vfs::cvfs::chanop_${subcmd}" $hashkey]

	return [eval $cmd]
}

### Actual handlers
#### Channel operation handlers
proc ::vfs::kitdll::chanop_initialize {hashkey chanId mode} {
proc ::vfs::cvfs::chanop_initialize {hashkey chanId mode} {
	return [list initialize finalize watch read seek]
}

proc ::vfs::kitdll::chanop_finalize {hashkey chanId} {
	unset -nocomplain ::vfs::kitdll::chandata([list $hashkey $chanId])
proc ::vfs::cvfs::chanop_finalize {hashkey chanId} {
	unset -nocomplain ::vfs::cvfs::chandata([list $hashkey $chanId])

	return
}

proc ::vfs::kitdll::chanop_watch {hashkey chanId eventSpec} {
	array set chaninfo $::vfs::kitdll::chandata([list $hashkey $chanId])
proc ::vfs::cvfs::chanop_watch {hashkey chanId eventSpec} {
	array set chaninfo $::vfs::cvfs::chandata([list $hashkey $chanId])

	set chaninfo(watching) $eventSpec

	set ::vfs::kitdll::chandata([list $hashkey $chanId]) [array get chaninfo]
	set ::vfs::cvfs::chandata([list $hashkey $chanId]) [array get chaninfo]

	if {[lsearch -exact $chaninfo(watching) "read"] != -1} {
		after 0 [list catch "chan postevent $chanId [list {read}]"]
	}

	return
}

proc ::vfs::kitdll::chanop_read {hashkey chanId bytes} {
	array set chaninfo $::vfs::kitdll::chandata([list $hashkey $chanId])
proc ::vfs::cvfs::chanop_read {hashkey chanId bytes} {
	array set chaninfo $::vfs::cvfs::chandata([list $hashkey $chanId])

	set pos $chaninfo(pos)
	set len $chaninfo(len)

	if {[lsearch -exact $chaninfo(watching) "read"] != -1} {
		after 0 [list catch "chan postevent $chanId [list {read}]"]
	}

	if {$pos == $len} {
		return ""
	}

	set end [expr {$pos + $bytes}]
	if {$end > $len} {
		set end $len
	}

	set data [::vfs::kitdll::data::getData $hashkey $chaninfo(file) $pos $end]
	set data [::vfs::cvfs::data::getData $hashkey $chaninfo(file) $pos $end]

	set dataLen [string length $data]
	incr pos $dataLen

	set chaninfo(pos) $pos

	set ::vfs::kitdll::chandata([list $hashkey $chanId]) [array get chaninfo]
	set ::vfs::cvfs::chandata([list $hashkey $chanId]) [array get chaninfo]

	return $data
}

proc ::vfs::kitdll::chanop_seek {hashkey chanId offset origin} {
	array set chaninfo $::vfs::kitdll::chandata([list $hashkey $chanId])
proc ::vfs::cvfs::chanop_seek {hashkey chanId offset origin} {
	array set chaninfo $::vfs::cvfs::chandata([list $hashkey $chanId])

	set pos $chaninfo(pos)
	set len $chaninfo(len)

	switch -- $origin {
		"start" - "0" {
			set pos $offset
142
143
144
145
146
147
148
149

150
151
152
153
154
155

156
157

158
159
160
161
162
163
164
165
166
167
168


169
170
171
172
173
174
175
176
177

178
179
180
181

182
183
184
185
186
187
188
189
190
191

192
193
194
195
196
197
198
199
200
201
202
203

204
205
206
207
208
209
210
142
143
144
145
146
147
148

149
150
151
152
153
154

155
156

157
158
159
160
161
162
163
164
165
166


167
168
169
170
171
172
173
174
175
176

177
178
179
180

181
182
183
184
185
186
187
188
189
190

191
192
193
194
195
196
197
198
199
200
201
202

203
204
205
206
207
208
209
210







-
+





-
+

-
+









-
-
+
+








-
+



-
+









-
+











-
+







	}

	if {$pos > $len} {
		set pos $len
	}

	set chaninfo(pos) $pos
	set ::vfs::kitdll::chandata([list $hashkey $chanId]) [array get chaninfo]
	set ::vfs::cvfs::chandata([list $hashkey $chanId]) [array get chaninfo]

	return $pos
}

#### VFS operation handlers
proc ::vfs::kitdll::vfsop_stat {hashkey root relative actualpath} {
proc ::vfs::cvfs::vfsop_stat {hashkey root relative actualpath} {
	catch {
		set ret [::vfs::kitdll::data::getMetadata $hashkey $relative]
		set ret [::vfs::cvfs::data::getMetadata $hashkey $relative]
	}

	if {![info exists ret]} {
		vfs::filesystem posixerror $::vfs::posix(ENOENT)
	}

	return $ret
}

proc ::vfs::kitdll::vfsop_access {hashkey root relative actualpath mode} {
	set ret [::vfs::kitdll::data::getMetadata $hashkey $relative]
proc ::vfs::cvfs::vfsop_access {hashkey root relative actualpath mode} {
	set ret [::vfs::cvfs::data::getMetadata $hashkey $relative]

	if {$mode & 0x2} {
		vfs::filesystem posixerror $::vfs::posix(EROFS)
	}

	return 1
}

proc ::vfs::kitdll::vfsop_matchindirectory {hashkey root relative actualpath pattern types} {
proc ::vfs::cvfs::vfsop_matchindirectory {hashkey root relative actualpath pattern types} {
	set ret [list]

	catch {
		array set metadata [::vfs::kitdll::data::getMetadata $hashkey $relative]
		array set metadata [::vfs::cvfs::data::getMetadata $hashkey $relative]
	}

	if {![info exists metadata]} {
		return [list]
	}

	if {$pattern == ""} {
		set children [list $relative]
	} else {
		set children [::vfs::kitdll::data::getChildren $hashkey $relative]
		set children [::vfs::cvfs::data::getChildren $hashkey $relative]
	}

	foreach child $children {
		if {$pattern != ""} {
			if {![string match $pattern $child]} {
				continue
			}
		}

		unset -nocomplain metadata
		catch {
			array set metadata [::vfs::kitdll::data::getMetadata $hashkey $child]
			array set metadata [::vfs::cvfs::data::getMetadata $hashkey $child]
		}

		if {[string index $root end] == "/"} {
			set child "${root}${child}"
		} else {
			set child "${root}/${child}"
		}
238
239
240
241
242
243
244
245

246
247
248
249
250
251
252
253
254
255
256

257
258
259
260
261
262
263
238
239
240
241
242
243
244

245
246
247
248
249
250
251
252
253
254
255

256
257
258
259
260
261
262
263







-
+










-
+








		lappend ret $child
	}

	return $ret
}

proc ::vfs::kitdll::vfsop_fileattributes {hashkey root relative actualpath {index -1} {value ""}} {
proc ::vfs::cvfs::vfsop_fileattributes {hashkey root relative actualpath {index -1} {value ""}} {
	set attrs [list -owner -group -permissions]

	if {$value != ""} {
		vfs::filesystem posixerror $::vfs::posix(EROFS)
	}

	if {$index == -1} {
		return $attrs
	}

	array set metadata [::vfs::kitdll::data::getMetadata $hashkey $relative]
	array set metadata [::vfs::cvfs::data::getMetadata $hashkey $relative]

	set attr [lindex $attrs $index]

	switch -- $attr {
		"-owner" {
			return $metadata(uid)
		}
272
273
274
275
276
277
278
279

280
281
282
283
284
285

286
287
288
289
290
291
292
293
294
295
296
297

298
299

300
301
302
303
304
305
306
307
308
309
310
311

312
313

314
315
316
317
318
319
320
321
322

323
324
325

326
327
328

329
330
331

332
333
334
335

272
273
274
275
276
277
278

279
280
281
282
283
284

285
286
287
288
289
290
291
292
293
294
295
296

297
298

299
300
301
302
303
304
305
306
307
308
309
310

311
312

313
314
315
316
317
318
319
320
321

322
323
324

325
326
327

328
329
330

331
332
333
334

335







-
+





-
+











-
+

-
+











-
+

-
+








-
+


-
+


-
+


-
+



-
+
			return [format {0%o} $metadata(mode)]
		}
	}

	return -code error "Invalid index"
}

proc ::vfs::kitdll::vfsop_open {hashkey root relative actualpath mode permissions} {
proc ::vfs::cvfs::vfsop_open {hashkey root relative actualpath mode permissions} {
	if {$mode != "" && $mode != "r"} {
		vfs::filesystem posixerror $::vfs::posix(EROFS)
	}

	catch {
		array set metadata [::vfs::kitdll::data::getMetadata $hashkey $relative]
		array set metadata [::vfs::cvfs::data::getMetadata $hashkey $relative]
	}

	if {![info exists metadata]} {
		vfs::filesystem posixerror $::vfs::posix(ENOENT)
	}

	if {$metadata(type) == "directory"} {
		vfs::filesystem posixerror $::vfs::posix(EISDIR)
	}

	if {[info command chan] != ""} {
		set chan [chan create [list "read"] [list ::vfs::kitdll::chanhandler $hashkey]]
		set chan [chan create [list "read"] [list ::vfs::cvfs::chanhandler $hashkey]]

		set ::vfs::kitdll::chandata([list $hashkey $chan]) [list file $relative pos 0 len $metadata(size) watching ""]
		set ::vfs::cvfs::chandata([list $hashkey $chan]) [list file $relative pos 0 len $metadata(size) watching ""]

		return [list $chan]
	}

	if {[info command rechan] == ""} {
		catch {
			package require rechan
		}
	}

	if {[info command rechan] != ""} {
		set chan [rechan [list ::vfs::kitdll::chanhandler $hashkey] 2]
		set chan [rechan [list ::vfs::cvfs::chanhandler $hashkey] 2]

		set ::vfs::kitdll::chandata([list $hashkey $chan]) [list file $relative pos 0 len $metadata(size) watching ""]
		set ::vfs::cvfs::chandata([list $hashkey $chan]) [list file $relative pos 0 len $metadata(size) watching ""]

		return [list $chan]
	}

	return -code error "No way to generate a channel, need either Tcl 8.5+, \"rechan\""
}

##### No-Ops since we are a readonly filesystem
proc ::vfs::kitdll::vfsop_createdirectory {args} {
proc ::vfs::cvfs::vfsop_createdirectory {args} {
	vfs::filesystem posixerror $::vfs::posix(EROFS)
}
proc ::vfs::kitdll::vfsop_deletefile {args} {
proc ::vfs::cvfs::vfsop_deletefile {args} {
	vfs::filesystem posixerror $::vfs::posix(EROFS)
}
proc ::vfs::kitdll::vfsop_removedirectory {args} {
proc ::vfs::cvfs::vfsop_removedirectory {args} {
	vfs::filesystem posixerror $::vfs::posix(EROFS)
}
proc ::vfs::kitdll::vfsop_utime {} {
proc ::vfs::cvfs::vfsop_utime {} {
	vfs::filesystem posixerror $::vfs::posix(EROFS)
}

package provide vfs::kitdll 1.0
package provide vfs::cvfs 1.0