Check if permissions are sufficient to read and write output folder

This commit is contained in:
Karl Mortensen 2015-12-28 17:12:07 -05:00
parent 754d1708e9
commit cc742bbcd6
3 changed files with 49 additions and 1 deletions

View File

@ -22,6 +22,11 @@ import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import org.openide.filesystems.FileObject;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* File and dir utilities
@ -29,6 +34,8 @@ import org.openide.filesystems.FileObject;
public class FileUtil {
private static final Logger logger = Logger.getLogger(FileUtil.class.getName());
private static String TEST_STRING = "Testing";
private static String TEMP_FILE_PREFIX = "Autopsy";
/**
* Recursively delete all of the files and sub-directories in a directory.
@ -167,4 +174,36 @@ public class FileUtil {
//with underscores. We are only keeping \ as it could be part of the path.
return fileName.replaceAll("[/:\"*?<>|]+", "_");
}
/**
* Test if the current user has read and write access to the path.
*
* @param path The path to test for read and write access.
*
* @return True if we have both read and write access, false otherwise.
*/
public static boolean arePermissionsAppropriate(Path path) {
Path p = null;
try {
p = Files.createTempFile(path, TEMP_FILE_PREFIX, null);
try (FileWriter fw = new FileWriter(p.toFile(), false)) {
fw.write(TEST_STRING);
}
String result;
try (BufferedReader br = new BufferedReader(new FileReader(p.toFile()))) {
result = br.readLine();
}
return result.compareTo(TEST_STRING) == 0;
} catch (Exception ex) {
return false;
} finally {
if (p != null) {
try {
p.toFile().delete();
} catch (Exception ignored) {
}
}
}
}
}

View File

@ -24,3 +24,5 @@ PhotoRecIngestModule.cancelledByUser=PhotoRec cancelled by user.
PhotoRecIngestModule.error.exitValue=PhotoRec carver returned error exit value = {0} when scanning {1}
PhotoRecIngestModule.error.msg=Error processing {0} with PhotoRec carver.
PhotoRecIngestModule.complete.numberOfErrors=Number of Errors while Carving\:
PhotoRecIngestModule.PermissionsNotSufficient=Insufficient permissions accessing
PhotoRecIngestModule.PermissionsNotSufficientSeeReference=See "Shared Drive Authentication" in Autopsy help.

View File

@ -77,6 +77,7 @@ final class PhotoRecCarverFileIngestModule implements FileIngestModule {
private static final String PHOTOREC_REPORT = "report.xml"; //NON-NLS
private static final String LOG_FILE = "run_log.txt"; //NON-NLS
private static final String TEMP_DIR_NAME = "temp"; // NON-NLS
private static final String SEP = System.getProperty("line.separator");
private static final Logger logger = Logger.getLogger(PhotoRecCarverFileIngestModule.class.getName());
private static final HashMap<Long, IngestJobTotals> totalsForIngestJobs = new HashMap<>();
private static final IngestModuleReferenceCounter refCounter = new IngestModuleReferenceCounter();
@ -383,6 +384,12 @@ final class PhotoRecCarverFileIngestModule implements FileIngestModule {
if (path == null) {
throw new IngestModule.IngestModuleException(NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.nonHostnameUNCPathUsed"));
}
if (false == FileUtil.arePermissionsAppropriate(path)) {
throw new IngestModule.IngestModuleException(
NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficient")
+ SEP + path.toString()+ SEP +
NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficientSeeReference"));
}
}
try {
Files.createDirectory(path);