1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.utils; |
20 | |
|
21 | |
import org.apache.hadoop.conf.Configuration; |
22 | |
import org.apache.hadoop.fs.FileSystem; |
23 | |
import org.apache.hadoop.fs.Path; |
24 | |
import org.apache.log4j.Logger; |
25 | |
|
26 | |
import com.google.common.base.Charsets; |
27 | |
import com.google.common.io.Closeables; |
28 | |
import com.google.common.io.Files; |
29 | |
|
30 | |
import java.io.File; |
31 | |
import java.io.FileFilter; |
32 | |
import java.io.IOException; |
33 | |
import java.io.Writer; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class FileUtils { |
39 | |
|
40 | 0 | private static final Logger LOG = Logger.getLogger(FileUtils.class); |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | private FileUtils() { |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public static File createTestDir(String computationName) |
55 | |
throws IOException { |
56 | 0 | String systemTmpDir = System.getProperty("java.io.tmpdir"); |
57 | 0 | long simpleRandomLong = (long) (Long.MAX_VALUE * Math.random()); |
58 | 0 | File testTempDir = new File(systemTmpDir, "giraph-" + |
59 | |
computationName + '-' + simpleRandomLong); |
60 | 0 | if (!testTempDir.mkdir()) { |
61 | 0 | throw new IOException("Could not create " + testTempDir); |
62 | |
} |
63 | 0 | testTempDir.deleteOnExit(); |
64 | 0 | return testTempDir; |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public static File createTempFile(File parent, String name) |
76 | |
throws IOException { |
77 | 0 | return createTestTempFileOrDir(parent, name, false); |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
public static File createTempDir(File parent, String name) |
89 | |
throws IOException { |
90 | 0 | File dir = createTestTempFileOrDir(parent, name, true); |
91 | 0 | if (!dir.delete()) { |
92 | 0 | LOG.error("createTempDir: Failed to create directory " + dir); |
93 | |
} |
94 | 0 | return dir; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public static File createTestTempFileOrDir(File parent, String name, |
107 | |
boolean dir) throws IOException { |
108 | 0 | File f = new File(parent, name); |
109 | 0 | f.deleteOnExit(); |
110 | 0 | if (dir && !f.mkdirs()) { |
111 | 0 | throw new IOException("Could not make directory " + f); |
112 | |
} |
113 | 0 | return f; |
114 | |
} |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public static void writeLines(File file, String[] lines) |
124 | |
throws IOException { |
125 | 0 | Writer writer = Files.newWriter(file, Charsets.UTF_8); |
126 | |
try { |
127 | 0 | for (String line : lines) { |
128 | 0 | writer.write(line); |
129 | 0 | writer.write('\n'); |
130 | |
} |
131 | |
} finally { |
132 | 0 | Closeables.close(writer, true); |
133 | 0 | } |
134 | 0 | } |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public static void delete(File dir) { |
142 | 0 | if (dir != null) { |
143 | 0 | new DeletingVisitor().accept(dir); |
144 | |
} |
145 | 0 | } |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | 0 | private static class DeletingVisitor implements FileFilter { |
151 | |
@Override |
152 | |
public boolean accept(File f) { |
153 | 0 | if (!f.isFile()) { |
154 | 0 | if (f.listFiles(this) == null) { |
155 | 0 | LOG.error("accept: Failed to list files of " + f); |
156 | |
} |
157 | |
} |
158 | 0 | if (!f.delete()) { |
159 | 0 | LOG.error("accept: Failed to delete file " + f); |
160 | |
} |
161 | 0 | return false; |
162 | |
} |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public static void deletePath(Configuration conf, String path) |
173 | |
throws IOException { |
174 | 0 | deletePath(conf, new Path(path)); |
175 | 0 | } |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public static void deletePath(Configuration conf, Path path) |
185 | |
throws IOException { |
186 | 0 | FileSystem fs = FileSystem.get(conf); |
187 | 0 | fs.delete(path, true); |
188 | 0 | } |
189 | |
} |