Autopsy 4.22.1
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 */
19package org.sleuthkit.autopsy.coreutils;
20
21import java.io.File;
22import java.io.IOException;
23import java.util.logging.Level;
24import org.openide.filesystems.FileObject;
25import java.nio.file.Files;
26import java.nio.file.Path;
27
31public 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 // Also trim empty space characters at the beginning and end of file name.
173 return fileName.replaceAll("[\\p{Cntrl}/:\"*?<>|]+", "_").trim();
174 }
175
184 public static boolean hasReadWriteAccess(Path dirPath) {
185 Path p = null;
186 try {
187 p = Files.createTempFile(dirPath, TEMP_FILE_NAME, TEMP_FILE_EXT);
188 return (p.toFile().canRead() && p.toFile().canWrite());
189 } catch (IOException ex) {
190 return false;
191 } finally {
192 if (p != null) {
193 try {
194 p.toFile().delete();
195 } catch (Exception ignored) {
196 }
197 }
198 }
199 }
200
204 private FileUtil() {
205 }
206}
static String copyFolder(String source, String path, String folderName)
static boolean deleteDir(File dirPath)
Definition FileUtil.java:47
static String escapeFileName(String fileName)
static String copyFile(String source, String destFolder, String newName, String ext, boolean overwrite)
static boolean hasReadWriteAccess(Path dirPath)
static boolean deleteFileDir(File path)
Definition FileUtil.java:87
synchronized static Logger getLogger(String name)
Definition Logger.java:124

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.