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 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.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.net.URL;
26 
33 public class LibraryUtils {
34 
35  public static final String[] EXTS = new String[]{".so", ".dylib", ".dll", ".jnilib"}; //NON-NLS
36 
40  public enum Lib {
41 
42  MSVCP("msvcp100", ""), //NON-NLS
43  MSVCR("msvcr100", ""), //NON-NLS
44  ZLIB("zlib", "z"), //NON-NLS
45  LIBEWF("libewf", "ewf"), //NON-NLS
46  LIBVMDK("libvmdk", "vmdk"), //NON-NLS
47  LIBVHDI("libvhdi", "vhd"), //NON-NLS
48  TSK_JNI("libtsk_jni", "tsk_jni"); //NON-NLS
49 
50  private final String name;
51  private final String unixName;
52 
53  Lib(String name, String unixName) {
54  this.name = name;
55  this.unixName = unixName;
56  }
57 
58  public String getLibName() {
59  return this.name;
60  }
61 
62  public String getUnixName() {
63  return this.unixName;
64  }
65  }
66 
72  public static boolean loadSleuthkitJNI() {
73  boolean loaded = LibraryUtils.loadNativeLibFromTskJar(Lib.TSK_JNI);
74  if (!loaded) {
75  System.out.println("SleuthkitJNI: failed to load " + Lib.TSK_JNI.getLibName()); //NON-NLS
76  } else {
77  // We want minimal console output for command line use case
78  //System.out.println("SleuthkitJNI: loaded " + Lib.TSK_JNI.getLibName()); //NON-NLS
79  }
80  return loaded;
81  }
82 
88  private static String getPlatform() {
89  String os = System.getProperty("os.name").toLowerCase();
90  if (LibraryUtils.isWindows()) {
91  os = "win"; //NON-NLS
92  } else if (LibraryUtils.isMac()) {
93  os = "mac"; //NON-NLS
94  } else if (LibraryUtils.isLinux()) {
95  os = "linux"; //NON-NLS
96  }
97  // os.arch represents the architecture of the JVM, not the os
98  String arch = System.getProperty("os.arch");
99  return arch.toLowerCase() + "/" + os.toLowerCase();
100  }
101 
107  private static boolean isWindows() {
108  return System.getProperty("os.name").toLowerCase().contains("windows"); //NON-NLS
109  }
110 
116  private static boolean isMac() {
117  return System.getProperty("os.name").toLowerCase().contains("mac"); //NON-NLS
118  }
119 
125  private static boolean isLinux() {
126  return System.getProperty("os.name").equals("Linux"); //NON-NLS
127  }
128 
136  private static boolean loadNativeLibFromTskJar(Lib library) {
137  String libName = library.getLibName();
138  String userName = System.getProperty("user.name");
139  // find the library in the jar file
140  StringBuilder pathInJarBase = new StringBuilder();
141  pathInJarBase.append("/NATIVELIBS/"); //NON-NLS
142  pathInJarBase.append(getPlatform());
143  pathInJarBase.append("/");
144  pathInJarBase.append(libName);
145 
146  URL urlInJar = null;
147  String libExt = null;
148  for (String ext : EXTS) {
149  urlInJar = SleuthkitJNI.class.getResource(pathInJarBase.toString() + ext);
150  if (urlInJar != null) {
151  libExt = ext;
152  break;
153  }
154  }
155 
156  if (urlInJar == null) {
157  System.out.println("Library not found in jar (" + libName + ")"); //NON-NLS
158  return false;
159  }
160  StringBuilder pathToTempFile = new StringBuilder();
161  pathToTempFile.append(System.getProperty("java.io.tmpdir"));
162  pathToTempFile.append(java.io.File.separator);
163  pathToTempFile.append(libName);
164  pathToTempFile.append("_");
165  pathToTempFile.append(userName);
166  pathToTempFile.append(libExt);
167  // copy library to temp folder and load it
168  try {
169  java.io.File tempLibFile = new java.io.File(pathToTempFile.toString()); //NON-NLS
170  // We want minimal console output for command line use case
171  //System.out.println("Temp Folder for Libraries: " + tempLibFile.getParent()); //NON-NLS
172 
173  // cycle through the libraries and delete them.
174  // we used to copy dlls into here.
175  // delete any than may still exist from previous installations.
176  // Dec 2013
177  for (Lib l : Lib.values()) {
178  String ext = getExtByPlatform();
179  // try the windows version
180  java.io.File f = new java.io.File(l.getLibName() + ext);
181  //System.out.println(f.getName());
182  if (f.exists()) {
183  f.delete();
184  } else {
185  // try the unix version
186  java.io.File fUnix = new java.io.File(l.getUnixName() + ext);
187  //System.out.println(fUnix.getName());
188  if (fUnix.exists()) {
189  fUnix.delete();
190  }
191  }
192  }
193 
194  // Delete old file
195  if (tempLibFile.exists()) {
196  if (tempLibFile.delete() == false) {
197  System.out.println("Error deleting old native library. Is the app already running? (" + tempLibFile.toString() + ")"); //NON-NLS
198  return false;
199  }
200  }
201 
202  // copy it
203  InputStream in = urlInJar.openStream();
204  OutputStream out = new FileOutputStream(tempLibFile);
205 
206  byte[] buffer = new byte[1024];
207  int length;
208  while ((length = in.read(buffer)) > 0) {
209  out.write(buffer, 0, length);
210  }
211  in.close();
212  out.close();
213 
214  // load it
215  System.load(tempLibFile.getAbsolutePath());
216  } catch (IOException e) {
217  // Loading failed.
218  System.out.println("Error loading library: " + e.getMessage()); //NON-NLS
219  return false;
220  }
221  return true;
222  }
223 
224  private static String getExtByPlatform() {
225  if (isWindows()) {
226  return ".dll"; //NON-NLS
227  } else if (isMac()) {
228  return ".dylib"; //NON-NLS
229  } else {
230  return ".so"; //NON-NLS
231  }
232  }
233 }
Lib(String name, String unixName)

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