ZzipArchivePlugin.cxx 4.66 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "ZzipArchivePlugin.hxx"
25 26 27
#include "../ArchivePlugin.hxx"
#include "../ArchiveFile.hxx"
#include "../ArchiveVisitor.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "input/InputStream.hxx"
29
#include "fs/Path.hxx"
30
#include "system/Error.hxx"
31
#include "util/RuntimeError.hxx"
32 33

#include <zzip/zzip.h>
34

35 36
#include <utility>

37 38
#include <inttypes.h> /* for PRIoffset (PRIu64) */

39
struct ZzipDir {
40
	ZZIP_DIR *const dir;
41

42 43 44 45 46 47
	explicit ZzipDir(Path path)
		:dir(zzip_dir_open(path.c_str(), nullptr)) {
		if (dir == nullptr)
			throw FormatRuntimeError("Failed to open ZIP file %s",
						 path.c_str());
	}
48

49
	~ZzipDir() noexcept {
50 51
		zzip_dir_close(dir);
	}
52

53 54 55 56 57 58 59 60
	ZzipDir(const ZzipDir &) = delete;
	ZzipDir &operator=(const ZzipDir &) = delete;
};

class ZzipArchiveFile final : public ArchiveFile {
	std::shared_ptr<ZzipDir> dir;

public:
61 62 63
	template<typename D>
	explicit ZzipArchiveFile(D &&_dir) noexcept
		:dir(std::forward<D>(_dir)) {}
64

65
	void Visit(ArchiveVisitor &visitor) override;
66

67
	InputStreamPtr OpenStream(const char *path,
68
				  Mutex &mutex) override;
69
};
70 71 72

/* archive open && listing routine */

73
static std::unique_ptr<ArchiveFile>
74
zzip_archive_open(Path pathname)
75
{
76
	return std::make_unique<ZzipArchiveFile>(std::make_shared<ZzipDir>(pathname));
77 78
}

79 80
inline void
ZzipArchiveFile::Visit(ArchiveVisitor &visitor)
81
{
82
	zzip_rewinddir(dir->dir);
83 84

	ZZIP_DIRENT dirent;
85
	while (zzip_dir_read(dir->dir, &dirent))
86 87 88
		//add only files
		if (dirent.st_size > 0)
			visitor.VisitArchiveEntry(dirent.d_name);
89 90 91 92
}

/* single archive handling */

93
class ZzipInputStream final : public InputStream {
94
	std::shared_ptr<ZzipDir> dir;
95

96
	ZZIP_FILE *const file;
97

98
public:
99 100
	template<typename D>
	ZzipInputStream(D &&_dir, const char *_uri,
101
			Mutex &_mutex,
102
			ZZIP_FILE *_file)
103
		:InputStream(_uri, _mutex),
104
		 dir(std::forward<D>(_dir)), file(_file) {
105
		//we are seekable (but its not recommendent to do so)
106
		seekable = true;
107 108 109

		ZZIP_STAT z_stat;
		zzip_file_stat(file, &z_stat);
110 111 112
		size = z_stat.st_size;

		SetReady();
113 114
	}

115
	~ZzipInputStream() noexcept override {
116 117
		zzip_file_close(file);
	}
118 119

	/* virtual methods from InputStream */
120
	[[nodiscard]] bool IsEOF() const noexcept override;
121 122 123
	size_t Read(std::unique_lock<Mutex> &lock,
		    void *ptr, size_t size) override;
	void Seek(std::unique_lock<Mutex> &lock, offset_type offset) override;
124 125
};

126
InputStreamPtr
127
ZzipArchiveFile::OpenStream(const char *pathname,
128
			    Mutex &mutex)
129
{
130
	ZZIP_FILE *_file = zzip_file_open(dir->dir, pathname, 0);
131 132 133 134 135 136 137 138 139 140 141 142 143
	if (_file == nullptr) {
		const auto error = (zzip_error_t)zzip_error(dir->dir);
		switch (error) {
		case ZZIP_ENOENT:
			throw FormatFileNotFound("Failed to open '%s' in ZIP file",
						 pathname);

		default:
			throw FormatRuntimeError("Failed to open '%s' in ZIP file: %s",
						 pathname,
						 zzip_strerror(error));
		}
	}
144

145
	return std::make_unique<ZzipInputStream>(dir, pathname,
146
						 mutex,
147
						 _file);
148 149
}

150
size_t
151
ZzipInputStream::Read(std::unique_lock<Mutex> &, void *ptr, size_t read_size)
152
{
153 154
	const ScopeUnlock unlock(mutex);

155 156
	zzip_ssize_t nbytes = zzip_file_read(file, ptr, read_size);
	if (nbytes < 0)
157
		throw std::runtime_error("zzip_file_read() has failed");
158

159 160 161 162 163
	if (nbytes == 0 && !IsEOF())
		throw FormatRuntimeError("Unexpected end of file %s"
					 " at %" PRIoffset " of %" PRIoffset,
					 GetURI(), GetOffset(), GetSize());

164
	offset = zzip_tell(file);
165
	return nbytes;
166 167
}

168
bool
169
ZzipInputStream::IsEOF() const noexcept
170
{
171
	return offset_type(zzip_tell(file)) == size;
172 173
}

174
void
175
ZzipInputStream::Seek(std::unique_lock<Mutex> &, offset_type new_offset)
176
{
177 178
	const ScopeUnlock unlock(mutex);

179
	zzip_off_t ofs = zzip_seek(file, new_offset, SEEK_SET);
180 181
	if (ofs < 0)
		throw std::runtime_error("zzip_seek() has failed");
182

183
	offset = ofs;
184 185 186 187
}

/* exported structures */

188
static const char *const zzip_archive_extensions[] = {
189
	"zip",
190
	nullptr
191 192
};

193
const ArchivePlugin zzip_archive_plugin = {
194 195 196 197 198
	"zzip",
	nullptr,
	nullptr,
	zzip_archive_open,
	zzip_archive_extensions,
199
};