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 0ab85aa commit fa825f2
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)) {
// 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");
break;
case UNIX:
case ZOS:
cmdLine.addArgument("cp");
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

0 comments on commit fa825f2

Please sign in to comment.