Iso9660ArchivePlugin.cxx 5.35 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 32 33

#include <cdio/iso9660.h>

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

#define CEILING(x, y) ((x+(y-1))/y)

39 40
struct Iso9660 {
	iso9660_t *const iso;
41

42 43 44 45 46
	explicit Iso9660(Path path)
		:iso(iso9660_open(path.c_str())) {
		if (iso == nullptr)
			throw FormatRuntimeError("Failed to open ISO9660 file %s",
						 path.c_str());
47 48
	}

49 50
	~Iso9660() noexcept {
		iso9660_close(iso);
51 52
	}

53 54
	Iso9660(const Iso9660 &) = delete;
	Iso9660 &operator=(const Iso9660 &) = delete;
55

56 57 58
	long SeekRead(void *ptr, lsn_t start, long int i_size) const {
		return iso9660_iso_seek_read(iso, ptr, start, i_size);
	}
59 60 61 62 63 64 65 66
};

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

public:
	Iso9660ArchiveFile(std::shared_ptr<Iso9660> &&_iso)
		:iso(std::move(_iso)) {}
67

68 69 70 71
	/**
	 * @param capacity the path buffer size
	 */
	void Visit(char *path, size_t length, size_t capacity,
72
		   ArchiveVisitor &visitor);
73 74 75

	virtual void Visit(ArchiveVisitor &visitor) override;

76
	InputStreamPtr OpenStream(const char *path,
77
				  Mutex &mutex) override;
78
};
79 80 81

/* archive open && listing routine */

82
inline void
83
Iso9660ArchiveFile::Visit(char *path, size_t length, size_t capacity,
84
			  ArchiveVisitor &visitor)
85
{
86
	auto *entlist = iso9660_ifs_readdir(iso->iso, path);
87 88 89 90
	if (!entlist) {
		return;
	}
	/* Iterate over the list of nodes that iso9660_ifs_readdir gives  */
91
	CdioListNode_t *entnode;
92
	_CDIO_LIST_FOREACH (entnode, entlist) {
93 94
		auto *statbuf = (iso9660_stat_t *)
			_cdio_list_node_data(entnode);
95
		const char *filename = statbuf->filename;
96 97
		if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
			continue;
98

99
		size_t filename_length = strlen(filename);
100 101 102
		if (length + filename_length + 1 >= capacity)
			/* file name is too long */
			continue;
103

104 105
		memcpy(path + length, filename, filename_length + 1);
		size_t new_length = length + filename_length;
106

107
		if (iso9660_stat_s::_STAT_DIR == statbuf->type ) {
108
			memcpy(path + new_length, "/", 2);
109
			Visit(path, new_length + 1, capacity, visitor);
110 111
		} else {
			//remove leading /
112
			visitor.VisitArchiveEntry(path + 1);
113 114
		}
	}
115 116 117 118

#if LIBCDIO_VERSION_NUM >= 20000
	iso9660_filelist_free(entlist);
#else
119
	_cdio_list_free (entlist, true);
120
#endif
121 122
}

123
static std::unique_ptr<ArchiveFile>
124
iso9660_archive_open(Path pathname)
125
{
126
	return std::make_unique<Iso9660ArchiveFile>(std::make_shared<Iso9660>(pathname));
127 128
}

129 130
void
Iso9660ArchiveFile::Visit(ArchiveVisitor &visitor)
131
{
132
	char path[4096] = "/";
133
	Visit(path, 1, sizeof(path), visitor);
134 135 136 137
}

/* single archive handling */

138
class Iso9660InputStream final : public InputStream {
139
	std::shared_ptr<Iso9660> iso;
140 141

	iso9660_stat_t *statbuf;
142

143
public:
144 145
	Iso9660InputStream(const std::shared_ptr<Iso9660> &_iso,
			   const char *_uri,
146
			   Mutex &_mutex,
147
			   iso9660_stat_t *_statbuf)
148
		:InputStream(_uri, _mutex),
149
		 iso(_iso), statbuf(_statbuf) {
150 151
		size = statbuf->size;
		SetReady();
152 153 154 155 156
	}

	~Iso9660InputStream() {
		free(statbuf);
	}
157

158
	/* virtual methods from InputStream */
159
	bool IsEOF() noexcept override;
160
	size_t Read(void *ptr, size_t size) override;
161 162
};

163
InputStreamPtr
164
Iso9660ArchiveFile::OpenStream(const char *pathname,
165
			       Mutex &mutex)
166
{
167
	auto statbuf = iso9660_ifs_stat_translate(iso->iso, pathname);
168 169 170
	if (statbuf == nullptr)
		throw FormatRuntimeError("not found in the ISO file: %s",
					 pathname);
171

172
	return std::make_unique<Iso9660InputStream>(iso, pathname, mutex,
173
						    statbuf);
174 175
}

176
size_t
177
Iso9660InputStream::Read(void *ptr, size_t read_size)
178
{
179 180
	const ScopeUnlock unlock(mutex);

181
	int readed = 0;
182
	int no_blocks, cur_block;
183
	size_t left_bytes = statbuf->size - offset;
184

185 186
	if (left_bytes < read_size) {
		no_blocks = CEILING(left_bytes, ISO_BLOCKSIZE);
187
	} else {
188
		no_blocks = read_size / ISO_BLOCKSIZE;
189 190
	}

191 192
	if (no_blocks == 0)
		return 0;
193

194
	cur_block = offset / ISO_BLOCKSIZE;
195

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

198 199 200 201
	if (readed != no_blocks * ISO_BLOCKSIZE)
		throw FormatRuntimeError("error reading ISO file at lsn %lu",
					 (unsigned long)cur_block);

202
	if (left_bytes < read_size) {
203 204 205
		readed = left_bytes;
	}

206
	offset += readed;
207 208 209
	return readed;
}

210
bool
211
Iso9660InputStream::IsEOF() noexcept
212
{
213
	return offset == size;
214 215 216 217
}

/* exported structures */

218
static const char *const iso9660_archive_extensions[] = {
219
	"iso",
220
	nullptr
221 222
};

223
const ArchivePlugin iso9660_archive_plugin = {
224 225 226 227 228
	"iso",
	nullptr,
	nullptr,
	iso9660_archive_open,
	iso9660_archive_extensions,
229
};