blob: e244e25aec525530ae0fe6f7b9549d9342da530c [file] [log] [blame]
/*
* Copyright 2012-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.facebook.buck.rules;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import com.google.common.base.Preconditions;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DirArtifactCache implements ArtifactCache {
private final static Logger logger = Logger.getLogger(DirArtifactCache.class.getName());
private final File cacheDir;
public DirArtifactCache(File cacheDir) throws IOException {
this.cacheDir = Preconditions.checkNotNull(cacheDir);
Files.createDirectories(cacheDir.toPath());
}
@Override
public CacheResult fetch(RuleKey ruleKey, File output) {
CacheResult success = CacheResult.MISS;
File cacheEntry = new File(cacheDir, ruleKey.toString());
if (cacheEntry.exists()) {
try {
Files.createDirectories(output.toPath().getParent());
Files.copy(cacheEntry.toPath(), output.toPath(), REPLACE_EXISTING);
success = CacheResult.DIR_HIT;
} catch (IOException e) {
logger.warning(String.format("Artifact fetch(%s, %s) error: %s",
ruleKey,
output.getPath(),
e.getMessage()));
}
}
logger.info(String.format("Artifact fetch(%s, %s) cache %s",
ruleKey,
output.getPath(),
(success.isSuccess() ? "hit" : "miss")));
return success;
}
@Override
public void store(RuleKey ruleKey, File output) {
File cacheEntry = new File(cacheDir, ruleKey.toString());
Path tmpCacheEntry = null;
try {
// Write to a temporary file and move the file to its final location atomically to protect
// against partial artifacts (whether due to buck interruption or filesystem failure) posing
// as valid artifacts during subsequent buck runs.
tmpCacheEntry = File.createTempFile(ruleKey.toString(), ".tmp", cacheDir).toPath();
Files.copy(output.toPath(), tmpCacheEntry, REPLACE_EXISTING);
Files.move(tmpCacheEntry, cacheEntry.toPath());
} catch (IOException e) {
logger.warning(String.format("Artifact store(%s, %s) error: %s",
ruleKey,
output.getPath(),
e.getMessage()));
if (tmpCacheEntry != null) {
try {
Files.deleteIfExists(tmpCacheEntry);
} catch (IOException ignored) {
// Unable to delete a temporary file. Nothing sane to do.
logger.log(Level.INFO, "Unable to delete temp cache file", ignored);
}
}
}
}
/** @return {@code true}: storing artifacts is always supported by this class. */
@Override
public boolean isStoreSupported() {
return true;
}
}