Sleuth Kit Java Bindings (JNI)  4.2
Java bindings for using The Sleuth Kit
FsContent.java
Go to the documentation of this file.
1 /*
2  * Sleuth Kit Data Model
3  *
4  * Copyright 2011 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.ArrayList;
22 import java.util.List;
23 import java.util.ResourceBundle;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
31 
39 public abstract class FsContent extends AbstractFile {
40 
41  private static final Logger logger = Logger.getLogger(AbstractFile.class.getName());
42  private static final ResourceBundle bundle = ResourceBundle.getBundle("org.sleuthkit.datamodel.Bundle");
44  protected final long fsObjId;
45  private String uniquePath;
47  private final SleuthkitCase tskCase;
48 
49  private List<String> metaDataText = null;
50 
54  private volatile FileSystem parentFileSystem;
56 
59  protected volatile long fileHandle = 0;
60 
88  String name, long metaAddr, int metaSeq,
90  long size, long ctime, long crtime, long atime, long mtime, short modes, int uid, int gid, String md5Hash, FileKnown knownState,
91  String parentPath) {
92  super(db, objId, attrType, attrId, name, TskData.TSK_DB_FILES_TYPE_ENUM.FS, metaAddr, metaSeq, dirType, metaType, dirFlag, metaFlags, size, ctime, crtime, atime, mtime, modes, uid, gid, md5Hash, knownState, parentPath);
93  this.tskCase = db;
94  this.fsObjId = fsObjId;
95  }
96 
102  public long getFileSystemId() {
103  return fsObjId;
104  }
105 
111  void setFileSystem(FileSystem parent) {
112  parentFileSystem = parent;
113  }
114 
122  if (parentFileSystem == null) {
123  synchronized (this) {
124  if (parentFileSystem == null) {
125  parentFileSystem = getSleuthkitCase().getFileSystemById(fsObjId, AbstractContent.UNKNOWN_ID);
126  }
127  }
128  }
129  return parentFileSystem;
130  }
131 
137  private void loadFileHandle() throws TskCoreException {
138  if (fileHandle == 0) {
139  synchronized (this) {
140  if (fileHandle == 0) {
141  fileHandle = SleuthkitJNI.openFile(getFileSystem().getFileSystemHandle(), metaAddr, attrType, attrId);
142  }
143  }
144  }
145  }
146 
147  @Override
148  @SuppressWarnings("deprecation")
149  protected int readInt(byte[] buf, long offset, long len) throws TskCoreException {
150  try {
151  if (offset == 0 && size == 0) {
152  //special case for 0-size file
153  return 0;
154  }
155  loadFileHandle();
156  return SleuthkitJNI.readFile(fileHandle, buf, offset, len);
157  } catch (TskCoreException ex) {
158  Content dataSource = getDataSource();
159  if ((dataSource != null) && (dataSource instanceof Image)) {
160  Image image = (Image) dataSource;
161  if (!image.imageFileExists()) {
162  tskCase.submitError(bundle.getString("FsContent.readInt.err.context.text"),
163  bundle.getString("FsContent.readInt.err.msg.text"));
164  }
165  }
166  throw ex;
167  }
168  }
169 
170  @Override
171  public boolean isRoot() {
172  try {
173  FileSystem fs = getFileSystem();
174  return fs.getRoot_inum() == this.getMetaAddr();
175  } catch (TskCoreException ex) {
176  logger.log(Level.SEVERE, "Exception while calling 'getFileSystem' on " + this, ex); //NON-NLS
177  return false;
178  }
179  }
180 
181  /*
182  * -------------------------------------------------------------------------
183  * Getters to retrieve meta-data attributes values
184  * -------------------------------------------------------------------------
185  */
193  return getSleuthkitCase().getParentDirectory(this);
194  }
195 
196  @Override
198  return getFileSystem().getDataSource();
199  }
200 
201  @Override
202  public synchronized String getUniquePath() throws TskCoreException {
203  if (uniquePath == null) {
204  StringBuilder sb = new StringBuilder();
205  sb.append(getFileSystem().getUniquePath());
206  sb.append(getParentPath());
207  sb.append(getName());
208  uniquePath = sb.toString();
209  }
210  return uniquePath;
211  }
212 
221  public synchronized List<String> getMetaDataText() throws TskCoreException {
222  if (metaDataText != null) {
223  return metaDataText;
224  }
225 
226  // if there is no metadata for this file, return empty string
227  if (metaAddr == 0) {
228  metaDataText = new ArrayList<String>();
229  metaDataText.add("");
230  return metaDataText;
231  }
232 
233  loadFileHandle();
234  metaDataText = SleuthkitJNI.getFileMetaDataText(fileHandle);
235  return metaDataText;
236  }
237 
238  @Override
239  public void close() {
240  if (fileHandle != 0) {
241  synchronized (this) {
242  //need to recheck the handle after unlock
243  if (fileHandle != 0) {
244  SleuthkitJNI.closeFile(fileHandle);
245  fileHandle = 0;
246  }
247  }
248  }
249  }
250 
251  @Override
252  public void finalize() throws Throwable {
253  try {
254  close();
255  } finally {
256  super.finalize();
257  }
258  }
259 
260  @Override
261  public String toString(boolean preserveState) {
262  return super.toString(preserveState)
263  + "FsContent [\t" //NON-NLS
264  + "fsObjId " + fsObjId //NON-NLS
265  + "\t" + "uniquePath " + uniquePath //NON-NLS
266  + "\t" + "fileHandle " + fileHandle //NON-NLS
267  + "]\t";
268  }
269 }
FS
File that can be found in file system tree.
Definition: TskData.java:644
final TSK_FS_NAME_TYPE_ENUM dirType
static final Logger logger
Definition: FsContent.java:41
static final ResourceBundle bundle
Definition: FsContent.java:42
int readInt(byte[] buf, long offset, long len)
Definition: FsContent.java:149
final SleuthkitCase tskCase
read-write database tsk_files fields
Definition: FsContent.java:47
final TSK_FS_NAME_FLAG_ENUM dirFlag
static long openFile(long fsHandle, long fileId, TSK_FS_ATTR_TYPE_ENUM attrType, int attrId)
static int readFile(long fileHandle, byte[] readBuffer, long offset, long len)
synchronized List< String > getMetaDataText()
Definition: FsContent.java:221
final TskData.TSK_FS_ATTR_TYPE_ENUM attrType
volatile FileSystem parentFileSystem
Definition: FsContent.java:54
final Set< TSK_FS_META_FLAG_ENUM > metaFlags
synchronized String getUniquePath()
Definition: FsContent.java:202
final Set< TskData.TSK_FS_META_MODE_ENUM > modes
static List< String > getFileMetaDataText(long fileHandle)
volatile long fileHandle
other members
Definition: FsContent.java:59
String toString(boolean preserveState)
Definition: FsContent.java:261
final long fsObjId
read only database tsk_files fields
Definition: FsContent.java:44
static void closeFile(long fileHandle)
void submitError(String context, String errorMessage)
final TSK_FS_META_TYPE_ENUM metaType

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.