Autopsy  4.19.0
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 getLogDirectory() {
239  return Places.getUserDirectory().getAbsolutePath() + File.separator
240  + "var" + File.separator + "log" + File.separator; //NON-NLS
241  }
242 
243  public static String getDefaultPlatformFileEncoding() {
244  return System.getProperty("file.encoding");
245  }
246 
247  public static String getDefaultPlatformCharset() {
248  return Charset.defaultCharset().name();
249  }
250 
251  public static String getLogFileEncoding() {
252  return Charset.forName("UTF-8").name();
253  }
254 
269  public static <T> boolean extractResourceToUserConfigDir(final Class<T> resourceClass, final String resourceFileName, boolean overWrite) throws IOException {
270  Path resourceFilePath = Paths.get(getUserConfigDirectory(), resourceFileName);
271  final File resourceFile = resourceFilePath.toFile();
272  if (resourceFile.exists() && !overWrite) {
273  return false;
274  }
275 
276  InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
277  if (null == inputStream) {
278  return false;
279  }
280 
281  resourceFile.getParentFile().mkdirs();
282  try (InputStream in = new BufferedInputStream(inputStream)) {
283  try (OutputStream out = new BufferedOutputStream(new FileOutputStream(resourceFile))) {
284  int readBytes;
285  while ((readBytes = in.read()) != -1) {
286  out.write(readBytes);
287  }
288  }
289  }
290  return true;
291  }
292 
298  public static String getOSName() {
299  return System.getProperty("os.name", OS_NAME_UNKNOWN); //NON-NLS
300  }
301 
307  public static String getOSVersion() {
308  return System.getProperty("os.version", OS_VERSION_UNKNOWN); //NON-NLS
309  }
310 
316  public static String getOSArch() {
317  return System.getProperty("os.arch", OS_ARCH_UNKNOWN); //NON-NLS
318  }
319 
325  public static boolean isWindowsOS() {
326  return PlatformUtil.getOSName().toLowerCase().contains("windows"); //NON-NLS
327  }
328 
336  public static String getOSFilePath(String origFilePath) {
337  if (isWindowsOS()) {
338  return "\"" + origFilePath + "\"";
339  } else {
340  return origFilePath;
341  }
342  }
343 
351  public static boolean is64BitOS() {
352  if (System.getProperty("os.name").contains("Windows")) { //NON-NLS
353  return (System.getenv("ProgramFiles(x86)") != null); //NON-NLS
354  } else {
355  return (System.getProperty("os.arch").contains("64")); //NON-NLS
356  }
357  }
358 
365  public static boolean is64BitJVM() {
366  return (System.getProperty("sun.arch.data.model").equals("64"));
367  }
368 
375  public static List<LocalDisk> getPhysicalDrives() {
376  List<LocalDisk> drives = new ArrayList<>();
377  // Windows drives
378  if (PlatformUtil.isWindowsOS()) {
379  int n = 0;
380  int breakCount = 0;
381  while (true) {
382  String path = "\\\\.\\PhysicalDrive" + n; //NON-NLS
383  if (canReadDrive(path)) {
384  try {
385  drives.add(new LocalDisk("Drive " + n, path, SleuthkitJNI.findDeviceSize(path))); //NON-NLS
386  } catch (TskCoreException ex) {
387  // Don't add the drive because we can't read the size
388  }
389  n++;
390  } else {
391  if (breakCount > 4) { // Give up after 4 non-existent drives
392  break;
393  }
394  breakCount++;
395  n++;
396  }
397  }
398  // Linux drives
399  } else {
400  File dev = new File("/dev/");
401  File[] files = dev.listFiles();
402  for (File f : files) {
403  String name = f.getName();
404  if ((name.contains("hd") || name.contains("sd") || name.contains("disk")) && f.canRead() && name.length() <= 5) { //NON-NLS
405  String path = "/dev/" + name; //NON-NLS
406  if (canReadDrive(path)) {
407  try {
408  drives.add(new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
409  } catch (TskCoreException ex) {
410  // Don't add the drive because we can't read the size
411  }
412  }
413  }
414  }
415 
416  }
417  return drives;
418  }
419 
426  public static List<LocalDisk> getPartitions() {
427  List<LocalDisk> drives = new ArrayList<>();
428  FileSystemView fsv = FileSystemView.getFileSystemView();
429  if (PlatformUtil.isWindowsOS()) {
430  File[] f = File.listRoots();
431  for (File f1 : f) {
432  String name = fsv.getSystemDisplayName(f1);
433  // Check if it is a drive, readable, and not mapped to the network
434  if (f1.canRead() && !name.contains("\\\\") && (fsv.isDrive(f1) || fsv.isFloppyDrive(f1))) {
435  String path = f1.getPath();
436  String diskPath = "\\\\.\\" + path.substring(0, path.length() - 1);
437  if (canReadDrive(diskPath)) {
438  drives.add(new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
439  }
440  }
441  }
442  } else {
443  File dev = new File("/dev/");
444  File[] files = dev.listFiles();
445  for (File f : files) {
446  String name = f.getName();
447  if ((name.contains("hd") || name.contains("sd") || name.contains("disk")) && f.canRead() && name.length() <= 7) { //NON-NLS
448  String path = "/dev/" + name; //NON-NLS
449  if (canReadDrive(path)) {
450  drives.add(new LocalDisk(path, path, f.getTotalSpace()));
451  }
452  }
453  }
454  }
455  return drives;
456  }
457 
473  private static boolean canReadDrive(String diskPath) {
474  BufferedInputStream br = null;
475  try {
476  File tmp = new File(diskPath);
477  br = new BufferedInputStream(new FileInputStream(tmp));
478  int b = br.read();
479  return b != -1;
480  } catch (IOException ex) {
481  return false;
482  } finally {
483  try {
484  if (br != null) {
485  br.close();
486  }
487  } catch (IOException ex) {
488  }
489  }
490  }
491 
497  public static synchronized long getPID() {
498 
499  if (pid != -1) {
500  return pid;
501  }
502 
503  try {
504  if (sigar == null) {
506  }
507  if (sigar != null) {
508  pid = sigar.getPid();
509  } else {
510  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.sigarNotInit.msg"));
511  }
512  } catch (Exception e) {
513  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.gen.msg", e.toString()));
514  }
515  return pid;
516 
517  }
518 
529  public static synchronized long getJavaPID(String sigarSubQuery) {
530  long jpid = -1;
531  final String sigarQuery = "State.Name.sw=java," + sigarSubQuery; //NON-NLS
532  try {
533  if (sigar == null) {
535  }
536  if (sigar != null) {
537  ProcessFinder finder = new ProcessFinder(sigar);
538  jpid = finder.findSingleProcess(sigarQuery);
539  } else {
540  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.sigarNotInit.msg"));
541  }
542  } catch (Exception e) {
543  System.out.println(
544  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
545  }
546  return jpid;
547 
548  }
549 
561  public static synchronized long[] getJavaPIDs(String sigarSubQuery) {
562  long[] jpids = null;
563  final String sigarQuery = "State.Name.sw=java," + sigarSubQuery; //NON-NLS
564  try {
565  if (sigar == null) {
567  }
568  if (sigar != null) {
569  ProcessFinder finder = new ProcessFinder(sigar);
570  jpids = finder.find(sigarQuery);
571  } else {
572  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.sigarNotInit"));
573  }
574  } catch (Exception e) {
575  System.out.println(
576  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
577  }
578  return jpids;
579 
580  }
581 
587  public static synchronized void killProcess(long pid) {
588  try {
589  if (sigar == null) {
591  }
592  if (sigar != null) {
593  sigar.kill(pid, 9);
594  } else {
595  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.sigarNotInit.msg"));
596  }
597  } catch (Exception e) {
598  System.out.println(
599  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.gen.msg", pid, e.toString()));
600  }
601 
602  }
603 
609  public static synchronized long getProcessVirtualMemoryUsed() {
610  long virtMem = -1;
611 
612  try {
613  if (sigar == null) {
615  }
616 
617  if (sigar == null || getPID() == -1) {
618  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
619  return -1;
620  }
621  virtMem = sigar.getProcMem(getPID()).getSize();
622  } catch (Exception e) {
623  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
624  }
625 
626  return virtMem;
627  }
628 
634  public static String getJvmMemInfo() {
635  synchronized (PlatformUtil.class) {
636  if (memoryManager == null) {
637  memoryManager = ManagementFactory.getMemoryMXBean();
638  }
639  }
640  final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
641  final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
642 
643  return NbBundle.getMessage(PlatformUtil.class,
644  "PlatformUtil.getJvmMemInfo.usageText",
645  heap.toString(), nonHeap.toString());
646  }
647 
653  public static String getPhysicalMemInfo() {
654  final Runtime runTime = Runtime.getRuntime();
655  final long maxMemory = runTime.maxMemory();
656  final long totalMemory = runTime.totalMemory();
657  final long freeMemory = runTime.freeMemory();
658  return NbBundle.getMessage(PlatformUtil.class,
659  "PlatformUtil.getPhysicalMemInfo.usageText",
660  Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
661  }
662 
668  public static String getAllMemUsageInfo() {
669  return NbBundle.getMessage(PlatformUtil.class,
670  "PlatformUtil.getAllMemUsageInfo.usageText",
673  }
674 }
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-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.