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

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

24
#include "config.h"
25
#include "Iso9660ArchivePlugin.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 34

#include <cdio/iso9660.h>

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

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

40 41
struct Iso9660 {
	iso9660_t *const iso;
42

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

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

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

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

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

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

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

	virtual void Visit(ArchiveVisitor &visitor) override;

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

/* archive open && listing routine */

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

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

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

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

119
static std::unique_ptr<ArchiveFile>
120
iso9660_archive_open(Path pathname)
121
{
122
	return std::make_unique<Iso9660ArchiveFile>(std::make_shared<Iso9660>(pathname));
123 124
}

125 126
void
Iso9660ArchiveFile::Visit(ArchiveVisitor &visitor)
127
{
128
	char path[4096] = "/";
129
	Visit(path, 1, sizeof(path), visitor);
130 131 132 133
}

/* single archive handling */

134
class Iso9660InputStream final : public InputStream {
135
	std::shared_ptr<Iso9660> iso;
136 137

	iso9660_stat_t *statbuf;
138

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

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

154
	/* virtual methods from InputStream */
155
	bool IsEOF() noexcept override;
156
	size_t Read(void *ptr, size_t size) override;
157 158
};

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

168 169
	return std::make_unique<Iso9660InputStream>(iso, pathname, mutex, cond,
						    statbuf);
170 171
}

172
size_t
173
Iso9660InputStream::Read(void *ptr, size_t read_size)
174
{
175 176
	const ScopeUnlock unlock(mutex);

177
	int readed = 0;
178
	int no_blocks, cur_block;
179
	size_t left_bytes = statbuf->size - offset;
180

181 182
	if (left_bytes < read_size) {
		no_blocks = CEILING(left_bytes, ISO_BLOCKSIZE);
183
	} else {
184
		no_blocks = read_size / ISO_BLOCKSIZE;
185 186
	}

187 188
	if (no_blocks == 0)
		return 0;
189

190
	cur_block = offset / ISO_BLOCKSIZE;
191

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

194 195 196 197
	if (readed != no_blocks * ISO_BLOCKSIZE)
		throw FormatRuntimeError("error reading ISO file at lsn %lu",
					 (unsigned long)cur_block);

198
	if (left_bytes < read_size) {
199 200 201
		readed = left_bytes;
	}

202
	offset += readed;
203 204 205
	return readed;
}

206
bool
207
Iso9660InputStream::IsEOF() noexcept
208
{
209
	return offset == size;
210 211 212 213
}

/* exported structures */

214
static const char *const iso9660_archive_extensions[] = {
215
	"iso",
216
	nullptr
217 218
};

219
const ArchivePlugin iso9660_archive_plugin = {
220 221 222 223 224
	"iso",
	nullptr,
	nullptr,
	iso9660_archive_open,
	iso9660_archive_extensions,
225
};