Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
LocalFileImporter.java
Go to the documentation of this file.
1 /*
2  *
3  * Autopsy Forensic Browser
4  *
5  * Copyright 2019 Basis Technology Corp.
6  * Contact: carrier <at> sleuthkit <dot> org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 package org.sleuthkit.autopsy.datamodel.utils;
21 
22 import java.io.File;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.logging.Level;
27 import org.sleuthkit.datamodel.AbstractFile;
28 import org.sleuthkit.datamodel.Content;
29 import org.sleuthkit.datamodel.DataSource;
30 import org.sleuthkit.datamodel.SleuthkitCase;
31 import org.sleuthkit.datamodel.SpecialDirectory;
32 import org.sleuthkit.datamodel.TskCoreException;
33 import org.sleuthkit.datamodel.TskData;
34 
41 public class LocalFileImporter {
42  private static final Logger logger = Logger.getLogger(LocalFileImporter.class.getName());
43 
44  SleuthkitCase.CaseDbTransaction globalTrans = null;
45  boolean useSingleTransaction = true;
46  SleuthkitCase sleuthkitCase;
47  private final Map<String, SpecialDirectory> localFileDirMap = new HashMap<>();
48 
54  public LocalFileImporter(SleuthkitCase sleuthkitCase) {
55  this.sleuthkitCase = sleuthkitCase;
56  this.useSingleTransaction = false;
57  }
58 
66  public LocalFileImporter(SleuthkitCase sleuthkitCase, SleuthkitCase.CaseDbTransaction trans) {
67  this.sleuthkitCase = sleuthkitCase;
68  this.globalTrans = trans;
69  this.useSingleTransaction = true;
70  }
71 
91  public AbstractFile addLocalFile(File fileOnDisk, String name, String parentPath,
92  Long ctime, Long crtime, Long atime, Long mtime,
93  DataSource dataSource) throws TskCoreException {
94 
95  // Get the parent folder, creating it and any of its parent folders if necessary
96  SpecialDirectory parentDir = getOrMakeDirInDataSource(new File(parentPath), dataSource);
97 
98  SleuthkitCase.CaseDbTransaction trans = null;
99  try {
100  if (useSingleTransaction) {
101  trans = globalTrans;
102  } else {
103  trans = sleuthkitCase.beginTransaction();
104  }
105 
106  // Try to get the file size
107  long size = 0;
108  if (fileOnDisk.exists()) {
109  size = fileOnDisk.length();
110  }
111 
112  // Create the new file
113  AbstractFile file = sleuthkitCase.addLocalFile(name, fileOnDisk.getAbsolutePath(), size,
114  ctime, crtime, atime, mtime,
115  true, TskData.EncodingType.NONE, parentDir, trans);
116 
117  if (! useSingleTransaction) {
118  trans.commit();
119  }
120  return file;
121  } catch (TskCoreException ex) {
122  if ((!useSingleTransaction) && (null != trans)) {
123  try {
124  trans.rollback();
125  } catch (TskCoreException ex2) {
126  logger.log(Level.SEVERE, String.format("Failed to rollback transaction after exception: %s", ex.getMessage()), ex2);
127  }
128  }
129  throw ex;
130  }
131  }
132 
144  private SpecialDirectory getOrMakeDirInDataSource(File directory, Content dataSource) throws TskCoreException {
145  if ((directory == null) || directory.getPath().isEmpty()) {
146  throw new TskCoreException("Can not create directory from null path");
147  }
148 
149  // Check if we've already created it
150  if (localFileDirMap.containsKey(directory.toString())) {
151  return localFileDirMap.get(directory.toString());
152  }
153 
154  File parent = directory.getParentFile();
155  if (parent == null) {
156  // This is the root of the path and it isn't in the map, so create it
157  SpecialDirectory dir = createLocalFilesDir(dataSource.getId(), directory.getName());
158  localFileDirMap.put(directory.getName(), dir);
159  return dir;
160 
161  } else {
162  // Create everything above this in the tree, and then add the parent folder
163  SpecialDirectory parentDir = getOrMakeDirInDataSource(parent, dataSource);
164  SpecialDirectory dir = createLocalFilesDir(parentDir.getId(), directory.getName());
165  localFileDirMap.put(directory.getPath(), dir);
166  return dir;
167  }
168  }
169 
180  private SpecialDirectory createLocalFilesDir(long parentId, String name) throws TskCoreException {
181  SleuthkitCase.CaseDbTransaction trans = null;
182 
183  try {
184  if (useSingleTransaction) {
185  trans = globalTrans;
186  } else {
187  trans = sleuthkitCase.beginTransaction();
188  }
189  SpecialDirectory dir;
190 
191  dir = sleuthkitCase.addLocalDirectory(parentId, name, trans);
192 
193  if (! useSingleTransaction) {
194  trans.commit();
195  }
196  return dir;
197  } catch (TskCoreException ex) {
198  if (( !useSingleTransaction) && (null != trans)) {
199  try {
200  trans.rollback();
201  } catch (TskCoreException ex2) {
202  logger.log(Level.SEVERE, String.format("Failed to rollback transaction after exception: %s", ex.getMessage()), ex2);
203  }
204  }
205  throw ex;
206  }
207  }
208 
209 }
SpecialDirectory getOrMakeDirInDataSource(File directory, Content dataSource)
LocalFileImporter(SleuthkitCase sleuthkitCase, SleuthkitCase.CaseDbTransaction trans)
final Map< String, SpecialDirectory > localFileDirMap
AbstractFile addLocalFile(File fileOnDisk, String name, String parentPath, Long ctime, Long crtime, Long atime, Long mtime, DataSource dataSource)
SpecialDirectory createLocalFilesDir(long parentId, String name)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.