Iso9660ArchivePlugin.cxx 5.55 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
 */

/**
  * 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
		size = statbuf->size;
151
		seekable = true;
152
		SetReady();
153 154 155 156 157
	}

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

159
	/* virtual methods from InputStream */
160
	bool IsEOF() const noexcept override;
161 162
	size_t Read(std::unique_lock<Mutex> &lock,
		    void *ptr, size_t size) override;
163 164 165 166

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

169
InputStreamPtr
170
Iso9660ArchiveFile::OpenStream(const char *pathname,
171
			       Mutex &mutex)
172
{
173
	auto statbuf = iso9660_ifs_stat_translate(iso->iso, pathname);
174 175 176
	if (statbuf == nullptr)
		throw FormatRuntimeError("not found in the ISO file: %s",
					 pathname);
177

178
	return std::make_unique<Iso9660InputStream>(iso, pathname, mutex,
179
						    statbuf);
180 181
}

182
size_t
183 184
Iso9660InputStream::Read(std::unique_lock<Mutex> &,
			 void *ptr, size_t read_size)
185
{
186 187
	const ScopeUnlock unlock(mutex);

188
	int readed = 0;
189
	int no_blocks, cur_block;
190
	size_t left_bytes = statbuf->size - offset;
191

192 193
	if (left_bytes < read_size) {
		no_blocks = CEILING(left_bytes, ISO_BLOCKSIZE);
194
	} else {
195
		no_blocks = read_size / ISO_BLOCKSIZE;
196 197
	}

198 199
	if (no_blocks == 0)
		return 0;
200

201
	cur_block = offset / ISO_BLOCKSIZE;
202

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

205 206 207 208
	if (readed != no_blocks * ISO_BLOCKSIZE)
		throw FormatRuntimeError("error reading ISO file at lsn %lu",
					 (unsigned long)cur_block);

209
	if (left_bytes < read_size) {
210 211 212
		readed = left_bytes;
	}

213
	offset += readed;
214 215 216
	return readed;
}

217
bool
218
Iso9660InputStream::IsEOF() const noexcept
219
{
220
	return offset == size;
221 222 223 224
}

/* exported structures */

225
static const char *const iso9660_archive_extensions[] = {
226
	"iso",
227
	nullptr
228 229
};

230
const ArchivePlugin iso9660_archive_plugin = {
231 232 233 234 235
	"iso",
	nullptr,
	nullptr,
	iso9660_archive_open,
	iso9660_archive_extensions,
236
};