From 251392c8a715f7b29180d84cce01194d04bd01e4 Mon Sep 17 00:00:00 2001
From: Ann Priestman <apriestman@basistech.com>
Date: Thu, 2 Mar 2017 12:56:05 -0500
Subject: [PATCH] Added deprecated version of makeAddImageProcess Improve error
 reporting in img_writer.cpp

---
 .../org/sleuthkit/datamodel/SleuthkitCase.java | 18 ++++++++++++++++++
 tsk/auto/auto_db.cpp                           |  5 ++++-
 tsk/img/img_writer.cpp                         | 15 +++++++++++----
 3 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java b/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java
index f20b28512..3110c1120 100755
--- a/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java
+++ b/bindings/java/src/org/sleuthkit/datamodel/SleuthkitCase.java
@@ -8408,5 +8408,23 @@ public LocalFile addLocalFile(String fileName, String localPath,
 		return addLocalFile(fileName, localPath, size, ctime, crtime, atime, mtime,
 				isFile, TskData.EncodingType.NONE, parent);
 	}
+	
+	/**
+	 * Start process of adding a image to the case. Adding an image is a
+	 * multi-step process and this returns an object that allows it to happen.
+	 *
+	 * @param timezone        TZ time zone string to use for ingest of image.
+	 * @param addUnallocSpace Set to true to create virtual files for
+	 *                        unallocated space in the image.
+	 * @param noFatFsOrphans  Set to true to skip processing orphan files of FAT
+	 *                        file systems.
+	 *
+	 * @return Object that encapsulates control of adding an image via the
+	 *         SleuthKit native code layer
+	 * @Deprecated Use the newer version with explicit image writer path parameter
+	 */
+	public AddImageProcess makeAddImageProcess(String timezone, boolean addUnallocSpace, boolean noFatFsOrphans) {
+		return this.caseHandle.initAddImageProcess(timezone, addUnallocSpace, noFatFsOrphans, "");
+	}
 
 }
diff --git a/tsk/auto/auto_db.cpp b/tsk/auto/auto_db.cpp
index af3a11565..d1f76e810 100644
--- a/tsk/auto/auto_db.cpp
+++ b/tsk/auto/auto_db.cpp
@@ -531,7 +531,10 @@ TskAutoDb::startAddImage(TSK_IMG_INFO * img_info, const char* deviceId)
     }
 
     if (m_imageWriterEnabled) {
-        tsk_img_writer_create(m_img_info, m_imageWriterPath);
+        if (tsk_img_writer_create(m_img_info, m_imageWriterPath)) {
+            registerError();
+            return 1;
+        }
     }
 
     if (m_addFileSystems) {
diff --git a/tsk/img/img_writer.cpp b/tsk/img/img_writer.cpp
index a27399379..adaa027dc 100644
--- a/tsk/img/img_writer.cpp
+++ b/tsk/img/img_writer.cpp
@@ -442,7 +442,7 @@ static TSK_RETVAL_ENUM tsk_img_writer_finish_image(TSK_IMG_WRITER* img_writer) {
 
         /* Simple progress indicator - current block / totalBlocks (as an integer)
          */
-        img_writer->finishProgress = (i * 100) / img_writer->totalBlocks;
+        img_writer->finishProgress = (int)((i * 100) / img_writer->totalBlocks);
 
         if (img_writer->blockStatus[i] != IMG_WRITER_BLOCK_STATUS_FINISHED) {
 
@@ -602,7 +602,6 @@ static TSK_RETVAL_ENUM writeDynamicDiskHeader(TSK_IMG_WRITER * writer) {
     if (FALSE == WriteFile(writer->outputFileHandle, diskHeader, VHD_DISK_HEADER_LENGTH, &bytesWritten, NULL)) {
         int lastError = GetLastError();
         tsk_error_reset();
-        tsk_error_reset();
         tsk_error_set_errno(TSK_ERR_IMG_WRITE);
         tsk_error_set_errstr("writeFooter: error writing VHD header",
             lastError);
@@ -630,6 +629,9 @@ TSK_RETVAL_ENUM tsk_img_writer_create(TSK_IMG_INFO * img_info, const TSK_TCHAR *
     /* Everything will break if the buffers coming in are larger than the block 
        size (i.e., they could span three blocks instead of just two)*/
     if (TSK_IMG_INFO_CACHE_LEN > VHD_DEFAULT_BLOCK_SIZE) {
+        tsk_error_reset();
+        tsk_error_set_errno(TSK_ERR_IMG_OPEN);
+        tsk_error_set_errstr("tsk_img_writer_create: tsk cache length is larger than the block size");
         return TSK_ERR;
     }
 
@@ -637,6 +639,9 @@ TSK_RETVAL_ENUM tsk_img_writer_create(TSK_IMG_INFO * img_info, const TSK_TCHAR *
 
     /* This should not be run on split images*/
     if (img_info->num_img != 1) {
+        tsk_error_reset();
+        tsk_error_set_errno(TSK_ERR_IMG_OPEN);
+        tsk_error_set_errstr("tsk_img_writer_create: image writer can not be used on split images");
         return TSK_ERR;
     }
 
@@ -659,6 +664,9 @@ TSK_RETVAL_ENUM tsk_img_writer_create(TSK_IMG_INFO * img_info, const TSK_TCHAR *
     /* Calculation time */
     writer->imageSize = raw_info->img_info.size;
     if (writer->imageSize > VHD_MAX_IMAGE_SIZE) {
+        tsk_error_reset();
+        tsk_error_set_errno(TSK_ERR_IMG_OPEN);
+        tsk_error_set_errstr("tsk_img_writer_create: image file is too large to copy");
         return TSK_ERR;
     }
     writer->blockSize = VHD_DEFAULT_BLOCK_SIZE;
@@ -691,8 +699,7 @@ TSK_RETVAL_ENUM tsk_img_writer_create(TSK_IMG_INFO * img_info, const TSK_TCHAR *
 
         tsk_error_reset();
         tsk_error_set_errno(TSK_ERR_IMG_OPEN);
-        tsk_error_set_errstr("tsk_img_writer_create: file \"%" PRIttocTSK
-            "\" - %d", outputFileName, lastError);
+        tsk_error_set_errstr("tsk_img_writer_create: error creating file \"%" PRIttocTSK "\"", outputFileName);
         return TSK_ERR;
     }
 
-- 
GitLab