Sleuth Kit Java Bindings (JNI)  4.11.0
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  System.out.println("SleuthkitJNI: loaded " + Lib.TSK_JNI.getLibName()); //NON-NLS
78  }
79  return loaded;
80  }
81 
87  private static String getPlatform() {
88  String os = System.getProperty("os.name").toLowerCase();
89  if (LibraryUtils.isWindows()) {
90  os = "win"; //NON-NLS
91  } else if (LibraryUtils.isMac()) {
92  os = "mac"; //NON-NLS
93  } else if (LibraryUtils.isLinux()) {
94  os = "linux"; //NON-NLS
95  }
96  // os.arch represents the architecture of the JVM, not the os
97  String arch = System.getProperty("os.arch");
98  return arch.toLowerCase() + "/" + os.toLowerCase();
99  }
100 
106  private static boolean isWindows() {
107  return System.getProperty("os.name").toLowerCase().contains("windows"); //NON-NLS
108  }
109 
115  private static boolean isMac() {
116  return System.getProperty("os.name").toLowerCase().contains("mac"); //NON-NLS
117  }
118 
124  private static boolean isLinux() {
125  return System.getProperty("os.name").equals("Linux"); //NON-NLS
126  }
127 
135  private static boolean loadNativeLibFromTskJar(Lib library) {
136  String libName = library.getLibName();
137  String userName = System.getProperty("user.name");
138  // find the library in the jar file
139  StringBuilder pathInJarBase = new StringBuilder();
140  pathInJarBase.append("/NATIVELIBS/"); //NON-NLS
141  pathInJarBase.append(getPlatform());
142  pathInJarBase.append("/");
143  pathInJarBase.append(libName);
144 
145  URL urlInJar = null;
146  String libExt = null;
147  for (String ext : EXTS) {
148  urlInJar = SleuthkitJNI.class.getResource(pathInJarBase.toString() + ext);
149  if (urlInJar != null) {
150  libExt = ext;
151  break;
152  }
153  }
154 
155  if (urlInJar == null) {
156  System.out.println("Library not found in jar (" + libName + ")"); //NON-NLS
157  return false;
158  }
159  StringBuilder pathToTempFile = new StringBuilder();
160  pathToTempFile.append(System.getProperty("java.io.tmpdir"));
161  pathToTempFile.append(java.io.File.separator);
162  pathToTempFile.append(libName);
163  pathToTempFile.append("_");
164  pathToTempFile.append(userName);
165  pathToTempFile.append(libExt);
166  // copy library to temp folder and load it
167  try {
168  java.io.File tempLibFile = new java.io.File(pathToTempFile.toString()); //NON-NLS
169  System.out.println("Temp Folder for Libraries: " + tempLibFile.getParent()); //NON-NLS
170 
171  // cycle through the libraries and delete them.
172  // we used to copy dlls into here.
173  // delete any than may still exist from previous installations.
174  // Dec 2013
175  for (Lib l : Lib.values()) {
176  String ext = getExtByPlatform();
177  // try the windows version
178  java.io.File f = new java.io.File(l.getLibName() + ext);
179  //System.out.println(f.getName());
180  if (f.exists()) {
181  f.delete();
182  } else {
183  // try the unix version
184  java.io.File fUnix = new java.io.File(l.getUnixName() + ext);
185  //System.out.println(fUnix.getName());
186  if (fUnix.exists()) {
187  fUnix.delete();
188  }
189  }
190  }
191 
192  // Delete old file
193  if (tempLibFile.exists()) {
194  if (tempLibFile.delete() == false) {
195  System.out.println("Error deleting old native library. Is the app already running? (" + tempLibFile.toString() + ")"); //NON-NLS
196  return false;
197  }
198  }
199 
200  // copy it
201  InputStream in = urlInJar.openStream();
202  OutputStream out = new FileOutputStream(tempLibFile);
203 
204  byte[] buffer = new byte[1024];
205  int length;
206  while ((length = in.read(buffer)) > 0) {
207  out.write(buffer, 0, length);
208  }
209  in.close();
210  out.close();
211 
212  // load it
213  System.load(tempLibFile.getAbsolutePath());
214  } catch (IOException e) {
215  // Loading failed.
216  System.out.println("Error loading library: " + e.getMessage()); //NON-NLS
217  return false;
218  }
219  return true;
220  }
221 
222  private static String getExtByPlatform() {
223  if (isWindows()) {
224  return ".dll"; //NON-NLS
225  } else if (isMac()) {
226  return ".dylib"; //NON-NLS
227  } else {
228  return ".so"; //NON-NLS
229  }
230  }
231 }
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.