Skip to content

Commit

Permalink
Creating temporary files with the exact name requested, each inside o…
Browse files Browse the repository at this point in the history
…f their own directory with a random name. Completely fixes #81.
  • Loading branch information
Vincent Partington committed Aug 20, 2013
1 parent f7fb7f9 commit ac1c62b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
import static com.xebialabs.overthere.util.ConsoleOverthereExecutionOutputHandler.sysoutHandler;
import static com.xebialabs.overthere.util.OverthereProcessOutputHandlerWrapper.wrapStderr;
import static com.xebialabs.overthere.util.OverthereProcessOutputHandlerWrapper.wrapStdout;
import static com.xebialabs.overthere.util.OverthereUtils.getBaseName;
import static com.xebialabs.overthere.util.OverthereUtils.getExtension;

/**
* A connection on a host (local or remote) on which to manipulate files and execute commands.
Expand Down Expand Up @@ -75,6 +73,8 @@ public abstract class BaseOverthereConnection implements OverthereConnection {

protected OverthereFile workingDirectory;

protected Random random = new Random();

protected BaseOverthereConnection(final String protocol, final ConnectionOptions options, final AddressPortMapper mapper, final boolean canStartProcess) {
this.protocol = checkNotNull(protocol, "Cannot create OverthereConnection with null protocol");
this.options = checkNotNull(options, "Cannot create OverthereConnection with null options");
Expand Down Expand Up @@ -125,34 +125,28 @@ public final void close() {
* removed when this connection is closed. <b>N.B.:</b> The file is not actually created until a put method is
* invoked.
*
* @param nameTemplate
* the template on which to base the name of the temporary file. May be <code>null</code>.
* @param name
* the name of the temporary file. May be <code>null</code>.
* @return a reference to the temporary file on the host
*/
@Override
public final OverthereFile getTempFile(String nameTemplate) {
String prefix, suffix;

if (nameTemplate != null) {
int pos = nameTemplate.lastIndexOf('/');
if (pos != -1) {
nameTemplate = nameTemplate.substring(pos + 1);
}
pos = nameTemplate.lastIndexOf('\\');
if (pos != -1) {
nameTemplate = nameTemplate.substring(pos + 1);
}
public final OverthereFile getTempFile(String name) {
if (isNullOrEmpty(name)) {
name = "tmp";
}

if (isNullOrEmpty(nameTemplate)) {
prefix = "hostsession";
suffix = ".tmp";
} else {
prefix = getBaseName(nameTemplate);
suffix = getExtension(nameTemplate);
for (int i = 0; i <= temporaryFileCreationRetries; i++) {
String infix = Long.toString(Math.abs(random.nextLong()));
OverthereFile f = getFileForTempFile(getConnectionTemporaryDirectory(), infix);
if (!f.exists()) {
logger.trace("Creating holder directory [{}] for temporary file with name [{}]", f, name);
f.mkdir();
OverthereFile t = f.getFile(name);
logger.debug("Created temporary file [{}]", t);
return t;
}
}

return getTempFile(prefix, suffix);
throw new RuntimeIOException("Cannot generate a unique temporary file name on " + this);
}

/**
Expand All @@ -176,17 +170,7 @@ public final OverthereFile getTempFile(String prefix, String suffix) throws Runt
suffix = ".tmp";
}

Random r = new Random();
String infix = "";
for (int i = 0; i < temporaryFileCreationRetries; i++) {
OverthereFile f = getFileForTempFile(getConnectionTemporaryDirectory(), prefix + infix + suffix);
if (!f.exists()) {
logger.debug("Created temporary file {}", f);
return f;
}
infix = "-" + Long.toString(Math.abs(r.nextLong()));
}
throw new RuntimeIOException("Cannot generate a unique temporary file name on " + this);
return getTempFile(prefix + suffix);
}

private OverthereFile getConnectionTemporaryDirectory() throws RuntimeIOException {
Expand All @@ -198,7 +182,6 @@ private OverthereFile getConnectionTemporaryDirectory() throws RuntimeIOExceptio

private OverthereFile createConnectionTemporaryDirectory() {
OverthereFile temporaryDirectory = getFile(temporaryDirectoryPath);
Random r = new Random();
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmssSSS");
String prefix = "overthere-" + dateFormat.format(new Date());
String infix = "";
Expand All @@ -210,7 +193,7 @@ private OverthereFile createConnectionTemporaryDirectory() {
logger.info("Created connection temporary directory {}", tempDir);
return tempDir;
}
infix = "-" + Long.toString(Math.abs(r.nextLong()));
infix = "-" + Long.toString(Math.abs(random.nextLong()));
}
throw new RuntimeIOException("Cannot create connection temporary directory on " + this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
Expand Down Expand Up @@ -586,21 +590,25 @@ public void shouldCopyDirectoryContentToExistingOtherLocation() {
tempDir.mkdir();

// Make sure targetFolder is not seen as a temporary folder. Sudo connection handles temp files differently.
OverthereFile target = connection.getFile(tempDir.getPath() + "/targetFolder");
target.mkdir();
OverthereFile targetFolder = connection.getFile(tempDir.getPath() + "/targetFolder");
targetFolder.mkdir();
try {
OverthereFile sourceFolder = LocalFile.valueOf(temp.newFolder("sourceFolder"));
OverthereFile sourceFile = sourceFolder.getFile("sourceFile");
OverthereUtils.write("Some test data", "UTF-8", sourceFile);

OverthereFile sourceFolder = LocalFile.valueOf(temp.newFolder("sourceFolder"));
OverthereFile sourceFile = sourceFolder.getFile("sourceFile");
OverthereUtils.write("Some test data", "UTF-8", sourceFile);
sourceFolder.copyTo(targetFolder);

sourceFolder.copyTo(target);
OverthereFile targetFile = targetFolder.getFile("sourceFile");
assertThat(targetFile.exists(), is(true));
assertThat(OverthereUtils.read(targetFile, "UTF-8"), equalTo("Some test data"));

try {
assertThat(target.getFile("sourceFile").exists(), is(true));
OverthereUtils.write("Some new test data", "UTF-8", sourceFile);
sourceFolder.copyTo(targetFolder);
assertThat("Expected new file data to be written to the correct location", OverthereUtils.read(targetFile, "UTF-8"), equalTo("Some new test data"));
} finally {
// When using a sudo connection, the target folder has different rights to the temp folder it was created
// in.
target.deleteRecursively();
// When using a sudo connection, the target folder has different rights to the temp folder it was created in.
targetFolder.deleteRecursively();
}
}

Expand Down

0 comments on commit ac1c62b

Please sign in to comment.