ZzipArchivePlugin.cxx 3.98 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
#include "input/InputStream.hxx"
30
#include "fs/Path.hxx"
31
#include "util/RuntimeError.hxx"
32 33

#include <zzip/zzip.h>
34

35
struct ZzipDir {
36
	ZZIP_DIR *const dir;
37

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

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

49 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:
	ZzipArchiveFile(std::shared_ptr<ZzipDir> &&_dir)
		:dir(std::move(_dir)) {}
59

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

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

/* archive open && listing routine */

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

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

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

/* single archive handling */

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

91
	ZZIP_FILE *const file;
92

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

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

		SetReady();
107 108 109 110 111
	}

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

	/* virtual methods from InputStream */
114
	bool IsEOF() noexcept override;
115 116
	size_t Read(void *ptr, size_t size) override;
	void Seek(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(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() noexcept
148
{
149
	return offset_type(zzip_tell(file)) == size;
150 151
}

152 153
void
ZzipInputStream::Seek(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
};