Iso9660ArchivePlugin.cxx 5.78 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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
 */

/**
  * iso archive handling (requires cdio, and iso9660)
  */

24
#include "Iso9660ArchivePlugin.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
#include "util/StringCompare.hxx"
32 33 34

#include <cdio/iso9660.h>

35
#include <stdlib.h>
36 37
#include <string.h>

38 39
#include <utility>

40 41 42 43 44
static constexpr size_t
CEILING(size_t x, size_t y) noexcept
{
	return (x + y - 1) / y;
}
45

46 47
struct Iso9660 {
	iso9660_t *const iso;
48

49 50 51 52 53
	explicit Iso9660(Path path)
		:iso(iso9660_open(path.c_str())) {
		if (iso == nullptr)
			throw FormatRuntimeError("Failed to open ISO9660 file %s",
						 path.c_str());
54 55
	}

56 57
	~Iso9660() noexcept {
		iso9660_close(iso);
58 59
	}

60 61
	Iso9660(const Iso9660 &) = delete;
	Iso9660 &operator=(const Iso9660 &) = delete;
62

63 64 65
	long SeekRead(void *ptr, lsn_t start, long int i_size) const {
		return iso9660_iso_seek_read(iso, ptr, start, i_size);
	}
66 67 68 69 70 71
};

class Iso9660ArchiveFile final : public ArchiveFile {
	std::shared_ptr<Iso9660> iso;

public:
Max Kellermann's avatar
Max Kellermann committed
72
	explicit Iso9660ArchiveFile(std::shared_ptr<Iso9660> &&_iso)
73
		:iso(std::move(_iso)) {}
74

75 76 77 78
	/**
	 * @param capacity the path buffer size
	 */
	void Visit(char *path, size_t length, size_t capacity,
79
		   ArchiveVisitor &visitor);
80

81
	void Visit(ArchiveVisitor &visitor) override;
82

83
	InputStreamPtr OpenStream(const char *path,
84
				  Mutex &mutex) override;
85
};
86 87 88

/* archive open && listing routine */

89
inline void
90
Iso9660ArchiveFile::Visit(char *path, size_t length, size_t capacity,
91
			  ArchiveVisitor &visitor)
92
{
93
	auto *entlist = iso9660_ifs_readdir(iso->iso, path);
94 95 96 97
	if (!entlist) {
		return;
	}
	/* Iterate over the list of nodes that iso9660_ifs_readdir gives  */
98
	CdioListNode_t *entnode;
99
	_CDIO_LIST_FOREACH (entnode, entlist) {
100 101
		auto *statbuf = (iso9660_stat_t *)
			_cdio_list_node_data(entnode);
102
		const char *filename = statbuf->filename;
103 104 105
		if (StringIsEmpty(filename) ||
		    PathTraitsUTF8::IsSpecialFilename(filename))
			/* skip empty names (libcdio bug?) */
106
			/* skip special names like "." and ".." */
107
			continue;
108

109
		size_t filename_length = strlen(filename);
110 111 112
		if (length + filename_length + 1 >= capacity)
			/* file name is too long */
			continue;
113

114 115
		memcpy(path + length, filename, filename_length + 1);
		size_t new_length = length + filename_length;
116

117
		if (iso9660_stat_s::_STAT_DIR == statbuf->type ) {
118
			memcpy(path + new_length, "/", 2);
119
			Visit(path, new_length + 1, capacity, visitor);
120 121
		} else {
			//remove leading /
122
			visitor.VisitArchiveEntry(path + 1);
123 124
		}
	}
125 126 127 128

#if LIBCDIO_VERSION_NUM >= 20000
	iso9660_filelist_free(entlist);
#else
129
	_cdio_list_free (entlist, true);
130
#endif
131 132
}

133
static std::unique_ptr<ArchiveFile>
134
iso9660_archive_open(Path pathname)
135
{
136
	return std::make_unique<Iso9660ArchiveFile>(std::make_shared<Iso9660>(pathname));
137 138
}

139 140
void
Iso9660ArchiveFile::Visit(ArchiveVisitor &visitor)
141
{
142
	char path[4096] = "/";
143
	Visit(path, 1, sizeof(path), visitor);
144 145 146 147
}

/* single archive handling */

148
class Iso9660InputStream final : public InputStream {
149
	std::shared_ptr<Iso9660> iso;
150 151

	iso9660_stat_t *statbuf;
152

153
public:
154
	Iso9660InputStream(std::shared_ptr<Iso9660> _iso,
155
			   const char *_uri,
156
			   Mutex &_mutex,
157
			   iso9660_stat_t *_statbuf)
158
		:InputStream(_uri, _mutex),
159
		 iso(std::move(_iso)), statbuf(_statbuf) {
160
		size = statbuf->size;
161
		seekable = true;
162
		SetReady();
163 164
	}

165
	~Iso9660InputStream() override {
166 167
		free(statbuf);
	}
168

169
	/* virtual methods from InputStream */
170
	[[nodiscard]] bool IsEOF() const noexcept override;
171 172
	size_t Read(std::unique_lock<Mutex> &lock,
		    void *ptr, size_t size) override;
173 174 175 176

	void Seek(std::unique_lock<Mutex> &, offset_type new_offset) override {
		offset = new_offset;
	}
177 178
};

179
InputStreamPtr
180
Iso9660ArchiveFile::OpenStream(const char *pathname,
181
			       Mutex &mutex)
182
{
183
	auto statbuf = iso9660_ifs_stat_translate(iso->iso, pathname);
184 185 186
	if (statbuf == nullptr)
		throw FormatRuntimeError("not found in the ISO file: %s",
					 pathname);
187

188
	return std::make_unique<Iso9660InputStream>(iso, pathname, mutex,
189
						    statbuf);
190 191
}

192
size_t
193 194
Iso9660InputStream::Read(std::unique_lock<Mutex> &,
			 void *ptr, size_t read_size)
195
{
196 197
	const ScopeUnlock unlock(mutex);

198
	int readed = 0;
199
	int no_blocks, cur_block;
200
	size_t left_bytes = statbuf->size - offset;
201

202 203
	if (left_bytes < read_size) {
		no_blocks = CEILING(left_bytes, ISO_BLOCKSIZE);
204
	} else {
205
		no_blocks = read_size / ISO_BLOCKSIZE;
206 207
	}

208 209
	if (no_blocks == 0)
		return 0;
210

211
	cur_block = offset / ISO_BLOCKSIZE;
212

213
	readed = iso->SeekRead(ptr, statbuf->lsn + cur_block, no_blocks);
214

215 216 217 218
	if (readed != no_blocks * ISO_BLOCKSIZE)
		throw FormatRuntimeError("error reading ISO file at lsn %lu",
					 (unsigned long)cur_block);

219
	if (left_bytes < read_size) {
220 221 222
		readed = left_bytes;
	}

223
	offset += readed;
224 225 226
	return readed;
}

227
bool
228
Iso9660InputStream::IsEOF() const noexcept
229
{
230
	return offset == size;
231 232 233 234
}

/* exported structures */

235
static const char *const iso9660_archive_extensions[] = {
236
	"iso",
237
	nullptr
238 239
};

240
const ArchivePlugin iso9660_archive_plugin = {
241 242 243 244 245
	"iso",
	nullptr,
	nullptr,
	iso9660_archive_open,
	iso9660_archive_extensions,
246
};