public static boolean deleteDirectoryContents(File directory) { Preconditions.checkArgument(directory.isDirectory(), "Not a directory: %s", directory); // Don't delete symbolic link directories if (isSymbolicLink(directory)) { return false; } boolean success = true; for (File file : listFiles(directory)) { success = deleteRecursively(file) && success; } return success; }
public static File createTempDir(String prefix) { return createTempDir(new File(System.getProperty("java.io.tmpdir")), prefix); }
public static boolean copyRecursively(File src, File target) { if (src.isDirectory()) { return copyDirectoryContents(src, target); } else { try { Files.copy(src, target); return true; } catch (IOException e) { return false; } } }
public static boolean copyDirectoryContents(File src, File target) { Preconditions.checkArgument(src.isDirectory(), "Source dir is not a directory: %s", src); // Don't delete symbolic link directories if (isSymbolicLink(src)) { return false; } target.mkdirs(); Preconditions.checkArgument(target.isDirectory(), "Target dir is not a directory: %s", src); boolean success = true; for (File file : listFiles(src)) { success = copyRecursively(file, new File(target, file.getName())) && success; } return success; }
File tempDir = createTempDir(baseDir, "tmp-install"); try { unpackDir.mkdirs(); try { extractTar(binary, unpackDir, tarTimeout); List<File> files = listFiles(unpackDir); if (files.size() != 1) { throw new RuntimeException("Invalid tar file: file does not have a root directory " + assignment.getBinary()); if (!deleteRecursively(tempDir)) { log.warn("Unable to delete temp directory: %s", tempDir.getAbsolutePath());
@Override public List<DeploymentManager> loadSlots() { ImmutableList.Builder<DeploymentManager> builder = ImmutableList.builder(); for (File dir : listFiles(slotDir)) { if (dir.isDirectory() && new File(dir, "galaxy-slot-id.txt").canRead()) { DirectoryDeploymentManager deploymentManager = new DirectoryDeploymentManager(dir, location + "/" + dir.getName(), tarTimeout); builder.add(deploymentManager); } } return builder.build(); }
@Override public void terminate() { deleteRecursively(baseDir); deployment = null; }
public static File newFile(File parent, String... paths) { Preconditions.checkNotNull(parent, "parent is null"); Preconditions.checkNotNull(paths, "paths is null"); return newFile(parent, ImmutableList.copyOf(paths)); }
public static boolean deleteRecursively(File file) { boolean success = true; if (file.isDirectory()) { success = deleteDirectoryContents(file); } return file.delete() && success; }
private synchronized File getSlotDirectory(Installation installation) { String baseName = toBaseName(installation); baseName = baseName.replace("[^a-zA-Z0-9_.-]", "_"); Set<String> fileNames = ImmutableSet.copyOf(Lists.transform(FileUtils.listFiles(slotDir), new Function<File, String>() { @Override public String apply(@Nullable File file) { return file.getName(); } })); if (!fileNames.contains(baseName)) { return new File(slotDir, baseName); } for (int i = 1; i < 10000; i++) { String directoryName = baseName + i; if (!fileNames.contains(directoryName)) { return new File(slotDir, directoryName); } } throw new IllegalStateException("Could not find an valid slot directory name"); }
@Override public void clear() { if (deployment == null) { return; } deploymentFile.delete(); deleteRecursively(deployment.getDeploymentDir()); deployment = null; }
public static File newFile(String parent, String... paths) { Preconditions.checkNotNull(parent, "parent is null"); Preconditions.checkNotNull(paths, "paths is null"); return newFile(new File(parent), ImmutableList.copyOf(paths)); }
@Override public Collection<ExpectedSlotStatus> getAllExpectedStates() { List<ExpectedSlotStatus> slots = newArrayList(); for (File file : FileUtils.listFiles(dataDir, new PatternFilenameFilter("[^\\.].*\\.json"))) { try { String json = Files.toString(file, Charsets.UTF_8); ExpectedSlotStatus expectedSlotStatus = codec.fromJson(json); slots.add(expectedSlotStatus); } catch (Exception e) { // skip corrupted entries... these will be marked as unexpected // and someone will resolve the conflict (and overwrite the corrupted record) } } return slots; }
AuthorizedKeyStore loadKeys() throws IOException { List<AuthorizedKey> keys = newArrayList(); for (File file : listFiles(authorizedKeysDir)) { String userId = file.getName(); for (String line : readLines(file, UTF_8)) { line = line.trim(); if (!line.isEmpty()) { PublicKey key = PublicKey.valueOf(line); keys.add(new AuthorizedKey(userId, key)); } } } return new InMemoryAuthorizedKeyStore(keys); } }