Debian Codegraph @

tcl/cache.tcl.in

raw
1 ##########################################################################
2
3 # CACHE.TCL, cache management procedures
4 # Copyright (C) 2022-2025 Xavier Delaruelle
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 ##########################################################################
20
21 # Get full path name of module cache file for given modulepath
22 proc getModuleCacheFilename {modpath} {
23 return $modpath/.modulecache
24 }
25
26 # test if a given file path has a limited access by check permission mode set
27 # for others
28 proc isFileAccessLimited {fpath isdir} {
29 # check if file stat info can be fetched
30 if {[file readable $fpath]} {
31 # extract permissions for "other"
32 set other_perms [string index [file attributes $fpath -permissions] end]
33 # test file can be read by other: o+r right, which means file should at
34 # least have 0b100 mode, which can be binary-compared against 0b011 (3)
35 # if file is a directory, test it can also be searched: o+x right, which
36 # means dir should at least have 0b101 mode, which can be compared
37 # against 0b010 (2)
38 set perms_mask [expr {$isdir ? {2} : {3}}]
39 return [expr {($other_perms | $perms_mask) != 7}]
40 } else {
41 return 1
42 }
43 }
44
45 # walk through directory content and return every files and dirs with limited
46 # access rights
47 proc getLimitedAccessesInDirectory {modpath} {
48 # cannot test limited accesses on Windows platform
49 if {[getState is_win]} {
50 return {}
51 }
52
53 foreach igndir [getConf ignored_dirs] {
54 set ignored_dirs($igndir) 1
55 }
56 # walk through directory to find elements with limited access
57 array set limited_arr {}
58 lappend full_list $modpath
59 for {set i 0} {$i < [llength $full_list]} {incr i 1} {
60 set elt [lindex $full_list $i]
61 set elttail [file tail $elt]
62 set eltname [getModuleNameFromModulepath $elt $modpath]
63 if {[file isdirectory $elt]} {
64 # skip ignored dirs
65 if {![info exists ignored_dirs($elttail)]} {
66 if {[isFileAccessLimited $elt 1]} {
67 set limited_arr($eltname) d
68 # only walk through directory content if its access is not limited
69 } else {
70 # get all elements found in directory (regular and dot files)
71 set direlt_list [glob -nocomplain -directory $elt *]
72 foreach eltindir [glob -nocomplain -types hidden -directory\
73 $elt -tails *] {
74 if {$eltindir ni {. ..}} {
75 lappend direlt_list $elt/$eltindir
76 }
77 }
78 if {[llength $direlt_list]} {
79 lappend full_list {*}$direlt_list
80 }
81 }
82 }
83 } else {
84 # skip ignored files
85 switch -glob -- $elttail {
86 *~ - *,v - \#*\# {}
87 default {
88 if {[isFileAccessLimited $elt 0]} {
89 set limited_arr($eltname) f
90 }
91 }
92 }
93 }
94 }
95
96 # filter .version/.modulecache files found at the root of modulepath dir
97 foreach elt [list .version .modulecache] {
98 if {[info exists limited_arr($elt)]} {
99 unset limited_arr($elt)
100 }
101 }
102
103 reportDebug "found [array names limited_arr] ([array size limited_arr])\
104 with limited access"
105 return [array get limited_arr]
106 }
107
108 # Build cache file content for given modulepath
109 proc formatModuleCacheContent {modpath} {
110 set content {}
111 # collect files from modulepath directory
112 array set found_list [findModules $modpath * 0 1]
113
114 # collect files and dirs from modulepath directory with limited access
115 array set limited_list [getLimitedAccessesInDirectory $modpath]
116
117 # concatenate element found and those with limited access as the second
118 # kind may not be part of found list
119 set elt_list [lsort -unique [concat [array names found_list] [array names\
120 limited_list]]]
121
122 # build cache entry for every file found
123 foreach elt $elt_list {
124 # ignore element if it has been flagged in skip list (one of its parent
125 # directory has limited access) or if modulepath has itself a limited
126 # access
127 if {[info exists skip_list($elt)] || $elt eq {}} {
128 continue
129 }
130
131 set entry_list [list]
132 set fetch_content 0
133
134 # mention in cache that file or directory has limited access to test if
135 # they can be accessed by user when cache is evaluated
136 if {[info exists limited_list($elt)]} {
137 if {$limited_list($elt) eq {d}} {
138 # mark all elements contained in dir to skip them as their parent
139 # directory has limited access (due to lsort on foreach loop we
140 # are assured to treat parent dir before the entries it contains)
141 foreach elttoskip [array names found_list -glob $elt/*] {
142 set skip_list($elttoskip) 1
143 }
144 lappend entry_list limited-access-directory $elt
145 } else {
146 lappend entry_list limited-access-file $elt
147 }
148 } else {
149 switch -- [lindex $found_list($elt) 0] {
150 modulerc {
151 lappend entry_list modulerc-content $elt
152 set fetch_content 1
153 }
154 modulefile {
155 lappend entry_list modulefile-content $elt [lindex\
156 $found_list($elt) 1]
157 set fetch_content 1
158 }
159 default {
160 # also record obtained error to get all the information to
161 # cover everything fetched by findModules. only modulefile
162 # validity is checked in findModules
163 lappend entry_list modulefile-invalid $elt {*}[lrange\
164 $found_list($elt) 0 1]
165 }
166 }
167 }
168 # fetch file content
169 if {$fetch_content} {
170 if {[catch {
171 set fcontent [readFile $modpath/$elt]
172 # extract module header from the start of the file
173 if {![regexp {^#%Module[0-9\.]*} [string range $fcontent 0 32]\
174 fheader]} {
175 set fheader {}
176 }
177 lappend entry_list $fheader $fcontent
178 } errMsg]} {
179 # rethrow read error after parsing message
180 knerror [parseAccessIssue $modpath/$elt]
181 }
182 }
183 # format cache entry
184 append content "\n$entry_list"
185 }
186
187 # prepend header if some content has been generated
188 if {[string length $content]} {
189 set cache_header "#%Module[getState cache_mcookie_version]"
190 set content $cache_header$content
191 }
192
193 return $content
194 }
195
196 # read a cache file
197 proc readCacheContent {cachefile} {
198 set res {}
199 if {[catch {
200 # read full file
201 set fid [open $cachefile r]
202 # use defined buffer size to limit number of read system call
203 fconfigure $fid -buffersize [getConf cache_buffer_bytes]
204 set fdata [read $fid]
205 close $fid
206 # extract magic cookie (first word of cache file)
207 set fh [string trimright [lindex [split [string range $fdata 0 32]]\
208 0] #]
209 set fhvers [string range $fh 8 end]
210 } errMsg ]} {
211 reportError [parseAccessIssue $cachefile]
212 } else {
213 # check cache validity
214 if {![string equal -length 8 $fh {#%Module}]} {
215 reportInternalBug {Magic cookie '#%Module' missing} $cachefile\
216 {Cache ERROR}
217 # check if version requirement is present
218 } elseif {[string length $fh] <= 8} {
219 reportInternalBug {Modules version requirement missing} $cachefile\
220 {Cache ERROR}
221 # check if min version requirement is met
222 } elseif {[versioncmp [getState modules_release] $fhvers] <0} {
223 reportDebug "Cache file $cachefile requires at least Modules version\
224 $fhvers"
225 } else {
226 # set file content as result
227 set res $fdata
228 }
229 }
230 return $res
231 }
232
233 # evaluate cache file
234 proc execute-cachefile {cachefile modpath} {
235 # register current modulepath for cachefile commands to know where they are
236 lappendState modulepath $modpath
237 lappendState debug_msg_prefix "\[cache:$cachefile\] "
238
239 # initialize cache gathering structures for modulepath
240 set ::g_cacheModpath($modpath) {}
241 set ::g_cacheFLimitedModpath($modpath) {}
242 set ::g_cacheDLimitedModpath($modpath) {}
243
244 # initialize cache file evaluation interp configuration
245 if {![info exists ::g_cachefileUntrackVars]} {
246 # list variable that should not be tracked for saving
247 array set ::g_cachefileUntrackVars [list ModulesCurrentCachefile 1\
248 cachecontent 1 env 1]
249
250 # commands that should be renamed before aliases setup
251 array set ::g_cachefileRenameCmds [list]
252
253 # list interpreter alias commands to define
254 array set ::g_cachefileAliases [list modulefile-content\
255 modulefile-content modulerc-content modulerc-content\
256 modulefile-invalid modulefile-invalid limited-access-file\
257 limited-access-file limited-access-directory\
258 limited-access-directory reportInternalBug reportInternalBug\
259 readCacheContent readCacheContent formatErrStackTrace\
260 formatErrStackTrace]
261
262 # alias commands where an argument should be passed
263 array set ::g_cachefileAliasesPassArg [list]
264
265 # trace commands that should be associated to aliases
266 array set ::g_cachefileAliasesTraces [list]
267
268 }
269 set itrp __cachefile
270
271 reportTrace '$cachefile' {Evaluate cache file}
272 # create cachefile interpreter at first interpretation
273 if {![interp exists $itrp]} {
274 reportDebug "creating interp $itrp"
275 interp create $itrp
276
277 # dump initial interpreter state to restore it before each cachefile
278 # interpretation.
279 dumpInterpState $itrp g_cachefileVars g_cachefileArrayVars\
280 g_cachefileUntrackVars g_cachefileProcs
281
282 # interp has just been created
283 set fresh 1
284 } else {
285 set fresh 0
286 }
287
288 # reset interp state command before each interpretation
289 resetInterpState $itrp $fresh g_cachefileVars g_cachefileArrayVars\
290 g_cachefileUntrackVars g_cachefileProcs g_cachefileAliases\
291 g_cachefileAliasesPassArg g_cachefileAliasesTraces\
292 g_cachefileRenameCmds g_cachefileCommands
293
294 # reset modulefile-specific variable before each interpretation
295 interp eval $itrp set ::ModulesCurrentCachefile "{$cachefile}"
296
297 # evaluate cache file
298 ##nagelfar ignore +4 Suspicious # char
299 set exec_res [interp eval $itrp {
300 set cachecontent [readCacheContent $::ModulesCurrentCachefile]
301 if {$cachecontent eq {}} {
302 # simply skip cache file, no exit on error here
303 return 0
304 }
305
306 info script $::ModulesCurrentCachefile
307 if {[catch {eval $cachecontent} errorMsg]} {
308 # format stack trace to report cachefile information only
309 reportInternalBug [formatErrStackTrace $::errorInfo\
310 $::ModulesCurrentCachefile [list {*}[info procs] {*}[info\
311 commands]]] {} {Cache ERROR}
312 return 0
313 } else {
314 return 1
315 }
316 }]
317
318 reportDebug "exiting $cachefile (result=$exec_res)"
319
320 lpopState debug_msg_prefix
321 lpopState modulepath
322
323 return $exec_res
324 }
325
326 # cache file command to record modulefile content
327 proc modulefile-content {mod mtime header content} {
328 set modfile [currentState modulepath]/$mod
329
330 # record modulefile information in memory structures
331 set ::g_modfileContent($modfile) [list $header $content]
332 set ::g_fileMtime($modfile) $mtime
333 set ::g_modfileValid($modfile) [list true {}]
334
335 # gather all modulefiles/modulercs of a modulepath in a global variable
336 # with same information structure than findModules result
337 lappend ::g_cacheModpath([currentState modulepath]) $mod [list modulefile\
338 $mtime $modfile]
339 }
340
341 # cache file command to record modulerc content
342 proc modulerc-content {modrc header content} {
343 set modrcfile [currentState modulepath]/$modrc
344
345 # record modulerc information in memory structures
346 set ::g_modfileContent($modrcfile) [list $header $content]
347
348 # gather all modulefiles/modulercs of a modulepath in a global variable
349 # with same information structure than findModules result
350 lappend ::g_cacheModpath([currentState modulepath]) $modrc [list modulerc]
351 }
352
353 # cache file command to record an invalid modulefile
354 proc modulefile-invalid {mod type msg} {
355 set modfile [currentState modulepath]/$mod
356
357 # record modulefile information in memory structures
358 set ::g_modfileValid($modfile) [list $type $msg]
359
360 # also gather all invalid modulefiles of a modulepath in a global variable
361 # with same information structure than findModules result
362 lappend ::g_cacheModpath([currentState modulepath]) $mod [list $type $msg\
363 $modfile]
364 }
365
366 # gather modulepath limited access files in specific structure
367 proc limited-access-file {mod} {
368 lappend ::g_cacheFLimitedModpath([currentState modulepath]) $mod 1
369 }
370
371 # gather modulepath limited access files in specific structure
372 proc limited-access-directory {dir} {
373 lappend ::g_cacheDLimitedModpath([currentState modulepath]) $dir 1
374 }
375
376 # finds all module-related files matching mod in the modulepath dir by looking
377 # into the cache file
378 proc findModulesInCacheFile {modpath mod depthlvl fetch_mtime} {
379 set cachefile [getModuleCacheFilename $modpath]
380
381 # check if cache should be ignored
382 if {[getConf ignore_cache]} {
383 return [list 0 {}]
384 }
385
386 # check if a cache file is available (exist, readable and not expired)
387 if {![info exists ::g_cachefilesAvail($cachefile)]} {
388 if {[file readable $cachefile]} {
389 reportDebug "cache file '$cachefile' exists and is readable"
390 # check expiry if enabled
391 if {[set expiry_secs [getConf cache_expiry_secs]] != 0} {
392 set cachemtime [getFileMtime $cachefile]
393 # cache is also considered expired if its mtime cannot be fetched
394 set ::g_cachefilesAvail($cachefile) [expr {$cachemtime ne {} &&\
395 $expiry_secs > ([getState clock_seconds] - $cachemtime)}]
396 if {!$::g_cachefilesAvail($cachefile)} {
397 reportDebug "cache file '$cachefile' has expired"
398 }
399 } else {
400 set ::g_cachefilesAvail($cachefile) 1
401 }
402 } else {
403 reportDebug "cache file '$cachefile' cannot be found or read"
404 set ::g_cachefilesAvail($cachefile) 0
405 }
406 }
407
408 # return if no cache file available
409 if {!$::g_cachefilesAvail($cachefile)} {
410 return [list 0 {}]
411 }
412
413 # evaluate cache file if not yet done
414 if {![info exists ::g_cachefilesSourced($cachefile)]} {
415 set exec_res [execute-cachefile $cachefile $modpath]
416 # keep track of already sourced cache files not to run them again
417 set ::g_cachefilesSourced($cachefile) $exec_res
418 } else {
419 reportDebug "cache file '$cachefile' has already been evaluated"
420 }
421
422 # return if cache file has not been correctly sourced
423 if {!$::g_cachefilesSourced($cachefile)} {
424 return [list 0 {}]
425 }
426
427 # tailor cache file content to what is requested
428 array set cache_arr $::g_cacheModpath($modpath)
429 reportDebug "[array size cache_arr] elements are cached for '$modpath'"
430 array set flimited_arr $::g_cacheFLimitedModpath($modpath)
431 reportDebug "[array size flimited_arr] limited access files for '$modpath'"
432 array set dlimited_arr $::g_cacheDLimitedModpath($modpath)
433 reportDebug "[array size dlimited_arr] limited access dirs for '$modpath'"
434
435 # filter entries unless all are requested
436 set findall [expr {$mod in {{} *}}]
437 defineModEqStaticProc [isIcase] [getConf extended_default] $mod
438 if {!$findall || $depthlvl > 0} {
439 foreach elt [array names cache_arr] {
440 set eltroot [lindex [file split $elt] 0]
441 set eltdepthlvl [llength [file split $elt]]
442 set elttail [file tail $elt]
443 set eltparent [file dirname $elt]
444
445 # exclude if not matching mod pattern (which is always a root name)
446 if {$eltroot ne {.modulerc} && !$findall && ![modEqStatic $eltroot\
447 match]} {
448 unset cache_arr($elt)
449 # also exclude if not corresponding to search depth level (or if one
450 # modulefile has already been added for directories lying at other
451 # depth level)
452 } elseif {$depthlvl > 0 && $elttail ne {.modulerc} && $eltdepthlvl\
453 != $depthlvl && [info exists modfile_indir($eltparent)]} {
454 unset cache_arr($elt)
455 } else {
456 # track a valid non-hidden modulefile has been added in parent
457 # directory
458 if {[string index $elttail 0] ne {.} && ![info exists\
459 modfile_indir($eltparent)] && [lindex $cache_arr($elt) 0] eq\
460 {modulefile}} {
461 set modfile_indir($eltparent) 1
462 }
463 }
464 }
465 }
466
467 array set hidden_list {}
468 set limited_list [list]
469 # add limited access files and directories to result if they match query
470 foreach elt [concat [array names flimited_arr] [array names\
471 dlimited_arr]] {
472 set eltroot [lindex [file split $elt] 0]
473 if {$eltroot eq {.modulerc} || $findall || [modEqStatic $eltroot\
474 match]} {
475 set fpelt [file join $modpath $elt]
476 lappend limited_list $fpelt
477 # indicate file is hidden in structure that will be transmitted to
478 # findModulesFromDirsAndFiles for no-indepth module search
479 if {[string index [file tail $elt] 0] eq {.} && [info exists\
480 flimited_arr($elt)]} {
481 set hidden_list($fpelt) 1
482 }
483 }
484 }
485
486 # walk through list of matching limited access dirs and files to find
487 # modules available to current user (transmit list of known files and dirs
488 # to avoid file stat tests)
489 findModulesFromDirsAndFiles $modpath $limited_list $depthlvl $fetch_mtime\
490 cache_arr modfile_indir hidden_list flimited_arr dlimited_arr
491
492 reportDebug "found [array names cache_arr] ([array size cache_arr])"
493 set cache_list [array get cache_arr]
494
495 return [list 1 $cache_list]
496 }
497
498 # ;;; Local Variables: ***
499 # ;;; mode:tcl ***
500 # ;;; End: ***
501 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: