ZzipArchivePlugin.cxx 4.65 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 "util/RefCount.hxx"
32 33
#include "util/Error.hxx"
#include "util/Domain.hxx"
34 35

#include <zzip/zzip.h>
36

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

41
	ZZIP_DIR *const dir;
42

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

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

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

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

	virtual void Visit(ArchiveVisitor &visitor) override;

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

66
extern const InputPlugin zzip_input_plugin;
67

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

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

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

82
	return new ZzipArchiveFile(dir);
83 84
}

85 86
inline void
ZzipArchiveFile::Visit(ArchiveVisitor &visitor)
87
{
88 89 90 91 92 93 94
	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);
95 96 97 98
}

/* single archive handling */

99
struct ZzipInputStream {
100
	InputStream base;
101

102
	ZzipArchiveFile *archive;
103 104

	ZZIP_FILE *file;
105 106 107 108

	ZzipInputStream(ZzipArchiveFile &_archive, const char *uri,
			Mutex &mutex, Cond &cond,
			ZZIP_FILE *_file)
109 110
		:base(zzip_input_plugin, uri, mutex, cond),
		 archive(&_archive), file(_file) {
111 112 113 114 115 116 117 118
		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;

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

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

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

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

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

152
	delete zis;
153 154 155
}

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

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

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

170 171 172 173
	return ret;
}

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

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

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

/* exported structures */

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

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

217
const struct archive_plugin zzip_archive_plugin = {
218 219 220 221 222
	"zzip",
	nullptr,
	nullptr,
	zzip_archive_open,
	zzip_archive_extensions,
223
};