ZzipArchivePlugin.cxx 4.09 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
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 "util/RuntimeError.hxx"
31 32

#include <zzip/zzip.h>
33

34
struct ZzipDir {
35
	ZZIP_DIR *const dir;
36

37 38 39 40 41 42
	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());
	}
43

44
	~ZzipDir() noexcept {
45 46
		zzip_dir_close(dir);
	}
47

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

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

public:
	ZzipArchiveFile(std::shared_ptr<ZzipDir> &&_dir)
		:dir(std::move(_dir)) {}
58

59 60
	virtual void Visit(ArchiveVisitor &visitor) override;

61
	InputStreamPtr OpenStream(const char *path,
62
				  Mutex &mutex) override;
63
};
64 65 66

/* archive open && listing routine */

67
static std::unique_ptr<ArchiveFile>
68
zzip_archive_open(Path pathname)
69
{
70
	return std::make_unique<ZzipArchiveFile>(std::make_shared<ZzipDir>(pathname));
71 72
}

73 74
inline void
ZzipArchiveFile::Visit(ArchiveVisitor &visitor)
75
{
76
	zzip_rewinddir(dir->dir);
77 78

	ZZIP_DIRENT dirent;
79
	while (zzip_dir_read(dir->dir, &dirent))
80 81 82
		//add only files
		if (dirent.st_size > 0)
			visitor.VisitArchiveEntry(dirent.d_name);
83 84 85 86
}

/* single archive handling */

87
class ZzipInputStream final : public InputStream {
88
	std::shared_ptr<ZzipDir> dir;
89

90
	ZZIP_FILE *const file;
91

92
public:
93
	ZzipInputStream(const std::shared_ptr<ZzipDir> _dir, const char *_uri,
94
			Mutex &_mutex,
95
			ZZIP_FILE *_file)
96
		:InputStream(_uri, _mutex),
97
		 dir(_dir), file(_file) {
98
		//we are seekable (but its not recommendent to do so)
99
		seekable = true;
100 101 102

		ZZIP_STAT z_stat;
		zzip_file_stat(file, &z_stat);
103 104 105
		size = z_stat.st_size;

		SetReady();
106 107 108 109 110
	}

	~ZzipInputStream() {
		zzip_file_close(file);
	}
111 112

	/* virtual methods from InputStream */
113
	bool IsEOF() const noexcept override;
114 115 116
	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;
117 118
};

119
InputStreamPtr
120
ZzipArchiveFile::OpenStream(const char *pathname,
121
			    Mutex &mutex)
122
{
123
	ZZIP_FILE *_file = zzip_file_open(dir->dir, pathname, 0);
124 125 126
	if (_file == nullptr)
		throw FormatRuntimeError("not found in the ZIP file: %s",
					 pathname);
127

128
	return std::make_unique<ZzipInputStream>(dir, pathname,
129
						 mutex,
130
						 _file);
131 132
}

133
size_t
134
ZzipInputStream::Read(std::unique_lock<Mutex> &, void *ptr, size_t read_size)
135
{
136 137
	const ScopeUnlock unlock(mutex);

138
	int ret = zzip_file_read(file, ptr, read_size);
139 140
	if (ret < 0)
		throw std::runtime_error("zzip_file_read() has failed");
141

142
	offset = zzip_tell(file);
143 144 145
	return ret;
}

146
bool
147
ZzipInputStream::IsEOF() const noexcept
148
{
149
	return offset_type(zzip_tell(file)) == size;
150 151
}

152
void
153
ZzipInputStream::Seek(std::unique_lock<Mutex> &, offset_type new_offset)
154
{
155 156
	const ScopeUnlock unlock(mutex);

157
	zzip_off_t ofs = zzip_seek(file, new_offset, SEEK_SET);
158 159
	if (ofs < 0)
		throw std::runtime_error("zzip_seek() has failed");
160

161
	offset = ofs;
162 163 164 165
}

/* exported structures */

166
static const char *const zzip_archive_extensions[] = {
167
	"zip",
168
	nullptr
169 170
};

171
const ArchivePlugin zzip_archive_plugin = {
172 173 174 175 176
	"zzip",
	nullptr,
	nullptr,
	zzip_archive_open,
	zzip_archive_extensions,
177
};