aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2013-11-07 08:21:36 +1000
committerSteve Bennett <steveb@workware.net.au>2013-11-07 08:26:52 +1000
commit93acd70060fae15b0985c74991486f2d68d468b2 (patch)
tree618028e1d90e444d551923c091d1bc9d466997c2
parenta077f0800f5bbc7ca5f3b368726f7d1757c16549 (diff)
downloadjimtcl-93acd70060fae15b0985c74991486f2d68d468b2.zip
jimtcl-93acd70060fae15b0985c74991486f2d68d468b2.tar.gz
jimtcl-93acd70060fae15b0985c74991486f2d68d468b2.tar.bz2
file copy -force handles identical source/target
No longer truncates the file Reported-by: sg0x40 <https://github.com/sg0x40> Signed-off-by: Steve Bennett <steveb@workware.net.au>
-rw-r--r--jim_tcl.txt1
-rw-r--r--tclcompat.tcl13
-rw-r--r--tests/filecopy.test5
3 files changed, 16 insertions, 3 deletions
diff --git a/jim_tcl.txt b/jim_tcl.txt
index 59fcb23..0f2af38 100644
--- a/jim_tcl.txt
+++ b/jim_tcl.txt
@@ -54,6 +54,7 @@ RECENT CHANGES
Changes between 0.74 and 0.75
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1. `binary`, `pack` and `unpack` now support floating point
+2. `file copy` '-force' handles source and target as the same file
Changes between 0.73 and 0.74
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/tclcompat.tcl b/tclcompat.tcl
index 21c3922..d736859 100644
--- a/tclcompat.tcl
+++ b/tclcompat.tcl
@@ -130,9 +130,16 @@ proc {file copy} {{force {}} source target} {
set in [open $source]
- if {$force eq "" && [file exists $target]} {
- $in close
- error "error copying \"$source\" to \"$target\": file already exists"
+ if {[file exists $target]} {
+ if {$force eq ""} {
+ error "error copying \"$source\" to \"$target\": file already exists"
+ }
+ # If source and target are the same, nothing to do
+ file stat $source ss
+ file stat $target ts
+ if {$ss(dev) == $ts(dev) && $ss(ino) == $ts(ino)} {
+ return
+ }
}
set out [open $target w]
$in copyto $out
diff --git a/tests/filecopy.test b/tests/filecopy.test
index d64a2d9..084beec 100644
--- a/tests/filecopy.test
+++ b/tests/filecopy.test
@@ -63,6 +63,11 @@ test filecopy-2.5 "Source doesn't exist and can't write to target (-force)" {
list [catch {file copy -force missing tempdir} msg] $msg
} {1 {missing: No such file or directory}}
+test filecopy-2.6 "Source and target identical (-force)" {
+ file copy -force tempfile tempfile
+ file size tempfile
+} 16
+
file delete tempfile
exec rm -rf tempdir