From bcb1d53a467c4770ce3034829d7de8d129547614 Mon Sep 17 00:00:00 2001
From: Brian Carrier <carrier@sleuthkit.org>
Date: Fri, 29 May 2009 17:36:26 +0000
Subject: [PATCH] Feature 2790367  to move stat call to after image format
 _open calls

---
 CHANGES.txt          |  4 +++
 tsk3/img/img_open.c  | 73 ++++++++++++++++----------------------------
 tsk3/img/img_types.c |  3 +-
 tsk3/img/raw.c       |  4 +--
 4 files changed, 34 insertions(+), 50 deletions(-)

diff --git a/CHANGES.txt b/CHANGES.txt
index ac114d1ca..11c909620 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -86,6 +86,10 @@ Reported by Simson Garfinkel.
 5/29/09: Update: Added feature 2677107 that allows support for all
 image formats supported by AFFLIB.
 
+5/29/09: Update: Added feature 2790367 to move the 'stat' function
+until after all image format open calls to support formats that do
+not use actual files. 
+
 
 ---------------- VERSION 3.0.1 -------------- 
 11/11/08: Bug Fix: Fixed crashing bug in ifind on FAT file system.
diff --git a/tsk3/img/img_open.c b/tsk3/img/img_open.c
index b28720526..2188f365b 100644
--- a/tsk3/img/img_open.c
+++ b/tsk3/img/img_open.c
@@ -100,51 +100,6 @@ tsk_img_open(int num_img,
 #endif
         struct STAT_STR stat_buf;
 
-        /* First verify that the image file exists */
-        if (TSTAT(images[0], &stat_buf) < 0) {
-
-            // special case to handle windows objects
-#if defined(TSK_WIN32) || defined(__CYGWIN__)
-            if (TSTRNCMP(_TSK_T("\\\\.\\"), images[0], 4) == 0) {
-                if (tsk_verbose)
-                    TFPRINTF(stderr,
-                        _TSK_T
-                        ("tsk_img_open: Ignoring stat error because of windows object: %s\n"),
-                        images[0]);
-            }
-#endif
-
-            // AFFLIB supports s3 storage on Amazon, so skip those 'file' paths 
-#if HAVE_LIBAFFLIB
-
-            // make it an "else if" instead of  just "if" if we did the device check
-#if defined(TSK_WIN32) || defined(__CYGWIN__)
-            else
-#endif
-            if ((TSTRNCMP(_TSK_T("s3://"), images[0], 5) == 0) ||
-                (TSTRNCMP(_TSK_T("http://"), images[0], 7) == 0)) {
-                if (tsk_verbose)
-                    TFPRINTF(stderr,
-                        _TSK_T
-                        ("tsk_img_open: Ignoring stat error because of s3/http object: %s\n"),
-                        images[0]);
-            }
-
-#endif
-
-#if HAVE_LIBAFFLIB || defined(TSK_WIN32) || defined (__CYGWIN__)
-            else {
-#endif
-                tsk_error_reset();
-                tsk_errno = TSK_ERR_IMG_STAT;
-                snprintf(tsk_errstr, TSK_ERRSTR_L, "%" PRIttocTSK " : %s",
-                    images[0], strerror(errno));
-                return NULL;
-#if HAVE_LIBAFFLIB || defined(TSK_WIN32) || defined (__CYGWIN__)
-            }
-#endif
-        }
-
         // we rely on tsk_errno, so make sure it is 0
         tsk_error_reset();
 
@@ -185,6 +140,7 @@ tsk_img_open(int num_img,
             tsk_error_reset();
         }
 #endif
+        // if any of the non-raw formats were detected, then use it. 
         if (img_set != NULL)
             return img_set;
 
@@ -205,6 +161,30 @@ tsk_img_open(int num_img,
                 return NULL;
             }
         }
+
+        /* To improve the error message, verify the file can be read. */
+        if (TSTAT(images[0], &stat_buf) < 0) {
+            // special case to handle windows objects
+#if defined(TSK_WIN32) || defined(__CYGWIN__)
+            if (TSTRNCMP(_TSK_T("\\\\.\\"), images[0], 4) == 0) {
+                if (tsk_verbose)
+                    TFPRINTF(stderr,
+                        _TSK_T
+                        ("tsk_img_open: Ignoring stat error because of windows object: %s\n"),
+                        images[0]);
+            }
+            else {
+#endif
+                tsk_error_reset();
+                tsk_errno = TSK_ERR_IMG_STAT;
+                snprintf(tsk_errstr, TSK_ERRSTR_L, "%" PRIttocTSK " : %s",
+                    images[0], strerror(errno));
+                return NULL;
+#if defined(TSK_WIN32) || defined(__CYGWIN__)
+            }
+#endif
+        }
+
         tsk_errno = TSK_ERR_IMG_UNKTYPE;
         tsk_errstr[0] = '\0';
         tsk_errstr2[0] = '\0';
@@ -346,8 +326,7 @@ tsk_img_open_utf8(int num_img, const char *const images[],
             *utf16 = '\0';
         }
 
-        retval =
-            tsk_img_open(num_img, images16, type);
+        retval = tsk_img_open(num_img, images16, type);
 
         // free up the memory
       tsk_utf8_cleanup:
diff --git a/tsk3/img/img_types.c b/tsk3/img/img_types.c
index 82e04bd1f..802cee36c 100644
--- a/tsk3/img/img_types.c
+++ b/tsk3/img/img_types.c
@@ -34,7 +34,8 @@ static IMG_TYPES img_open_table[] = {
     {"aff", TSK_IMG_TYPE_AFF_AFF, "Advanced Forensic Format"},
     {"afd", TSK_IMG_TYPE_AFF_AFD, "AFF Multiple File"},
     {"afm", TSK_IMG_TYPE_AFF_AFM, "AFF with external metadata"},
-    {"afflib", TSK_IMG_TYPE_AFF_ANY, "All AFFLIB image formats (including beta ones)"},
+    {"afflib", TSK_IMG_TYPE_AFF_ANY,
+            "All AFFLIB image formats (including beta ones)"},
 #endif
 #if HAVE_LIBEWF
     {"ewf", TSK_IMG_TYPE_EWF_EWF, "Expert Witness format (encase)"},
diff --git a/tsk3/img/raw.c b/tsk3/img/raw.c
index 6e20eb292..d345087c3 100644
--- a/tsk3/img/raw.c
+++ b/tsk3/img/raw.c
@@ -178,7 +178,7 @@ raw_open(const TSK_TCHAR * image)
             tsk_error_reset();
             tsk_errno = TSK_ERR_IMG_STAT;
             snprintf(tsk_errstr, TSK_ERRSTR_L,
-                "raw_open directory check: %s", strerror(errno));
+                "raw_open: %s", strerror(errno));
             return NULL;
         }
         else if ((stat_buf.st_mode & S_IFMT) == S_IFDIR) {
@@ -189,7 +189,7 @@ raw_open(const TSK_TCHAR * image)
             tsk_error_reset();
             tsk_errno = TSK_ERR_IMG_MAGIC;
             snprintf(tsk_errstr, TSK_ERRSTR_L,
-                "raw_open: Image is a directory");
+                "raw_open: path is for a directory");
             return NULL;
         }
     }
-- 
GitLab