19 package org.sleuthkit.autopsy.coreutils;
21 import java.io.BufferedInputStream;
22 import java.io.BufferedOutputStream;
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;
58 private static volatile long pid = -1;
59 private static volatile Sigar
sigar = null;
68 File coreFolder = InstalledFileLocator.getDefault().locate(
"core",
PlatformUtil.class.getPackage().getName(),
false);
69 File rootPath = coreFolder.getParentFile().getParentFile();
70 return rootPath.getAbsolutePath();
80 File coreFolder = InstalledFileLocator.getDefault().locate(
"core",
PlatformUtil.class.getPackage().getName(),
false);
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()) {
88 rootPath = rootPath.getParentFile();
89 modulesPath = rootPath.getAbsolutePath() + File.separator +
"modules";
90 modulesPathF =
new File(modulesPath);
91 if (modulesPathF.exists() && modulesPathF.isDirectory()) {
127 if (javaPath != null) {
131 File jrePath =
new File(
getInstallPath() + File.separator +
"jre");
132 if (jrePath.exists() && jrePath.isDirectory()) {
135 "PlatformUtil.jrePath.jreDir.msg",
136 jrePath.getAbsolutePath()));
137 javaPath = jrePath.getAbsolutePath() + File.separator +
"bin" + File.separator +
"java";
144 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.jrePath.usingJavaPath.msg", javaPath));
156 return Places.getUserDirectory();
165 List<String> ret =
new ArrayList<>();
166 String projectDir = System.getProperty(
"netbeans.dirs");
167 if (projectDir == null) {
170 String[] split = projectDir.split(
";");
171 if (split == null || split.length == 0) {
174 ret.addAll(Arrays.asList(split));
185 return Places.getUserDirectory() + File.separator +
"config";
194 return Places.getUserDirectory().getAbsolutePath() + File.separator
195 +
"var" + File.separator +
"log" + File.separator;
199 return System.getProperty(
"file.encoding");
203 return Charset.defaultCharset().name();
207 return Charset.forName(
"UTF-8").name();
224 final File resourceFile = resourceFilePath.toFile();
225 if (resourceFile.exists() && !overWrite) {
229 InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
230 if (null == inputStream) {
234 resourceFile.getParentFile().mkdirs();
235 try (InputStream in =
new BufferedInputStream(inputStream)) {
236 try (OutputStream out =
new BufferedOutputStream(
new FileOutputStream(resourceFile))) {
238 while ((readBytes = in.read()) != -1) {
239 out.write(readBytes);
252 return System.getProperty(
"os.name", OS_NAME_UNKNOWN);
261 return System.getProperty(
"os.version", OS_VERSION_UNKNOWN);
270 return System.getProperty(
"os.arch", OS_ARCH_UNKNOWN);
290 return "\"" + origFilePath +
"\"";
304 if (System.getProperty(
"os.name").contains(
"Windows")) {
305 return (System.getenv(
"ProgramFiles(x86)") != null);
307 return (System.getProperty(
"os.arch").contains(
"64"));
318 List<LocalDisk> drives =
new ArrayList<>();
324 String path =
"\\\\.\\PhysicalDrive" + n;
333 if (breakCount > 4) {
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) {
347 String path =
"/dev/" + name;
369 List<LocalDisk> drives =
new ArrayList<>();
370 FileSystemView fsv = FileSystemView.getFileSystemView();
372 File[] f = File.listRoots();
374 String name = fsv.getSystemDisplayName(f1);
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);
380 drives.add(
new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
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) {
390 String path =
"/dev/" + name;
392 drives.add(
new LocalDisk(path, path, f.getTotalSpace()));
414 BufferedInputStream br = null;
416 File tmp =
new File(diskPath);
417 br =
new BufferedInputStream(
new FileInputStream(tmp));
420 }
catch (IOException ex) {
427 }
catch (IOException ex) {
437 public static synchronized long getPID() {
448 pid = sigar.getPid();
450 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.sigarNotInit.msg"));
452 }
catch (Exception e) {
453 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.gen.msg", e.toString()));
469 public static synchronized long getJavaPID(String sigarSubQuery) {
471 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
477 ProcessFinder finder =
new ProcessFinder(sigar);
478 jpid = finder.findSingleProcess(sigarQuery);
480 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.sigarNotInit.msg"));
482 }
catch (Exception e) {
484 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
501 public static synchronized long[]
getJavaPIDs(String sigarSubQuery) {
503 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
509 ProcessFinder finder =
new ProcessFinder(sigar);
510 jpids = finder.find(sigarQuery);
512 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.sigarNotInit"));
514 }
catch (Exception e) {
516 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
535 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.sigarNotInit.msg"));
537 }
catch (Exception e) {
539 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.gen.msg", pid, e.toString()));
557 if (sigar == null ||
getPID() == -1) {
558 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
561 virtMem = sigar.getProcMem(
getPID()).getSize();
562 }
catch (Exception e) {
563 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
576 if (memoryManager == null) {
577 memoryManager = ManagementFactory.getMemoryMXBean();
580 final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
581 final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
584 "PlatformUtil.getJvmMemInfo.usageText",
585 heap.toString(), nonHeap.toString());
594 final Runtime runTime = Runtime.getRuntime();
595 final long maxMemory = runTime.maxMemory();
596 final long totalMemory = runTime.totalMemory();
597 final long freeMemory = runTime.freeMemory();
599 "PlatformUtil.getPhysicalMemInfo.usageText",
600 Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
610 "PlatformUtil.getAllMemUsageInfo.usageText",
static long findDeviceSize(String devPath)