19 package org.sleuthkit.autopsy.coreutils;
21 import java.io.BufferedInputStream;
22 import java.io.BufferedOutputStream;
23 import java.io.BufferedReader;
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.io.OutputStream;
31 import java.lang.management.ManagementFactory;
32 import java.lang.management.MemoryMXBean;
33 import java.lang.management.MemoryUsage;
34 import java.nio.charset.Charset;
35 import java.nio.charset.StandardCharsets;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.HashMap;
41 import java.util.List;
43 import java.util.regex.Pattern;
44 import java.util.stream.Stream;
45 import javax.swing.filechooser.FileSystemView;
46 import org.apache.commons.io.FilenameUtils;
47 import org.apache.commons.io.IOUtils;
48 import org.openide.modules.InstalledFileLocator;
49 import org.openide.modules.Places;
50 import org.openide.util.NbBundle;
68 private static volatile long pid = -1;
77 File coreFolder = InstalledFileLocator.getDefault().locate(
"core",
PlatformUtil.class.getPackage().getName(),
false);
78 File rootPath = coreFolder.getParentFile().getParentFile();
79 return rootPath.getAbsolutePath();
89 File coreFolder = InstalledFileLocator.getDefault().locate(
"core",
PlatformUtil.class.getPackage().getName(),
false);
91 File rootPath = coreFolder.getParentFile();
92 String modulesPath = rootPath.getAbsolutePath() + File.separator +
"modules";
93 File modulesPathF =
new File(modulesPath);
94 if (modulesPathF.exists() && modulesPathF.isDirectory()) {
97 rootPath = rootPath.getParentFile();
98 modulesPath = rootPath.getAbsolutePath() + File.separator +
"modules";
99 modulesPathF =
new File(modulesPath);
100 if (modulesPathF.exists() && modulesPathF.isDirectory()) {
145 List<String> languagePacks =
new ArrayList<>();
146 for (File languagePack : languagePackRootDir.listFiles()) {
147 String fileExt = FilenameUtils.getExtension(languagePack.getName());
148 if (!languagePack.isDirectory() && OCR_LANGUAGE_PACK_EXT.equals(fileExt)) {
149 String packageName = FilenameUtils.getBaseName(languagePack.getName());
150 languagePacks.add(packageName);
154 return languagePacks;
174 if (javaPath != null) {
179 File jrePath =
new File(
getInstallPath() + File.separator +
"jre");
180 if (jrePath.exists() && jrePath.isDirectory()) {
183 "PlatformUtil.jrePath.jreDir.msg",
184 jrePath.getAbsolutePath()));
185 javaPath = jrePath.getAbsolutePath() + File.separator +
"bin" + File.separator +
"java";
186 }
else if (System.getProperty(
"java.home") != null && !(System.getProperty(
"java.home").isEmpty())) {
188 return System.getProperty(
"java.home") + File.separator +
"bin" + File.separator +
"java";
194 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.jrePath.usingJavaPath.msg", javaPath));
206 return Places.getUserDirectory();
215 List<String> ret =
new ArrayList<>();
216 String projectDir = System.getProperty(
"netbeans.dirs");
217 if (projectDir == null) {
220 String[] split = projectDir.split(
";");
221 if (split == null || split.length == 0) {
224 ret.addAll(Arrays.asList(split));
235 return Places.getUserDirectory() + File.separator +
"config";
253 return Places.getUserDirectory().getAbsolutePath() + File.separator
254 +
"var" + File.separator +
"log" + File.separator;
258 return System.getProperty(
"file.encoding");
262 return Charset.defaultCharset().name();
266 return Charset.forName(
"UTF-8").name();
285 final File resourceFile = resourceFilePath.toFile();
286 if (resourceFile.exists() && !overWrite) {
290 InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
291 if (null == inputStream) {
295 resourceFile.getParentFile().mkdirs();
296 try (InputStream in =
new BufferedInputStream(inputStream)) {
297 try (OutputStream out =
new BufferedOutputStream(
new FileOutputStream(resourceFile))) {
299 while ((readBytes = in.read()) != -1) {
300 out.write(readBytes);
313 return System.getProperty(
"os.name", OS_NAME_UNKNOWN);
322 return System.getProperty(
"os.version", OS_VERSION_UNKNOWN);
331 return System.getProperty(
"os.arch", OS_ARCH_UNKNOWN);
352 return "\"" + origFilePath +
"\"";
366 if (System.getProperty(
"os.name").contains(
"Windows")) {
367 return (System.getenv(
"ProgramFiles(x86)") != null);
369 return (System.getProperty(
"os.arch").contains(
"64"));
380 return (System.getProperty(
"sun.arch.data.model").equals(
"64"));
390 List<LocalDisk> drives =
new ArrayList<>();
396 String path =
"\\\\.\\PhysicalDrive" + n;
399 drives.add(
new LocalDisk(
"Drive " + n, path, SleuthkitJNI.findDeviceSize(path)));
400 }
catch (TskCoreException ex) {
405 if (breakCount > 4) {
414 File dev =
new File(
"/dev/");
415 File[] files = dev.listFiles();
416 for (File f : files) {
417 String name = f.getName();
418 if ((name.contains(
"hd") || name.contains(
"sd") || name.contains(
"disk")) && f.canRead() && name.length() <= 5) {
419 String path =
"/dev/" + name;
422 drives.add(
new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
423 }
catch (TskCoreException ex) {
441 List<LocalDisk> drives =
new ArrayList<>();
442 FileSystemView fsv = FileSystemView.getFileSystemView();
444 File[] f = File.listRoots();
446 String name = fsv.getSystemDisplayName(f1);
448 if (f1.canRead() && !name.contains(
"\\\\") && (fsv.isDrive(f1) || fsv.isFloppyDrive(f1))) {
449 String path = f1.getPath();
450 String diskPath =
"\\\\.\\" + path.substring(0, path.length() - 1);
452 drives.add(
new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
457 File dev =
new File(
"/dev/");
458 File[] files = dev.listFiles();
459 for (File f : files) {
460 String name = f.getName();
461 if ((name.contains(
"hd") || name.contains(
"sd") || name.contains(
"disk")) && f.canRead() && name.length() <= 7) {
462 String path =
"/dev/" + name;
464 drives.add(
new LocalDisk(path, path, f.getTotalSpace()));
488 BufferedInputStream br = null;
490 File tmp =
new File(diskPath);
491 br =
new BufferedInputStream(
new FileInputStream(tmp));
494 }
catch (IOException ex) {
501 }
catch (IOException ex) {
511 public static synchronized long getPID() {
513 return ProcessHandle.current().pid();
526 public static synchronized long getJavaPID(String sigarSubQuery) {
528 return pids == null || pids.length < 1
541 if (originalLikeStatement == null) {
545 Map<Character, String> likeEscapeSequences =
new HashMap<>() {
552 String regexQuoted = Pattern.quote(originalLikeStatement);
553 char[] charArr = regexQuoted.toCharArray();
554 StringBuilder sb =
new StringBuilder();
556 for (
int i = 0; i < charArr.length; i++) {
557 char curChar = charArr[i];
558 String regexReplacement = likeEscapeSequences.get(curChar);
559 if (regexReplacement == null) {
562 Character nextChar = charArr.length > i + 1 ? charArr[i + 1] : null;
563 if (nextChar != null && curChar == nextChar) {
567 sb.append(regexReplacement);
572 return sb.toString();
583 public static synchronized long[]
getJavaPIDs(String argsSubQuery) {
587 ProcessBuilder pb =
new ProcessBuilder(
"wmic process where \"name='java.exe' AND commandline LIKE '%" + argsSubQuery +
"%'\" get ProcessID");
588 String output = IOUtils.toString(pb.start().getInputStream(), StandardCharsets.UTF_8);
589 String[] lines = output.split(
"\\r?\\n");
591 return Stream.of(lines).skip(1).map(ln -> {
592 if (ln == null || ln.trim().isEmpty()) {
597 return Long.parseLong(ln.trim());
598 }
catch (NumberFormatException ex) {
602 .filter(num -> num != null)
608 ProcessBuilder pb =
new ProcessBuilder(
"sh",
"-c",
"ps -ef | grep -E 'java.*" + sigarRegexQuery +
".*'");
609 String output = IOUtils.toString(pb.start().getInputStream(), StandardCharsets.UTF_8);
610 List<String> lines = Arrays.asList(output.split(
"\\r?\\n"));
612 if (lines.size() > 0) {
614 lines.remove(lines.size() - 1);
617 return lines.stream().skip(1).map(ln -> {
618 if (ln == null || ln.trim().isEmpty()) {
624 String[] pieces = ln.split(
"\\s*");
625 if (pieces.length < 2) {
630 return Long.parseLong(pieces[1]);
631 }
catch (NumberFormatException ex) {
635 .filter(num -> num != null)
639 }
catch (IOException ex) {
640 System.out.println(
"An exception occurred while fetching java pids with query: " + argsSubQuery +
" with IO Exception: " + ex.getMessage());
641 ex.printStackTrace();
653 ?
"taskkill /F /PID " + pid
657 Runtime.getRuntime().exec(cmd);
658 }
catch (IOException ex) {
659 System.out.println(
"An exception occurred while killing process pid: " + pid);
660 ex.printStackTrace();
671 return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
681 if (memoryManager == null) {
682 memoryManager = ManagementFactory.getMemoryMXBean();
685 final MemoryUsage heap = memoryManager.getHeapMemoryUsage();
686 final MemoryUsage nonHeap = memoryManager.getNonHeapMemoryUsage();
689 "PlatformUtil.getJvmMemInfo.usageText",
690 heap.toString(), nonHeap.toString());
699 final Runtime runTime = Runtime.getRuntime();
700 final long maxMemory = runTime.maxMemory();
701 final long totalMemory = runTime.totalMemory();
702 final long freeMemory = runTime.freeMemory();
704 "PlatformUtil.getPhysicalMemInfo.usageText",
705 Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
715 "PlatformUtil.getAllMemUsageInfo.usageText",