Autopsy  4.12.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.nio.charset.Charset;
24 import java.nio.charset.StandardCharsets;
25 import java.util.logging.Level;
26 import org.openide.filesystems.FileObject;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 
33 public class FileUtil {
34 
35  private static final Logger logger = Logger.getLogger(FileUtil.class.getName());
36  private static final String TEMP_FILE_NAME = "Autopsy"; //NON-NLS
37  private static final String TEMP_FILE_EXT = null; //NON-NLS
38 
49  public static boolean deleteDir(File dirPath) {
50  if (dirPath.isDirectory() == false || dirPath.exists() == false) {
51  logger.log(Level.WARNING, "deleteDir passed in a non-directory: {0}", dirPath.getPath()); //NON-NLS
52  return false;
53  }
54 
55  File[] files = dirPath.listFiles();
56  boolean hadErrors = false;
57  if (files != null) {
58  for (File file : files) {
59  if (file.isDirectory()) {
60  if (deleteDir(file) == false) {
61  // message was already logged
62  hadErrors = true;
63  }
64  } else {
65  if (file.delete() == false) {
66  logger.log(Level.WARNING, "Failed to delete file {0}", file.getPath()); //NON-NLS
67  hadErrors = true;
68  }
69  }
70  }
71  }
72  if (dirPath.delete() == false) {
73  logger.log(Level.WARNING, "Failed to delete the empty directory at {0}", dirPath.getPath()); //NON-NLS
74  hadErrors = true;
75  }
76 
77  return !hadErrors;
78  }
79 
89  public static boolean deleteFileDir(File path) {
90  boolean sucess = true;
91  if (path.isFile()) { // If it's a file
92  if (!path.delete()) {
93  sucess = false;
94  logger.log(Level.WARNING, "Failed to delete file {0}", path.getPath()); //NON-NLS
95  }
96  } else { // If it's a directory
97  sucess = deleteDir(path);
98  }
99  return sucess;
100  }
101 
118  public static String copyFile(String source, String destFolder, String newName, String ext, boolean overwrite)
119  throws IOException {
120 
121  final String destFileName = destFolder + File.separator + newName + ext;
122  final File destFile = new File(destFileName);
123  if (destFile.exists()) {
124  if (overwrite) {
125  destFile.delete();
126  } else {
127  return null;
128  }
129  }
130 
131  final FileObject sourceFileObj = org.openide.filesystems.FileUtil.createData(new File(source));
132  final FileObject destFolderObj = org.openide.filesystems.FileUtil.createData(new File(destFolder));
133 
134  // org.openide.filesystems.FileUtil.copyFile requires an extension without the "." e.g. "java"
135  FileObject created = org.openide.filesystems.FileUtil.copyFile(sourceFileObj, destFolderObj, newName, ext.substring(1));
136 
137  return created.getPath();
138 
139  }
140 
152  public static String copyFolder(String source, String path, String folderName) throws IOException {
153  String destFolder = path + File.separator + folderName;
154  org.openide.filesystems.FileUtil.createFolder(new File(destFolder));
155 
156  final FileObject sourceFileObj = org.openide.filesystems.FileUtil.createData(new File(source));
157  final FileObject destFolderObj = org.openide.filesystems.FileUtil.createData(new File(destFolder));
158 
159  FileObject created = org.openide.filesystems.FileUtil.copyFile(sourceFileObj, destFolderObj, sourceFileObj.getName(), sourceFileObj.getExt());
160 
161  return created.getPath();
162  }
163 
171  public static String escapeFileName(String fileName) {
172  //for now escaping /:"*?<>| (not valid in file name, at least on Windows)
173  //with underscores. We are only keeping \ as it could be part of the path.
174  return fileName.replaceAll("[\\p{Cntrl}/:\"*?<>|]+", "_");
175  }
176 
184  public static String utf8SanitizeFileName(String fileName) {
185  Charset charset = StandardCharsets.UTF_8;
186  return charset.decode(charset.encode(escapeFileName(fileName))).toString();
187  }
188 
197  public static boolean hasReadWriteAccess(Path dirPath) {
198  Path p = null;
199  try {
200  p = Files.createTempFile(dirPath, TEMP_FILE_NAME, TEMP_FILE_EXT);
201  return (p.toFile().canRead() && p.toFile().canWrite());
202  } catch (IOException ex) {
203  return false;
204  } finally {
205  if (p != null) {
206  try {
207  p.toFile().delete();
208  } catch (Exception ignored) {
209  }
210  }
211  }
212  }
213 
217  private FileUtil() {
218  }
219 }
static String copyFile(String source, String destFolder, String newName, String ext, boolean overwrite)
Definition: FileUtil.java:118
static boolean deleteFileDir(File path)
Definition: FileUtil.java:89
static boolean hasReadWriteAccess(Path dirPath)
Definition: FileUtil.java:197
static String utf8SanitizeFileName(String fileName)
Definition: FileUtil.java:184
static String copyFolder(String source, String path, String folderName)
Definition: FileUtil.java:152
static String escapeFileName(String fileName)
Definition: FileUtil.java:171
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static boolean deleteDir(File dirPath)
Definition: FileUtil.java:49

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