Sleuth Kit Java Bindings (JNI)  4.12.1
Java bindings for using The Sleuth Kit
FileSystem.java
Go to the documentation of this file.
1 /*
2  * Sleuth Kit Data Model
3  *
4  * Copyright 2011-2017 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.util.List;
22 import java.util.Map;
23 import org.apache.commons.lang3.ArrayUtils;
24 import org.apache.commons.lang3.StringUtils;
25 import com.google.gson.Gson;
26 import com.google.gson.JsonSyntaxException;
27 import static org.sleuthkit.datamodel.SleuthkitCase.IMAGE_PASSWORD_KEY;
28 
36 public class FileSystem extends AbstractContent {
37 
38  private long imgOffset, blockSize, blockCount, rootInum,
39  firstInum, lastInum;
40  private TskData.TSK_FS_TYPE_ENUM fsType;
41  private Content parent;
42  private volatile long filesystemHandle = 0;
43 
58  protected FileSystem(SleuthkitCase db, long obj_id, String name, long img_offset,
59  TskData.TSK_FS_TYPE_ENUM fs_type, long block_size, long block_count, long root_inum,
60  long first_inum, long last_inum) {
61  super(db, obj_id, name);
62  this.imgOffset = img_offset;
63  this.fsType = fs_type;
64  this.blockSize = block_size;
65  this.blockCount = block_count;
66  this.rootInum = root_inum;
67  this.firstInum = first_inum;
68  this.lastInum = last_inum;
69  }
70 
71  @Override
72  public void close() {
73  //does nothing currently, we are caching the fs handles
74  }
75 
76  @Override
77  public int read(byte[] buf, long offset, long len) throws TskCoreException {
78  Content dataSource = getDataSource();
79  if (dataSource instanceof Image && ArrayUtils.isEmpty(((Image) dataSource).getPaths())) {
80  return 0;
81  }
82  return SleuthkitJNI.readFs(getFileSystemHandle(), buf, offset, len);
83  }
84 
85  @Override
86  public long getSize() {
87  return blockSize * blockCount;
88  }
89 
99  long getFileSystemHandle() throws TskCoreException {
100  if (filesystemHandle == 0) {
101  synchronized (this) {
102  if (filesystemHandle == 0) {
103  Content dataSource = getDataSource();
104  if ((dataSource == null) || ( !(dataSource instanceof Image))) {
105  throw new TskCoreException("Data Source of File System is not an image");
106  }
107 
108  Image image = (Image) dataSource;
109 
110  // Check if this file system is in a pool
111  if (isPoolContent()) {
112  Pool pool = getPool();
113  if (pool == null) {
114  throw new TskCoreException("Error finding pool for file system");
115  }
116 
117  Volume poolVolume = getPoolVolume();
118  if (poolVolume == null) {
119  throw new TskCoreException("File system is in a pool but has no volume");
120  }
121  filesystemHandle = SleuthkitJNI.openFsPool(image.getImageHandle(), imgOffset, pool.getPoolHandle(), poolVolume.getStart(), getSleuthkitCase());
122  } else {
123  String password = getImagePasswordFromSettings(image.getAcquisitionToolSettings());
124  filesystemHandle = SleuthkitJNI.openFs(image.getImageHandle(), imgOffset, password, getSleuthkitCase());
125  }
126  }
127  }
128  }
129  return this.filesystemHandle;
130  }
131 
139  private String getImagePasswordFromSettings(String settingsStr) {
140 
141  if(StringUtils.isBlank(settingsStr)){
142  return "";
143  }
144 
145  try {
146  Map<String, Object> settingsMap = (new Gson()).fromJson(settingsStr, Map.class);
147  return (String)settingsMap.getOrDefault(IMAGE_PASSWORD_KEY, "");
148  } catch (JsonSyntaxException ex) {
149  // There's no guarantee that acquisition settings will contain a valid JSON string
150  return "";
151  }
152  }
153 
154  public Directory getRootDirectory() throws TskCoreException {
155 
156  List<Content> children = getChildren();
157  if (children.size() != 1) {
158  throw new TskCoreException("FileSystem must have only one child.");
159  }
160 
161  if (!(children.get(0) instanceof Directory)) {
162  throw new TskCoreException("Child of FileSystem must be a Directory.");
163  }
164 
165  return (Directory) children.get(0);
166  }
167 
173  public long getImageOffset() {
174  return imgOffset;
175  }
176 
183  return fsType;
184  }
185 
191  public long getBlock_size() {
192  return blockSize;
193  }
194 
200  public long getBlock_count() {
201  return blockCount;
202  }
203 
209  public long getRoot_inum() {
210  return rootInum;
211  }
212 
218  public long getFirst_inum() {
219  return firstInum;
220  }
221 
227  public long getLastInum() {
228  return lastInum;
229  }
230 
231  @SuppressWarnings("deprecation")
232  @Override
233  public void finalize() throws Throwable {
234  try {
235  if (filesystemHandle != 0) {
236  // SleuthkitJNI.closeFs(filesystemHandle); // closeFs is currently a no-op
237  filesystemHandle = 0;
238  }
239  } finally {
240  super.finalize();
241  }
242  }
243 
244  @Override
245  public <T> T accept(SleuthkitItemVisitor<T> v) {
246  return v.visit(this);
247  }
248 
249  @Override
250  public <T> T accept(ContentVisitor<T> v) {
251  return v.visit(this);
252  }
253 
254  @Override
255  public String toString(boolean preserveState) {
256  return super.toString(preserveState) + "FileSystem [\t" + " blockCount " + blockCount + "\t" + "blockSize " + blockSize + "\t" + "firstInum " + firstInum + "\t" + "fsType " + fsType + "\t" + "imgOffset " + imgOffset + "\t" + "lastInum " + lastInum + "\t" + "rootInum " + rootInum + "\t" + "]"; //NON-NLS
257  }
258 }
String toString(boolean preserveState)
int read(byte[] buf, long offset, long len)
Definition: FileSystem.java:77
FileSystem(SleuthkitCase db, long obj_id, String name, long img_offset, TskData.TSK_FS_TYPE_ENUM fs_type, long block_size, long block_count, long root_inum, long first_inum, long last_inum)
Definition: FileSystem.java:58
TskData.TSK_FS_TYPE_ENUM getFsType()
static int readFs(long fsHandle, byte[] readBuffer, long offset, long len)

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.