ZzipArchivePlugin.cxx 3.9 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/RefCount.hxx"
32
#include "util/RuntimeError.hxx"
33 34

#include <zzip/zzip.h>
35

36
class ZzipArchiveFile final : public ArchiveFile {
37
public:
38
	RefCount ref;
39

40
	ZZIP_DIR *const dir;
41

42 43
	ZzipArchiveFile(ZZIP_DIR *_dir)
		:ArchiveFile(zzip_archive_plugin), dir(_dir) {}
44

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

49 50 51
	void Unref() {
		if (ref.Decrement())
			delete this;
52
	}
53

54 55 56 57 58 59
	virtual void Close() override {
		Unref();
	}

	virtual void Visit(ArchiveVisitor &visitor) override;

60 61
	InputStream *OpenStream(const char *path,
				Mutex &mutex, Cond &cond) override;
62
};
63 64 65

/* archive open && listing routine */

66
static ArchiveFile *
67
zzip_archive_open(Path pathname)
68
{
69
	ZZIP_DIR *dir = zzip_dir_open(pathname.c_str(), nullptr);
70 71 72
	if (dir == nullptr)
		throw FormatRuntimeError("Failed to open ZIP file %s",
					 pathname.c_str());
73

74
	return new ZzipArchiveFile(dir);
75 76
}

77 78
inline void
ZzipArchiveFile::Visit(ArchiveVisitor &visitor)
79
{
80 81 82 83 84 85 86
	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);
87 88 89 90
}

/* single archive handling */

91
struct ZzipInputStream final : public InputStream {
92
	ZzipArchiveFile *archive;
93 94

	ZZIP_FILE *file;
95

96 97
	ZzipInputStream(ZzipArchiveFile &_archive, const char *_uri,
			Mutex &_mutex, Cond &_cond,
98
			ZZIP_FILE *_file)
99
		:InputStream(_uri, _mutex, _cond),
100
		 archive(&_archive), file(_file) {
101
		//we are seekable (but its not recommendent to do so)
102
		seekable = true;
103 104 105

		ZZIP_STAT z_stat;
		zzip_file_stat(file, &z_stat);
106 107 108
		size = z_stat.st_size;

		SetReady();
109

110
		archive->ref.Increment();
111 112 113 114 115 116
	}

	~ZzipInputStream() {
		zzip_file_close(file);
		archive->Unref();
	}
117 118

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

124
InputStream *
125
ZzipArchiveFile::OpenStream(const char *pathname,
126
			    Mutex &mutex, Cond &cond)
127
{
128
	ZZIP_FILE *_file = zzip_file_open(dir, pathname, 0);
129 130 131
	if (_file == nullptr)
		throw FormatRuntimeError("not found in the ZIP file: %s",
					 pathname);
132

133 134 135
	return new ZzipInputStream(*this, pathname,
				   mutex, cond,
				   _file);
136 137
}

138
size_t
139
ZzipInputStream::Read(void *ptr, size_t read_size)
140
{
141
	int ret = zzip_file_read(file, ptr, read_size);
142 143
	if (ret < 0)
		throw std::runtime_error("zzip_file_read() has failed");
144

145
	offset = zzip_tell(file);
146 147 148
	return ret;
}

149
bool
150
ZzipInputStream::IsEOF() noexcept
151
{
152
	return offset_type(zzip_tell(file)) == size;
153 154
}

155 156
void
ZzipInputStream::Seek(offset_type new_offset)
157
{
158
	zzip_off_t ofs = zzip_seek(file, new_offset, SEEK_SET);
159 160
	if (ofs < 0)
		throw std::runtime_error("zzip_seek() has failed");
161

162
	offset = ofs;
163 164 165 166
}

/* exported structures */

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

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