Merge "Fix build errors in autorelease full clean build"
[vfc/nfvo/wfengine.git] / winery / org.eclipse.winery.repository / src / main / java / org / eclipse / winery / repository / backend / filebased / FileUtils.java
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
8  *
9  * Contributors:
10  *     Oliver Kopp - initial API and implementation
11  *******************************************************************************/
12 package org.eclipse.winery.repository.backend.filebased;
13
14 import static java.nio.file.FileVisitResult.CONTINUE;
15
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;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class FileUtils {
27         
28         private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
29         
30         
31         /**
32          * Deletes given path. If path a file, it is directly deleted. If it is a
33          * directory, the directory is recursively deleted.
34          * 
35          * Does not try to change read-only files to read-write files
36          * 
37          * Only uses Java7's nio, does not fall back to Java6.
38          * 
39          * @param path the path to delete
40          * @throws IOException
41          */
42         public static void forceDelete(Path path) throws IOException {
43                 if (Files.isDirectory(path)) {
44                         try {
45                                 Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
46                                         
47                                         @Override
48                                         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
49                                                 try {
50                                                         Files.delete(file);
51                                                 } catch (IOException e) {
52                                                         FileUtils.logger.debug("Could not delete file", e.getMessage());
53                                                 }
54                                                 return CONTINUE;
55                                         }
56                                         
57                                         @Override
58                                         public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
59                                                 if (exc == null) {
60                                                         try {
61                                                                 Files.delete(dir);
62                                                         } catch (IOException e) {
63                                                                 FileUtils.logger.debug("Could not delete dir", e);
64                                                         }
65                                                         return CONTINUE;
66                                                 } else {
67                                                         FileUtils.logger.debug("Could not delete file", exc);
68                                                         return CONTINUE;
69                                                 }
70                                         }
71                                 });
72                         } catch (IOException e) {
73                                 FileUtils.logger.debug("Could not delete dir", e);
74                         }
75                 } else {
76                         try {
77                                 Files.delete(path);
78                         } catch (IOException e) {
79                                 FileUtils.logger.debug("Could not delete file", e.getMessage());
80                         }
81                 }
82         }
83         
84         /**
85          * Creates the given directory including its parent directories, if they do
86          * not exist.
87          * 
88          * @param path
89          * @throws IOException
90          */
91         public static void createDirectory(Path path) throws IOException {
92                 Path parent = path.getParent();
93                 if (parent == null) {
94                         throw new IOException("No parent found");
95                 }
96                 if (!Files.exists(parent)) {
97                         FileUtils.createDirectory(parent);
98                 }
99                 if (!Files.exists(path)) {
100                         Files.createDirectory(path);
101                 }
102         }
103         
104         // public static Response readContentFromFile(RepositoryFileReference ref) {
105         // try {
106         // Repository.INSTANCE.readContentFromFile(ref);
107         // }
108         // }
109         
110 }