19 package org.sleuthkit.datamodel;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.RandomAccessFile;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.text.MessageFormat;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.ResourceBundle;
31 import java.util.SortedSet;
32 import java.util.TimeZone;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
51 protected final Set<TSK_FS_META_FLAG_ENUM>
metaFlags;
53 protected final long metaAddr, ctime, crtime, atime, mtime;
55 protected final int uid, gid;
60 private boolean localPathSet =
false;
61 private String localPath;
62 private String localAbsPath;
63 private volatile RandomAccessFile localFileHandle;
64 private volatile java.io.File localFile;
67 private List<TskFileRange> ranges;
76 private boolean knownStateDirty =
false;
81 private boolean md5HashDirty =
false;
86 private boolean sha256HashDirty =
false;
87 private String mimeType;
88 private boolean mimeTypeDirty =
false;
90 private static final ResourceBundle BUNDLE = ResourceBundle.getBundle(
"org.sleuthkit.datamodel.Bundle");
91 private long dataSourceObjectId;
92 private final String extension;
133 long dataSourceObjectId,
137 long metaAddr,
int metaSeq,
141 long ctime,
long crtime,
long atime,
long mtime,
148 super(db, objId, name);
149 this.dataSourceObjectId = dataSourceObjectId;
161 this.crtime = crtime;
170 if (knownState == null) {
176 this.mimeType = mimeType;
177 this.extension = extension == null ?
"" : extension;
346 if ((mode & irusr) == irusr) {
351 if ((mode & iwusr) == iwusr) {
358 if ((mode & isuid) == isuid) {
359 if ((mode & ixusr) == ixusr) {
365 if ((mode & ixusr) == ixusr) {
373 if ((mode & irgrp) == irgrp) {
378 if ((mode & iwgrp) == iwgrp) {
385 if ((mode & isgid) == isgid) {
386 if ((mode & ixgrp) == ixgrp) {
392 if ((mode & ixgrp) == ixgrp) {
400 if ((mode & iroth) == iroth) {
405 if ((mode & iwoth) == iwoth) {
412 if ((mode & isvtx) == isvtx) {
413 if ((mode & ixoth) == ixoth) {
419 if ((mode & ixoth) == ixoth) {
427 if (result.length() != 10) {
452 this.mimeType = mimeType;
453 this.mimeTypeDirty =
true;
457 return modes.contains(mode);
470 this.md5HashDirty =
true;
492 this.sha256HashDirty =
true;
522 this.knownStateDirty =
true;
587 return dataSourceObjectId;
601 if (ranges == null) {
626 long rangeLength = byteRange.getByteLen();
627 if (fileOffset < rangeLength) {
628 imgOffset = byteRange.getByteStart() + fileOffset;
634 fileOffset -= rangeLength;
654 if (fileOffset < 0 || length < 0) {
658 List<TskFileRange> thisRanges =
getRanges();
659 List<TskFileRange> toRet =
new ArrayList<>();
661 long requestedEnd = fileOffset + length;
664 long bytesCounted = 0;
666 for (
int curRangeIdx = 0; curRangeIdx < thisRanges.size(); curRangeIdx++) {
668 if (bytesCounted >= requestedEnd) {
675 long curRangeEnd = bytesCounted + curRangeLen;
680 if (fileOffset < curRangeEnd) {
682 long rangeOffset = Math.max(0, fileOffset - bytesCounted);
685 long newRangeStart = curRange.
getByteStart() + rangeOffset;
688 long rangeOvershoot = Math.max(0, curRangeEnd - requestedEnd);
690 long newRangeLen = curRangeLen - rangeOffset - rangeOvershoot;
691 toRet.add(
new TskFileRange(newRangeStart, newRangeLen, toRet.size()));
694 bytesCounted = curRangeEnd;
742 public abstract boolean isRoot();
755 String[] pathSegments = uniquePath.split(
"/");
759 if (pathSegments[0].startsWith(
"img_")) {
762 if (pathSegments[1].startsWith(
"vol_")) {
768 StringBuilder strbuf =
new StringBuilder();
769 for (; index < pathSegments.length; ++index) {
770 if (!pathSegments[index].isEmpty()) {
771 strbuf.append(
"/").append(pathSegments[index]);
775 return strbuf.toString();
789 List<AbstractFile> files =
new ArrayList<AbstractFile>();
790 for (
Content child : children) {
792 AbstractFile afChild = (AbstractFile) child;
822 return dirType.toString();
831 return dirFlag == flag;
861 return metaFlags.contains(metaFlag);
871 return readInt(buf, offset, len);
905 BUNDLE.getString(
"AbstractFile.readLocal.exception.msg1.text"));
918 if (!localFile.
exists()) {
920 MessageFormat.format(BUNDLE.getString(
"AbstractFile.readLocal.exception.msg2.text"), localAbsPath));
924 MessageFormat.format(BUNDLE.getString(
"AbstractFile.readLocal.exception.msg3.text"), localAbsPath));
929 if (localFileHandle == null) {
930 synchronized (
this) {
931 if (localFileHandle == null) {
933 localFileHandle =
new RandomAccessFile(localFile,
"r");
934 }
catch (FileNotFoundException ex) {
935 final String msg = MessageFormat.format(BUNDLE.getString(
936 "AbstractFile.readLocal.exception.msg4.text"),
938 LOGGER.log(Level.SEVERE, msg, ex);
950 long encodedOffset = offset + EncodedFileUtil.getHeaderLength();
953 long curOffset = localFileHandle.getFilePointer();
954 if (curOffset != encodedOffset) {
955 localFileHandle.seek(encodedOffset);
957 bytesRead = localFileHandle.read(buf, 0, (
int) len);
958 for (
int i = 0; i < bytesRead; i++) {
959 buf[i] = EncodedFileUtil.decodeByte(buf[i], encodingType);
964 long curOffset = localFileHandle.getFilePointer();
965 if (curOffset != offset) {
966 localFileHandle.seek(offset);
969 return localFileHandle.read(buf, 0, (
int) len);
971 }
catch (IOException ex) {
972 final String msg = MessageFormat.format(BUNDLE.getString(
"AbstractFile.readLocal.exception.msg5.text"), localAbsPath);
973 LOGGER.log(Level.SEVERE, msg, ex);
986 void setLocalFilePath(String localPath) {
988 if (localPath == null || localPath.equals(
"")) {
991 localPathSet =
false;
996 this.localPath = localPath;
997 if (
this instanceof DerivedFile) {
1003 if (localPath.startsWith(
"/") || localPath.startsWith(
"\\")
1004 || localPath.matches(
"[A-Za-z]:[/\\\\].*")) {
1005 this.localAbsPath = localPath;
1010 this.localPathSet =
true;
1029 return localAbsPath;
1038 this.encodingType = encodingType;
1048 if (!localPathSet) {
1053 return localFile.
exists();
1055 LOGGER.log(Level.SEVERE, ex.getMessage());
1069 if (!localPathSet) {
1076 LOGGER.log(Level.SEVERE, ex.getMessage());
1089 if (!localPathSet) {
1090 throw new TskCoreException(
1091 BUNDLE.getString(
"AbstractFile.readLocal.exception.msg1.text"));
1095 if (localFile != null) {
1099 synchronized (
this) {
1100 if (localFile == null) {
1101 localFile =
new java.io.File(localAbsPath);
1110 if (localFileHandle != null) {
1111 synchronized (
this) {
1112 if (localFileHandle != null) {
1114 localFileHandle.close();
1115 }
catch (IOException ex) {
1116 LOGGER.log(Level.SEVERE,
"Could not close file handle for file: " +
getParentPath() +
getName(), ex);
1118 localFileHandle = null;
1136 return super.toString(preserveState) +
"AbstractFile [\t"
1138 +
"\tctime " + ctime
1139 +
"\tcrtime " + crtime
1140 +
"\t" +
"mtime " + mtime +
"\t" +
"atime " + atime
1141 +
"\t" +
"attrId " + attrId
1143 +
"\t" +
"dirFlag " + dirFlag +
"\t" +
"dirType " + dirType
1144 +
"\t" +
"uid " + uid
1145 +
"\t" +
"gid " + gid
1146 +
"\t" +
"metaAddr " + metaAddr +
"\t" +
"metaSeq " + metaSeq +
"\t" +
"metaFlags " + metaFlags
1147 +
"\t" +
"metaType " + metaType +
"\t" +
"modes " +
modes
1148 +
"\t" +
"parentPath " + parentPath +
"\t" +
"size " + size
1149 +
"\t" +
"knownState " +
knownState +
"\t" +
"md5Hash " + md5Hash +
"\t" +
"sha256Hash " + sha256Hash
1150 +
"\t" +
"localPathSet " + localPathSet +
"\t" +
"localPath " + localPath
1151 +
"\t" +
"localAbsPath " + localAbsPath +
"\t" +
"localFile " + localFile
1174 if (this.mimeType == null) {
1177 if (mimeTypes.contains(
this.mimeType)) {
1190 public void save() throws TskCoreException {
1193 if (!(md5HashDirty || sha256HashDirty || mimeTypeDirty || knownStateDirty)) {
1197 String queryStr =
"";
1198 if (mimeTypeDirty) {
1199 queryStr =
"mime_type = '" + this.
getMIMEType() +
"'";
1202 if (!queryStr.isEmpty()) {
1205 queryStr +=
"md5 = '" + this.
getMd5Hash() +
"'";
1207 if (sha256HashDirty) {
1208 if (!queryStr.isEmpty()) {
1213 if (knownStateDirty) {
1214 if (!queryStr.isEmpty()) {
1217 queryStr +=
"known = '" + this.
getKnown().getFileKnownValue() +
"'";
1220 queryStr =
"UPDATE tsk_files SET " + queryStr +
" WHERE obj_id = " + this.
getId();
1224 Statement statement = connection.createStatement();) {
1226 connection.executeUpdate(statement, queryStr);
1227 md5HashDirty =
false;
1228 sha256HashDirty =
false;
1229 mimeTypeDirty =
false;
1230 knownStateDirty =
false;
1231 }
catch (SQLException ex) {
1232 throw new TskCoreException(String.format(
"Error saving properties for file (obj_id = %s)",
this.getId()), ex);
1279 @SuppressWarnings(
"deprecation")
1283 long size,
long ctime,
long crtime,
long atime,
long mtime,
short modes,
int uid,
int gid, String md5Hash,
FileKnown knownState,
1284 String parentPath) {
1285 this(db, objId, db.getDataSourceObjectId(objId),
attrType, (int) attrId, name, fileType, metaAddr, metaSeq, dirType, metaType, dirFlag, metaFlags, size, ctime, crtime, atime, mtime, modes, uid, gid, md5Hash, null, knownState, parentPath, null, null);
1325 @SuppressWarnings(
"deprecation")
1329 int uid,
int gid, String md5Hash,
FileKnown knownState, String parentPath, String mimeType) {
1330 this(db, objId, dataSourceObjectId,
attrType, (int) attrId, name, fileType, metaAddr, metaSeq, dirType, metaType, dirFlag, metaFlags, size, ctime, crtime, atime, mtime, modes, uid, gid, md5Hash, null, knownState, parentPath, null, null);
1342 @SuppressWarnings(
"deprecation")
1369 setLocalFilePath(localPath);
VIRT
Special (TSK added "Virtual" files) NON-NLS.
boolean isModeSet(TskData.TSK_FS_META_MODE_ENUM mode)
final TSK_FS_NAME_TYPE_ENUM dirType
static long timeToEpoch(String time)
static String epochToTime(long epoch)
static String epochToTime(long epoch)
static Set< TSK_FS_META_FLAG_ENUM > valuesOf(short metaFlags)
String getMetaFlagsAsString()
final int readLocal(byte[] buf, long offset, long len)
boolean isDirNameFlagSet(TSK_FS_NAME_FLAG_ENUM flag)
List< Content > getChildren()
ALLOC
Metadata structure is currently in an allocated state.
final TSK_FS_NAME_FLAG_ENUM dirFlag
TSK_FS_META_TYPE_DIR
Directory file NON-NLS.
void setMIMEType(String mimeType)
void setSha256Hash(String sha256Hash)
TSK_FS_META_MODE_ISVTX
sticky bit
final TskData.TSK_DB_FILES_TYPE_ENUM fileType
UNALLOC
Metadata structure is currently in an unallocated state.
TskData.TSK_DB_FILES_TYPE_ENUM getType()
final TskData.TSK_FS_ATTR_TYPE_ENUM attrType
TSK_FS_META_MODE_IXOTH
X for other.
TSK_FS_META_TYPE_VIRT_DIR
"Virtual Directory" created by TSK for Orphan Files NON-NLS
long convertToImgOffset(long fileOffset)
TSK_FS_META_MODE_ISUID
set user id on execution
TSK_FS_NAME_TYPE_ENUM getDirType()
Content getContentById(long id)
String getDirFlagAsString()
TSK_FS_META_MODE_IXGRP
X for group.
String getNameExtension()
TSK_FS_META_MODE_IWOTH
W for other.
BlackboardArtifact newArtifact(int artifactTypeID)
TSK_FS_META_MODE_IRGRP
R for group.
long getDataSourceObjectId()
TSK_FS_META_MODE_IWUSR
W for owner.
MimeMatchEnum isMimeType(SortedSet< String > mimeTypes)
String getDirTypeAsString()
final Set< TSK_FS_META_FLAG_ENUM > metaFlags
TSK_FS_META_MODE_IROTH
R for other.
String toString(boolean preserveState)
TskData.FileKnown getKnown()
void setKnown(TskData.FileKnown knownState)
FALSE
file has a defined mime type and it is one of the given ones
final Set< TskData.TSK_FS_META_MODE_ENUM > modes
List< AbstractFile > listFiles()
TskData.FileKnown knownState
BlackboardArtifact newBlackboardArtifact(int artifactTypeID, long obj_id)
BlackboardArtifact getGenInfoArtifact()
TSK_FS_META_TYPE_VIRT
"Virtual File" created by TSK for file system areas NON-NLS
TSK_FS_META_TYPE_ENUM getMetaType()
TSK_FS_META_MODE_IRUSR
R for owner.
SleuthkitCase getSleuthkitCase()
boolean isMetaFlagSet(TSK_FS_META_FLAG_ENUM metaFlag)
TRUE
file does not have a defined mime time in blackboard
void acquireSingleUserCaseWriteLock()
static short toInt(Set< TSK_FS_META_MODE_ENUM > modes)
void releaseSingleUserCaseWriteLock()
static String epochToTime(long epoch, TimeZone tzone)
static long timeToEpoch(String time)
String getModesAsString()
int readInt(byte[] buf, long offset, long len)
UNKNOWN
File marked as unknown by hash db.
static Set< TSK_FS_META_MODE_ENUM > valuesOf(short modes)
List< TskFileRange > convertToImgRanges(long fileOffset, long length)
void setLocalPath(String localPath, boolean isAbsolute)
void setMd5Hash(String md5Hash)
static String createNonUniquePath(String uniquePath)
TSK_FS_META_MODE_IWGRP
W for group.
VIRTUAL_DIR
Virtual directory (not on fs) with no meta-data entry that can be used to group files of types other ...
List< TskFileRange > getRanges()
final int read(byte[] buf, long offset, long len)
String getMetaTypeAsString()
final TSK_FS_META_TYPE_ENUM metaType
TSK_FS_META_TYPE_REG
Regular file NON-NLS.
TSK_FS_META_MODE_ISGID
set group id on execution
List< TskFileRange > getFileRanges(long id)
TSK_FS_META_MODE_IXUSR
X for owner.
TskData.TSK_FS_ATTR_TYPE_ENUM getAttrType()
abstract boolean isRoot()