Skip to content

Commit

Permalink
Fix #91 Local copying for files over same connection
Browse files Browse the repository at this point in the history
  • Loading branch information
hierynomus committed Jan 24, 2014
1 parent 9a85d94 commit 6527560
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/main/java/com/xebialabs/overthere/spi/BaseOverthereFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
package com.xebialabs.overthere.spi;

import com.xebialabs.overthere.CmdLine;
import com.xebialabs.overthere.OperatingSystemFamily;
import com.xebialabs.overthere.OverthereFile;
import com.xebialabs.overthere.RuntimeIOException;
import com.xebialabs.overthere.util.OverthereFileCopier;
Expand Down Expand Up @@ -73,9 +75,37 @@ public final void copyTo(final OverthereFile dest) {
}

protected void copyFrom(OverthereFile source) {
OverthereFileCopier.copy(source, this);
if (source.getConnection().equals(connection)) {

This comment has been minimized.

Copy link
@vpartington

vpartington Jan 24, 2014

Contributor

Is the equals on an OverthereConnection guaranteed to be true when they are the same connection? Or could it also be true if it's a connection to the same host? Isn't == safer here?

This comment has been minimized.

Copy link
@hierynomus

hierynomus Jan 27, 2014

Author Contributor

If equals is implemented, I hope we take into account all differentiators. == is very safe, only the physically same connection.

// Copying over same connection, let's do local
localCopyFrom(source);
} else {
OverthereFileCopier.copy(source, this);
}
}

/**
* Copies this file or directory (recursively) to a (new) destination in the same connection.
* @param source The source file or directory
*/
protected void localCopyFrom(OverthereFile source) {
OperatingSystemFamily hostOperatingSystem = source.getConnection().getHostOperatingSystem();
CmdLine cmdLine = new CmdLine();
switch (hostOperatingSystem) {
case WINDOWS:
cmdLine.addArgument("copy");

This comment has been minimized.

Copy link
@vpartington

vpartington Jan 24, 2014

Contributor

Is a plain copy good enough? Don't we need any arguments?

This comment has been minimized.

Copy link
@hierynomus

hierynomus Jan 27, 2014

Author Contributor

I was wondering the same, especially with recursiveness of directories.

break;
case UNIX:
case ZOS:
cmdLine.addArgument("cp");

This comment has been minimized.

Copy link
@vpartington

vpartington Jan 24, 2014

Contributor

Is a plain cp good enough? Don't we need any arguments, e.g. cp -p?

This comment has been minimized.

Copy link
@hierynomus

hierynomus Jan 27, 2014

Author Contributor

Probably cp -pr to also accommodate for recursing into directories.

break;
}

cmdLine.addArgument(source.getPath()).addArgument(getPath());

source.getConnection().execute(cmdLine);
}


/**
* Subclasses MUST implement toString properly.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,20 @@ public void shouldWriteLargeFile() throws IOException {
assertThat(largeFileContentsRead, equalTo(largeFileContentsWritten));
}

@Test
public void shouldCopyRemoteFileAsToRemoteLocationOnSameConnection() throws IOException {
File largeFile = temp.newFile("large.dat");
byte[] largeFileContentsWritten = writeRandomBytes(largeFile, SMALL_FILE_SIZE);

OverthereFile remoteSmallFile = connection.getTempFile("small.dat");
LocalFile.valueOf(largeFile).copyTo(remoteSmallFile);

OverthereFile remoteSmallCopy = connection.getTempFile("copy-of-small.dat");

remoteSmallFile.copyTo(remoteSmallCopy);
}


@Test
public void shouldCopyLargeFile() throws IOException {
File largeFile = temp.newFile("large.dat");
Expand Down

1 comment on commit 6527560

@vpartington
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Short and sweet! :-)

Please sign in to comment.