From 2e0949d8e674b1e62391ae7a7febe9a644a55f24 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Wed, 27 Dec 2017 09:07:21 +0100
Subject: [PATCH] archive/Plugin: return std::unique_ptr<ArchiveFile>

---
 src/archive/ArchivePlugin.cxx                | 3 ++-
 src/archive/ArchivePlugin.hxx                | 6 ++++--
 src/archive/plugins/Bzip2ArchivePlugin.cxx   | 4 ++--
 src/archive/plugins/Iso9660ArchivePlugin.cxx | 4 ++--
 src/archive/plugins/ZzipArchivePlugin.cxx    | 4 ++--
 src/db/update/Archive.cxx                    | 3 +--
 src/input/plugins/ArchiveInputPlugin.cxx     | 9 ++-------
 test/visit_archive.cxx                       | 3 +--
 8 files changed, 16 insertions(+), 20 deletions(-)

diff --git a/src/archive/ArchivePlugin.cxx b/src/archive/ArchivePlugin.cxx
index ab4e2b1ff..6d9a8546c 100644
--- a/src/archive/ArchivePlugin.cxx
+++ b/src/archive/ArchivePlugin.cxx
@@ -19,11 +19,12 @@
 
 #include "config.h"
 #include "ArchivePlugin.hxx"
+#include "ArchiveFile.hxx"
 #include "fs/Path.hxx"
 
 #include <assert.h>
 
-ArchiveFile *
+std::unique_ptr<ArchiveFile>
 archive_file_open(const ArchivePlugin *plugin, Path path)
 {
 	assert(plugin != nullptr);
diff --git a/src/archive/ArchivePlugin.hxx b/src/archive/ArchivePlugin.hxx
index 4e4bfc233..708988215 100644
--- a/src/archive/ArchivePlugin.hxx
+++ b/src/archive/ArchivePlugin.hxx
@@ -20,6 +20,8 @@
 #ifndef MPD_ARCHIVE_PLUGIN_HXX
 #define MPD_ARCHIVE_PLUGIN_HXX
 
+#include <memory>
+
 class ArchiveFile;
 class Path;
 
@@ -45,7 +47,7 @@ struct ArchivePlugin {
 	 *
 	 * Throws std::runtime_error on error.
 	 */
-	ArchiveFile *(*open)(Path path_fs);
+	std::unique_ptr<ArchiveFile> (*open)(Path path_fs);
 
 	/**
 	 * suffixes handled by this plugin.
@@ -54,7 +56,7 @@ struct ArchivePlugin {
 	const char *const*suffixes;
 };
 
-ArchiveFile *
+std::unique_ptr<ArchiveFile>
 archive_file_open(const ArchivePlugin *plugin, Path path);
 
 #endif
diff --git a/src/archive/plugins/Bzip2ArchivePlugin.cxx b/src/archive/plugins/Bzip2ArchivePlugin.cxx
index 0cd849c19..ec29f81f9 100644
--- a/src/archive/plugins/Bzip2ArchivePlugin.cxx
+++ b/src/archive/plugins/Bzip2ArchivePlugin.cxx
@@ -102,13 +102,13 @@ Bzip2InputStream::Open()
 
 /* archive open && listing routine */
 
-static ArchiveFile *
+static std::unique_ptr<ArchiveFile>
 bz2_open(Path pathname)
 {
 	static Mutex mutex;
 	static Cond cond;
 	auto is = OpenLocalInputStream(pathname, mutex, cond);
-	return new Bzip2ArchiveFile(pathname, std::move(is));
+	return std::make_unique<Bzip2ArchiveFile>(pathname, std::move(is));
 }
 
 /* single archive handling */
diff --git a/src/archive/plugins/Iso9660ArchivePlugin.cxx b/src/archive/plugins/Iso9660ArchivePlugin.cxx
index 5a20c637c..a5d8f4d94 100644
--- a/src/archive/plugins/Iso9660ArchivePlugin.cxx
+++ b/src/archive/plugins/Iso9660ArchivePlugin.cxx
@@ -116,10 +116,10 @@ Iso9660ArchiveFile::Visit(char *path, size_t length, size_t capacity,
 	_cdio_list_free (entlist, true);
 }
 
-static ArchiveFile *
+static std::unique_ptr<ArchiveFile>
 iso9660_archive_open(Path pathname)
 {
-	return new Iso9660ArchiveFile(std::make_shared<Iso9660>(pathname));
+	return std::make_unique<Iso9660ArchiveFile>(std::make_shared<Iso9660>(pathname));
 }
 
 void
diff --git a/src/archive/plugins/ZzipArchivePlugin.cxx b/src/archive/plugins/ZzipArchivePlugin.cxx
index 5660e4286..0fbc4cc35 100644
--- a/src/archive/plugins/ZzipArchivePlugin.cxx
+++ b/src/archive/plugins/ZzipArchivePlugin.cxx
@@ -65,10 +65,10 @@ public:
 
 /* archive open && listing routine */
 
-static ArchiveFile *
+static std::unique_ptr<ArchiveFile>
 zzip_archive_open(Path pathname)
 {
-	return new ZzipArchiveFile(std::make_shared<ZzipDir>(pathname));
+	return std::make_unique<ZzipArchiveFile>(std::make_shared<ZzipDir>(pathname));
 }
 
 inline void
diff --git a/src/db/update/Archive.cxx b/src/db/update/Archive.cxx
index d20bbf462..956201067 100644
--- a/src/db/update/Archive.cxx
+++ b/src/db/update/Archive.cxx
@@ -150,7 +150,7 @@ UpdateWalk::UpdateArchiveFile(Directory &parent, const char *name,
 		return;
 
 	/* open archive */
-	ArchiveFile *file;
+	std::unique_ptr<ArchiveFile> file;
 	try {
 		file = archive_file_open(&plugin, path_fs);
 	} catch (...) {
@@ -177,7 +177,6 @@ UpdateWalk::UpdateArchiveFile(Directory &parent, const char *name,
 
 	UpdateArchiveVisitor visitor(*this, *file, directory);
 	file->Visit(visitor);
-	delete file;
 }
 
 bool
diff --git a/src/input/plugins/ArchiveInputPlugin.cxx b/src/input/plugins/ArchiveInputPlugin.cxx
index 84565548a..1b5914864 100644
--- a/src/input/plugins/ArchiveInputPlugin.cxx
+++ b/src/input/plugins/ArchiveInputPlugin.cxx
@@ -60,13 +60,8 @@ OpenArchiveInputStream(Path path, Mutex &mutex, Cond &cond)
 		return nullptr;
 	}
 
-	auto file = archive_file_open(arplug, Path::FromFS(archive));
-
-	AtScopeExit(file) {
-		delete file;
-	};
-
-	return file->OpenStream(filename, mutex, cond);
+	return archive_file_open(arplug, Path::FromFS(archive))
+		->OpenStream(filename, mutex, cond);
 }
 
 static InputStreamPtr
diff --git a/test/visit_archive.cxx b/test/visit_archive.cxx
index 490bc7c4a..5bb02a02e 100644
--- a/test/visit_archive.cxx
+++ b/test/visit_archive.cxx
@@ -89,11 +89,10 @@ try {
 
 	int result = EXIT_SUCCESS;
 
-	ArchiveFile *file = archive_file_open(plugin, path);
+	auto file = archive_file_open(plugin, path);
 
 	MyArchiveVisitor visitor;
 	file->Visit(visitor);
-	delete file;
 
 	return result;
 } catch (const std::exception &e) {
-- 
2.24.1