Autopsy  4.16.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
UserMachinePreferences.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2020 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.machinesettings;
20 
21 import java.io.File;
22 import java.nio.file.Paths;
23 import java.util.prefs.Preferences;
24 import org.apache.commons.lang3.StringUtils;
25 import org.openide.util.NbBundle;
26 import org.openide.util.NbPreferences;
28 
32 public final class UserMachinePreferences {
33 
34  private static final Preferences preferences = NbPreferences.forModule(UserMachinePreferences.class);
35 
36  private static final String TEMP_DIR_KEY = "TempDirectory";
37 
44  private static String getDefaultTempDirectory() {
45  return Paths.get(System.getProperty("java.io.tmpdir")).toAbsolutePath().toString();
46  }
47 
53  public static String getBaseTempDirectory() {
54  String tempDir = preferences.get(TEMP_DIR_KEY, null);
55  return StringUtils.isBlank(tempDir) ? getDefaultTempDirectory() : tempDir;
56  }
57 
70  @NbBundle.Messages({
71  "# {0} - path",
72  "UserMachinePreferences_validateTempDirectory_errorOnCreate_text=There was an error creating the temp directory for path: {0}",
73  "# {0} - path",
74  "UserMachinePreferences_validateTempDirectory_errorOnReadWrite_text=There was an error reading or writing to temp directory path: {0}"
75  })
76  private static boolean validateTempDirectory(String path) throws UserMachinePreferencesException {
77  if (StringUtils.isBlank(path)) {
78  // in this instance, the default path will be used.
79  return true;
80  }
81 
82  File f = new File(path);
83  if (!f.exists() && !f.mkdirs()) {
84  throw new UserMachinePreferencesException(Bundle.UserMachinePreferences_validateTempDirectory_errorOnCreate_text(path));
85  }
86 
87  if (!FileUtil.hasReadWriteAccess(Paths.get(path))) {
88  throw new UserMachinePreferencesException(Bundle.UserMachinePreferences_validateTempDirectory_errorOnReadWrite_text(path));
89  }
90  return true;
91  }
92 
101  public static void setBaseTempDirectory(String path) throws UserMachinePreferencesException {
102  validateTempDirectory(path);
103  preferences.put(TEMP_DIR_KEY, path);
104  }
105 
107  }
108 }
static boolean hasReadWriteAccess(Path dirPath)
Definition: FileUtil.java:183

Copyright © 2012-2020 Basis Technology. Generated on: Tue Sep 22 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.