Autopsy  4.7.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileUtil.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2012-2016 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.coreutils;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.logging.Level;
24 import org.openide.filesystems.FileObject;
25 import java.nio.file.Files;
26 import java.nio.file.Path;
27 
31 public class FileUtil {
32 
33  private static final Logger logger = Logger.getLogger(FileUtil.class.getName());
34  private static final String TEMP_FILE_NAME = "Autopsy"; //NON-NLS
35  private static final String TEMP_FILE_EXT = null; //NON-NLS
36 
47  public static boolean deleteDir(File dirPath) {
48  if (dirPath.isDirectory() == false || dirPath.exists() == false) {
49  logger.log(Level.WARNING, "deleteDir passed in a non-directory: {0}", dirPath.getPath()); //NON-NLS
50  return false;
51  }
52 
53  File[] files = dirPath.listFiles();
54  boolean hadErrors = false;
55  if (files != null) {
56  for (File file : files) {
57  if (file.isDirectory()) {
58  if (deleteDir(file) == false) {
59  // message was already logged
60  hadErrors = true;
61  }
62  } else {
63  if (file.delete() == false) {
64  logger.log(Level.WARNING, "Failed to delete file {0}", file.getPath()); //NON-NLS
65  hadErrors = true;
66  }
67  }
68  }
69  }
70  if (dirPath.delete() == false) {
71  logger.log(Level.WARNING, "Failed to delete the empty directory at {0}", dirPath.getPath()); //NON-NLS
72  hadErrors = true;
73  }
74 
75  return !hadErrors;
76  }
77 
87  public static boolean deleteFileDir(File path) {
88  boolean sucess = true;
89  if (path.isFile()) { // If it's a file
90  if (!path.delete()) {
91  sucess = false;
92  logger.log(Level.WARNING, "Failed to delete file {0}", path.getPath()); //NON-NLS
93  }
94  } else { // If it's a directory
95  sucess = deleteDir(path);
96  }
97  return sucess;
98  }
99 
116  public static String copyFile(String source, String destFolder, String newName, String ext, boolean overwrite)
117  throws IOException {
118 
119  final String destFileName = destFolder + File.separator + newName + ext;
120  final File destFile = new File(destFileName);
121  if (destFile.exists()) {
122  if (overwrite) {
123  destFile.delete();
124  } else {
125  return null;
126  }
127  }
128 
129  final FileObject sourceFileObj = org.openide.filesystems.FileUtil.createData(new File(source));
130  final FileObject destFolderObj = org.openide.filesystems.FileUtil.createData(new File(destFolder));
131 
132  // org.openide.filesystems.FileUtil.copyFile requires an extension without the "." e.g. "java"
133  FileObject created = org.openide.filesystems.FileUtil.copyFile(sourceFileObj, destFolderObj, newName, ext.substring(1));
134 
135  return created.getPath();
136 
137  }
138 
150  public static String copyFolder(String source, String path, String folderName) throws IOException {
151  String destFolder = path + File.separator + folderName;
152  org.openide.filesystems.FileUtil.createFolder(new File(destFolder));
153 
154  final FileObject sourceFileObj = org.openide.filesystems.FileUtil.createData(new File(source));
155  final FileObject destFolderObj = org.openide.filesystems.FileUtil.createData(new File(destFolder));
156 
157  FileObject created = org.openide.filesystems.FileUtil.copyFile(sourceFileObj, destFolderObj, sourceFileObj.getName(), sourceFileObj.getExt());
158 
159  return created.getPath();
160  }
161 
169  public static String escapeFileName(String fileName) {
170  //for now escaping /:"*?<>| (not valid in file name, at least on Windows)
171  //with underscores. We are only keeping \ as it could be part of the path.
172  return fileName.replaceAll("[/:\"*?<>|]+", "_");
173  }
174 
183  public static boolean hasReadWriteAccess(Path dirPath) {
184  Path p = null;
185  try {
186  p = Files.createTempFile(dirPath, TEMP_FILE_NAME, TEMP_FILE_EXT);
187  return (p.toFile().canRead() && p.toFile().canWrite());
188  } catch (IOException ex) {
189  return false;
190  } finally {
191  if (p != null) {
192  try {
193  p.toFile().delete();
194  } catch (Exception ignored) {
195  }
196  }
197  }
198  }
199 
203  private FileUtil() {
204  }
205 }
static String copyFile(String source, String destFolder, String newName, String ext, boolean overwrite)
Definition: FileUtil.java:116
static boolean deleteFileDir(File path)
Definition: FileUtil.java:87
static boolean hasReadWriteAccess(Path dirPath)
Definition: FileUtil.java:183
static String copyFolder(String source, String path, String folderName)
Definition: FileUtil.java:150
static String escapeFileName(String fileName)
Definition: FileUtil.java:169
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static boolean deleteDir(File dirPath)
Definition: FileUtil.java:47

Copyright © 2012-2016 Basis Technology. Generated on: Mon Jun 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.