ArchiveInputPlugin.cxx 2.31 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20
#include "config.h"
21
#include "ArchiveInputPlugin.hxx"
22 23 24 25 26
#include "archive/ArchiveDomain.hxx"
#include "archive/ArchiveLookup.hxx"
#include "archive/ArchiveList.hxx"
#include "archive/ArchivePlugin.hxx"
#include "archive/ArchiveFile.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "../InputPlugin.hxx"
28
#include "../InputStream.hxx"
29
#include "fs/Path.hxx"
30
#include "Log.hxx"
31
#include "util/ScopeExit.hxx"
32 33

#include <stdexcept>
34

35
#include <stdlib.h>
36

37 38
InputStreamPtr
OpenArchiveInputStream(Path path, Mutex &mutex, Cond &cond)
39
{
40
	const ArchivePlugin *arplug;
41

42
	char *pname = strdup(path.c_str());
43 44 45 46
	AtScopeExit(pname) {
		free(pname);
	};

47
	// archive_lookup will modify pname when true is returned
48
	const char *archive, *filename, *suffix;
49
	if (!archive_lookup(pname, &archive, &filename, &suffix)) {
50 51
		FormatDebug(archive_domain,
			    "not an archive, lookup %s failed", pname);
52
		return nullptr;
53 54 55 56 57
	}

	//check which archive plugin to use (by ext)
	arplug = archive_plugin_from_suffix(suffix);
	if (!arplug) {
58 59
		FormatWarning(archive_domain,
			      "can't handle archive %s", archive);
60
		return nullptr;
61 62
	}

63
	auto file = archive_file_open(arplug, Path::FromFS(archive));
64

65 66 67
	AtScopeExit(file) {
		file->Close();
	};
68

69
	return InputStreamPtr(file->OpenStream(filename, mutex, cond));
70 71
}

72
static InputStream *
73
input_archive_open(gcc_unused const char *filename,
74
		   gcc_unused Mutex &mutex, gcc_unused Cond &cond)
75
{
76
	/* dummy method; use OpenArchiveInputStream() instead */
77

78
	return nullptr;
79 80
}

81
const InputPlugin input_plugin_archive = {
82 83 84 85
	"archive",
	nullptr,
	nullptr,
	input_archive_open,
86
};