Coverage Report - org.apache.giraph.utils.FileUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FileUtils
0%
0/36
0%
0/12
2.1
FileUtils$1
N/A
N/A
2.1
FileUtils$DeletingVisitor
0%
0/7
0%
0/6
2.1
 
 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  0
 public class FileUtils {
 39  
   /** Logger */
 40  0
   private static final Logger LOG = Logger.getLogger(FileUtils.class);
 41  
 
 42  
   /**
 43  
    * Utility class should not be instantiatable
 44  
    */
 45  0
   private FileUtils() {
 46  0
   }
 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  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  
    * 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  0
     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  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  
    * 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  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  
    * 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  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  
    * Recursively delete a directory
 138  
    *
 139  
    * @param dir Directory to delete
 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  
    * Deletes files.
 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  
    * 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  0
     deletePath(conf, new Path(path));
 175  0
   }
 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  0
     FileSystem fs = FileSystem.get(conf);
 187  0
     fs.delete(path, true);
 188  0
   }
 189  
 }