ArchiveInputPlugin.cxx 2.39 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "fs/Traits.hxx"
29
#include "fs/Path.hxx"
30
#include "util/Alloc.hxx"
31
#include "Log.hxx"
32

33
#include <stdlib.h>
34

35 36
InputStream *
OpenArchiveInputStream(Path path, Mutex &mutex, Cond &cond, Error &error)
37
{
38
	const ArchivePlugin *arplug;
39
	InputStream *is;
40

41
	char *pname = strdup(path.c_str());
42
	// archive_lookup will modify pname when true is returned
43
	const char *archive, *filename, *suffix;
44
	if (!archive_lookup(pname, &archive, &filename, &suffix)) {
45 46
		FormatDebug(archive_domain,
			    "not an archive, lookup %s failed", pname);
47
		free(pname);
48
		return nullptr;
49 50 51 52 53
	}

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

60
	auto file = archive_file_open(arplug, Path::FromFS(archive), error);
61
	if (file == nullptr) {
62
		free(pname);
63
		return nullptr;
64
	}
65 66

	//setup fileops
67
	is = file->OpenStream(filename, mutex, cond, error);
68
	free(pname);
69
	file->Close();
70

71
	return is;
72 73
}

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

81
	return nullptr;
82 83
}

84
const InputPlugin input_plugin_archive = {
85 86 87 88
	"archive",
	nullptr,
	nullptr,
	input_archive_open,
89
};