【发布时间】:2014-07-03 19:37:14
【问题描述】:
在调试第三方 Tcl 脚本时,我遇到了一个非常奇怪的行为:当脚本通过 gunzip 解压缩现有文件时,Tcl 'file exists' 否认结果文件存在,即使它通过 'ls 可见' 从 shell 甚至通过 Tcl 'glob'。我能够在这个简短的(ish)脚本中复制行为:
#!/usr/bin/tclsh
proc assert {msg asserted} {
if {!$asserted} {
puts "Assertion failed: $msg"
exit 1
}
}
set script_path [info script]
set test_file [file join [file dirname $script_path] "test.tcl.gz"]
puts "The test file is '$test_file'"
assert "The test file does not exist" [file exists $test_file]
set this_dir [pwd]
set test_dir [file join $this_dir "test"]
if {![file exist $test_dir]} {
file mkdir $test_dir
}
puts "The test directory is '$test_dir'"
assert "'$test_dir' does not exist or is not a directory" [file isdirectory $test_dir]
file copy $test_file $test_dir
set working_file [file join $test_dir [file tail $test_file]]
assert "Failed to to copy the test file to '$working_file'" [file exists $working_file]
exec gunzip $working_file
puts "After decompression, $test_dir contains:"
foreach file [glob -dir $test_dir *] {
puts $file
}
set decompressed_file [file rootname $working_file]
assert "$decompressed_file does not exist" [file exists $decompressed_file]
我使用的测试文件只是脚本源的 gzip 副本。正如脚本所期望和检查的那样,它与脚本本身位于同一目录中。这是我运行它时得到的结果(tcl 8.5 / CentOS 6.5):
bash-4.1$ ./test.tcl
The test file is './test.tcl.gz'
The test directory is '/home/jbolling/tmp/test'
After decompression, /home/jbolling/tmp/test contains:
/home/jbolling/tmp/test/test.tcl
Assertion failed: /home/jbolling/tmp/test/test.tcl does not exist
谁能解释这种行为?有没有办法说服“文件存在”识别解压文件?
【问题讨论】:
-
我在运行脚本时没有收到断言失败错误。你能做一个
ls -ld . test test/test.tcl。 -
我也没有看到断言失败。我的环境:Mac OS X 10.9.x,Tcl 8.5.9。
-
file exists命令几乎直接映射到所有 Unix 系统(包括 OSX)上带有F_OK标志的access()系统调用;由于文件锁定模型的不同,Windows 更加复杂。你在哪个平台上? -
@Brad Lanam: 'ls' 显示文件存在,就像 'glob' 一样。工作目录 (. = /home/jbolling/tmp) 的权限为 0750,子目录“test”的权限为 0755,test/test.tcl 的权限为 0644。
-
@Donal Fellows:这是 CentOS 6.5 (Linux)。
标签: tcl