/** * Removes the repository completely, even with the .git directory */ public void forceClear() { try (DirectoryStream<Path> ds = Files.newDirectoryStream(this.repositoryRoot)) { for (Path p : ds) { FileUtils.forceDelete(p); } } catch (IOException e) { FilebasedRepository.LOGGER.error(e.getMessage()); } }
@Override public boolean flagAsExisting(GenericId id) { Path path = this.id2AbsolutePath(id); try { FileUtils.createDirectory(path); } catch (IOException e) { FilebasedRepository.LOGGER.debug(e.toString()); return false; } return true; }
/** * Removes all files and dirs except the .git directory */ @Override public void doClear() { try { DirectoryStream.Filter<Path> noGitDirFilter = entry -> !(entry.getFileName().toString().equals(".git")); try (DirectoryStream<Path> ds = Files.newDirectoryStream(this.repositoryRoot, noGitDirFilter)) { for (Path p : ds) { FileUtils.forceDelete(p); } } } catch (IOException e) { FilebasedRepository.LOGGER.error(e.getMessage()); } }
/** * Creates the given directory including its parent directories, if they do * not exist. */ public static void createDirectory(Path path) throws IOException { Path parent = path.getParent(); if (parent == null) { throw new IOException("No parent found"); } if (!Files.exists(parent)) { FileUtils.createDirectory(parent); } if (!Files.exists(path)) { Files.createDirectory(path); } }
@Override public void forceDelete(GenericId id) { FileUtils.forceDelete(this.id2AbsolutePath(id)); }
@Override public void doImport(InputStream in) { ZipInputStream zis = new ZipInputStream(in); ZipEntry entry; try { while ((entry = zis.getNextEntry()) != null) { if (!entry.isDirectory()) { Path path = this.repositoryRoot.resolve(entry.getName()); try { FileUtils.createDirectory(path.getParent()); try { Files.copy(zis, path, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { FilebasedRepository.LOGGER.error("Files.copy did not succeed", e); } } catch (IOException e) { FilebasedRepository.LOGGER.error("Directory creation did not succeed", e); } } } } catch (IOException e) { FilebasedRepository.LOGGER.error("Unzipping did not succeed", e); } }
public static Path unzipFile(InputStream in) { Path dir = Utils.getTmpDir(Paths.get("zip")); FileUtils.forceDelete(dir); try (ZipInputStream inputStream = new ZipInputStream(in)) { ZipEntry entry; while (Objects.nonNull(entry = inputStream.getNextEntry())) { if (!entry.isDirectory()) { Path targetPath = dir.resolve(entry.getName()); Files.createDirectories(targetPath.getParent()); Files.copy(inputStream, targetPath); logger.debug("Write tmp file: {}", targetPath.toString()); } } } catch (Exception e) { logger.error("Create zip tmp file error: ", e); } return dir; }
@Override public void putContentToFile(RepositoryFileReference ref, String content, MediaType mediaType) throws IOException { if (mediaType == null) { // quick hack for storing mime type called this method assert (ref.getFileName().endsWith(Constants.SUFFIX_MIMETYPE)); // we do not need to store the mime type of the file containing the mime type information } else { this.setMimeType(ref, mediaType); } Path path = this.ref2AbsolutePath(ref); FileUtils.createDirectory(path.getParent()); Files.write(path, content.getBytes()); }
} finally { FileUtils.forceDelete(csarDir);
@Override public void putContentToFile(RepositoryFileReference ref, InputStream inputStream, MediaType mediaType) throws IOException { if (mediaType == null) { // quick hack for storing mime type called this method assert (ref.getFileName().endsWith(Constants.SUFFIX_MIMETYPE)); // we do not need to store the mime type of the file containing the mime type information } else { this.setMimeType(ref, mediaType); } Path targetPath = this.ref2AbsolutePath(ref); // ensure that parent directory exists FileUtils.createDirectory(targetPath.getParent()); try { Files.copy(inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING); } catch (IllegalStateException e) { FilebasedRepository.LOGGER.debug("Guessing that stream with length 0 is to be written to a file", e); // copy throws an "java.lang.IllegalStateException: Stream already closed" if the InputStream contains 0 bytes // For instance, this case happens if SugarCE-6.4.2.zip.removed is tried to be uploaded // We work around the Java7 issue and create an empty file if (Files.exists(targetPath)) { // semantics of putContentToFile: existing content is replaced without notification Files.delete(targetPath); } Files.createFile(targetPath); } }
public void forceDelete(Class<? extends DefinitionsChildId> definitionsChildIdClazz, Namespace namespace) { // instantiate new definitions child id with "ID" as id // this is used to get the absolute path DefinitionsChildId id = BackendUtils.getDefinitionsChildId(definitionsChildIdClazz, namespace.getEncoded(), "ID", true); Path path = this.id2AbsolutePath(id); // do not delete the id, delete the complete namespace // patrent folder is the namespace folder path = path.getParent(); FileUtils.forceDelete(path); }
FileUtils.forceDelete(workingDir);