From 93acd70060fae15b0985c74991486f2d68d468b2 Mon Sep 17 00:00:00 2001 From: Steve Bennett Date: Thu, 7 Nov 2013 08:21:36 +1000 Subject: [PATCH] file copy -force handles identical source/target No longer truncates the file Reported-by: sg0x40 Signed-off-by: Steve Bennett --- jim_tcl.txt | 1 + tclcompat.tcl | 13 ++++++++++--- tests/filecopy.test | 5 +++++ 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 -- 2.11.4.GIT