resources.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 catch {
00016 package require base64
00017 }
00018
00019 package provide xslt::resources 1.3
00020
00021 namespace xslt::resources {
00022 namespace export list type exists modified
00023 }
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 ret xslt::resources::list (type locnNd , optional baseNd ={) args} {
00038 # What kind of resource is this? file, http, ftp, etc?
00039
00040 if {[llength $args]} {
00041 return -code error "too many arguments"
00042 }
00043
00044 locn = $locnNd
00045
00046 catch { locn = [dom::node stringValue [lindex $locnNd 0]]}
00047 base = $baseNd
00048 catch { base = [dom::node stringValue [lindex $baseNd 0]]}
00049
00050 if {[string match
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 ret xslt::resources::type (type locnNd , type args) {
00074
00075 if {[llength $args]} {
00076 return -code error "too many arguments"
00077 }
00078
00079 set locn $locnNd
00080 catch {set locn [dom::node stringValue [lindex $locnNd 0]]}
00081
00082 if {[file isdir $locn]} {
00083 return directory
00084 } elseif {[file isfile $locn]} {
00085 return file
00086 } else {
00087 return other
00088 }
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 ret xslt::resources::exists (type locnNd , type args) {
00103
00104 if {[llength $args]} {
00105 return -code error "too many arguments"
00106 }
00107
00108 set locn $locnNd
00109 catch {set locn [dom::node stringValue [lindex $locnNd 0]]}
00110
00111 if {[file exists $locn]} {
00112 return 1
00113 } else {
00114 return 0
00115 }
00116 }
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 ret xslt::resources::modified (type locnNd , type args) {
00130
00131 if {[llength $args]} {
00132 return -code error "too many arguments"
00133 }
00134
00135 set locn $locnNd
00136 catch {set locn [dom::node stringValue [lindex $locnNd 0]]}
00137
00138 if {[file exists $locn]} {
00139 return [clock format [file mtime $locn] -format {%Y-%m-%dT%H:%M:%S}]
00140 } else {
00141 return {}
00142 }
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 ret xslt::resources::mkdir (type locnNd , type args) {
00157
00158 if {[llength $args]} {
00159 return {}
00160 }
00161
00162 set locn $locnNd
00163 catch {set locn [dom::node stringValue [lindex $locnNd 0]]}
00164
00165 set dir [file split $locn]
00166 set current [lindex $dir 0]
00167 set remaining [lrange $dir 1 end]
00168 while {[llength $remaining]} {
00169 set current [file join $current [lindex $remaining 0]]
00170 set remaining [lrange $remaining 1 end]
00171 if {[file exists $current]} {
00172 if {![file isdir $current]} {
00173 return {}
00174 }
00175 } elseif {[file isdir $current]} {
00176 continue
00177 } else {
00178 if {[catch {file mkdir $current}]} {
00179 return {}
00180 }
00181 }
00182 }
00183
00184 return $locn
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199 ret xslt::resources::copy (type srcNd , type destNd , type args) {
00200 set src $srcNd
00201 catch {set src [dom::node stringValue [lindex $srcNd 0]]}
00202 set dest $destNd
00203 catch {set dest [dom::node stringValue [lindex $destNd 0]]}
00204
00205 if {[catch {file copy -force $src $dest} msg]} {
00206 catch {
00207 package require log
00208 log::log error "copy failed due to \"$msg\""
00209 }
00210 return 0
00211 } else {
00212 return 1
00213 }
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 ret xslt::resources::move (type srcNd , type destNd , type args) {
00229 set src $srcNd
00230 catch {set src [dom::node stringValue [lindex $srcNd 0]]}
00231 set dest $destNd
00232 catch {set dest [dom::node stringValue [lindex $destNd 0]]}
00233
00234 if {[catch {file rename -force $src $dest}]} {
00235 return 0
00236 } else {
00237 return 1
00238 }
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 ret xslt::resources::file-set-attributes (type srcNd , type whatNd , type detailNd , type args) {
00255 set src $srcNd
00256 catch {set src [dom::node stringValue [lindex $srcNd 0]]}
00257 set what $whatNd
00258 catch {set what [dom::node stringValue [lindex $whatNd 0]]}
00259 set detail $detailNd
00260 catch {set detail [dom::node stringValue [lindex $detailNd 0]]}
00261
00262 if {[catch {file attributes $src -$what $detail} result]} {
00263 return {}
00264 } else {
00265 return $result
00266 }
00267 }
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 ret xslt::resources::delete (type locnNd , type args) {
00281
00282 if {[llength $args]} {
00283 return -code error "too many arguments"
00284 }
00285
00286 set locn $locnNd
00287 catch {set locn [dom::node stringValue [lindex $locnNd 0]]}
00288
00289 if {[catch {file delete -force $locn} msg]} {
00290 catch {
00291 package require log
00292 log::log error "delete failed due to \"$msg\""
00293 }
00294 return 0
00295 } else {
00296 return 1
00297 }
00298 }
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 ret xslt::resources::link (type fromNd , type toNd , type args) {
00313 set from $fromNd
00314 catch {set from [dom::node stringValue [lindex $fromNd 0]]}
00315 set to $toNd
00316 catch {set to [dom::node stringValue [lindex $toNd 0]]}
00317
00318 if {[catch {file link $from $to}]} {
00319 return 0
00320 } else {
00321 return 1
00322 }
00323 }
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338 ret xslt::resources::write-base64 (type fnameNd , type b64Nd , type args) {
00339 set fname $fnameNd
00340 catch {set fname [dom::node stringValue [lindex $fnameNd 0]]}
00341 set b64 $b64Nd
00342 catch {set b64 [dom::node stringValue [lindex $b64Nd 0]]}
00343
00344 if {[catch {package require base64}]} {
00345 return 0
00346 }
00347
00348 if {[catch {open $fname w} ch]} {
00349 return 0
00350 } else {
00351 set binarydata [base64::decode $b64]
00352 fconfigure $ch -trans binary -encoding binary
00353 puts -nonewline $ch $binarydata
00354 close $ch
00355 return 1
00356 }
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 ret xslt::resources::read-base64 (type fnameNd , type args) {
00372 set fname $fnameNd
00373 catch {set fname [dom::node stringValue [lindex $fnameNd 0]]}
00374
00375 if {[catch {package require base64}]} {
00376 return 0
00377 }
00378
00379 if {[catch {open $fname} ch]} {
00380 return 0
00381 } else {
00382 fconfigure $ch -trans binary -encoding binary
00383 set binarydata [read $ch]
00384 close $ch
00385 return [base64::encode $binarydata]
00386 }
00387 }
00388
00389