Autopsy  4.12.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  File jrePath = new File(getInstallPath() + File.separator + "jre");
174  if (jrePath.exists() && jrePath.isDirectory()) {
175  System.out.println(
176  NbBundle.getMessage(PlatformUtil.class,
177  "PlatformUtil.jrePath.jreDir.msg",
178  jrePath.getAbsolutePath()));
179  javaPath = jrePath.getAbsolutePath() + File.separator + "bin" + File.separator + "java"; //NON-NLS
180  } else {
181  //else use system installed java in PATH env variable
182  javaPath = "java"; //NON-NLS
183 
184  }
185 
186  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.jrePath.usingJavaPath.msg", javaPath));
187 
188  return javaPath;
189  }
190 
197  public static File getUserDirectory() {
198  return Places.getUserDirectory();
199  }
200 
206  public static List<String> getProjectsDirs() {
207  List<String> ret = new ArrayList<>();
208  String projectDir = System.getProperty("netbeans.dirs");
209  if (projectDir == null) {
210  return ret;
211  }
212  String[] split = projectDir.split(";");
213  if (split == null || split.length == 0) {
214  return ret;
215  }
216  ret.addAll(Arrays.asList(split));
217 
218  return ret;
219  }
220 
226  public static String getUserConfigDirectory() {
227  return Places.getUserDirectory() + File.separator + "config"; //NON-NLS
228  }
229 
235  public static String getLogDirectory() {
236  return Places.getUserDirectory().getAbsolutePath() + File.separator
237  + "var" + File.separator + "log" + File.separator; //NON-NLS
238  }
239 
240  public static String getDefaultPlatformFileEncoding() {
241  return System.getProperty("file.encoding");
242  }
243 
244  public static String getDefaultPlatformCharset() {
245  return Charset.defaultCharset().name();
246  }
247 
248  public static String getLogFileEncoding() {
249  return Charset.forName("UTF-8").name();
250  }
251 
266  public static <T> boolean extractResourceToUserConfigDir(final Class<T> resourceClass, final String resourceFileName, boolean overWrite) throws IOException {
267  Path resourceFilePath = Paths.get(getUserConfigDirectory(), resourceFileName);
268  final File resourceFile = resourceFilePath.toFile();
269  if (resourceFile.exists() && !overWrite) {
270  return false;
271  }
272 
273  InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
274  if (null == inputStream) {
275  return false;
276  }
277 
278  resourceFile.getParentFile().mkdirs();
279  try (InputStream in = new BufferedInputStream(inputStream)) {
280  try (OutputStream out = new BufferedOutputStream(new FileOutputStream(resourceFile))) {
281  int readBytes;
282  while ((readBytes = in.read()) != -1) {
283  out.write(readBytes);
284  }
285  }
286  }
287  return true;
288  }
289 
295  public static String getOSName() {
296  return System.getProperty("os.name", OS_NAME_UNKNOWN); //NON-NLS
297  }
298 
304  public static String getOSVersion() {
305  return System.getProperty("os.version", OS_VERSION_UNKNOWN); //NON-NLS
306  }
307 
313  public static String getOSArch() {
314  return System.getProperty("os.arch", OS_ARCH_UNKNOWN); //NON-NLS
315  }
316 
322  public static boolean isWindowsOS() {
323  return PlatformUtil.getOSName().toLowerCase().contains("windows"); //NON-NLS
324  }
325 
333  public static String getOSFilePath(String origFilePath) {
334  if (isWindowsOS()) {
335  return "\"" + origFilePath + "\"";
336  } else {
337  return origFilePath;
338  }
339  }
340 
348  public static boolean is64BitOS() {
349  if (System.getProperty("os.name").contains("Windows")) { //NON-NLS
350  return (System.getenv("ProgramFiles(x86)") != null); //NON-NLS
351  } else {
352  return (System.getProperty("os.arch").contains("64")); //NON-NLS
353  }
354  }
355 
362  public static boolean is64BitJVM() {
363  return (System.getProperty("sun.arch.data.model").equals("64"));
364  }
365 
372  public static List<LocalDisk> getPhysicalDrives() {
373  List<LocalDisk> drives = new ArrayList<>();
374  // Windows drives
375  if (PlatformUtil.isWindowsOS()) {
376  int n = 0;
377  int breakCount = 0;
378  while (true) {
379  String path = "\\\\.\\PhysicalDrive" + n; //NON-NLS
380  if (canReadDrive(path)) {
381  try {
382  drives.add(new LocalDisk("Drive " + n, path, SleuthkitJNI.findDeviceSize(path))); //NON-NLS
383  } catch (TskCoreException ex) {
384  // Don't add the drive because we can't read the size
385  }
386  n++;
387  } else {
388  if (breakCount > 4) { // Give up after 4 non-existent drives
389  break;
390  }
391  breakCount++;
392  n++;
393  }
394  }
395  // Linux drives
396  } else {
397  File dev = new File("/dev/");
398  File[] files = dev.listFiles();
399  for (File f : files) {
400  String name = f.getName();
401  if ((name.contains("hd") || name.contains("sd") || name.contains("disk")) && f.canRead() && name.length() <= 5) { //NON-NLS
402  String path = "/dev/" + name; //NON-NLS
403  if (canReadDrive(path)) {
404  try {
405  drives.add(new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
406  } catch (TskCoreException ex) {
407  // Don't add the drive because we can't read the size
408  }
409  }
410  }
411  }
412 
413  }
414  return drives;
415  }
416 
423  public static List<LocalDisk> getPartitions() {
424  List<LocalDisk> drives = new ArrayList<>();
425  FileSystemView fsv = FileSystemView.getFileSystemView();
426  if (PlatformUtil.isWindowsOS()) {
427  File[] f = File.listRoots();
428  for (File f1 : f) {
429  String name = fsv.getSystemDisplayName(f1);
430  // Check if it is a drive, readable, and not mapped to the network
431  if (f1.canRead() && !name.contains("\\\\") && (fsv.isDrive(f1) || fsv.isFloppyDrive(f1))) {
432  String path = f1.getPath();
433  String diskPath = "\\\\.\\" + path.substring(0, path.length() - 1);
434  if (canReadDrive(diskPath)) {
435  drives.add(new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
436  }
437  }
438  }
439  } else {
440  File dev = new File("/dev/");
441  File[] files = dev.listFiles();
442  for (File f : files) {
443  String name = f.getName();
444  if ((name.contains("hd") || name.contains("sd") || name.contains("disk")) && f.canRead() && name.length() <= 7) { //NON-NLS
445  String path = "/dev/" + name; //NON-NLS
446  if (canReadDrive(path)) {
447  drives.add(new LocalDisk(path, path, f.getTotalSpace()));
448  }
449  }
450  }
451  }
452  return drives;
453  }
454 
470  private static boolean canReadDrive(String diskPath) {
471  BufferedInputStream br = null;
472  try {
473  File tmp = new File(diskPath);
474  br = new BufferedInputStream(new FileInputStream(tmp));
475  int b = br.read();
476  return b != -1;
477  } catch (IOException ex) {
478  return false;
479  } finally {
480  try {
481  if (br != null) {
482  br.close();
483  }
484  } catch (IOException ex) {
485  }
486  }
487  }
488 
494  public static synchronized long getPID() {
495 
496  if (pid != -1) {
497  return pid;
498  }
499 
500  try {
501  if (sigar == null) {
503  }
504  if (sigar != null) {
505  pid = sigar.getPid();
506  } else {
507  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.sigarNotInit.msg"));
508  }
509  } catch (Exception e) {
510  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.gen.msg", e.toString()));
511  }
512  return pid;
513 
514  }
515 
526  public static synchronized long getJavaPID(String sigarSubQuery) {
527  long jpid = -1;
528  final String sigarQuery = "State.Name.sw=java," + sigarSubQuery; //NON-NLS
529  try {
530  if (sigar == null) {
532  }
533  if (sigar != null) {
534  ProcessFinder finder = new ProcessFinder(sigar);
535  jpid = finder.findSingleProcess(sigarQuery);
536  } else {
537  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.sigarNotInit.msg"));
538  }
539  } catch (Exception e) {
540  System.out.println(
541  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
542  }
543  return jpid;
544 
545  }
546 
558  public static synchronized long[] getJavaPIDs(String sigarSubQuery) {
559  long[] jpids = null;
560  final String sigarQuery = "State.Name.sw=java," + sigarSubQuery; //NON-NLS
561  try {
562  if (sigar == null) {
564  }
565  if (sigar != null) {
566  ProcessFinder finder = new ProcessFinder(sigar);
567  jpids = finder.find(sigarQuery);
568  } else {
569  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.sigarNotInit"));
570  }
571  } catch (Exception e) {
572  System.out.println(
573  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
574  }
575  return jpids;
576 
577  }
578 
584  public static synchronized void killProcess(long pid) {
585  try {
586  if (sigar == null) {
588  }
589  if (sigar != null) {
590  sigar.kill(pid, 9);
591  } else {
592  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.sigarNotInit.msg"));
593  }
594  } catch (Exception e) {
595  System.out.println(
596  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.gen.msg", pid, e.toString()));
597  }
598 
599  }
600 
606  public static synchronized long getProcessVirtualMemoryUsed() {
607  long virtMem = -1;
608 
609  try {
610  if (sigar == null) {
612  }
613 
614  if (sigar == null || getPID() == -1) {
615  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
616  return -1;
617  }
618  virtMem = sigar.getProcMem(getPID()).getSize();
619  } catch (Exception e) {
620  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
621  }
622 
623  return virtMem;
624  }
625 
631  public static String getJvmMemInfo() {
632  synchronized (PlatformUtil.class) {
633  if (memoryManager == null) {
634  memoryManager = ManagementFactory.getMemoryMXBean();
635  }
636  }
637  final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
638  final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
639 
640  return NbBundle.getMessage(PlatformUtil.class,
641  "PlatformUtil.getJvmMemInfo.usageText",
642  heap.toString(), nonHeap.toString());
643  }
644 
650  public static String getPhysicalMemInfo() {
651  final Runtime runTime = Runtime.getRuntime();
652  final long maxMemory = runTime.maxMemory();
653  final long totalMemory = runTime.totalMemory();
654  final long freeMemory = runTime.freeMemory();
655  return NbBundle.getMessage(PlatformUtil.class,
656  "PlatformUtil.getPhysicalMemInfo.usageText",
657  Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
658  }
659 
665  public static String getAllMemUsageInfo() {
666  return NbBundle.getMessage(PlatformUtil.class,
667  "PlatformUtil.getAllMemUsageInfo.usageText",
670  }
671 }
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-2018 Basis Technology. Generated on: Wed Sep 18 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.