mengdiwang / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Files.copy deletes the file contents when both from and to refer to the same file #515

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
@Test
public void shouldNotDestroyContents() throws IOException {
    File file = File.createTempFile("test", ".txt");
    Files.write("hello world", file, Charsets.UTF_8);
    Assert.assertTrue(file.length()>0);
    Files.copy(file, file);
    Assert.assertTrue(file.length()>0);         
    file.delete();
}

I expect the copy operation to do nothing when file is copied over itself. 
Instead, the file contents get truncated to zero length and the test fails.

Version r06.

This could get very complicated to test, especially on Linux where files can 
appear to have distinct paths, yet refer physically to the same file. Anyway, I 
should have been at least warned in the documentation that this function can 
destroy my data.

Original issue reported on code.google.com by Roman.Ze...@gmail.com on 6 Jan 2011 at 10:40

GoogleCodeExporter commented 9 years ago
Although the unix 'cp' command is very smart about this kind of detection, I 
don't think we'll be able to be very smart from within the confines of Java.

Our two choices are (a) just warn in javadoc, or (b) do a rudimentary sanity 
check only (which still won't help the other copy() overloads).

Original comment by kevinb@google.com on 12 Jan 2011 at 8:46

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Java 7 will have a copy method that includes a check for this situation:

http://download.java.net/jdk7/docs/api/java/nio/file/Path.html#copyTo%28java.nio
.file.Path,%20java.nio.file.CopyOption...%29

It could be confusing if Guava attempts to solve this in a different way from 
the JRE

Original comment by fin...@gmail.com on 22 Jan 2011 at 1:23

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 27 Jan 2011 at 1:33

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 22 Mar 2011 at 6:29

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 23 Mar 2011 at 1:49

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 13 Jul 2011 at 6:18

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 16 Jul 2011 at 8:32

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 10 Dec 2011 at 4:00

GoogleCodeExporter commented 9 years ago

Original comment by kurt.kluever on 24 Jul 2012 at 5:42

GoogleCodeExporter commented 9 years ago
A warning has been added to the Javadoc about this.

Original comment by cgdecker@google.com on 26 Jul 2012 at 8:04

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09