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.apache.commons.io.FilenameUtils;
40 import org.hyperic.sigar.Sigar;
41 import org.hyperic.sigar.ptql.ProcessFinder;
42 import org.openide.modules.InstalledFileLocator;
43 import org.openide.modules.Places;
44 import org.openide.util.NbBundle;
62 private static volatile long pid = -1;
63 private static volatile Sigar
sigar = null;
72 File coreFolder = InstalledFileLocator.getDefault().locate(
"core",
PlatformUtil.class.getPackage().getName(),
false);
73 File rootPath = coreFolder.getParentFile().getParentFile();
74 return rootPath.getAbsolutePath();
84 File coreFolder = InstalledFileLocator.getDefault().locate(
"core",
PlatformUtil.class.getPackage().getName(),
false);
86 File rootPath = coreFolder.getParentFile();
87 String modulesPath = rootPath.getAbsolutePath() + File.separator +
"modules";
88 File modulesPathF =
new File(modulesPath);
89 if (modulesPathF.exists() && modulesPathF.isDirectory()) {
92 rootPath = rootPath.getParentFile();
93 modulesPath = rootPath.getAbsolutePath() + File.separator +
"modules";
94 modulesPathF =
new File(modulesPath);
95 if (modulesPathF.exists() && modulesPathF.isDirectory()) {
140 List<String> languagePacks =
new ArrayList<>();
141 for (File languagePack : languagePackRootDir.listFiles()) {
142 String fileExt = FilenameUtils.getExtension(languagePack.getName());
143 if (!languagePack.isDirectory() && OCR_LANGUAGE_PACK_EXT.equals(fileExt)) {
144 String packageName = FilenameUtils.getBaseName(languagePack.getName());
145 languagePacks.add(packageName);
149 return languagePacks;
169 if (javaPath != null) {
174 File jrePath =
new File(
getInstallPath() + File.separator +
"jre");
175 if (jrePath.exists() && jrePath.isDirectory()) {
178 "PlatformUtil.jrePath.jreDir.msg",
179 jrePath.getAbsolutePath()));
180 javaPath = jrePath.getAbsolutePath() + File.separator +
"bin" + File.separator +
"java";
181 }
else if (System.getProperty(
"java.home") != null && !(System.getProperty(
"java.home").isEmpty())) {
183 return System.getProperty(
"java.home") + File.separator +
"bin" + File.separator +
"java";
189 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.jrePath.usingJavaPath.msg", javaPath));
201 return Places.getUserDirectory();
210 List<String> ret =
new ArrayList<>();
211 String projectDir = System.getProperty(
"netbeans.dirs");
212 if (projectDir == null) {
215 String[] split = projectDir.split(
";");
216 if (split == null || split.length == 0) {
219 ret.addAll(Arrays.asList(split));
230 return Places.getUserDirectory() + File.separator +
"config";
248 return Places.getUserDirectory().getAbsolutePath() + File.separator
249 +
"var" + File.separator +
"log" + File.separator;
253 return System.getProperty(
"file.encoding");
257 return Charset.defaultCharset().name();
261 return Charset.forName(
"UTF-8").name();
280 final File resourceFile = resourceFilePath.toFile();
281 if (resourceFile.exists() && !overWrite) {
285 InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
286 if (null == inputStream) {
290 resourceFile.getParentFile().mkdirs();
291 try (InputStream in =
new BufferedInputStream(inputStream)) {
292 try (OutputStream out =
new BufferedOutputStream(
new FileOutputStream(resourceFile))) {
294 while ((readBytes = in.read()) != -1) {
295 out.write(readBytes);
308 return System.getProperty(
"os.name", OS_NAME_UNKNOWN);
317 return System.getProperty(
"os.version", OS_VERSION_UNKNOWN);
326 return System.getProperty(
"os.arch", OS_ARCH_UNKNOWN);
347 return "\"" + origFilePath +
"\"";
361 if (System.getProperty(
"os.name").contains(
"Windows")) {
362 return (System.getenv(
"ProgramFiles(x86)") != null);
364 return (System.getProperty(
"os.arch").contains(
"64"));
375 return (System.getProperty(
"sun.arch.data.model").equals(
"64"));
385 List<LocalDisk> drives =
new ArrayList<>();
391 String path =
"\\\\.\\PhysicalDrive" + n;
394 drives.add(
new LocalDisk(
"Drive " + n, path, SleuthkitJNI.findDeviceSize(path)));
395 }
catch (TskCoreException ex) {
400 if (breakCount > 4) {
409 File dev =
new File(
"/dev/");
410 File[] files = dev.listFiles();
411 for (File f : files) {
412 String name = f.getName();
413 if ((name.contains(
"hd") || name.contains(
"sd") || name.contains(
"disk")) && f.canRead() && name.length() <= 5) {
414 String path =
"/dev/" + name;
417 drives.add(
new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
418 }
catch (TskCoreException ex) {
436 List<LocalDisk> drives =
new ArrayList<>();
437 FileSystemView fsv = FileSystemView.getFileSystemView();
439 File[] f = File.listRoots();
441 String name = fsv.getSystemDisplayName(f1);
443 if (f1.canRead() && !name.contains(
"\\\\") && (fsv.isDrive(f1) || fsv.isFloppyDrive(f1))) {
444 String path = f1.getPath();
445 String diskPath =
"\\\\.\\" + path.substring(0, path.length() - 1);
447 drives.add(
new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
452 File dev =
new File(
"/dev/");
453 File[] files = dev.listFiles();
454 for (File f : files) {
455 String name = f.getName();
456 if ((name.contains(
"hd") || name.contains(
"sd") || name.contains(
"disk")) && f.canRead() && name.length() <= 7) {
457 String path =
"/dev/" + name;
459 drives.add(
new LocalDisk(path, path, f.getTotalSpace()));
483 BufferedInputStream br = null;
485 File tmp =
new File(diskPath);
486 br =
new BufferedInputStream(
new FileInputStream(tmp));
489 }
catch (IOException ex) {
496 }
catch (IOException ex) {
506 public static synchronized long getPID() {
517 pid = sigar.getPid();
519 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.sigarNotInit.msg"));
521 }
catch (Exception e) {
522 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.gen.msg", e.toString()));
538 public static synchronized long getJavaPID(String sigarSubQuery) {
540 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
546 ProcessFinder finder =
new ProcessFinder(sigar);
547 jpid = finder.findSingleProcess(sigarQuery);
549 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.sigarNotInit.msg"));
551 }
catch (Exception e) {
553 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
570 public static synchronized long[]
getJavaPIDs(String sigarSubQuery) {
572 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
578 ProcessFinder finder =
new ProcessFinder(sigar);
579 jpids = finder.find(sigarQuery);
581 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.sigarNotInit"));
583 }
catch (Exception e) {
585 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
604 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.sigarNotInit.msg"));
606 }
catch (Exception e) {
608 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.gen.msg", pid, e.toString()));
626 if (sigar == null ||
getPID() == -1) {
627 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
630 virtMem = sigar.getProcMem(
getPID()).getSize();
631 }
catch (Exception e) {
632 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
645 if (memoryManager == null) {
646 memoryManager = ManagementFactory.getMemoryMXBean();
649 final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
650 final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
653 "PlatformUtil.getJvmMemInfo.usageText",
654 heap.toString(), nonHeap.toString());
663 final Runtime runTime = Runtime.getRuntime();
664 final long maxMemory = runTime.maxMemory();
665 final long totalMemory = runTime.totalMemory();
666 final long freeMemory = runTime.freeMemory();
668 "PlatformUtil.getPhysicalMemInfo.usageText",
669 Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
679 "PlatformUtil.getAllMemUsageInfo.usageText",