archive/Plugin: migrate open() from class Error to C++ exceptions
This commit is contained in:
		@@ -22,7 +22,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class Mutex;
 | 
					class Mutex;
 | 
				
			||||||
class Cond;
 | 
					class Cond;
 | 
				
			||||||
class Error;
 | 
					 | 
				
			||||||
struct ArchivePlugin;
 | 
					struct ArchivePlugin;
 | 
				
			||||||
class ArchiveVisitor;
 | 
					class ArchiveVisitor;
 | 
				
			||||||
class InputStream;
 | 
					class InputStream;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,20 +20,15 @@
 | 
				
			|||||||
#include "config.h"
 | 
					#include "config.h"
 | 
				
			||||||
#include "ArchivePlugin.hxx"
 | 
					#include "ArchivePlugin.hxx"
 | 
				
			||||||
#include "fs/Path.hxx"
 | 
					#include "fs/Path.hxx"
 | 
				
			||||||
#include "util/Error.hxx"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <assert.h>
 | 
					#include <assert.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ArchiveFile *
 | 
					ArchiveFile *
 | 
				
			||||||
archive_file_open(const ArchivePlugin *plugin, Path path,
 | 
					archive_file_open(const ArchivePlugin *plugin, Path path)
 | 
				
			||||||
		  Error &error)
 | 
					 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	assert(plugin != nullptr);
 | 
						assert(plugin != nullptr);
 | 
				
			||||||
	assert(plugin->open != nullptr);
 | 
						assert(plugin->open != nullptr);
 | 
				
			||||||
	assert(!path.IsNull());
 | 
						assert(!path.IsNull());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ArchiveFile *file = plugin->open(path, error);
 | 
						return plugin->open(path);
 | 
				
			||||||
	assert((file == nullptr) == error.IsDefined());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return file;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,7 +22,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class ArchiveFile;
 | 
					class ArchiveFile;
 | 
				
			||||||
class Path;
 | 
					class Path;
 | 
				
			||||||
class Error;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct ArchivePlugin {
 | 
					struct ArchivePlugin {
 | 
				
			||||||
	const char *name;
 | 
						const char *name;
 | 
				
			||||||
@@ -43,9 +42,10 @@ struct ArchivePlugin {
 | 
				
			|||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * tryes to open archive file and associates handle with archive
 | 
						 * tryes to open archive file and associates handle with archive
 | 
				
			||||||
	 * returns pointer to handle used is all operations with this archive
 | 
						 * returns pointer to handle used is all operations with this archive
 | 
				
			||||||
	 * or nullptr when opening fails
 | 
						 *
 | 
				
			||||||
 | 
						 * Throws std::runtime_error on error.
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	ArchiveFile *(*open)(Path path_fs, Error &error);
 | 
						ArchiveFile *(*open)(Path path_fs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * suffixes handled by this plugin.
 | 
						 * suffixes handled by this plugin.
 | 
				
			||||||
@@ -55,7 +55,6 @@ struct ArchivePlugin {
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ArchiveFile *
 | 
					ArchiveFile *
 | 
				
			||||||
archive_file_open(const ArchivePlugin *plugin, Path path,
 | 
					archive_file_open(const ArchivePlugin *plugin, Path path);
 | 
				
			||||||
		  Error &error);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -127,7 +127,7 @@ Bzip2InputStream::Open()
 | 
				
			|||||||
/* archive open && listing routine */
 | 
					/* archive open && listing routine */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static ArchiveFile *
 | 
					static ArchiveFile *
 | 
				
			||||||
bz2_open(Path pathname, gcc_unused Error &error)
 | 
					bz2_open(Path pathname)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	static Mutex mutex;
 | 
						static Mutex mutex;
 | 
				
			||||||
	static Cond cond;
 | 
						static Cond cond;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -123,16 +123,13 @@ Iso9660ArchiveFile::Visit(char *path, size_t length, size_t capacity,
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static ArchiveFile *
 | 
					static ArchiveFile *
 | 
				
			||||||
iso9660_archive_open(Path pathname, Error &error)
 | 
					iso9660_archive_open(Path pathname)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	/* open archive */
 | 
						/* open archive */
 | 
				
			||||||
	auto iso = iso9660_open(pathname.c_str());
 | 
						auto iso = iso9660_open(pathname.c_str());
 | 
				
			||||||
	if (iso == nullptr) {
 | 
						if (iso == nullptr)
 | 
				
			||||||
		error.Format(iso9660_domain,
 | 
							throw FormatRuntimeError("Failed to open ISO9660 file %s",
 | 
				
			||||||
			     "Failed to open ISO9660 file %s",
 | 
										 pathname.c_str());
 | 
				
			||||||
			     pathname.c_str());
 | 
					 | 
				
			||||||
		return nullptr;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return new Iso9660ArchiveFile(iso);
 | 
						return new Iso9660ArchiveFile(iso);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -68,14 +68,12 @@ static constexpr Domain zzip_domain("zzip");
 | 
				
			|||||||
/* archive open && listing routine */
 | 
					/* archive open && listing routine */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static ArchiveFile *
 | 
					static ArchiveFile *
 | 
				
			||||||
zzip_archive_open(Path pathname, Error &error)
 | 
					zzip_archive_open(Path pathname)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	ZZIP_DIR *dir = zzip_dir_open(pathname.c_str(), nullptr);
 | 
						ZZIP_DIR *dir = zzip_dir_open(pathname.c_str(), nullptr);
 | 
				
			||||||
	if (dir == nullptr) {
 | 
						if (dir == nullptr)
 | 
				
			||||||
		error.Format(zzip_domain, "Failed to open ZIP file %s",
 | 
							throw FormatRuntimeError("Failed to open ZIP file %s",
 | 
				
			||||||
			     pathname.c_str());
 | 
										 pathname.c_str());
 | 
				
			||||||
		return nullptr;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return new ZzipArchiveFile(dir);
 | 
						return new ZzipArchiveFile(dir);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,11 +30,11 @@
 | 
				
			|||||||
#include "archive/ArchivePlugin.hxx"
 | 
					#include "archive/ArchivePlugin.hxx"
 | 
				
			||||||
#include "archive/ArchiveFile.hxx"
 | 
					#include "archive/ArchiveFile.hxx"
 | 
				
			||||||
#include "archive/ArchiveVisitor.hxx"
 | 
					#include "archive/ArchiveVisitor.hxx"
 | 
				
			||||||
#include "util/Error.hxx"
 | 
					 | 
				
			||||||
#include "util/StringCompare.hxx"
 | 
					#include "util/StringCompare.hxx"
 | 
				
			||||||
#include "Log.hxx"
 | 
					#include "Log.hxx"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
 | 
					#include <stdexcept>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -150,10 +150,11 @@ UpdateWalk::UpdateArchiveFile(Directory &parent, const char *name,
 | 
				
			|||||||
		return;
 | 
							return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* open archive */
 | 
						/* open archive */
 | 
				
			||||||
	Error error;
 | 
						ArchiveFile *file;
 | 
				
			||||||
	ArchiveFile *file = archive_file_open(&plugin, path_fs, error);
 | 
						try {
 | 
				
			||||||
	if (file == nullptr) {
 | 
							file = archive_file_open(&plugin, path_fs);
 | 
				
			||||||
		LogError(error);
 | 
						} catch (const std::runtime_error &e) {
 | 
				
			||||||
 | 
							LogError(e);
 | 
				
			||||||
		if (directory != nullptr)
 | 
							if (directory != nullptr)
 | 
				
			||||||
			editor.LockDeleteDirectory(directory);
 | 
								editor.LockDeleteDirectory(directory);
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,7 +29,6 @@
 | 
				
			|||||||
#include "fs/Path.hxx"
 | 
					#include "fs/Path.hxx"
 | 
				
			||||||
#include "Log.hxx"
 | 
					#include "Log.hxx"
 | 
				
			||||||
#include "util/ScopeExit.hxx"
 | 
					#include "util/ScopeExit.hxx"
 | 
				
			||||||
#include "util/Error.hxx"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <stdexcept>
 | 
					#include <stdexcept>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -61,10 +60,7 @@ OpenArchiveInputStream(Path path, Mutex &mutex, Cond &cond)
 | 
				
			|||||||
		return nullptr;
 | 
							return nullptr;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Error error;
 | 
						auto file = archive_file_open(arplug, Path::FromFS(archive));
 | 
				
			||||||
	auto file = archive_file_open(arplug, Path::FromFS(archive), error);
 | 
					 | 
				
			||||||
	if (file == nullptr)
 | 
					 | 
				
			||||||
		throw std::runtime_error(error.GetMessage());
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	AtScopeExit(file) {
 | 
						AtScopeExit(file) {
 | 
				
			||||||
		file->Close();
 | 
							file->Close();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,7 +27,6 @@
 | 
				
			|||||||
#include "archive/ArchiveFile.hxx"
 | 
					#include "archive/ArchiveFile.hxx"
 | 
				
			||||||
#include "archive/ArchiveVisitor.hxx"
 | 
					#include "archive/ArchiveVisitor.hxx"
 | 
				
			||||||
#include "fs/Path.hxx"
 | 
					#include "fs/Path.hxx"
 | 
				
			||||||
#include "util/Error.hxx"
 | 
					 | 
				
			||||||
#include "Log.hxx"
 | 
					#include "Log.hxx"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <stdexcept>
 | 
					#include <stdexcept>
 | 
				
			||||||
@@ -46,8 +45,6 @@ class MyArchiveVisitor final : public ArchiveVisitor {
 | 
				
			|||||||
int
 | 
					int
 | 
				
			||||||
main(int argc, char **argv)
 | 
					main(int argc, char **argv)
 | 
				
			||||||
try {
 | 
					try {
 | 
				
			||||||
	Error error;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (argc != 3) {
 | 
						if (argc != 3) {
 | 
				
			||||||
		fprintf(stderr, "Usage: visit_archive PLUGIN PATH\n");
 | 
							fprintf(stderr, "Usage: visit_archive PLUGIN PATH\n");
 | 
				
			||||||
		return EXIT_FAILURE;
 | 
							return EXIT_FAILURE;
 | 
				
			||||||
@@ -76,15 +73,11 @@ try {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	int result = EXIT_SUCCESS;
 | 
						int result = EXIT_SUCCESS;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ArchiveFile *file = archive_file_open(plugin, path, error);
 | 
						ArchiveFile *file = archive_file_open(plugin, path);
 | 
				
			||||||
	if (file != nullptr) {
 | 
					
 | 
				
			||||||
		MyArchiveVisitor visitor;
 | 
						MyArchiveVisitor visitor;
 | 
				
			||||||
		file->Visit(visitor);
 | 
						file->Visit(visitor);
 | 
				
			||||||
		file->Close();
 | 
						file->Close();
 | 
				
			||||||
	} else {
 | 
					 | 
				
			||||||
		fprintf(stderr, "%s", error.GetMessage());
 | 
					 | 
				
			||||||
		result = EXIT_FAILURE;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* deinitialize everything */
 | 
						/* deinitialize everything */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user