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();
226 final File resourceFile = resourceFilePath.toFile();
227 if (resourceFile.exists() && !overWrite) {
231 InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
232 if (null == inputStream) {
236 resourceFile.getParentFile().mkdirs();
237 try (InputStream in =
new BufferedInputStream(inputStream)) {
238 try (OutputStream out =
new BufferedOutputStream(
new FileOutputStream(resourceFile))) {
240 while ((readBytes = in.read()) != -1) {
241 out.write(readBytes);
254 return System.getProperty(
"os.name", OS_NAME_UNKNOWN);
263 return System.getProperty(
"os.version", OS_VERSION_UNKNOWN);
272 return System.getProperty(
"os.arch", OS_ARCH_UNKNOWN);
293 return "\"" + origFilePath +
"\"";
307 if (System.getProperty(
"os.name").contains(
"Windows")) {
308 return (System.getenv(
"ProgramFiles(x86)") != null);
310 return (System.getProperty(
"os.arch").contains(
"64"));
322 return (System.getProperty(
"sun.arch.data.model").equals(
"64"));
333 List<LocalDisk> drives =
new ArrayList<>();
339 String path =
"\\\\.\\PhysicalDrive" + n;
342 drives.add(
new LocalDisk(
"Drive " + n, path, SleuthkitJNI.findDeviceSize(path)));
343 }
catch (TskCoreException ex) {
348 if (breakCount > 4) {
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") || name.contains(
"disk")) && f.canRead() && name.length() <= 5) {
362 String path =
"/dev/" + name;
365 drives.add(
new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
366 }
catch (TskCoreException ex) {
384 List<LocalDisk> drives =
new ArrayList<>();
385 FileSystemView fsv = FileSystemView.getFileSystemView();
387 File[] f = File.listRoots();
389 String name = fsv.getSystemDisplayName(f1);
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);
395 drives.add(
new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
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") || name.contains(
"disk")) && f.canRead() && name.length() <= 7) {
405 String path =
"/dev/" + name;
407 drives.add(
new LocalDisk(path, path, f.getTotalSpace()));
431 BufferedInputStream br = null;
433 File tmp =
new File(diskPath);
434 br =
new BufferedInputStream(
new FileInputStream(tmp));
437 }
catch (IOException ex) {
444 }
catch (IOException ex) {
454 public static synchronized long getPID() {
465 pid = sigar.getPid();
467 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.sigarNotInit.msg"));
469 }
catch (Exception e) {
470 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.gen.msg", e.toString()));
486 public static synchronized long getJavaPID(String sigarSubQuery) {
488 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
494 ProcessFinder finder =
new ProcessFinder(sigar);
495 jpid = finder.findSingleProcess(sigarQuery);
497 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.sigarNotInit.msg"));
499 }
catch (Exception e) {
501 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
518 public static synchronized long[]
getJavaPIDs(String sigarSubQuery) {
520 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
526 ProcessFinder finder =
new ProcessFinder(sigar);
527 jpids = finder.find(sigarQuery);
529 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.sigarNotInit"));
531 }
catch (Exception e) {
533 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
552 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.sigarNotInit.msg"));
554 }
catch (Exception e) {
556 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.gen.msg", pid, e.toString()));
574 if (sigar == null ||
getPID() == -1) {
575 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
578 virtMem = sigar.getProcMem(
getPID()).getSize();
579 }
catch (Exception e) {
580 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
593 if (memoryManager == null) {
594 memoryManager = ManagementFactory.getMemoryMXBean();
597 final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
598 final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
601 "PlatformUtil.getJvmMemInfo.usageText",
602 heap.toString(), nonHeap.toString());
611 final Runtime runTime = Runtime.getRuntime();
612 final long maxMemory = runTime.maxMemory();
613 final long totalMemory = runTime.totalMemory();
614 final long freeMemory = runTime.freeMemory();
616 "PlatformUtil.getPhysicalMemInfo.usageText",
617 Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
627 "PlatformUtil.getAllMemUsageInfo.usageText",