Autopsy  4.9.1
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-2018 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.hyperic.sigar.Sigar;
40 import org.hyperic.sigar.ptql.ProcessFinder;
41 import org.openide.modules.InstalledFileLocator;
42 import org.openide.modules.Places;
43 import org.openide.util.NbBundle;
44 import org.sleuthkit.datamodel.SleuthkitJNI;
45 import org.sleuthkit.datamodel.TskCoreException;
46 
51 public class PlatformUtil {
52 
53  private static final String PYTHON_MODULES_SUBDIRECTORY = "python_modules"; //NON-NLS
54  private static final String CLASSIFIERS_SUBDIRECTORY = "object_detection_classifiers"; //NON-NLS
55  private static String javaPath = null;
56  public static final String OS_NAME_UNKNOWN = NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.nameUnknown");
57  public static final String OS_VERSION_UNKNOWN = NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.verUnknown");
58  public static final String OS_ARCH_UNKNOWN = NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.archUnknown");
59  private static volatile long pid = -1;
60  private static volatile Sigar sigar = null;
61  private static volatile MemoryMXBean memoryManager = null;
62 
68  public static String getInstallPath() {
69  File coreFolder = InstalledFileLocator.getDefault().locate("core", PlatformUtil.class.getPackage().getName(), false); //NON-NLS
70  File rootPath = coreFolder.getParentFile().getParentFile();
71  return rootPath.getAbsolutePath();
72  }
73 
80  public static String getInstallModulesPath() {
81  File coreFolder = InstalledFileLocator.getDefault().locate("core", PlatformUtil.class.getPackage().getName(), false); //NON-NLS
82 
83  File rootPath = coreFolder.getParentFile();
84  String modulesPath = rootPath.getAbsolutePath() + File.separator + "modules"; //NON-NLS
85  File modulesPathF = new File(modulesPath);
86  if (modulesPathF.exists() && modulesPathF.isDirectory()) {
87  return modulesPath;
88  } else {
89  rootPath = rootPath.getParentFile();
90  modulesPath = rootPath.getAbsolutePath() + File.separator + "modules"; //NON-NLS
91  modulesPathF = new File(modulesPath);
92  if (modulesPathF.exists() && modulesPathF.isDirectory()) {
93  return modulesPath;
94  } else {
95  return null;
96  }
97  }
98 
99  }
100 
107  public static String getUserModulesPath() {
108  return getUserDirectory().getAbsolutePath() + File.separator + "modules"; //NON-NLS
109  }
110 
116  public static String getUserPythonModulesPath() {
117  return getUserDirectory().getAbsolutePath() + File.separator + PYTHON_MODULES_SUBDIRECTORY;
118  }
119 
125  public static String getObjectDetectionClassifierPath() {
126  return getUserDirectory().getAbsolutePath() + File.separator + CLASSIFIERS_SUBDIRECTORY;
127  }
128 
136  public synchronized static String getJavaPath() {
137  if (javaPath != null) {
138  return javaPath;
139  }
140 
141  File jrePath = new File(getInstallPath() + File.separator + "jre");
142  if (jrePath.exists() && jrePath.isDirectory()) {
143  System.out.println(
144  NbBundle.getMessage(PlatformUtil.class,
145  "PlatformUtil.jrePath.jreDir.msg",
146  jrePath.getAbsolutePath()));
147  javaPath = jrePath.getAbsolutePath() + File.separator + "bin" + File.separator + "java"; //NON-NLS
148  } else {
149  //else use system installed java in PATH env variable
150  javaPath = "java"; //NON-NLS
151 
152  }
153 
154  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.jrePath.usingJavaPath.msg", javaPath));
155 
156  return javaPath;
157  }
158 
165  public static File getUserDirectory() {
166  return Places.getUserDirectory();
167  }
168 
174  public static List<String> getProjectsDirs() {
175  List<String> ret = new ArrayList<>();
176  String projectDir = System.getProperty("netbeans.dirs");
177  if (projectDir == null) {
178  return ret;
179  }
180  String[] split = projectDir.split(";");
181  if (split == null || split.length == 0) {
182  return ret;
183  }
184  ret.addAll(Arrays.asList(split));
185 
186  return ret;
187  }
188 
194  public static String getUserConfigDirectory() {
195  return Places.getUserDirectory() + File.separator + "config"; //NON-NLS
196  }
197 
203  public static String getLogDirectory() {
204  return Places.getUserDirectory().getAbsolutePath() + File.separator
205  + "var" + File.separator + "log" + File.separator; //NON-NLS
206  }
207 
208  public static String getDefaultPlatformFileEncoding() {
209  return System.getProperty("file.encoding");
210  }
211 
212  public static String getDefaultPlatformCharset() {
213  return Charset.defaultCharset().name();
214  }
215 
216  public static String getLogFileEncoding() {
217  return Charset.forName("UTF-8").name();
218  }
219 
234  public static <T> boolean extractResourceToUserConfigDir(final Class<T> resourceClass, final String resourceFileName, boolean overWrite) throws IOException {
235  Path resourceFilePath = Paths.get(getUserConfigDirectory(), resourceFileName);
236  final File resourceFile = resourceFilePath.toFile();
237  if (resourceFile.exists() && !overWrite) {
238  return false;
239  }
240 
241  InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
242  if (null == inputStream) {
243  return false;
244  }
245 
246  resourceFile.getParentFile().mkdirs();
247  try (InputStream in = new BufferedInputStream(inputStream)) {
248  try (OutputStream out = new BufferedOutputStream(new FileOutputStream(resourceFile))) {
249  int readBytes;
250  while ((readBytes = in.read()) != -1) {
251  out.write(readBytes);
252  }
253  }
254  }
255  return true;
256  }
257 
263  public static String getOSName() {
264  return System.getProperty("os.name", OS_NAME_UNKNOWN); //NON-NLS
265  }
266 
272  public static String getOSVersion() {
273  return System.getProperty("os.version", OS_VERSION_UNKNOWN); //NON-NLS
274  }
275 
281  public static String getOSArch() {
282  return System.getProperty("os.arch", OS_ARCH_UNKNOWN); //NON-NLS
283  }
284 
290  public static boolean isWindowsOS() {
291  return PlatformUtil.getOSName().toLowerCase().contains("windows"); //NON-NLS
292  }
293 
301  public static String getOSFilePath(String origFilePath) {
302  if (isWindowsOS()) {
303  return "\"" + origFilePath + "\"";
304  } else {
305  return origFilePath;
306  }
307  }
308 
316  public static boolean is64BitOS() {
317  if (System.getProperty("os.name").contains("Windows")) { //NON-NLS
318  return (System.getenv("ProgramFiles(x86)") != null); //NON-NLS
319  } else {
320  return (System.getProperty("os.arch").contains("64")); //NON-NLS
321  }
322  }
323 
330  public static boolean is64BitJVM() {
331  return (System.getProperty("sun.arch.data.model").equals("64"));
332  }
333 
340  public static List<LocalDisk> getPhysicalDrives() {
341  List<LocalDisk> drives = new ArrayList<>();
342  // Windows drives
343  if (PlatformUtil.isWindowsOS()) {
344  int n = 0;
345  int breakCount = 0;
346  while (true) {
347  String path = "\\\\.\\PhysicalDrive" + n; //NON-NLS
348  if (canReadDrive(path)) {
349  try {
350  drives.add(new LocalDisk("Drive " + n, path, SleuthkitJNI.findDeviceSize(path))); //NON-NLS
351  } catch (TskCoreException ex) {
352  // Don't add the drive because we can't read the size
353  }
354  n++;
355  } else {
356  if (breakCount > 4) { // Give up after 4 non-existent drives
357  break;
358  }
359  breakCount++;
360  n++;
361  }
362  }
363  // Linux drives
364  } else {
365  File dev = new File("/dev/");
366  File[] files = dev.listFiles();
367  for (File f : files) {
368  String name = f.getName();
369  if ((name.contains("hd") || name.contains("sd") || name.contains("disk")) && f.canRead() && name.length() <= 5) { //NON-NLS
370  String path = "/dev/" + name; //NON-NLS
371  if (canReadDrive(path)) {
372  try {
373  drives.add(new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
374  } catch (TskCoreException ex) {
375  // Don't add the drive because we can't read the size
376  }
377  }
378  }
379  }
380 
381  }
382  return drives;
383  }
384 
391  public static List<LocalDisk> getPartitions() {
392  List<LocalDisk> drives = new ArrayList<>();
393  FileSystemView fsv = FileSystemView.getFileSystemView();
394  if (PlatformUtil.isWindowsOS()) {
395  File[] f = File.listRoots();
396  for (File f1 : f) {
397  String name = fsv.getSystemDisplayName(f1);
398  // Check if it is a drive, readable, and not mapped to the network
399  if (f1.canRead() && !name.contains("\\\\") && (fsv.isDrive(f1) || fsv.isFloppyDrive(f1))) {
400  String path = f1.getPath();
401  String diskPath = "\\\\.\\" + path.substring(0, path.length() - 1);
402  if (canReadDrive(diskPath)) {
403  drives.add(new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
404  }
405  }
406  }
407  } else {
408  File dev = new File("/dev/");
409  File[] files = dev.listFiles();
410  for (File f : files) {
411  String name = f.getName();
412  if ((name.contains("hd") || name.contains("sd") || name.contains("disk")) && f.canRead() && name.length() <= 7) { //NON-NLS
413  String path = "/dev/" + name; //NON-NLS
414  if (canReadDrive(path)) {
415  drives.add(new LocalDisk(path, path, f.getTotalSpace()));
416  }
417  }
418  }
419  }
420  return drives;
421  }
422 
438  private static boolean canReadDrive(String diskPath) {
439  BufferedInputStream br = null;
440  try {
441  File tmp = new File(diskPath);
442  br = new BufferedInputStream(new FileInputStream(tmp));
443  int b = br.read();
444  return b != -1;
445  } catch (IOException ex) {
446  return false;
447  } finally {
448  try {
449  if (br != null) {
450  br.close();
451  }
452  } catch (IOException ex) {
453  }
454  }
455  }
456 
462  public static synchronized long getPID() {
463 
464  if (pid != -1) {
465  return pid;
466  }
467 
468  try {
469  if (sigar == null) {
471  }
472  if (sigar != null) {
473  pid = sigar.getPid();
474  } else {
475  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.sigarNotInit.msg"));
476  }
477  } catch (Exception e) {
478  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.gen.msg", e.toString()));
479  }
480  return pid;
481 
482  }
483 
494  public static synchronized long getJavaPID(String sigarSubQuery) {
495  long jpid = -1;
496  final String sigarQuery = "State.Name.sw=java," + sigarSubQuery; //NON-NLS
497  try {
498  if (sigar == null) {
500  }
501  if (sigar != null) {
502  ProcessFinder finder = new ProcessFinder(sigar);
503  jpid = finder.findSingleProcess(sigarQuery);
504  } else {
505  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.sigarNotInit.msg"));
506  }
507  } catch (Exception e) {
508  System.out.println(
509  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
510  }
511  return jpid;
512 
513  }
514 
526  public static synchronized long[] getJavaPIDs(String sigarSubQuery) {
527  long[] jpids = null;
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  jpids = finder.find(sigarQuery);
536  } else {
537  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.sigarNotInit"));
538  }
539  } catch (Exception e) {
540  System.out.println(
541  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
542  }
543  return jpids;
544 
545  }
546 
552  public static synchronized void killProcess(long pid) {
553  try {
554  if (sigar == null) {
556  }
557  if (sigar != null) {
558  sigar.kill(pid, 9);
559  } else {
560  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.sigarNotInit.msg"));
561  }
562  } catch (Exception e) {
563  System.out.println(
564  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.gen.msg", pid, e.toString()));
565  }
566 
567  }
568 
574  public static synchronized long getProcessVirtualMemoryUsed() {
575  long virtMem = -1;
576 
577  try {
578  if (sigar == null) {
580  }
581 
582  if (sigar == null || getPID() == -1) {
583  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
584  return -1;
585  }
586  virtMem = sigar.getProcMem(getPID()).getSize();
587  } catch (Exception e) {
588  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
589  }
590 
591  return virtMem;
592  }
593 
599  public static String getJvmMemInfo() {
600  synchronized (PlatformUtil.class) {
601  if (memoryManager == null) {
602  memoryManager = ManagementFactory.getMemoryMXBean();
603  }
604  }
605  final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
606  final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
607 
608  return NbBundle.getMessage(PlatformUtil.class,
609  "PlatformUtil.getJvmMemInfo.usageText",
610  heap.toString(), nonHeap.toString());
611  }
612 
618  public static String getPhysicalMemInfo() {
619  final Runtime runTime = Runtime.getRuntime();
620  final long maxMemory = runTime.maxMemory();
621  final long totalMemory = runTime.totalMemory();
622  final long freeMemory = runTime.freeMemory();
623  return NbBundle.getMessage(PlatformUtil.class,
624  "PlatformUtil.getPhysicalMemInfo.usageText",
625  Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
626  }
627 
633  public static String getAllMemUsageInfo() {
634  return NbBundle.getMessage(PlatformUtil.class,
635  "PlatformUtil.getAllMemUsageInfo.usageText",
638  }
639 }
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: Tue Dec 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.