ArchiveInputPlugin.cxx 1.89 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2018 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 "ArchiveInputPlugin.hxx"
21 22 23 24 25
#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
26
#include "../InputPlugin.hxx"
27
#include "../InputStream.hxx"
28
#include "fs/Path.hxx"
29
#include "Log.hxx"
30
#include "util/ScopeExit.hxx"
31 32

#include <stdexcept>
33

34
#include <stdlib.h>
35

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

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

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

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

62
	return archive_file_open(arplug, Path::FromFS(archive))
63
		->OpenStream(filename, mutex);
64
}