Autopsy  3.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";
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";
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";
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";
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 
222  public static <T> boolean extractResourceToUserConfigDir(final Class<T> resourceClass, final String resourceFileName, boolean overWrite) throws IOException {
223  Path resourceFilePath = Paths.get(getUserConfigDirectory(), resourceFileName);
224  final File resourceFile = resourceFilePath.toFile();
225  if (resourceFile.exists() && !overWrite) {
226  return false;
227  }
228 
229  InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
230  if (null == inputStream) {
231  return false;
232  }
233 
234  resourceFile.getParentFile().mkdirs();
235  try (InputStream in = new BufferedInputStream(inputStream)) {
236  try (OutputStream out = new BufferedOutputStream(new FileOutputStream(resourceFile))) {
237  int readBytes;
238  while ((readBytes = in.read()) != -1) {
239  out.write(readBytes);
240  }
241  }
242  }
243  return true;
244  }
245 
251  public static String getOSName() {
252  return System.getProperty("os.name", OS_NAME_UNKNOWN); //NON-NLS
253  }
254 
260  public static String getOSVersion() {
261  return System.getProperty("os.version", OS_VERSION_UNKNOWN); //NON-NLS
262  }
263 
269  public static String getOSArch() {
270  return System.getProperty("os.arch", OS_ARCH_UNKNOWN); //NON-NLS
271  }
272 
278  public static boolean isWindowsOS() {
279  return PlatformUtil.getOSName().toLowerCase().contains("windows"); //NON-NLS
280  }
281 
288  public static String getOSFilePath(String origFilePath) {
289  if (isWindowsOS()) {
290  return "\"" + origFilePath + "\"";
291  } else {
292  return origFilePath;
293  }
294  }
295 
303  public static boolean is64BitOS() {
304  if (System.getProperty("os.name").contains("Windows")) { //NON-NLS
305  return (System.getenv("ProgramFiles(x86)") != null); //NON-NLS
306  } else {
307  return (System.getProperty("os.arch").contains("64")); //NON-NLS
308  }
309  }
310 
317  public static List<LocalDisk> getPhysicalDrives() {
318  List<LocalDisk> drives = new ArrayList<>();
319  // Windows drives
320  if (PlatformUtil.isWindowsOS()) {
321  int n = 0;
322  int breakCount = 0;
323  while (true) {
324  String path = "\\\\.\\PhysicalDrive" + n; //NON-NLS
325  if (canReadDrive(path)) {
326  try {
327  drives.add(new LocalDisk("Drive " + n, path, SleuthkitJNI.findDeviceSize(path))); //NON-NLS
328  } catch (TskCoreException ex) {
329  // Don't add the drive because we can't read the size
330  }
331  n++;
332  } else {
333  if (breakCount > 4) { // Give up after 4 non-existent drives
334  break;
335  }
336  breakCount++;
337  n++;
338  }
339  }
340  // Linux drives
341  } else {
342  File dev = new File("/dev/");
343  File[] files = dev.listFiles();
344  for (File f : files) {
345  String name = f.getName();
346  if ((name.contains("hd") || name.contains("sd")) && f.canRead() && name.length() == 3) { //NON-NLS
347  String path = "/dev/" + name; //NON-NLS
348  if (canReadDrive(path)) {
349  try {
350  drives.add(new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
351  } catch (TskCoreException ex) {
352  // Don't add the drive because we can't read the size
353  }
354  }
355  }
356  }
357 
358  }
359  return drives;
360  }
361 
368  public static List<LocalDisk> getPartitions() {
369  List<LocalDisk> drives = new ArrayList<>();
370  FileSystemView fsv = FileSystemView.getFileSystemView();
371  if (PlatformUtil.isWindowsOS()) {
372  File[] f = File.listRoots();
373  for (File f1 : f) {
374  String name = fsv.getSystemDisplayName(f1);
375  // Check if it is a drive, readable, and not mapped to the network
376  if (f1.canRead() && !name.contains("\\\\") && (fsv.isDrive(f1) || fsv.isFloppyDrive(f1))) {
377  String path = f1.getPath();
378  String diskPath = "\\\\.\\" + path.substring(0, path.length() - 1);
379  if (canReadDrive(diskPath)) {
380  drives.add(new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
381  }
382  }
383  }
384  } else {
385  File dev = new File("/dev/");
386  File[] files = dev.listFiles();
387  for (File f : files) {
388  String name = f.getName();
389  if ((name.contains("hd") || name.contains("sd")) && f.canRead() && name.length() == 4) { //NON-NLS
390  String path = "/dev/" + name; //NON-NLS
391  if (canReadDrive(path)) {
392  drives.add(new LocalDisk(path, path, f.getTotalSpace()));
393  }
394  }
395  }
396  }
397  return drives;
398  }
399 
413  private static boolean canReadDrive(String diskPath) {
414  BufferedInputStream br = null;
415  try {
416  File tmp = new File(diskPath);
417  br = new BufferedInputStream(new FileInputStream(tmp));
418  int b = br.read();
419  return b != -1;
420  } catch (IOException ex) {
421  return false;
422  } finally {
423  try {
424  if (br != null) {
425  br.close();
426  }
427  } catch (IOException ex) {
428  }
429  }
430  }
431 
437  public static synchronized long getPID() {
438 
439  if (pid != -1) {
440  return pid;
441  }
442 
443  try {
444  if (sigar == null) {
446  }
447  if (sigar != null) {
448  pid = sigar.getPid();
449  } else {
450  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.sigarNotInit.msg"));
451  }
452  } catch (Exception e) {
453  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getPID.gen.msg", e.toString()));
454  }
455  return pid;
456 
457  }
458 
469  public static synchronized long getJavaPID(String sigarSubQuery) {
470  long jpid = -1;
471  final String sigarQuery = "State.Name.sw=java," + sigarSubQuery; //NON-NLS
472  try {
473  if (sigar == null) {
475  }
476  if (sigar != null) {
477  ProcessFinder finder = new ProcessFinder(sigar);
478  jpid = finder.findSingleProcess(sigarQuery);
479  } else {
480  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.sigarNotInit.msg"));
481  }
482  } catch (Exception e) {
483  System.out.println(
484  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
485  }
486  return jpid;
487 
488  }
489 
501  public static synchronized long[] getJavaPIDs(String sigarSubQuery) {
502  long[] jpids = null;
503  final String sigarQuery = "State.Name.sw=java," + sigarSubQuery; //NON-NLS
504  try {
505  if (sigar == null) {
507  }
508  if (sigar != null) {
509  ProcessFinder finder = new ProcessFinder(sigar);
510  jpids = finder.find(sigarQuery);
511  } else {
512  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.sigarNotInit"));
513  }
514  } catch (Exception e) {
515  System.out.println(
516  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
517  }
518  return jpids;
519 
520  }
521 
527  public static synchronized void killProcess(long pid) {
528  try {
529  if (sigar == null) {
531  }
532  if (sigar != null) {
533  sigar.kill(pid, 9);
534  } else {
535  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.sigarNotInit.msg"));
536  }
537  } catch (Exception e) {
538  System.out.println(
539  NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.killProcess.gen.msg", pid, e.toString()));
540  }
541 
542  }
543 
549  public static synchronized long getProcessVirtualMemoryUsed() {
550  long virtMem = -1;
551 
552  try {
553  if (sigar == null) {
555  }
556 
557  if (sigar == null || getPID() == -1) {
558  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
559  return -1;
560  }
561  virtMem = sigar.getProcMem(getPID()).getSize();
562  } catch (Exception e) {
563  System.out.println(NbBundle.getMessage(PlatformUtil.class, "PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
564  }
565 
566  return virtMem;
567  }
568 
574  public static String getJvmMemInfo() {
575  synchronized (PlatformUtil.class) {
576  if (memoryManager == null) {
577  memoryManager = ManagementFactory.getMemoryMXBean();
578  }
579  }
580  final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
581  final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
582 
583  return NbBundle.getMessage(PlatformUtil.class,
584  "PlatformUtil.getJvmMemInfo.usageText",
585  heap.toString(), nonHeap.toString());
586  }
587 
593  public static String getPhysicalMemInfo() {
594  final Runtime runTime = Runtime.getRuntime();
595  final long maxMemory = runTime.maxMemory();
596  final long totalMemory = runTime.totalMemory();
597  final long freeMemory = runTime.freeMemory();
598  return NbBundle.getMessage(PlatformUtil.class,
599  "PlatformUtil.getPhysicalMemInfo.usageText",
600  Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
601  }
602 
608  public static String getAllMemUsageInfo() {
609  return NbBundle.getMessage(PlatformUtil.class,
610  "PlatformUtil.getAllMemUsageInfo.usageText",
613  }
614 }
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-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.