@Override public void onDelete() { try { fileUtilities.remove(this.localFolder); LOGGER.info("Deleted " + this); } catch (IOException e) { LOGGER.error("Couldn't delete " + this, e); } }
@Bean public FileUtilities fileUtilities() { return new FileUtilities(); }
/** * fetches content of the given file * @param file * @return content string * @throws IOException */ public String getFileContent(File file) throws IOException { assertInDirectory(file); return FileUtils.readFileToString(file, "UTF-8"); }
public Set<PosixFilePermission> octToPosixFilePermission(int modeOct) { // TODO: optimize this method and make it cleaner int modeInt = Integer.parseInt(Integer.toString(modeOct, 8)); return intToPosixFilePermission(modeInt); } }
public Set<PosixFilePermission> intToPosixFilePermission(int mode) { if (mode >= 1000 || mode < 0) { throw new IllegalArgumentException("Invalid mode " + mode); } final int owner = mode / 100; final int group = (mode - owner * 100) / 10; final int others = mode - owner * 100 - group * 10; if (owner > 7 || group > 7 || others > 7) { throw new IllegalArgumentException("Invalid mode " + mode); } return Sets.union( Sets.union(singleIntToFilePermission(owner, "OWNER"), singleIntToFilePermission(group, "GROUP")), singleIntToFilePermission(others, "OTHERS")); }
fileUtilities.octToPosixFilePermission(entry.getMode()));
/** * creates a directory * @param directoryToCreate */ public void mkdir(File directoryToCreate) { assertInDirectory(directoryToCreate); directoryToCreate.mkdirs(); }
public void deleteContainer(ContainerDTO container, Consumer<Exception> errorCallback) { try { this.fileUtilities.remove(new File(container.getPath())); } catch (IOException e) { LOGGER.error("Cannot delete container (" + container.getPath() + ")! Exception: " + e.toString());
/** * writes content to file * @param file * @param content * @throws IOException */ public void writeToFile(File file, String content) throws IOException { assertInDirectory(file); FileUtils.writeStringToFile(file, content, "UTF-8"); }
/** * creates symbolic link * @param destination * @param target * @throws IOException */ public void createSymbolicLink(File destination, File target) throws IOException { assertInDirectory(destination); assertInDirectory(target); Files.createSymbolicLink(destination.toPath(), target.toPath()); }
/** * deletes a file only if it is inside Phoenicis root * @param fileToDelete fileOrDirectoryToDelete */ public void remove(File fileToDelete) throws IOException { assertInDirectory(fileToDelete); if (fileToDelete.isDirectory()) { FileUtils.deleteDirectory(fileToDelete); } else { Files.deleteIfExists(fileToDelete.toPath()); } }
/** * lists files and directories in given directory (non-recursive) * @param directory * @return list of files and directories */ public String[] ls(File directory) { assertInDirectory(directory); File[] files = directory.listFiles(); return Arrays.stream(files) .map(file -> file.getName()) .toArray(String[]::new); }
/** * computes file size of the given file * @param file * @return file size * @throws IOException */ public long getSize(File file) throws IOException { assertInDirectory(file); Path folder = Paths.get(file.getAbsolutePath()); return Files.walk(folder).filter(p -> p.toFile().isFile()).mapToLong(p -> p.toFile().length()).sum(); }
/** * copies file * @param source * @param target * @throws IOException */ public void copy(File source, File target) throws IOException { assertInDirectory(source); assertInDirectory(target); if (source.isDirectory()) { FileUtils.copyDirectory(source, target); } else { if (target.isDirectory()) { FileUtils.copyFile(source, new File(target, source.getName())); } else { FileUtils.copyFile(source, target); } } }