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;
59 private static volatile long pid = -1;
60 private static volatile Sigar
sigar = null;
69 File coreFolder = InstalledFileLocator.getDefault().locate(
"core",
PlatformUtil.class.getPackage().getName(),
false);
70 File rootPath = coreFolder.getParentFile().getParentFile();
71 return rootPath.getAbsolutePath();
81 File coreFolder = InstalledFileLocator.getDefault().locate(
"core",
PlatformUtil.class.getPackage().getName(),
false);
83 File rootPath = coreFolder.getParentFile();
84 String modulesPath = rootPath.getAbsolutePath() + File.separator +
"modules";
85 File modulesPathF =
new File(modulesPath);
86 if (modulesPathF.exists() && modulesPathF.isDirectory()) {
89 rootPath = rootPath.getParentFile();
90 modulesPath = rootPath.getAbsolutePath() + File.separator +
"modules";
91 modulesPathF =
new File(modulesPath);
92 if (modulesPathF.exists() && modulesPathF.isDirectory()) {
137 if (javaPath != null) {
141 File jrePath =
new File(
getInstallPath() + File.separator +
"jre");
142 if (jrePath.exists() && jrePath.isDirectory()) {
145 "PlatformUtil.jrePath.jreDir.msg",
146 jrePath.getAbsolutePath()));
147 javaPath = jrePath.getAbsolutePath() + File.separator +
"bin" + File.separator +
"java";
154 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.jrePath.usingJavaPath.msg", javaPath));
166 return Places.getUserDirectory();
175 List<String> ret =
new ArrayList<>();
176 String projectDir = System.getProperty(
"netbeans.dirs");
177 if (projectDir == null) {
180 String[] split = projectDir.split(
";");
181 if (split == null || split.length == 0) {
184 ret.addAll(Arrays.asList(split));
195 return Places.getUserDirectory() + File.separator +
"config";
204 return Places.getUserDirectory().getAbsolutePath() + File.separator
205 +
"var" + File.separator +
"log" + File.separator;
209 return System.getProperty(
"file.encoding");
213 return Charset.defaultCharset().name();
217 return Charset.forName(
"UTF-8").name();
236 final File resourceFile = resourceFilePath.toFile();
237 if (resourceFile.exists() && !overWrite) {
241 InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
242 if (null == inputStream) {
246 resourceFile.getParentFile().mkdirs();
247 try (InputStream in =
new BufferedInputStream(inputStream)) {
248 try (OutputStream out =
new BufferedOutputStream(
new FileOutputStream(resourceFile))) {
250 while ((readBytes = in.read()) != -1) {
251 out.write(readBytes);
264 return System.getProperty(
"os.name", OS_NAME_UNKNOWN);
273 return System.getProperty(
"os.version", OS_VERSION_UNKNOWN);
282 return System.getProperty(
"os.arch", OS_ARCH_UNKNOWN);
303 return "\"" + origFilePath +
"\"";
317 if (System.getProperty(
"os.name").contains(
"Windows")) {
318 return (System.getenv(
"ProgramFiles(x86)") != null);
320 return (System.getProperty(
"os.arch").contains(
"64"));
331 return (System.getProperty(
"sun.arch.data.model").equals(
"64"));
341 List<LocalDisk> drives =
new ArrayList<>();
347 String path =
"\\\\.\\PhysicalDrive" + n;
350 drives.add(
new LocalDisk(
"Drive " + n, path, SleuthkitJNI.findDeviceSize(path)));
351 }
catch (TskCoreException ex) {
356 if (breakCount > 4) {
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) {
370 String path =
"/dev/" + name;
373 drives.add(
new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
374 }
catch (TskCoreException ex) {
392 List<LocalDisk> drives =
new ArrayList<>();
393 FileSystemView fsv = FileSystemView.getFileSystemView();
395 File[] f = File.listRoots();
397 String name = fsv.getSystemDisplayName(f1);
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);
403 drives.add(
new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
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) {
413 String path =
"/dev/" + name;
415 drives.add(
new LocalDisk(path, path, f.getTotalSpace()));
439 BufferedInputStream br = null;
441 File tmp =
new File(diskPath);
442 br =
new BufferedInputStream(
new FileInputStream(tmp));
445 }
catch (IOException ex) {
452 }
catch (IOException ex) {
462 public static synchronized long getPID() {
473 pid = sigar.getPid();
475 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.sigarNotInit.msg"));
477 }
catch (Exception e) {
478 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.gen.msg", e.toString()));
494 public static synchronized long getJavaPID(String sigarSubQuery) {
496 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
502 ProcessFinder finder =
new ProcessFinder(sigar);
503 jpid = finder.findSingleProcess(sigarQuery);
505 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.sigarNotInit.msg"));
507 }
catch (Exception e) {
509 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
526 public static synchronized long[]
getJavaPIDs(String sigarSubQuery) {
528 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
534 ProcessFinder finder =
new ProcessFinder(sigar);
535 jpids = finder.find(sigarQuery);
537 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.sigarNotInit"));
539 }
catch (Exception e) {
541 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
560 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.sigarNotInit.msg"));
562 }
catch (Exception e) {
564 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.gen.msg", pid, e.toString()));
582 if (sigar == null ||
getPID() == -1) {
583 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
586 virtMem = sigar.getProcMem(
getPID()).getSize();
587 }
catch (Exception e) {
588 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
601 if (memoryManager == null) {
602 memoryManager = ManagementFactory.getMemoryMXBean();
605 final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
606 final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
609 "PlatformUtil.getJvmMemInfo.usageText",
610 heap.toString(), nonHeap.toString());
619 final Runtime runTime = Runtime.getRuntime();
620 final long maxMemory = runTime.maxMemory();
621 final long totalMemory = runTime.totalMemory();
622 final long freeMemory = runTime.freeMemory();
624 "PlatformUtil.getPhysicalMemInfo.usageText",
625 Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
635 "PlatformUtil.getAllMemUsageInfo.usageText",