19package org.sleuthkit.autopsy.coreutils;
21import java.io.BufferedInputStream;
22import java.io.BufferedOutputStream;
23import java.io.BufferedReader;
25import java.io.FileInputStream;
26import java.io.FileOutputStream;
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.InputStreamReader;
30import java.io.OutputStream;
31import java.lang.management.ManagementFactory;
32import java.lang.management.MemoryMXBean;
33import java.lang.management.MemoryUsage;
34import java.nio.charset.Charset;
35import java.nio.charset.StandardCharsets;
36import java.nio.file.Path;
37import java.nio.file.Paths;
38import java.util.ArrayList;
39import java.util.Arrays;
42import java.util.regex.Pattern;
43import java.util.stream.Stream;
44import javax.swing.filechooser.FileSystemView;
45import org.apache.commons.io.FilenameUtils;
46import org.openide.modules.InstalledFileLocator;
47import org.openide.modules.Places;
48import org.openide.util.NbBundle;
49import org.sleuthkit.datamodel.SleuthkitJNI;
50import org.sleuthkit.datamodel.TskCoreException;
66 private static volatile long pid = -1;
75 File coreFolder = InstalledFileLocator.getDefault().locate(
"core",
PlatformUtil.class.getPackage().getName(),
false);
76 File rootPath = coreFolder.getParentFile().getParentFile();
77 return rootPath.getAbsolutePath();
87 File coreFolder = InstalledFileLocator.getDefault().locate(
"core",
PlatformUtil.class.getPackage().getName(),
false);
89 File rootPath = coreFolder.getParentFile();
90 String modulesPath = rootPath.getAbsolutePath() + File.separator +
"modules";
91 File modulesPathF =
new File(modulesPath);
92 if (modulesPathF.exists() && modulesPathF.isDirectory()) {
95 rootPath = rootPath.getParentFile();
96 modulesPath = rootPath.getAbsolutePath() + File.separator +
"modules";
97 modulesPathF =
new File(modulesPath);
98 if (modulesPathF.exists() && modulesPathF.isDirectory()) {
143 List<String> languagePacks =
new ArrayList<>();
144 for (File languagePack : languagePackRootDir.listFiles()) {
145 String fileExt = FilenameUtils.getExtension(languagePack.getName());
147 String packageName = FilenameUtils.getBaseName(languagePack.getName());
148 languagePacks.add(packageName);
152 return languagePacks;
177 File jrePath =
new File(
getInstallPath() + File.separator +
"jre");
178 if (jrePath.exists() && jrePath.isDirectory()) {
181 "PlatformUtil.jrePath.jreDir.msg",
182 jrePath.getAbsolutePath()));
183 javaPath = jrePath.getAbsolutePath() + File.separator +
"bin" + File.separator +
"java";
184 }
else if (System.getProperty(
"java.home") !=
null && !(System.getProperty(
"java.home").isEmpty())) {
186 return System.getProperty(
"java.home") + File.separator +
"bin" + File.separator +
"java";
192 System.out.println(NbBundle.getMessage(
PlatformUtil.class,
"PlatformUtil.jrePath.usingJavaPath.msg",
javaPath));
204 return Places.getUserDirectory();
213 List<String> ret =
new ArrayList<>();
214 String projectDir = System.getProperty(
"netbeans.dirs");
215 if (projectDir ==
null) {
218 String[] split = projectDir.split(
";");
219 if (split ==
null || split.length == 0) {
222 ret.addAll(Arrays.asList(split));
233 return Places.getUserDirectory() + File.separator +
"config";
251 return Places.getUserDirectory().getAbsolutePath() + File.separator
252 +
"var" + File.separator +
"log" + File.separator;
256 return System.getProperty(
"file.encoding");
260 return Charset.defaultCharset().name();
264 return Charset.forName(
"UTF-8").name();
283 final File resourceFile = resourceFilePath.toFile();
284 if (resourceFile.exists() && !overWrite) {
288 InputStream inputStream = resourceClass.getResourceAsStream(resourceFileName);
289 if (
null == inputStream) {
293 resourceFile.getParentFile().mkdirs();
294 try (InputStream in =
new BufferedInputStream(inputStream)) {
295 try (OutputStream out =
new BufferedOutputStream(
new FileOutputStream(resourceFile))) {
297 while ((readBytes = in.read()) != -1) {
298 out.write(readBytes);
368 return "\"" + origFilePath +
"\"";
382 if (System.getProperty(
"os.name").contains(
"Windows")) {
383 return (System.getenv(
"ProgramFiles(x86)") !=
null);
385 return (System.getProperty(
"os.arch").contains(
"64"));
396 return (System.getProperty(
"sun.arch.data.model").equals(
"64"));
406 List<LocalDisk> drives =
new ArrayList<>();
412 String path =
"\\\\.\\PhysicalDrive" + n;
415 drives.add(
new LocalDisk(
"Drive " + n, path, SleuthkitJNI.findDeviceSize(path)));
416 }
catch (TskCoreException ex) {
421 if (breakCount > 4) {
430 File dev =
new File(
"/dev/");
431 File[] files = dev.listFiles();
432 for (File f : files) {
433 String name = f.getName();
434 if ((name.contains(
"hd") || name.contains(
"sd") || name.contains(
"disk")) && f.canRead() && name.length() <= 5) {
435 String path =
"/dev/" + name;
438 drives.add(
new LocalDisk(path, path, SleuthkitJNI.findDeviceSize(path)));
439 }
catch (TskCoreException ex) {
457 List<LocalDisk> drives =
new ArrayList<>();
458 FileSystemView fsv = FileSystemView.getFileSystemView();
460 File[] f = File.listRoots();
462 String name = fsv.getSystemDisplayName(f1);
464 if (f1.canRead() && !name.contains(
"\\\\") && (fsv.isDrive(f1) || fsv.isFloppyDrive(f1))) {
465 String path = f1.getPath();
466 String diskPath =
"\\\\.\\" + path.substring(0, path.length() - 1);
468 drives.add(
new LocalDisk(fsv.getSystemDisplayName(f1), diskPath, f1.getTotalSpace()));
473 File dev =
new File(
"/dev/");
474 File[] files = dev.listFiles();
475 for (File f : files) {
476 String name = f.getName();
477 if ((name.contains(
"hd") || name.contains(
"sd") || name.contains(
"disk")) && f.canRead() && name.length() <= 7) {
478 String path =
"/dev/" + name;
480 drives.add(
new LocalDisk(path, path, f.getTotalSpace()));
504 BufferedInputStream br =
null;
506 File tmp =
new File(diskPath);
507 br =
new BufferedInputStream(
new FileInputStream(tmp));
510 }
catch (IOException ex) {
517 }
catch (IOException ex) {
527 public static synchronized long getPID() {
529 return ProcessHandle.current().pid();
542 public static synchronized long getJavaPID(String sigarSubQuery) {
544 return pids ==
null || pids.length < 1
557 if (originalLikeStatement ==
null) {
566 StringBuilder regex =
new StringBuilder();
567 StringBuilder literal =
new StringBuilder();
569 char[] chars = originalLikeStatement.toCharArray();
570 for (
int i = 0; i < chars.length; i++) {
572 if (cur ==
'%' || cur ==
'_') {
574 if (i + 1 < chars.length && chars[i + 1] == cur) {
578 if (literal.length() > 0) {
579 regex.append(Pattern.quote(literal.toString()));
580 literal.setLength(0);
582 regex.append(cur ==
'%' ?
".*" :
".");
589 if (literal.length() > 0) {
590 regex.append(Pattern.quote(literal.toString()));
593 return regex.toString();
604 public static synchronized long[]
getJavaPIDs(String argsSubQuery) {
622 Pattern pattern = Pattern.compile(regexStr, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
623 return ProcessHandle.allProcesses()
624 .filter(ph -> ph.info().commandLine()
625 .map(cmd -> pattern.matcher(cmd).matches())
627 .mapToLong(ProcessHandle::pid)
629 }
catch (Exception ex) {
630 System.out.println(
"An exception occurred while fetching java pids with query: " + argsSubQuery +
" : " + ex.getMessage());
642 ?
"taskkill /F /PID " +
pid
646 Runtime.getRuntime().exec(cmd);
647 }
catch (IOException ex) {
648 System.out.println(
"An exception occurred while killing process pid: " +
pid);
649 ex.printStackTrace();
660 return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
675 final MemoryUsage nonHeap =
memoryManager.getNonHeapMemoryUsage();
678 "PlatformUtil.getJvmMemInfo.usageText",
679 heap.toString(), nonHeap.toString());
688 final Runtime runTime = Runtime.getRuntime();
689 final long maxMemory = runTime.maxMemory();
690 final long totalMemory = runTime.totalMemory();
691 final long freeMemory = runTime.freeMemory();
693 "PlatformUtil.getPhysicalMemInfo.usageText",
694 Long.toString(maxMemory), Long.toString(totalMemory), Long.toString(freeMemory));
704 "PlatformUtil.getAllMemUsageInfo.usageText",