Sleuth Kit Java Bindings (JNI)  4.3
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() {
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 
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 
160  // copy library to temp folder and load it
161  try {
162  java.io.File tempLibFile = new java.io.File(System.getProperty("java.io.tmpdir") + java.io.File.separator + libName + libExt); //NON-NLS
163  System.out.println("Temp Folder for Libraries: " + tempLibFile.getParent()); //NON-NLS
164 
165  // cycle through the libraries and delete them.
166  // we used to copy dlls into here.
167  // delete any than may still exist from previous installations.
168  // Dec 2013
169  for (Lib l : Lib.values()) {
170  String ext = getExtByPlatform();
171  // try the windows version
172  java.io.File f = new java.io.File(l.getLibName() + ext);
173  //System.out.println(f.getName());
174  if (f.exists()) {
175  f.delete();
176  } else {
177  // try the unix version
178  java.io.File fUnix = new java.io.File(l.getUnixName() + ext);
179  //System.out.println(fUnix.getName());
180  if (fUnix.exists()) {
181  fUnix.delete();
182  }
183  }
184  }
185 
186  // Delete old file
187  if (tempLibFile.exists()) {
188  if (tempLibFile.delete() == false) {
189  System.out.println("Error deleting old native library. Is the app already running? (" + tempLibFile.toString() + ")"); //NON-NLS
190  return false;
191  }
192  }
193 
194  // copy it
195  InputStream in = urlInJar.openStream();
196  OutputStream out = new FileOutputStream(tempLibFile);
197 
198  byte[] buffer = new byte[1024];
199  int length;
200  while ((length = in.read(buffer)) > 0) {
201  out.write(buffer, 0, length);
202  }
203  in.close();
204  out.close();
205 
206  // load it
207  System.load(tempLibFile.getAbsolutePath());
208  } catch (IOException e) {
209  // Loading failed.
210  System.out.println("Error loading library: " + e.getMessage()); //NON-NLS
211  return false;
212  }
213  return true;
214  }
215 
216  private static String getExtByPlatform() {
217  if (isWindows()) {
218  return ".dll"; //NON-NLS
219  } else if (isMac()) {
220  return ".dylib"; //NON-NLS
221  } else {
222  return ".so"; //NON-NLS
223  }
224  }
225 }
Lib(String name, String unixName)
static boolean loadNativeLibFromTskJar(Lib library)

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