ZzipArchivePlugin.cxx 4.67 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 21 22 23
 */

/**
  * zip archive handling (requires zziplib)
  */

24
#include "config.h"
25
#include "ZzipArchivePlugin.hxx"
26 27 28
#include "../ArchivePlugin.hxx"
#include "../ArchiveFile.hxx"
#include "../ArchiveVisitor.hxx"
Max Kellermann's avatar
Max Kellermann committed
29 30
#include "input/InputStream.hxx"
#include "input/InputPlugin.hxx"
31
#include "fs/Path.hxx"
32
#include "util/RefCount.hxx"
33 34
#include "util/Error.hxx"
#include "util/Domain.hxx"
35 36

#include <zzip/zzip.h>
37

38
class ZzipArchiveFile final : public ArchiveFile {
39
public:
40
	RefCount ref;
41

42
	ZZIP_DIR *const dir;
43

44 45
	ZzipArchiveFile(ZZIP_DIR *_dir)
		:ArchiveFile(zzip_archive_plugin), dir(_dir) {}
46

47 48 49
	~ZzipArchiveFile() {
		zzip_dir_close(dir);
	}
50

51 52 53
	void Unref() {
		if (ref.Decrement())
			delete this;
54
	}
55

56 57 58 59 60 61
	virtual void Close() override {
		Unref();
	}

	virtual void Visit(ArchiveVisitor &visitor) override;

62 63 64
	virtual InputStream *OpenStream(const char *path,
					Mutex &mutex, Cond &cond,
					Error &error) override;
65
};
66

67
extern const InputPlugin zzip_input_plugin;
68

69
static constexpr Domain zzip_domain("zzip");
70

71 72
/* archive open && listing routine */

73
static ArchiveFile *
74
zzip_archive_open(Path pathname, Error &error)
75
{
76
	ZZIP_DIR *dir = zzip_dir_open(pathname.c_str(), nullptr);
77
	if (dir == nullptr) {
78
		error.Format(zzip_domain, "Failed to open ZIP file %s",
79
			     pathname.c_str());
80
		return nullptr;
81 82
	}

83
	return new ZzipArchiveFile(dir);
84 85
}

86 87
inline void
ZzipArchiveFile::Visit(ArchiveVisitor &visitor)
88
{
89 90 91 92 93 94 95
	zzip_rewinddir(dir);

	ZZIP_DIRENT dirent;
	while (zzip_dir_read(dir, &dirent))
		//add only files
		if (dirent.st_size > 0)
			visitor.VisitArchiveEntry(dirent.d_name);
96 97 98 99
}

/* single archive handling */

100
struct ZzipInputStream {
101
	InputStream base;
102

103
	ZzipArchiveFile *archive;
104 105

	ZZIP_FILE *file;
106 107 108 109

	ZzipInputStream(ZzipArchiveFile &_archive, const char *uri,
			Mutex &mutex, Cond &cond,
			ZZIP_FILE *_file)
110 111
		:base(zzip_input_plugin, uri, mutex, cond),
		 archive(&_archive), file(_file) {
112 113 114 115 116 117 118 119
		base.ready = true;
		//we are seekable (but its not recommendent to do so)
		base.seekable = true;

		ZZIP_STAT z_stat;
		zzip_file_stat(file, &z_stat);
		base.size = z_stat.st_size;

120
		archive->ref.Increment();
121 122 123 124 125 126
	}

	~ZzipInputStream() {
		zzip_file_close(file);
		archive->Unref();
	}
127 128
};

129
InputStream *
130 131
ZzipArchiveFile::OpenStream(const char *pathname,
			    Mutex &mutex, Cond &cond,
132
			    Error &error)
133
{
134
	ZZIP_FILE *_file = zzip_file_open(dir, pathname, 0);
135
	if (_file == nullptr) {
136 137
		error.Format(zzip_domain, "not found in the ZIP file: %s",
			     pathname);
138
		return nullptr;
139 140
	}

141
	ZzipInputStream *zis =
142
		new ZzipInputStream(*this, pathname,
143 144
				    mutex, cond,
				    _file);
145
	return &zis->base;
146 147 148
}

static void
149
zzip_input_close(InputStream *is)
150
{
151
	ZzipInputStream *zis = (ZzipInputStream *)is;
152

153
	delete zis;
154 155 156
}

static size_t
157
zzip_input_read(InputStream *is, void *ptr, size_t size,
158
		Error &error)
159
{
160
	ZzipInputStream *zis = (ZzipInputStream *)is;
161
	int ret;
162 163

	ret = zzip_file_read(zis->file, ptr, size);
164
	if (ret < 0) {
165
		error.Set(zzip_domain, "zzip_file_read() has failed");
166 167
		return 0;
	}
168

169
	is->offset = zzip_tell(zis->file);
170

171 172 173 174
	return ret;
}

static bool
175
zzip_input_eof(InputStream *is)
176
{
177
	ZzipInputStream *zis = (ZzipInputStream *)is;
178

179
	return (InputPlugin::offset_type)zzip_tell(zis->file) == is->size;
180 181 182
}

static bool
183
zzip_input_seek(InputStream *is, InputPlugin::offset_type offset,
184
		int whence, Error &error)
185
{
186
	ZzipInputStream *zis = (ZzipInputStream *)is;
187
	zzip_off_t ofs = zzip_seek(zis->file, offset, whence);
188
	if (ofs != -1) {
189
		error.Set(zzip_domain, "zzip_seek() has failed");
190 191 192
		is->offset = ofs;
		return true;
	}
193 194 195 196 197
	return false;
}

/* exported structures */

198
static const char *const zzip_archive_extensions[] = {
199
	"zip",
200
	nullptr
201 202
};

203
const InputPlugin zzip_input_plugin = {
204 205 206 207 208 209 210 211 212 213 214 215
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	zzip_input_close,
	nullptr,
	nullptr,
	nullptr,
	nullptr,
	zzip_input_read,
	zzip_input_eof,
	zzip_input_seek,
216 217
};

218
const ArchivePlugin zzip_archive_plugin = {
219 220 221 222 223
	"zzip",
	nullptr,
	nullptr,
	zzip_archive_open,
	zzip_archive_extensions,
224
};