From d1a9217c5149793b26b1686765e971a3a1378d8d Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nicolas.pope@utu.fi>
Date: Mon, 13 Jun 2022 07:41:29 +0000
Subject: [PATCH] #54 Check file after begin

---
 src/streams/filestream.cpp | 18 ++++++------------
 test/filestream_unit.cpp   |  4 ++++
 2 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/src/streams/filestream.cpp b/src/streams/filestream.cpp
index bc09295..db69504 100644
--- a/src/streams/filestream.cpp
+++ b/src/streams/filestream.cpp
@@ -38,21 +38,10 @@ File::File(const std::string &uri, bool writeable) :
         istream_(nullptr),
         active_(false) {
     mode_ = (writeable) ? Mode::Write : Mode::Read;
-
-    // Open the file
-    if (!writeable) {
-        if (!_checkFile()) {
-            throw FTL_Error("Could not open file");
-        }
-    }
 }
 
 File::File(std::ifstream *is) : Stream(), ostream_(nullptr), istream_(is), active_(false) {
     mode_ = Mode::Read;
-
-    if (!_checkFile()) {
-        throw FTL_Error("Could not open file");
-    }
 }
 
 File::File(std::ofstream *os) : Stream(), ostream_(os), istream_(nullptr), active_(false) {
@@ -492,7 +481,12 @@ bool File::run() {
 bool File::begin() {
     if (active_) return true;
     if (mode_ == Mode::Read) {
-        if (!checked_) _checkFile();
+        if (!checked_) {
+            if (!_checkFile()) {
+                LOG(ERROR) << "Could not open file: " << uri_.toFilePath();
+                return false;
+            }
+        }
         _open();
 
         // Capture current time to adjust timestamps
diff --git a/test/filestream_unit.cpp b/test/filestream_unit.cpp
index 685fa4b..2cb0f30 100644
--- a/test/filestream_unit.cpp
+++ b/test/filestream_unit.cpp
@@ -27,6 +27,8 @@ TEST_CASE("File write and read", "[stream]") {
 
         auto reader = ftl::getStream(filename);
 
+        REQUIRE( reader->frames().size() == 0 );
+
         StreamPacket tspkt = {4,0,0,1, Channel::kColour};
         auto h = reader->onPacket([&tspkt](const StreamPacket &spkt, const DataPacket &pkt) {
             if (spkt.channel == Channel::kEndFrame) return true;
@@ -35,6 +37,8 @@ TEST_CASE("File write and read", "[stream]") {
         });
         REQUIRE( reader->begin() );
 
+        REQUIRE( reader->frames().size() == 1 );
+
         std::this_thread::sleep_for(std::chrono::milliseconds(200));
         reader->end();
 
-- 
GitLab