Sleuth Kit Java Bindings (JNI)  4.12.1
Java bindings for using The Sleuth Kit
LibraryUtils.java
Go to the documentation of this file.
1 /*
2  * Sleuth Kit Data Model
3  *
4  * Copyright 2013-2024 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.datamodel;
20 
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.net.URL;
27 
34 public class LibraryUtils {
35 
36  public static final String[] EXTS = new String[]{".so", ".dylib", ".dll", ".jnilib"}; //NON-NLS
37  private static final String TSK_TEMP_OVERRIDE = "tsk.tmpdir";
38  private static final String JAVA_TEMP = "java.io.tmpdir";
39 
43  public enum Lib {
44 
45  MSVCP("msvcp100", ""), //NON-NLS
46  MSVCR("msvcr100", ""), //NON-NLS
47  ZLIB("zlib", "z"), //NON-NLS
48  LIBEWF("libewf", "ewf"), //NON-NLS
49  LIBVMDK("libvmdk", "vmdk"), //NON-NLS
50  LIBVHDI("libvhdi", "vhd"), //NON-NLS
51  TSK_JNI("libtsk_jni", "tsk_jni"); //NON-NLS
52 
53  private final String name;
54  private final String unixName;
55 
56  Lib(String name, String unixName) {
57  this.name = name;
58  this.unixName = unixName;
59  }
60 
61  public String getLibName() {
62  return this.name;
63  }
64 
65  public String getUnixName() {
66  return this.unixName;
67  }
68  }
69 
75  public static boolean loadSleuthkitJNI() {
76  boolean loaded = LibraryUtils.loadNativeLibFromTskJar(Lib.TSK_JNI);
77  if (!loaded) {
78  System.out.println("SleuthkitJNI: failed to load " + Lib.TSK_JNI.getLibName()); //NON-NLS
79  } else {
80  // We want minimal console output for command line use case
81  //System.out.println("SleuthkitJNI: loaded " + Lib.TSK_JNI.getLibName()); //NON-NLS
82  }
83  return loaded;
84  }
85 
91  private static String getPlatform() {
92  String os = System.getProperty("os.name").toLowerCase();
93  if (LibraryUtils.isWindows()) {
94  os = "win"; //NON-NLS
95  } else if (LibraryUtils.isMac()) {
96  os = "mac"; //NON-NLS
97  } else if (LibraryUtils.isLinux()) {
98  os = "linux"; //NON-NLS
99  }
100  // os.arch represents the architecture of the JVM, not the os
101  String arch = System.getProperty("os.arch");
102  return arch.toLowerCase() + "/" + os.toLowerCase();
103  }
104 
110  private static boolean isWindows() {
111  return System.getProperty("os.name").toLowerCase().contains("windows"); //NON-NLS
112  }
113 
119  private static boolean isMac() {
120  return System.getProperty("os.name").toLowerCase().contains("mac"); //NON-NLS
121  }
122 
128  private static boolean isLinux() {
129  return System.getProperty("os.name").equals("Linux"); //NON-NLS
130  }
131 
139  private static boolean loadNativeLibFromTskJar(Lib library) {
140  String libName = library.getLibName();
141  // find the library in the jar file
142  StringBuilder pathInJarBase = new StringBuilder();
143  pathInJarBase.append("/NATIVELIBS/"); //NON-NLS
144  pathInJarBase.append(getPlatform());
145  pathInJarBase.append("/");
146  pathInJarBase.append(libName);
147 
148  URL urlInJar = null;
149  String libExt = null;
150  for (String ext : EXTS) {
151  urlInJar = SleuthkitJNI.class.getResource(pathInJarBase.toString() + ext);
152  if (urlInJar != null) {
153  libExt = ext;
154  break;
155  }
156  }
157 
158  if (urlInJar == null) {
159  System.out.println("Library not found in jar (" + libName + ")"); //NON-NLS
160  return false;
161  }
162 
163  // copy library to temp folder and load it
164  try {
165  String userName = System.getProperty("user.name");
166  String tempDir = System.getProperty(TSK_TEMP_OVERRIDE, System.getProperty(JAVA_TEMP));
167  File tempLibFile = getTempFile(tempDir, libName, userName, libExt);
168 
169  // cycle through the libraries and delete them.
170  // we used to copy dlls into here.
171  // delete any than may still exist from previous installations.
172  // Dec 2013
173  for (Lib l : Lib.values()) {
174  String ext = getExtByPlatform();
175  // try the windows version
176  java.io.File f = new java.io.File(l.getLibName() + ext);
177  //System.out.println(f.getName());
178  if (f.exists()) {
179  f.delete();
180  } else {
181  // try the unix version
182  java.io.File fUnix = new java.io.File(l.getUnixName() + ext);
183  //System.out.println(fUnix.getName());
184  if (fUnix.exists()) {
185  fUnix.delete();
186  }
187  }
188  }
189 
190  // Delete old file
191  if (tempLibFile.exists()) {
192  if (tempLibFile.delete() == false) {
193  System.out.println("Error deleting old native library. Is the app already running? (" + tempLibFile.toString() + ")"); //NON-NLS
194  return false;
195  }
196  }
197 
198  // copy it
199  InputStream in = urlInJar.openStream();
200  OutputStream out = new FileOutputStream(tempLibFile);
201 
202  byte[] buffer = new byte[1024];
203  int length;
204  while ((length = in.read(buffer)) > 0) {
205  out.write(buffer, 0, length);
206  }
207  in.close();
208  out.close();
209 
210  // load it
211  System.load(tempLibFile.getAbsolutePath());
212  } catch (IOException e) {
213  // Loading failed.
214  System.out.println("Error loading library: " + e.getMessage()); //NON-NLS
215  return false;
216  }
217  return true;
218  }
219 
231  static File getTempFile(String tempDir, String libName, String userName, String libExt) {
232  StringBuilder pathToTempFile = new StringBuilder();
233  pathToTempFile.append(tempDir);
234  pathToTempFile.append(java.io.File.separator);
235  pathToTempFile.append(libName);
236  pathToTempFile.append("_");
237  pathToTempFile.append(userName);
238  pathToTempFile.append(libExt);
239  java.io.File tempLibFile = new java.io.File(pathToTempFile.toString()); //NON-NLS
240  // We want minimal console output for command line use case
241  //System.out.println("Temp Folder for Libraries: " + tempLibFile.getParent()); //NON-NLS
242  return tempLibFile;
243  }
244 
251  static String getExtByPlatform() {
252  if (isWindows()) {
253  return ".dll"; //NON-NLS
254  } else if (isMac()) {
255  return ".dylib"; //NON-NLS
256  } else {
257  return ".so"; //NON-NLS
258  }
259  }
260 }
Lib(String name, String unixName)

Copyright © 2011-2024 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.