ZzipArchivePlugin.cxx 4.2 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
static constexpr Domain zzip_domain("zzip");
68

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

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

81
	return new ZzipArchiveFile(dir);
82 83
}

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

/* single archive handling */

98
struct ZzipInputStream final : public InputStream {
99
	ZzipArchiveFile *archive;
100 101

	ZZIP_FILE *file;
102

103 104
	ZzipInputStream(ZzipArchiveFile &_archive, const char *_uri,
			Mutex &_mutex, Cond &_cond,
105
			ZZIP_FILE *_file)
106
		:InputStream(_uri, _mutex, _cond),
107
		 archive(&_archive), file(_file) {
108
		//we are seekable (but its not recommendent to do so)
109
		seekable = true;
110 111 112

		ZZIP_STAT z_stat;
		zzip_file_stat(file, &z_stat);
113 114 115
		size = z_stat.st_size;

		SetReady();
116

117
		archive->ref.Increment();
118 119 120 121 122 123
	}

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

	/* virtual methods from InputStream */
	bool IsEOF() override;
	size_t Read(void *ptr, size_t size, Error &error) override;
128
	bool Seek(offset_type offset, Error &error) override;
129 130
};

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

143 144 145
	return new ZzipInputStream(*this, pathname,
				   mutex, cond,
				   _file);
146 147
}

148 149
size_t
ZzipInputStream::Read(void *ptr, size_t read_size, Error &error)
150
{
151
	int ret = zzip_file_read(file, ptr, read_size);
152
	if (ret < 0) {
153
		error.Set(zzip_domain, "zzip_file_read() has failed");
154 155
		return 0;
	}
156

157
	offset = zzip_tell(file);
158 159 160
	return ret;
}

161 162
bool
ZzipInputStream::IsEOF()
163
{
164
	return offset_type(zzip_tell(file)) == size;
165 166
}

167
bool
168
ZzipInputStream::Seek(offset_type new_offset, Error &error)
169
{
170
	zzip_off_t ofs = zzip_seek(file, new_offset, SEEK_SET);
171
	if (ofs < 0) {
172
		error.Set(zzip_domain, "zzip_seek() has failed");
173
		return false;
174
	}
175

176
	offset = ofs;
177
	return true;
178 179 180 181
}

/* exported structures */

182
static const char *const zzip_archive_extensions[] = {
183
	"zip",
184
	nullptr
185 186
};

187
const ArchivePlugin zzip_archive_plugin = {
188 189 190 191 192
	"zzip",
	nullptr,
	nullptr,
	zzip_archive_open,
	zzip_archive_extensions,
193
};