Iso9660ArchivePlugin.cxx 5.27 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 30
#include "input/InputStream.hxx"
#include "input/InputPlugin.hxx"
31
#include "fs/Path.hxx"
32
#include "util/RefCount.hxx"
33 34
#include "util/Error.hxx"
#include "util/Domain.hxx"
35 36 37

#include <cdio/iso9660.h>

38
#include <stdlib.h>
39 40 41 42
#include <string.h>

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

43
class Iso9660ArchiveFile final : public ArchiveFile {
44
	RefCount ref;
45

46
	iso9660_t *iso;
47

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

	~Iso9660ArchiveFile() {
		iso9660_close(iso);
	}

56 57 58 59
	void Ref() {
		ref.Increment();
	}

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

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

69
	void Visit(const char *path, ArchiveVisitor &visitor);
70 71 72 73 74 75 76

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

	virtual void Visit(ArchiveVisitor &visitor) override;

77 78 79
	virtual InputStream *OpenStream(const char *path,
					Mutex &mutex, Cond &cond,
					Error &error) override;
80
};
81

82
static constexpr Domain iso9660_domain("iso9660");
83

84 85
/* archive open && listing routine */

86 87
inline void
Iso9660ArchiveFile::Visit(const char *psz_path, ArchiveVisitor &visitor)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
	CdioList_t *entlist;
	CdioListNode_t *entnode;
	iso9660_stat_t *statbuf;
	char pathname[4096];

	entlist = iso9660_ifs_readdir (iso, psz_path);
	if (!entlist) {
		return;
	}
	/* Iterate over the list of nodes that iso9660_ifs_readdir gives  */
	_CDIO_LIST_FOREACH (entnode, entlist) {
		statbuf = (iso9660_stat_t *) _cdio_list_node_data (entnode);

		strcpy(pathname, psz_path);
		strcat(pathname, statbuf->filename);

105
		if (iso9660_stat_s::_STAT_DIR == statbuf->type ) {
106 107
			if (strcmp(statbuf->filename, ".") && strcmp(statbuf->filename, "..")) {
				strcat(pathname, "/");
108
				Visit(pathname, visitor);
109 110 111
			}
		} else {
			//remove leading /
112
			visitor.VisitArchiveEntry(pathname + 1);
113 114 115 116 117
		}
	}
	_cdio_list_free (entlist, true);
}

118
static ArchiveFile *
119
iso9660_archive_open(Path pathname, Error &error)
120 121
{
	/* open archive */
122
	auto iso = iso9660_open(pathname.c_str());
123
	if (iso == nullptr) {
124
		error.Format(iso9660_domain,
125 126
			     "Failed to open ISO9660 file %s",
			     pathname.c_str());
127
		return nullptr;
128 129
	}

130
	return new Iso9660ArchiveFile(iso);
131 132
}

133 134
void
Iso9660ArchiveFile::Visit(ArchiveVisitor &visitor)
135
{
136
	Visit("/", visitor);
137 138 139 140
}

/* single archive handling */

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

	iso9660_stat_t *statbuf;
145

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

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

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

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

168
InputStream *
169 170
Iso9660ArchiveFile::OpenStream(const char *pathname,
			       Mutex &mutex, Cond &cond,
171
			       Error &error)
172
{
173
	auto statbuf = iso9660_ifs_stat_translate(iso, pathname);
174
	if (statbuf == nullptr) {
175 176
		error.Format(iso9660_domain,
			     "not found in the ISO file: %s", pathname);
177
		return nullptr;
178
	}
179

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

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

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

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

200
	cur_block = offset / ISO_BLOCKSIZE;
201

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

205 206 207 208 209
	if (readed != no_blocks * ISO_BLOCKSIZE) {
		error.Format(iso9660_domain,
			     "error reading ISO file at lsn %lu",
			     (unsigned long)cur_block);
		return 0;
210
	}
211
	if (left_bytes < read_size) {
212 213 214
		readed = left_bytes;
	}

215
	offset += readed;
216 217 218
	return readed;
}

219 220
bool
Iso9660InputStream::IsEOF()
221
{
222
	return offset == size;
223 224 225 226
}

/* exported structures */

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

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