ZzipArchivePlugin.cxx 4.5 KB
Newer Older
1
/*
2
 * Copyright 2003-2019 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 <inttypes.h> /* for PRIoffset (PRIu64) */

37
struct ZzipDir {
38
	ZZIP_DIR *const dir;
39

40 41 42 43 44 45
	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());
	}
46

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

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

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

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

63
	void Visit(ArchiveVisitor &visitor) override;
64

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

/* archive open && listing routine */

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

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

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

/* single archive handling */

91
class ZzipInputStream final : public InputStream {
92
	std::shared_ptr<ZzipDir> dir;
93

94
	ZZIP_FILE *const file;
95

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

		ZZIP_STAT z_stat;
		zzip_file_stat(file, &z_stat);
108 109 110
		size = z_stat.st_size;

		SetReady();
111 112
	}

113
	~ZzipInputStream() noexcept override {
114 115
		zzip_file_close(file);
	}
116 117

	/* virtual methods from InputStream */
118
	bool IsEOF() noexcept override;
119 120
	size_t Read(void *ptr, size_t size) override;
	void Seek(offset_type offset) override;
121 122
};

123
InputStreamPtr
124
ZzipArchiveFile::OpenStream(const char *pathname,
125
			    Mutex &mutex)
126
{
127
	ZZIP_FILE *_file = zzip_file_open(dir->dir, pathname, 0);
128 129 130 131 132 133 134 135 136 137 138 139 140
	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));
		}
	}
141

142
	return std::make_unique<ZzipInputStream>(dir, pathname,
143
						 mutex,
144
						 _file);
145 146
}

147
size_t
148
ZzipInputStream::Read(void *ptr, size_t read_size)
149
{
150 151
	const ScopeUnlock unlock(mutex);

152 153
	zzip_ssize_t nbytes = zzip_file_read(file, ptr, read_size);
	if (nbytes < 0)
154
		throw std::runtime_error("zzip_file_read() has failed");
155

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

161
	offset = zzip_tell(file);
162
	return nbytes;
163 164
}

165
bool
166
ZzipInputStream::IsEOF() noexcept
167
{
168
	return offset_type(zzip_tell(file)) == size;
169 170
}

171 172
void
ZzipInputStream::Seek(offset_type new_offset)
173
{
174 175
	const ScopeUnlock unlock(mutex);

176
	zzip_off_t ofs = zzip_seek(file, new_offset, SEEK_SET);
177 178
	if (ofs < 0)
		throw std::runtime_error("zzip_seek() has failed");
179

180
	offset = ofs;
181 182 183 184
}

/* exported structures */

185
static const char *const zzip_archive_extensions[] = {
186
	"zip",
187
	nullptr
188 189
};

190
const ArchivePlugin zzip_archive_plugin = {
191 192 193 194 195
	"zzip",
	nullptr,
	nullptr,
	zzip_archive_open,
	zzip_archive_extensions,
196
};