Autopsy  4.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-2014 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;
46 
51 public class PlatformUtil {
52 
53  private static final String PYTHON_MODULES_SUBDIRECTORY = "python_modules"; //NON-NLS
54  private static String javaPath = null;
55  public static final String OS_NAME_UNKNOWN = NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.nameUnknown");
56  public static final String OS_VERSION_UNKNOWN = NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.verUnknown");
57  public static final String OS_ARCH_UNKNOWN = NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.archUnknown");
58  private static volatile long pid = -1;
59  private static volatile Sigar sigar = null;
60  private static volatile MemoryMXBean memoryManager = null;
61 
67  public static String getInstallPath() {
68  File coreFolder = InstalledFileLocator.getDefault().locate("core", PlatformUtil.class.getPackage().getName(), false); //NON-NLS
69  File rootPath = coreFolder.getParentFile().getParentFile();
70  return rootPath.getAbsolutePath();
71  }
72 
79  public static String getInstallModulesPath() {
80  File coreFolder = InstalledFileLocator.getDefault().locate("core", PlatformUtil.class.getPackage().getName(), false); //NON-NLS
81 
82  File rootPath = coreFolder.getParentFile();
83  String modulesPath = rootPath.getAbsolutePath() + File.separator + "modules"; //NON-NLS
84  File modulesPathF = new File(modulesPath);
85  if (modulesPathF.exists() && modulesPathF.isDirectory()) {
86  return modulesPath;
87  } else {
88  rootPath = rootPath.getParentFile();
89  modulesPath = rootPath.getAbsolutePath() + File.separator + "modules"; //NON-NLS
90  modulesPathF = new File(modulesPath);
91  if (modulesPathF.exists() && modulesPathF.isDirectory()) {
92  return modulesPath;
93  } else {
94  return null;
95  }
96  }
97 
98  }
99 
106  public static String getUserModulesPath() {
107  return getUserDirectory().getAbsolutePath() + File.separator + "modules"; //NON-NLS
108  }
109 
115  public static String getUserPythonModulesPath() {
116  return getUserDirectory().getAbsolutePath() + File.separator + PYTHON_MODULES_SUBDIRECTORY;
117  }
118 
126  public synchronized static String getJavaPath() {
127  if (javaPath != null) {
128  return javaPath;
129  }
130 
131  File jrePath = new File(getInstallPath() + File.separator + "jre");
132  if (jrePath.exists() && jrePath.isDirectory()) {
133  System.out.println(
134  NbBundle.getMessage(PlatformUtil.class,
135  "PlatformUtil.jrePath.jreDir.msg",
136  jrePath.getAbsolutePath()));
137  javaPath = jrePath.getAbsolutePath() + File.separator + "bin" + File.separator + "java"; //NON-NLS
138  } else {
139  //else use system installed java in PATH env variable
140  javaPath = "java"; //NON-NLS
141 
142  }
143 
144  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.jrePath.usingJavaPath.msg", javaPath));
145 
146  return javaPath;
147  }
148 
155  public static File getUserDirectory() {
156  return Places.getUserDirectory();
157  }
158 
164  public static List<String> getProjectsDirs() {
165  List<String> ret = new ArrayList<>();
166  String projectDir = System.getProperty("netbeans.dirs");
167  if (projectDir == null) {
168  return ret;
169  }
170  String[] split = projectDir.split(";");
171  if (split == null || split.length == 0) {
172  return ret;
173  }
174  ret.addAll(Arrays.asList(split));
175 
176  return ret;
177  }
178 
184  public static String getUserConfigDirectory() {
185  return Places.getUserDirectory() + File.separator + "config"; //NON-NLS
186  }
187 
193  public static String getLogDirectory() {
194  return Places.getUserDirectory().getAbsolutePath() + File.separator
195  + "var" + File.separator + "log" + File.separator; //NON-NLS
196  }
197 
198  public static String getDefaultPlatformFileEncoding() {
199  return System.getProperty("file.encoding");
200  }
201 
202  public static String getDefaultPlatformCharset() {
203  return Charset.defaultCharset().name();
204  }
205 
206  public static String getLogFileEncoding() {
207  return Charset.forName("UTF-8").name();
208  }
209 
224  public static <T> boolean extractResourceToUserConfigDir(final Class<T> resourceClass, final String resourceFileName, boolean overWrite) throws IOException {
225  Path resourceFilePath = Paths.get(getUserConfigDirectory(), resourceFileName);
226  final File resourceFile = resourceFilePath.toFile();
227  if (resourceFile.exists() && !overWrite) {
228  return false;
229  }
230 
231  InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
232  if (null == inputStream) {
233  return false;
234  }
235 
236  resourceFile.getParentFile().mkdirs();
237  try (InputStream in = new BufferedInputStream(inputStream)) {
238  try (OutputStream out = new BufferedOutputStream(new FileOutputStream(resourceFile))) {
239  int readBytes;
240  while ((readBytes = in.read()) != -1) {
241  out.write(readBytes);
242  }
243  }
244  }
245  return true;
246  }
247 
253  public static String getOSName() {
254  return System.getProperty("os.name", OS_NAME_UNKNOWN); //NON-NLS
255  }
256 
262  public static String getOSVersion() {
263  return System.getProperty("os.version", OS_VERSION_UNKNOWN); //NON-NLS
264  }
265 
271  public static String getOSArch() {
272  return System.getProperty("os.arch", OS_ARCH_UNKNOWN); //NON-NLS
273  }
274 
280  public static boolean isWindowsOS() {
281  return PlatformUtil.getOSName().toLowerCase().contains("windows"); //NON-NLS
282  }
283 
291  public static String getOSFilePath(String origFilePath) {
292  if (isWindowsOS()) {
293  return "\"" + origFilePath + "\"";
294  } else {
295  return origFilePath;
296  }
297  }
298 
306  public static boolean is64BitOS() {
307  if (System.getProperty("os.name").contains("Windows")) { //NON-NLS
308  return (System.getenv("ProgramFiles(x86)") != null); //NON-NLS
309  } else {
310  return (System.getProperty("os.arch").contains("64")); //NON-NLS
311  }
312  }
313 
314 
321  public static boolean is64BitJVM() {
322  return (System.getProperty("sun.arch.data.model").equals("64"));
323  }
324 
325 
332  public static List<LocalDisk> getPhysicalDrives() {
333  List<LocalDisk> drives = new ArrayList<>();
334  // Windows drives
335  if (PlatformUtil.isWindowsOS()) {
336  int n = 0;
337  int breakCount = 0;
338  while (true) {
339  String path = "\\\\.\\PhysicalDrive" + n; //NON-NLS
340  if (canReadDrive(path)) {
341  try {
342  drives.add(new LocalDisk("Drive " + n, path, SleuthkitJNI.findDeviceSize(path))); //NON-NLS
343  } catch (TskCoreException ex) {
344  // Don't add the drive because we can't read the size
345  }
346  n++;
347  } else {
348  if (breakCount > 4) { // Give up after 4 non-existent drives
349  break;
350  }
351  breakCount++;
352  n++;
353  }
354  }
355  // Linux drives
356  } else {
357  File dev = new File("/dev/");
358  File[] files = dev.listFiles();
359  for (File f : files) {
360  String name = f.getName();
361  if ((name.contains("hd") || name.contains("sd")) && f.canRead() && name.length() == 3) { //NON-NLS
362  String path = "/dev/" + name; //NON-NLS
363  if (canReadDrive(path)) {
364  try {
365  drives.add(new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
366  } catch (TskCoreException ex) {
367  // Don't add the drive because we can't read the size
368  }
369  }
370  }
371  }
372 
373  }
374  return drives;
375  }
376 
383  public static List<LocalDisk> getPartitions() {
384  List<LocalDisk> drives = new ArrayList<>();
385  FileSystemView fsv = FileSystemView.getFileSystemView();
386  if (PlatformUtil.isWindowsOS()) {
387  File[] f = File.listRoots();
388  for (File f1 : f) {
389  String name = fsv.getSystemDisplayName(f1);
390  // Check if it is a drive, readable, and not mapped to the network
391  if (f1.canRead() && !name.contains("\\\\") && (fsv.isDrive(f1) || fsv.isFloppyDrive(f1))) {
392  String path = f1.getPath();
393  String diskPath = "\\\\.\\" + path.substring(0, path.length() - 1);
394  if (canReadDrive(diskPath)) {
395  drives.add(new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
396  }
397  }
398  }
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")) && f.canRead() && name.length() == 4) { //NON-NLS
405  String path = "/dev/" + name; //NON-NLS
406  if (canReadDrive(path)) {
407  drives.add(new LocalDisk(path, path, f.getTotalSpace()));
408  }
409  }
410  }
411  }
412  return drives;
413  }
414 
430  private static boolean canReadDrive(String diskPath) {
431  BufferedInputStream br = null;
432  try {
433  File tmp = new File(diskPath);
434  br = new BufferedInputStream(new FileInputStream(tmp));
435  int b = br.read();
436  return b != -1;
437  } catch (IOException ex) {
438  return false;
439  } finally {
440  try {
441  if (br != null) {
442  br.close();
443  }
444  } catch (IOException ex) {
445  }
446  }
447  }
448 
454  public static synchronized long getPID() {
455 
456  if (pid != -1) {
457  return pid;
458  }
459 
460  try {
461  if (sigar == null) {
463  }
464  if (sigar != null) {
465  pid = sigar.getPid();
466  } else {
467  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.sigarNotInit.msg"));
468  }
469  } catch (Exception e) {
470  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.gen.msg", e.toString()));
471  }
472  return pid;
473 
474  }
475 
486  public static synchronized long getJavaPID(String sigarSubQuery) {
487  long jpid = -1;
488  final String sigarQuery = "State.Name.sw=java," + sigarSubQuery; //NON-NLS
489  try {
490  if (sigar == null) {
492  }
493  if (sigar != null) {
494  ProcessFinder finder = new ProcessFinder(sigar);
495  jpid = finder.findSingleProcess(sigarQuery);
496  } else {
497  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.sigarNotInit.msg"));
498  }
499  } catch (Exception e) {
500  System.out.println(
501  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
502  }
503  return jpid;
504 
505  }
506 
518  public static synchronized long[] getJavaPIDs(String sigarSubQuery) {
519  long[] jpids = null;
520  final String sigarQuery = "State.Name.sw=java," + sigarSubQuery; //NON-NLS
521  try {
522  if (sigar == null) {
524  }
525  if (sigar != null) {
526  ProcessFinder finder = new ProcessFinder(sigar);
527  jpids = finder.find(sigarQuery);
528  } else {
529  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.sigarNotInit"));
530  }
531  } catch (Exception e) {
532  System.out.println(
533  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
534  }
535  return jpids;
536 
537  }
538 
544  public static synchronized void killProcess(long pid) {
545  try {
546  if (sigar == null) {
548  }
549  if (sigar != null) {
550  sigar.kill(pid, 9);
551  } else {
552  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.sigarNotInit.msg"));
553  }
554  } catch (Exception e) {
555  System.out.println(
556  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.gen.msg", pid, e.toString()));
557  }
558 
559  }
560 
566  public static synchronized long getProcessVirtualMemoryUsed() {
567  long virtMem = -1;
568 
569  try {
570  if (sigar == null) {
572  }
573 
574  if (sigar == null || getPID() == -1) {
575  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
576  return -1;
577  }
578  virtMem = sigar.getProcMem(getPID()).getSize();
579  } catch (Exception e) {
580  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
581  }
582 
583  return virtMem;
584  }
585 
591  public static String getJvmMemInfo() {
592  synchronized (PlatformUtil.class) {
593  if (memoryManager == null) {
594  memoryManager = ManagementFactory.getMemoryMXBean();
595  }
596  }
597  final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
598  final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
599 
600  return NbBundle.getMessage(PlatformUtil.class,
601  "PlatformUtil.getJvmMemInfo.usageText",
602  heap.toString(), nonHeap.toString());
603  }
604 
610  public static String getPhysicalMemInfo() {
611  final Runtime runTime = Runtime.getRuntime();
612  final long maxMemory = runTime.maxMemory();
613  final long totalMemory = runTime.totalMemory();
614  final long freeMemory = runTime.freeMemory();
615  return NbBundle.getMessage(PlatformUtil.class,
616  "PlatformUtil.getPhysicalMemInfo.usageText",
617  Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
618  }
619 
625  public static String getAllMemUsageInfo() {
626  return NbBundle.getMessage(PlatformUtil.class,
627  "PlatformUtil.getAllMemUsageInfo.usageText",
630  }
631 }
static synchronized void killProcess(long pid)
static synchronized long getProcessVirtualMemoryUsed()
static boolean canReadDrive(String diskPath)
static List< LocalDisk > getPhysicalDrives()
static long findDeviceSize(String devPath)
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-2016 Basis Technology. Generated on: Mon Apr 24 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.