Skip to content

Commit

Permalink
Stores temporary cache files under cache directory. (GoogleContainerT…
Browse files Browse the repository at this point in the history
  • Loading branch information
coollog authored Sep 26, 2018
1 parent 5316bff commit b2bd6b4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ public class TemporaryDirectory implements Closeable {
private final Path temporaryDirectory;

/**
* Creates a new temporary directory.
* Creates a new temporary directory under an existing {@code parentDirectory}.
*
* @param parentDirectory the directory to create the temporary directory within
* @throws IOException if an I/O exception occurs
*/
public TemporaryDirectory() throws IOException {
temporaryDirectory = Files.createTempDirectory(null);
public TemporaryDirectory(Path parentDirectory) throws IOException {
temporaryDirectory = Files.createTempDirectory(parentDirectory, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class DefaultCacheStorageFiles {
private static final String LAYERS_DIRECTORY = "layers";
private static final String METADATA_FILENAME = "metadata";
private static final String SELECTORS_DIRECTORY = "selectors";
private static final String TEMPORARY_DIRECTORY = "tmp";

/**
* Returns whether or not {@code file} is a layer contents file.
Expand Down Expand Up @@ -140,4 +141,13 @@ Path getLayersDirectory() {
Path getLayerDirectory(DescriptorDigest layerDigest) {
return getLayersDirectory().resolve(layerDigest.getHash());
}

/**
* Gets the directory to store temporary files.
*
* @return the directory for temporary files
*/
Path getTemporaryDirectory() {
return cacheDirectory.resolve(TEMPORARY_DIRECTORY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.GZIPOutputStream;

/** Writes to the default cache storage engine. */
Expand Down Expand Up @@ -59,6 +62,10 @@ private WrittenLayer(
* @throws IOException if an I/O exception occurs
*/
private static void moveIfDoesNotExist(Path source, Path destination) throws IOException {
if (Files.exists(destination)) {
return;
}

try {
Files.move(source, destination, StandardCopyOption.ATOMIC_MOVE);

Expand All @@ -75,6 +82,15 @@ private static void moveIfDoesNotExist(Path source, Path destination) throws IOE

} catch (FileAlreadyExistsException alsoIgnored) {
// Same reasoning

} catch (DirectoryNotEmptyException ex) {
// The file system cannot rename directories, so we must resort to copying the directory.
Files.createDirectory(destination);
try (Stream<Path> sourceFiles = Files.list(source)) {
for (Path sourceFile : sourceFiles.collect(Collectors.toList())) {
Files.copy(sourceFile, destination.resolve(sourceFile.getFileName()));
}
}
}
}
}
Expand Down Expand Up @@ -108,8 +124,11 @@ CacheEntry write(CacheWrite cacheWrite) throws IOException {
// Creates the layers directory if it doesn't exist.
Files.createDirectories(defaultCacheStorageFiles.getLayersDirectory());

// Creates the temporary directory.
try (TemporaryDirectory temporaryDirectory = new TemporaryDirectory()) {
// Creates the temporary directory. The temporary directory must be in the same FileStore as the
// final location for Files.move to work.
Files.createDirectories(defaultCacheStorageFiles.getTemporaryDirectory());
try (TemporaryDirectory temporaryDirectory =
new TemporaryDirectory(defaultCacheStorageFiles.getTemporaryDirectory())) {
Path temporaryLayerDirectory = temporaryDirectory.getDirectory();

// Writes the layer file to the temporary directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ private static void createFilesInDirectory(Path directory)

@Test
public void testClose_directoryDeleted() throws IOException, URISyntaxException {
try (TemporaryDirectory temporaryDirectory = new TemporaryDirectory()) {
try (TemporaryDirectory temporaryDirectory =
new TemporaryDirectory(temporaryFolder.newFolder().toPath())) {
createFilesInDirectory(temporaryDirectory.getDirectory());

temporaryDirectory.close();
Expand All @@ -54,7 +55,8 @@ public void testClose_directoryDeleted() throws IOException, URISyntaxException
public void testClose_directoryNotDeletedIfMoved() throws IOException, URISyntaxException {
Path destinationParent = temporaryFolder.newFolder().toPath();

try (TemporaryDirectory temporaryDirectory = new TemporaryDirectory()) {
try (TemporaryDirectory temporaryDirectory =
new TemporaryDirectory(temporaryFolder.newFolder().toPath())) {
createFilesInDirectory(temporaryDirectory.getDirectory());

Assert.assertFalse(Files.exists(destinationParent.resolve("destination")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,10 @@ public void testGetLayerDirectory() throws DigestException {
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
testDefaultCacheStorageFiles.getLayerDirectory(layerDigest));
}

@Test
public void testGetTemporaryDirectory() {
Assert.assertEquals(
Paths.get("cache/directory/tmp"), testDefaultCacheStorageFiles.getTemporaryDirectory());
}
}

0 comments on commit b2bd6b4

Please sign in to comment.