Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
PlatformUtil.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2012-2019 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.BufferedInputStream;
22 import java.io.BufferedOutputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.lang.management.ManagementFactory;
30 import java.lang.management.MemoryMXBean;
31 import java.lang.management.MemoryUsage;
32 import java.nio.charset.Charset;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.List;
38 import javax.swing.filechooser.FileSystemView;
39 import org.apache.commons.io.FilenameUtils;
40 import org.hyperic.sigar.Sigar;
41 import org.hyperic.sigar.ptql.ProcessFinder;
42 import org.openide.modules.InstalledFileLocator;
43 import org.openide.modules.Places;
44 import org.openide.util.NbBundle;
45 import org.sleuthkit.datamodel.SleuthkitJNI;
46 import org.sleuthkit.datamodel.TskCoreException;
47 
52 public class PlatformUtil {
53 
54  private static final String PYTHON_MODULES_SUBDIRECTORY = "python_modules"; //NON-NLS
55  private static final String CLASSIFIERS_SUBDIRECTORY = "object_detection_classifiers"; //NON-NLS
56  private static final String OCR_LANGUAGE_SUBDIRECTORY = "ocr_language_packs"; //NON-NLS
57  private static final String OCR_LANGUAGE_PACK_EXT = "traineddata";
58  private static String javaPath = null;
59  public static final String OS_NAME_UNKNOWN = NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.nameUnknown");
60  public static final String OS_VERSION_UNKNOWN = NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.verUnknown");
61  public static final String OS_ARCH_UNKNOWN = NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.archUnknown");
62  private static volatile long pid = -1;
63  private static volatile Sigar sigar = null;
64  private static volatile MemoryMXBean memoryManager = null;
65 
71  public static String getInstallPath() {
72  File coreFolder = InstalledFileLocator.getDefault().locate("core", PlatformUtil.class.getPackage().getName(), false); //NON-NLS
73  File rootPath = coreFolder.getParentFile().getParentFile();
74  return rootPath.getAbsolutePath();
75  }
76 
83  public static String getInstallModulesPath() {
84  File coreFolder = InstalledFileLocator.getDefault().locate("core", PlatformUtil.class.getPackage().getName(), false); //NON-NLS
85 
86  File rootPath = coreFolder.getParentFile();
87  String modulesPath = rootPath.getAbsolutePath() + File.separator + "modules"; //NON-NLS
88  File modulesPathF = new File(modulesPath);
89  if (modulesPathF.exists() && modulesPathF.isDirectory()) {
90  return modulesPath;
91  } else {
92  rootPath = rootPath.getParentFile();
93  modulesPath = rootPath.getAbsolutePath() + File.separator + "modules"; //NON-NLS
94  modulesPathF = new File(modulesPath);
95  if (modulesPathF.exists() && modulesPathF.isDirectory()) {
96  return modulesPath;
97  } else {
98  return null;
99  }
100  }
101 
102  }
103 
110  public static String getUserModulesPath() {
111  return getUserDirectory().getAbsolutePath() + File.separator + "modules"; //NON-NLS
112  }
113 
119  public static String getUserPythonModulesPath() {
120  return getUserDirectory().getAbsolutePath() + File.separator + PYTHON_MODULES_SUBDIRECTORY;
121  }
122 
128  public static String getOcrLanguagePacksPath() {
129  return getUserDirectory().getAbsolutePath() + File.separator + OCR_LANGUAGE_SUBDIRECTORY;
130  }
131 
137  public static List<String> getOcrLanguagePacks() {
138  File languagePackRootDir = new File(getOcrLanguagePacksPath());
139 
140  List<String> languagePacks = new ArrayList<>();
141  for (File languagePack : languagePackRootDir.listFiles()) {
142  String fileExt = FilenameUtils.getExtension(languagePack.getName());
143  if (!languagePack.isDirectory() && OCR_LANGUAGE_PACK_EXT.equals(fileExt)) {
144  String packageName = FilenameUtils.getBaseName(languagePack.getName());
145  languagePacks.add(packageName);
146  }
147  }
148 
149  return languagePacks;
150  }
151 
157  public static String getObjectDetectionClassifierPath() {
158  return getUserDirectory().getAbsolutePath() + File.separator + CLASSIFIERS_SUBDIRECTORY;
159  }
160 
168  public synchronized static String getJavaPath() {
169  if (javaPath != null) {
170  return javaPath;
171  }
172 
173  // by default, use Java that came with Autopsy
174  File jrePath = new File(getInstallPath() + File.separator + "jre");
175  if (jrePath.exists() && jrePath.isDirectory()) {
176  System.out.println(
177  NbBundle.getMessage(PlatformUtil.class,
178  "PlatformUtil.jrePath.jreDir.msg",
179  jrePath.getAbsolutePath()));
180  javaPath = jrePath.getAbsolutePath() + File.separator + "bin" + File.separator + "java"; //NON-NLS
181  } else if (System.getProperty("java.home") != null && !(System.getProperty("java.home").isEmpty())) {
182  // if OS knows where Java is located
183  return System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; //NON-NLS
184  } else {
185  //else use system installed java in PATH env variable
186  javaPath = "java"; //NON-NLS
187  }
188 
189  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.jrePath.usingJavaPath.msg", javaPath));
190 
191  return javaPath;
192  }
193 
200  public static File getUserDirectory() {
201  return Places.getUserDirectory();
202  }
203 
209  public static List<String> getProjectsDirs() {
210  List<String> ret = new ArrayList<>();
211  String projectDir = System.getProperty("netbeans.dirs");
212  if (projectDir == null) {
213  return ret;
214  }
215  String[] split = projectDir.split(";");
216  if (split == null || split.length == 0) {
217  return ret;
218  }
219  ret.addAll(Arrays.asList(split));
220 
221  return ret;
222  }
223 
229  public static String getUserConfigDirectory() {
230  return Places.getUserDirectory() + File.separator + "config"; //NON-NLS
231  }
232 
238  public static String getModuleConfigDirectory() {
239  return Paths.get(getUserConfigDirectory(), "ModuleConfig").toString();
240  }
241 
247  public static String getLogDirectory() {
248  return Places.getUserDirectory().getAbsolutePath() + File.separator
249  + "var" + File.separator + "log" + File.separator; //NON-NLS
250  }
251 
252  public static String getDefaultPlatformFileEncoding() {
253  return System.getProperty("file.encoding");
254  }
255 
256  public static String getDefaultPlatformCharset() {
257  return Charset.defaultCharset().name();
258  }
259 
260  public static String getLogFileEncoding() {
261  return Charset.forName("UTF-8").name();
262  }
263 
278  public static <T> boolean extractResourceToUserConfigDir(final Class<T> resourceClass, final String resourceFileName, boolean overWrite) throws IOException {
279  Path resourceFilePath = Paths.get(getUserConfigDirectory(), resourceFileName);
280  final File resourceFile = resourceFilePath.toFile();
281  if (resourceFile.exists() && !overWrite) {
282  return false;
283  }
284 
285  InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
286  if (null == inputStream) {
287  return false;
288  }
289 
290  resourceFile.getParentFile().mkdirs();
291  try (InputStream in = new BufferedInputStream(inputStream)) {
292  try (OutputStream out = new BufferedOutputStream(new FileOutputStream(resourceFile))) {
293  int readBytes;
294  while ((readBytes = in.read()) != -1) {
295  out.write(readBytes);
296  }
297  }
298  }
299  return true;
300  }
301 
307  public static String getOSName() {
308  return System.getProperty("os.name", OS_NAME_UNKNOWN); //NON-NLS
309  }
310 
316  public static String getOSVersion() {
317  return System.getProperty("os.version", OS_VERSION_UNKNOWN); //NON-NLS
318  }
319 
325  public static String getOSArch() {
326  return System.getProperty("os.arch", OS_ARCH_UNKNOWN); //NON-NLS
327  }
328 
334  public static boolean isWindowsOS() {
335  return PlatformUtil.getOSName().toLowerCase().contains("windows"); //NON-NLS
336  }
337 
345  public static String getOSFilePath(String origFilePath) {
346  if (isWindowsOS()) {
347  return "\"" + origFilePath + "\"";
348  } else {
349  return origFilePath;
350  }
351  }
352 
360  public static boolean is64BitOS() {
361  if (System.getProperty("os.name").contains("Windows")) { //NON-NLS
362  return (System.getenv("ProgramFiles(x86)") != null); //NON-NLS
363  } else {
364  return (System.getProperty("os.arch").contains("64")); //NON-NLS
365  }
366  }
367 
374  public static boolean is64BitJVM() {
375  return (System.getProperty("sun.arch.data.model").equals("64"));
376  }
377 
384  public static List<LocalDisk> getPhysicalDrives() {
385  List<LocalDisk> drives = new ArrayList<>();
386  // Windows drives
387  if (PlatformUtil.isWindowsOS()) {
388  int n = 0;
389  int breakCount = 0;
390  while (true) {
391  String path = "\\\\.\\PhysicalDrive" + n; //NON-NLS
392  if (canReadDrive(path)) {
393  try {
394  drives.add(new LocalDisk("Drive " + n, path, SleuthkitJNI.findDeviceSize(path))); //NON-NLS
395  } catch (TskCoreException ex) {
396  // Don't add the drive because we can't read the size
397  }
398  n++;
399  } else {
400  if (breakCount > 4) { // Give up after 4 non-existent drives
401  break;
402  }
403  breakCount++;
404  n++;
405  }
406  }
407  // Linux drives
408  } else {
409  File dev = new File("/dev/");
410  File[] files = dev.listFiles();
411  for (File f : files) {
412  String name = f.getName();
413  if ((name.contains("hd") || name.contains("sd") || name.contains("disk")) && f.canRead() && name.length() <= 5) { //NON-NLS
414  String path = "/dev/" + name; //NON-NLS
415  if (canReadDrive(path)) {
416  try {
417  drives.add(new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
418  } catch (TskCoreException ex) {
419  // Don't add the drive because we can't read the size
420  }
421  }
422  }
423  }
424 
425  }
426  return drives;
427  }
428 
435  public static List<LocalDisk> getPartitions() {
436  List<LocalDisk> drives = new ArrayList<>();
437  FileSystemView fsv = FileSystemView.getFileSystemView();
438  if (PlatformUtil.isWindowsOS()) {
439  File[] f = File.listRoots();
440  for (File f1 : f) {
441  String name = fsv.getSystemDisplayName(f1);
442  // Check if it is a drive, readable, and not mapped to the network
443  if (f1.canRead() && !name.contains("\\\\") && (fsv.isDrive(f1) || fsv.isFloppyDrive(f1))) {
444  String path = f1.getPath();
445  String diskPath = "\\\\.\\" + path.substring(0, path.length() - 1);
446  if (canReadDrive(diskPath)) {
447  drives.add(new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
448  }
449  }
450  }
451  } else {
452  File dev = new File("/dev/");
453  File[] files = dev.listFiles();
454  for (File f : files) {
455  String name = f.getName();
456  if ((name.contains("hd") || name.contains("sd") || name.contains("disk")) && f.canRead() && name.length() <= 7) { //NON-NLS
457  String path = "/dev/" + name; //NON-NLS
458  if (canReadDrive(path)) {
459  drives.add(new LocalDisk(path, path, f.getTotalSpace()));
460  }
461  }
462  }
463  }
464  return drives;
465  }
466 
482  private static boolean canReadDrive(String diskPath) {
483  BufferedInputStream br = null;
484  try {
485  File tmp = new File(diskPath);
486  br = new BufferedInputStream(new FileInputStream(tmp));
487  int b = br.read();
488  return b != -1;
489  } catch (IOException ex) {
490  return false;
491  } finally {
492  try {
493  if (br != null) {
494  br.close();
495  }
496  } catch (IOException ex) {
497  }
498  }
499  }
500 
506  public static synchronized long getPID() {
507 
508  if (pid != -1) {
509  return pid;
510  }
511 
512  try {
513  if (sigar == null) {
515  }
516  if (sigar != null) {
517  pid = sigar.getPid();
518  } else {
519  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.sigarNotInit.msg"));
520  }
521  } catch (Exception e) {
522  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.gen.msg", e.toString()));
523  }
524  return pid;
525 
526  }
527 
538  public static synchronized long getJavaPID(String sigarSubQuery) {
539  long jpid = -1;
540  final String sigarQuery = "State.Name.sw=java," + sigarSubQuery; //NON-NLS
541  try {
542  if (sigar == null) {
544  }
545  if (sigar != null) {
546  ProcessFinder finder = new ProcessFinder(sigar);
547  jpid = finder.findSingleProcess(sigarQuery);
548  } else {
549  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.sigarNotInit.msg"));
550  }
551  } catch (Exception e) {
552  System.out.println(
553  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
554  }
555  return jpid;
556 
557  }
558 
570  public static synchronized long[] getJavaPIDs(String sigarSubQuery) {
571  long[] jpids = null;
572  final String sigarQuery = "State.Name.sw=java," + sigarSubQuery; //NON-NLS
573  try {
574  if (sigar == null) {
576  }
577  if (sigar != null) {
578  ProcessFinder finder = new ProcessFinder(sigar);
579  jpids = finder.find(sigarQuery);
580  } else {
581  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.sigarNotInit"));
582  }
583  } catch (Exception e) {
584  System.out.println(
585  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
586  }
587  return jpids;
588 
589  }
590 
596  public static synchronized void killProcess(long pid) {
597  try {
598  if (sigar == null) {
600  }
601  if (sigar != null) {
602  sigar.kill(pid, 9);
603  } else {
604  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.sigarNotInit.msg"));
605  }
606  } catch (Exception e) {
607  System.out.println(
608  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.gen.msg", pid, e.toString()));
609  }
610 
611  }
612 
618  public static synchronized long getProcessVirtualMemoryUsed() {
619  long virtMem = -1;
620 
621  try {
622  if (sigar == null) {
624  }
625 
626  if (sigar == null || getPID() == -1) {
627  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
628  return -1;
629  }
630  virtMem = sigar.getProcMem(getPID()).getSize();
631  } catch (Exception e) {
632  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
633  }
634 
635  return virtMem;
636  }
637 
643  public static String getJvmMemInfo() {
644  synchronized (PlatformUtil.class) {
645  if (memoryManager == null) {
646  memoryManager = ManagementFactory.getMemoryMXBean();
647  }
648  }
649  final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
650  final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
651 
652  return NbBundle.getMessage(PlatformUtil.class,
653  "PlatformUtil.getJvmMemInfo.usageText",
654  heap.toString(), nonHeap.toString());
655  }
656 
662  public static String getPhysicalMemInfo() {
663  final Runtime runTime = Runtime.getRuntime();
664  final long maxMemory = runTime.maxMemory();
665  final long totalMemory = runTime.totalMemory();
666  final long freeMemory = runTime.freeMemory();
667  return NbBundle.getMessage(PlatformUtil.class,
668  "PlatformUtil.getPhysicalMemInfo.usageText",
669  Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
670  }
671 
677  public static String getAllMemUsageInfo() {
678  return NbBundle.getMessage(PlatformUtil.class,
679  "PlatformUtil.getAllMemUsageInfo.usageText",
682  }
683 }
static synchronized void killProcess(long pid)
static synchronized long getProcessVirtualMemoryUsed()
static boolean canReadDrive(String diskPath)
static List< LocalDisk > getPhysicalDrives()
static String getOSFilePath(String origFilePath)
static< T > boolean extractResourceToUserConfigDir(final Class< T > resourceClass, final String resourceFileName, boolean overWrite)
static synchronized long getJavaPID(String sigarSubQuery)
static volatile MemoryMXBean memoryManager
static synchronized String getJavaPath()
static synchronized long[] getJavaPIDs(String sigarSubQuery)

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