1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/*******************************************************************************
* Copyright (c) 2012-2013 University of Stuttgart.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and the Apache License 2.0 which both accompany this distribution,
* and are available at http://www.eclipse.org/legal/epl-v10.html
* and http://www.apache.org/licenses/LICENSE-2.0
*
* Contributors:
* Oliver Kopp - initial API and implementation
*******************************************************************************/
package org.eclipse.winery.repository.backend.filebased;
import static java.nio.file.FileVisitResult.CONTINUE;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FileUtils {
private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
/**
* Deletes given path. If path a file, it is directly deleted. If it is a
* directory, the directory is recursively deleted.
*
* Does not try to change read-only files to read-write files
*
* Only uses Java7's nio, does not fall back to Java6.
*
* @param path the path to delete
* @throws IOException
*/
public static void forceDelete(Path path) throws IOException {
if (Files.isDirectory(path)) {
try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
try {
Files.delete(file);
} catch (IOException e) {
FileUtils.logger.debug("Could not delete file", e.getMessage());
}
return CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (exc == null) {
try {
Files.delete(dir);
} catch (IOException e) {
FileUtils.logger.debug("Could not delete dir", e);
}
return CONTINUE;
} else {
FileUtils.logger.debug("Could not delete file", exc);
return CONTINUE;
}
}
});
} catch (IOException e) {
FileUtils.logger.debug("Could not delete dir", e);
}
} else {
try {
Files.delete(path);
} catch (IOException e) {
FileUtils.logger.debug("Could not delete file", e.getMessage());
}
}
}
/**
* Creates the given directory including its parent directories, if they do
* not exist.
*
* @param path
* @throws IOException
*/
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);
}
}
// public static Response readContentFromFile(RepositoryFileReference ref) {
// try {
// Repository.INSTANCE.readContentFromFile(ref);
// }
// }
}
|