From 6a5b16c6448c9b2e98b77a4a6dc803773f46039d Mon Sep 17 00:00:00 2001
From: Gary Talent <gtalent2@gmail.com>
Date: Sat, 22 Jun 2019 01:29:55 -0500
Subject: [PATCH] [nostalgia/tools/pack] Add verify, cleanup linking options,
 cleanup error handling

---
 src/nostalgia/tools/CMakeLists.txt      |  7 ------
 src/nostalgia/tools/pack.cpp            |  4 +++-
 src/nostalgia/tools/pack/CMakeLists.txt |  2 +-
 src/nostalgia/tools/pack/pack.cpp       | 30 ++++++++++++++++---------
 4 files changed, 24 insertions(+), 19 deletions(-)

diff --git a/src/nostalgia/tools/CMakeLists.txt b/src/nostalgia/tools/CMakeLists.txt
index f2f9758b..3d1717c5 100644
--- a/src/nostalgia/tools/CMakeLists.txt
+++ b/src/nostalgia/tools/CMakeLists.txt
@@ -5,13 +5,6 @@ add_executable(nost-pack pack.cpp)
 
 target_link_libraries(
 	nost-pack
-		Qt5::Widgets
-		OxClArgs
-		OxFS
-		OxStd
-		OxMetalClaw
-		NostalgiaCommon
-		NostalgiaCore
 		NostalgiaPack
 )
 
diff --git a/src/nostalgia/tools/pack.cpp b/src/nostalgia/tools/pack.cpp
index d3b5d7ef..92153979 100644
--- a/src/nostalgia/tools/pack.cpp
+++ b/src/nostalgia/tools/pack.cpp
@@ -54,7 +54,9 @@ ox::Error run(ClArgs args) {
 	ox::FileSystem32::format(buff.data(), buff.size());
 	ox::PassThroughFS src(argSrc.c_str());
 	ox::FileSystem32 dst(ox::FileStore32(buff.data(), buff.size()));
-	oxReturnError(nostalgia::pack(&src, &dst));
+	auto err = nostalgia::pack(&src, &dst);
+	oxAssert(err, "pack failed");
+	oxReturnError(err);
 	return OxError(0);
 }
 
diff --git a/src/nostalgia/tools/pack/CMakeLists.txt b/src/nostalgia/tools/pack/CMakeLists.txt
index 05071c06..c0380389 100644
--- a/src/nostalgia/tools/pack/CMakeLists.txt
+++ b/src/nostalgia/tools/pack/CMakeLists.txt
@@ -6,7 +6,7 @@ add_library(
 )
 
 target_link_libraries(
-	NostalgiaPack
+	NostalgiaPack PUBLIC
 		Qt5::Widgets
 		OxClArgs
 		OxFS
diff --git a/src/nostalgia/tools/pack/pack.cpp b/src/nostalgia/tools/pack/pack.cpp
index ffe57fb2..5e261c0f 100644
--- a/src/nostalgia/tools/pack/pack.cpp
+++ b/src/nostalgia/tools/pack/pack.cpp
@@ -6,6 +6,7 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  */
 
+#include <iostream>
 #include <string_view>
 #include <vector>
 
@@ -63,21 +64,29 @@ ox::Error transformClaw(ox::FileSystem32 *dest, std::string path) {
 	return OxError(0);
 }
 
+[[nodiscard]] ox::Error verifyFile(ox::FileSystem32 *fs, const std::string &path, const std::vector<char> &expected) noexcept {
+	std::vector<char> buff(expected.size());
+	oxReturnError(fs->read(path.c_str(), buff.data(), buff.size()));
+	return buff == expected ? 0 : OxError(1);
+}
+
 ox::Error copy(ox::PassThroughFS *src, ox::FileSystem32 *dest, std::string path) {
+	std::cout << "copying directory: " << path << '\n';
 	// copy
-	src->ls(path.c_str(), [src, dest, path](const char *name, ox::InodeId_t) {
-		auto [stat, err] = src->stat(path.c_str());
+	return src->ls(path.c_str(), [src, dest, path](const char *name, ox::InodeId_t) {
+		std::cout << "reading " << name << '\n';
+		const auto currentFile = path + name;
+		auto [stat, err] = src->stat((currentFile).c_str());
 		oxReturnError(err);
 		if (stat.fileType == ox::FileType_Directory) {
-			const auto dir = path + name + '/';
-			oxReturnError(dest->mkdir(dir.c_str()));
-			oxReturnError(copy(src, dest, dir));
+			oxReturnError(dest->mkdir(currentFile.c_str(), true));
+			oxReturnError(copy(src, dest, currentFile + '/'));
 		} else {
 			std::vector<char> buff;
 			// do transforms
-			if (endsWith(path, ".png")) {
+			if (endsWith(currentFile, ".png")) {
 				// load file from full path and transform
-				const auto fullPath = src->basePath() + path;
+				const auto fullPath = src->basePath() + currentFile;
 				buff = pngToGba(fullPath.c_str(), 0, 0);
 				if (!buff.size()) {
 					return OxError(1);
@@ -85,14 +94,15 @@ ox::Error copy(ox::PassThroughFS *src, ox::FileSystem32 *dest, std::string path)
 			} else {
 				// load file
 				buff.resize(stat.size);
-				oxReturnError(src->read(path.c_str(), buff.data(), buff.size()));
+				oxReturnError(src->read(currentFile.c_str(), buff.data(), buff.size()));
 			}
 			// write file to dest
-			oxReturnError(dest->write(path.c_str(), buff.data(), buff.size()));
+			std::cout << "writing " << currentFile << '\n';
+			oxReturnError(dest->write(currentFile.c_str(), buff.data(), buff.size()));
+			oxReturnError(verifyFile(dest, currentFile, buff));
 		}
 		return OxError(0);
 	});
-	return OxError(0);
 }
 
 }