1 /*******************************************************************************
2 * Copyright (c) 2012-2013 University of Stuttgart.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and the Apache License 2.0 which both accompany this distribution,
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
7 * and http://www.apache.org/licenses/LICENSE-2.0
10 * Oliver Kopp - initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.winery.repository.backend.filebased;
14 import static java.nio.file.FileVisitResult.CONTINUE;
16 import java.io.IOException;
17 import java.nio.file.FileVisitResult;
18 import java.nio.file.Files;
19 import java.nio.file.Path;
20 import java.nio.file.SimpleFileVisitor;
21 import java.nio.file.attribute.BasicFileAttributes;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class FileUtils {
28 private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
32 * Deletes given path. If path a file, it is directly deleted. If it is a
33 * directory, the directory is recursively deleted.
35 * Does not try to change read-only files to read-write files
37 * Only uses Java7's nio, does not fall back to Java6.
39 * @param path the path to delete
42 public static void forceDelete(Path path) throws IOException {
43 if (Files.isDirectory(path)) {
45 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
48 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
51 } catch (IOException e) {
52 FileUtils.logger.debug("Could not delete file", e.getMessage());
58 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
62 } catch (IOException e) {
63 FileUtils.logger.debug("Could not delete dir", e);
67 FileUtils.logger.debug("Could not delete file", exc);
72 } catch (IOException e) {
73 FileUtils.logger.debug("Could not delete dir", e);
78 } catch (IOException e) {
79 FileUtils.logger.debug("Could not delete file", e.getMessage());
85 * Creates the given directory including its parent directories, if they do
91 public static void createDirectory(Path path) throws IOException {
92 Path parent = path.getParent();
94 throw new IOException("No parent found");
96 if (!Files.exists(parent)) {
97 FileUtils.createDirectory(parent);
99 if (!Files.exists(path)) {
100 Files.createDirectory(path);
104 // public static Response readContentFromFile(RepositoryFileReference ref) {
106 // Repository.INSTANCE.readContentFromFile(ref);