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) {
173 File jrePath =
new File(
getInstallPath() + File.separator +
"jre");
174 if (jrePath.exists() && jrePath.isDirectory()) {
177 "PlatformUtil.jrePath.jreDir.msg",
178 jrePath.getAbsolutePath()));
179 javaPath = jrePath.getAbsolutePath() + File.separator +
"bin" + File.separator +
"java";
186 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.jrePath.usingJavaPath.msg", javaPath));
198 return Places.getUserDirectory();
207 List<String> ret =
new ArrayList<>();
208 String projectDir = System.getProperty(
"netbeans.dirs");
209 if (projectDir == null) {
212 String[] split = projectDir.split(
";");
213 if (split == null || split.length == 0) {
216 ret.addAll(Arrays.asList(split));
227 return Places.getUserDirectory() + File.separator +
"config";
236 return Places.getUserDirectory().getAbsolutePath() + File.separator
237 +
"var" + File.separator +
"log" + File.separator;
241 return System.getProperty(
"file.encoding");
245 return Charset.defaultCharset().name();
249 return Charset.forName(
"UTF-8").name();
268 final File resourceFile = resourceFilePath.toFile();
269 if (resourceFile.exists() && !overWrite) {
273 InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
274 if (null == inputStream) {
278 resourceFile.getParentFile().mkdirs();
279 try (InputStream in =
new BufferedInputStream(inputStream)) {
280 try (OutputStream out =
new BufferedOutputStream(
new FileOutputStream(resourceFile))) {
282 while ((readBytes = in.read()) != -1) {
283 out.write(readBytes);
296 return System.getProperty(
"os.name", OS_NAME_UNKNOWN);
305 return System.getProperty(
"os.version", OS_VERSION_UNKNOWN);
314 return System.getProperty(
"os.arch", OS_ARCH_UNKNOWN);
335 return "\"" + origFilePath +
"\"";
349 if (System.getProperty(
"os.name").contains(
"Windows")) {
350 return (System.getenv(
"ProgramFiles(x86)") != null);
352 return (System.getProperty(
"os.arch").contains(
"64"));
363 return (System.getProperty(
"sun.arch.data.model").equals(
"64"));
373 List<LocalDisk> drives =
new ArrayList<>();
379 String path =
"\\\\.\\PhysicalDrive" + n;
382 drives.add(
new LocalDisk(
"Drive " + n, path, SleuthkitJNI.findDeviceSize(path)));
383 }
catch (TskCoreException ex) {
388 if (breakCount > 4) {
397 File dev =
new File(
"/dev/");
398 File[] files = dev.listFiles();
399 for (File f : files) {
400 String name = f.getName();
401 if ((name.contains(
"hd") || name.contains(
"sd") || name.contains(
"disk")) && f.canRead() && name.length() <= 5) {
402 String path =
"/dev/" + name;
405 drives.add(
new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
406 }
catch (TskCoreException ex) {
424 List<LocalDisk> drives =
new ArrayList<>();
425 FileSystemView fsv = FileSystemView.getFileSystemView();
427 File[] f = File.listRoots();
429 String name = fsv.getSystemDisplayName(f1);
431 if (f1.canRead() && !name.contains(
"\\\\") && (fsv.isDrive(f1) || fsv.isFloppyDrive(f1))) {
432 String path = f1.getPath();
433 String diskPath =
"\\\\.\\" + path.substring(0, path.length() - 1);
435 drives.add(
new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
440 File dev =
new File(
"/dev/");
441 File[] files = dev.listFiles();
442 for (File f : files) {
443 String name = f.getName();
444 if ((name.contains(
"hd") || name.contains(
"sd") || name.contains(
"disk")) && f.canRead() && name.length() <= 7) {
445 String path =
"/dev/" + name;
447 drives.add(
new LocalDisk(path, path, f.getTotalSpace()));
471 BufferedInputStream br = null;
473 File tmp =
new File(diskPath);
474 br =
new BufferedInputStream(
new FileInputStream(tmp));
477 }
catch (IOException ex) {
484 }
catch (IOException ex) {
494 public static synchronized long getPID() {
505 pid = sigar.getPid();
507 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.sigarNotInit.msg"));
509 }
catch (Exception e) {
510 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getPID.gen.msg", e.toString()));
526 public static synchronized long getJavaPID(String sigarSubQuery) {
528 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
534 ProcessFinder finder =
new ProcessFinder(sigar);
535 jpid = finder.findSingleProcess(sigarQuery);
537 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.sigarNotInit.msg"));
539 }
catch (Exception e) {
541 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPID.gen.msg", sigarQuery, e.toString()));
558 public static synchronized long[]
getJavaPIDs(String sigarSubQuery) {
560 final String sigarQuery =
"State.Name.sw=java," + sigarSubQuery;
566 ProcessFinder finder =
new ProcessFinder(sigar);
567 jpids = finder.find(sigarQuery);
569 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.sigarNotInit"));
571 }
catch (Exception e) {
573 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getJavaPIDs.gen.msg", sigarQuery, e.toString()));
592 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.sigarNotInit.msg"));
594 }
catch (Exception e) {
596 NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.killProcess.gen.msg", pid, e.toString()));
614 if (sigar == null ||
getPID() == -1) {
615 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.sigarNotInit.msg"));
618 virtMem = sigar.getProcMem(
getPID()).getSize();
619 }
catch (Exception e) {
620 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.getProcVmUsed.gen.msg", e.toString()));
633 if (memoryManager == null) {
634 memoryManager = ManagementFactory.getMemoryMXBean();
637 final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
638 final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
641 "PlatformUtil.getJvmMemInfo.usageText",
642 heap.toString(), nonHeap.toString());
651 final Runtime runTime = Runtime.getRuntime();
652 final long maxMemory = runTime.maxMemory();
653 final long totalMemory = runTime.totalMemory();
654 final long freeMemory = runTime.freeMemory();
656 "PlatformUtil.getPhysicalMemInfo.usageText",
657 Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
667 "PlatformUtil.getAllMemUsageInfo.usageText",