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";
239 return Places.getUserDirectory().getAbsolutePath() + File.separator
240 +
"var" + File.separator +
"log" + File.separator;
244 return System.getProperty(
"file.encoding");
248 return Charset.defaultCharset().name();
252 return Charset.forName(
"UTF-8").name();
271 final File resourceFile = resourceFilePath.toFile();
272 if (resourceFile.exists() && !overWrite) {
276 InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
277 if (null == inputStream) {
281 resourceFile.getParentFile().mkdirs();
282 try (InputStream in =
new BufferedInputStream(inputStream)) {
283 try (OutputStream out =
new BufferedOutputStream(
new FileOutputStream(resourceFile))) {
285 while ((readBytes = in.read()) != -1) {
286 out.write(readBytes);
299 return System.getProperty(
"os.name", OS_NAME_UNKNOWN);
308 return System.getProperty(
"os.version", OS_VERSION_UNKNOWN);
317 return System.getProperty(
"os.arch", OS_ARCH_UNKNOWN);
338 return "\"" + origFilePath +
"\"";
352 if (System.getProperty(
"os.name").contains(
"Windows")) {
353 return (System.getenv(
"ProgramFiles(x86)") != null);
355 return (System.getProperty(
"os.arch").contains(
"64"));
366 return (System.getProperty(
"sun.arch.data.model").equals(
"64"));
376 List<LocalDisk> drives =
new ArrayList<>();
382 String path =
"\\\\.\\PhysicalDrive" + n;
385 drives.add(
new LocalDisk(
"Drive " + n, path, SleuthkitJNI.findDeviceSize(path)));
386 }
catch (TskCoreException ex) {
391 if (breakCount > 4) {
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() <= 5) {
405 String path =
"/dev/" + name;
408 drives.add(
new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
409 }
catch (TskCoreException ex) {
427 List<LocalDisk> drives =
new ArrayList<>();
428 FileSystemView fsv = FileSystemView.getFileSystemView();
430 File[] f = File.listRoots();
432 String name = fsv.getSystemDisplayName(f1);
434 if (f1.canRead() && !name.contains(
"\\\\") && (fsv.isDrive(f1) || fsv.isFloppyDrive(f1))) {
435 String path = f1.getPath();
436 String diskPath =
"\\\\.\\" + path.substring(0, path.length() - 1);
438 drives.add(
new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
443 File dev =
new File(
"/dev/");
444 File[] files = dev.listFiles();
445 for (File f : files) {
446 String name = f.getName();
447 if ((name.contains(
"hd") || name.contains(
"sd") || name.contains(
"disk")) && f.canRead() && name.length() <= 7) {
448 String path =
"/dev/" + name;
450 drives.add(
new LocalDisk(path, path, f.getTotalSpace()));
474 BufferedInputStream br = null;
476 File tmp =
new File(diskPath);
477 br =
new BufferedInputStream(
new FileInputStream(tmp));
480 }
catch (IOException ex) {
487 }
catch (IOException ex) {
497 public static synchronized long getPID() {
508 pid = sigar.getPid();
510 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.sigarNotInit.msg"));
512 }
catch (Exception e) {
513 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.gen.msg", e.toString()));
529 public static synchronized long getJavaPID(String sigarSubQuery) {
531 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
537 ProcessFinder finder =
new ProcessFinder(sigar);
538 jpid = finder.findSingleProcess(sigarQuery);
540 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.sigarNotInit.msg"));
542 }
catch (Exception e) {
544 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
561 public static synchronized long[]
getJavaPIDs(String sigarSubQuery) {
563 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
569 ProcessFinder finder =
new ProcessFinder(sigar);
570 jpids = finder.find(sigarQuery);
572 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.sigarNotInit"));
574 }
catch (Exception e) {
576 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
595 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.sigarNotInit.msg"));
597 }
catch (Exception e) {
599 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.gen.msg", pid, e.toString()));
617 if (sigar == null ||
getPID() == -1) {
618 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
621 virtMem = sigar.getProcMem(
getPID()).getSize();
622 }
catch (Exception e) {
623 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
636 if (memoryManager == null) {
637 memoryManager = ManagementFactory.getMemoryMXBean();
640 final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
641 final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
644 "PlatformUtil.getJvmMemInfo.usageText",
645 heap.toString(), nonHeap.toString());
654 final Runtime runTime = Runtime.getRuntime();
655 final long maxMemory = runTime.maxMemory();
656 final long totalMemory = runTime.totalMemory();
657 final long freeMemory = runTime.freeMemory();
659 "PlatformUtil.getPhysicalMemInfo.usageText",
660 Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
670 "PlatformUtil.getAllMemUsageInfo.usageText",