This project has retired. For details please refer to its Attic page.
FileUtils xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Helper class for filesystem operations during testing
37   */
38  public class FileUtils {
39    /** Logger */
40    private static final Logger LOG = Logger.getLogger(FileUtils.class);
41  
42    /**
43     * Utility class should not be instantiatable
44     */
45    private FileUtils() {
46    }
47  
48    /**
49     * Create a temporary folder that will be removed after the test.
50     *
51     * @param computationName Used for generating the folder name.
52     * @return File object for the directory.
53     */
54    public static File createTestDir(String computationName)
55      throws IOException {
56      String systemTmpDir = System.getProperty("java.io.tmpdir");
57      long simpleRandomLong = (long) (Long.MAX_VALUE * Math.random());
58      File testTempDir = new File(systemTmpDir, "giraph-" +
59          computationName + '-' + simpleRandomLong);
60      if (!testTempDir.mkdir()) {
61        throw new IOException("Could not create " + testTempDir);
62      }
63      testTempDir.deleteOnExit();
64      return testTempDir;
65    }
66  
67    /**
68     * Make a temporary file.
69     *
70     * @param parent Parent directory.
71     * @param name File name.
72     * @return File object to temporary file.
73     * @throws IOException
74     */
75    public static File createTempFile(File parent, String name)
76      throws IOException {
77      return createTestTempFileOrDir(parent, name, false);
78    }
79  
80    /**
81     * Make a temporary directory.
82     *
83     * @param parent Parent directory.
84     * @param name Directory name.
85     * @return File object to temporary file.
86     * @throws IOException
87     */
88    public static File createTempDir(File parent, String name)
89      throws IOException {
90      File dir = createTestTempFileOrDir(parent, name, true);
91      if (!dir.delete()) {
92        LOG.error("createTempDir: Failed to create directory " + dir);
93      }
94      return dir;
95    }
96  
97    /**
98     * Create a test temp file or directory.
99     *
100    * @param parent Parent directory
101    * @param name Name of file
102    * @param dir Is directory?
103    * @return File object
104    * @throws IOException
105    */
106   public static File createTestTempFileOrDir(File parent, String name,
107     boolean dir) throws IOException {
108     File f = new File(parent, name);
109     f.deleteOnExit();
110     if (dir && !f.mkdirs()) {
111       throw new IOException("Could not make directory " + f);
112     }
113     return f;
114   }
115 
116   /**
117    * Write lines to a file.
118    *
119    * @param file File to write lines to
120    * @param lines Strings written to the file
121    * @throws IOException
122    */
123   public static void writeLines(File file, String[] lines)
124     throws IOException {
125     Writer writer = Files.newWriter(file, Charsets.UTF_8);
126     try {
127       for (String line : lines) {
128         writer.write(line);
129         writer.write('\n');
130       }
131     } finally {
132       Closeables.close(writer, true);
133     }
134   }
135 
136   /**
137    * Recursively delete a directory
138    *
139    * @param dir Directory to delete
140    */
141   public static void delete(File dir) {
142     if (dir != null) {
143       new DeletingVisitor().accept(dir);
144     }
145   }
146 
147   /**
148    * Deletes files.
149    */
150   private static class DeletingVisitor implements FileFilter {
151     @Override
152     public boolean accept(File f) {
153       if (!f.isFile()) {
154         if (f.listFiles(this) == null) {
155           LOG.error("accept: Failed to list files of " + f);
156         }
157       }
158       if (!f.delete()) {
159         LOG.error("accept: Failed to delete file " + f);
160       }
161       return false;
162     }
163   }
164 
165   /**
166    * Helper method to remove a path if it exists.
167    *
168    * @param conf Configuration to load FileSystem from
169    * @param path Path to remove
170    * @throws IOException
171    */
172   public static void deletePath(Configuration conf, String path)
173     throws IOException {
174     deletePath(conf, new Path(path));
175   }
176 
177   /**
178    * Helper method to remove a path if it exists.
179    *
180    * @param conf Configuration to load FileSystem from
181    * @param path Path to remove
182    * @throws IOException
183    */
184   public static void deletePath(Configuration conf, Path path)
185     throws IOException {
186     FileSystem fs = FileSystem.get(conf);
187     fs.delete(path, true);
188   }
189 }