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

#include <cdio/iso9660.h>

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

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

41
class Iso9660ArchiveFile final : public ArchiveFile {
42
	RefCount ref;
43

44
	iso9660_t *iso;
45

46
public:
47
	Iso9660ArchiveFile(iso9660_t *_iso)
48
		:ArchiveFile(iso9660_archive_plugin), iso(_iso) {}
49 50 51 52 53

	~Iso9660ArchiveFile() {
		iso9660_close(iso);
	}

54 55 56 57
	void Ref() {
		ref.Increment();
	}

58
	void Unref() {
59
		if (ref.Decrement())
60 61 62
			delete this;
	}

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

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

	virtual void Close() override {
		Unref();
	}

	virtual void Visit(ArchiveVisitor &visitor) override;

79 80
	InputStream *OpenStream(const char *path,
				Mutex &mutex, Cond &cond) override;
81
};
82 83 84

/* archive open && listing routine */

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

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

107 108
		memcpy(path + length, filename, filename_length + 1);
		size_t new_length = length + filename_length;
109

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

121
static ArchiveFile *
122
iso9660_archive_open(Path pathname)
123 124
{
	/* open archive */
125
	auto iso = iso9660_open(pathname.c_str());
126 127 128
	if (iso == nullptr)
		throw FormatRuntimeError("Failed to open ISO9660 file %s",
					 pathname.c_str());
129

130
	return new Iso9660ArchiveFile(iso);
131 132
}

133 134
void
Iso9660ArchiveFile::Visit(ArchiveVisitor &visitor)
135
{
136
	char path[4096] = "/";
137
	Visit(path, 1, sizeof(path), visitor);
138 139 140 141
}

/* single archive handling */

142
class Iso9660InputStream final : public InputStream {
143
	Iso9660ArchiveFile &archive;
144 145

	iso9660_stat_t *statbuf;
146

147
public:
148 149
	Iso9660InputStream(Iso9660ArchiveFile &_archive, const char *_uri,
			   Mutex &_mutex, Cond &_cond,
150
			   iso9660_stat_t *_statbuf)
151
		:InputStream(_uri, _mutex, _cond),
152
		 archive(_archive), statbuf(_statbuf) {
153 154
		size = statbuf->size;
		SetReady();
155

156
		archive.Ref();
157 158 159 160
	}

	~Iso9660InputStream() {
		free(statbuf);
161
		archive.Unref();
162
	}
163

164 165
	/* virtual methods from InputStream */
	bool IsEOF() override;
166
	size_t Read(void *ptr, size_t size) override;
167 168
};

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

178 179
	return new Iso9660InputStream(*this, pathname, mutex, cond,
				      statbuf);
180 181
}

182
size_t
183
Iso9660InputStream::Read(void *ptr, size_t read_size)
184
{
185
	int readed = 0;
186
	int no_blocks, cur_block;
187
	size_t left_bytes = statbuf->size - offset;
188

189 190
	if (left_bytes < read_size) {
		no_blocks = CEILING(left_bytes, ISO_BLOCKSIZE);
191
	} else {
192
		no_blocks = read_size / ISO_BLOCKSIZE;
193 194
	}

195 196
	if (no_blocks == 0)
		return 0;
197

198
	cur_block = offset / ISO_BLOCKSIZE;
199

200 201
	readed = archive.SeekRead(ptr, statbuf->lsn + cur_block,
				  no_blocks);
202

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

207
	if (left_bytes < read_size) {
208 209 210
		readed = left_bytes;
	}

211
	offset += readed;
212 213 214
	return readed;
}

215 216
bool
Iso9660InputStream::IsEOF()
217
{
218
	return offset == size;
219 220 221 222
}

/* exported structures */

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

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