ArchiveInputPlugin.cxx 2.6 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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
#include "ArchiveDomain.hxx"
23 24 25
#include "ArchiveLookup.hxx"
#include "ArchiveList.hxx"
#include "ArchivePlugin.hxx"
26
#include "ArchiveFile.hxx"
27
#include "InputPlugin.hxx"
28
#include "fs/Traits.hxx"
29
#include "util/Alloc.hxx"
30
#include "Log.hxx"
31

32
#include <stdlib.h>
33 34 35 36 37 38 39 40 41

/**
 * select correct archive plugin to handle the input stream
 * may allow stacking of archive plugins. for example for handling
 * tar.gz a gzip handler opens file (through inputfile stream)
 * then it opens a tar handler and sets gzip inputstream as
 * parent_stream so tar plugin fetches file data from gzip
 * plugin and gzip fetches file from disk
 */
42
static InputStream *
43
input_archive_open(const char *pathname,
44
		   Mutex &mutex, Cond &cond,
45
		   Error &error)
46 47
{
	const struct archive_plugin *arplug;
48
	InputStream *is;
49

50
	if (!PathTraitsFS::IsAbsolute(pathname))
51
		return nullptr;
52

53
	char *pname = strdup(pathname);
54
	// archive_lookup will modify pname when true is returned
55
	const char *archive, *filename, *suffix;
56
	if (!archive_lookup(pname, &archive, &filename, &suffix)) {
57 58
		FormatDebug(archive_domain,
			    "not an archive, lookup %s failed", pname);
59
		free(pname);
60
		return nullptr;
61 62 63 64 65
	}

	//check which archive plugin to use (by ext)
	arplug = archive_plugin_from_suffix(suffix);
	if (!arplug) {
66 67
		FormatWarning(archive_domain,
			      "can't handle archive %s", archive);
68
		free(pname);
69
		return nullptr;
70 71
	}

72
	auto file = archive_file_open(arplug, archive, error);
73
	if (file == nullptr) {
74
		free(pname);
75
		return nullptr;
76
	}
77 78

	//setup fileops
79
	is = file->OpenStream(filename, mutex, cond, error);
80
	free(pname);
81
	file->Close();
82

83
	return is;
84 85
}

86
const InputPlugin input_plugin_archive = {
87 88 89 90 91 92 93 94 95 96 97 98
	"archive",
	nullptr,
	nullptr,
	input_archive_open,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
99
};