2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2014-01-13 22:30:36 +01:00
|
|
|
* Copyright (C) 2003-2014 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-12-16 21:48:26 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-12-16 21:48:26 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* iso archive handling (requires cdio, and iso9660)
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-24 19:18:58 +01:00
|
|
|
#include "Iso9660ArchivePlugin.hxx"
|
2014-01-24 00:09:37 +01:00
|
|
|
#include "../ArchivePlugin.hxx"
|
|
|
|
#include "../ArchiveFile.hxx"
|
|
|
|
#include "../ArchiveVisitor.hxx"
|
2014-01-24 16:18:21 +01:00
|
|
|
#include "input/InputStream.hxx"
|
|
|
|
#include "input/InputPlugin.hxx"
|
2014-02-08 13:21:50 +01:00
|
|
|
#include "fs/Path.hxx"
|
2013-01-29 23:20:19 +01:00
|
|
|
#include "util/RefCount.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
|
|
|
#include "util/Domain.hxx"
|
2008-12-16 21:48:26 +01:00
|
|
|
|
|
|
|
#include <cdio/iso9660.h>
|
|
|
|
|
2013-01-28 22:54:07 +01:00
|
|
|
#include <stdlib.h>
|
2008-12-16 21:48:26 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define CEILING(x, y) ((x+(y-1))/y)
|
|
|
|
|
2013-01-29 23:36:58 +01:00
|
|
|
class Iso9660ArchiveFile final : public ArchiveFile {
|
2013-01-29 23:20:19 +01:00
|
|
|
RefCount ref;
|
2009-12-31 10:02:55 +01:00
|
|
|
|
2008-12-16 21:48:26 +01:00
|
|
|
iso9660_t *iso;
|
2013-01-28 22:54:07 +01:00
|
|
|
|
2013-11-24 22:41:23 +01:00
|
|
|
public:
|
2013-01-28 22:54:07 +01:00
|
|
|
Iso9660ArchiveFile(iso9660_t *_iso)
|
2013-01-29 23:26:51 +01:00
|
|
|
:ArchiveFile(iso9660_archive_plugin), iso(_iso) {}
|
2013-01-28 22:54:07 +01:00
|
|
|
|
|
|
|
~Iso9660ArchiveFile() {
|
|
|
|
iso9660_close(iso);
|
|
|
|
}
|
|
|
|
|
2013-11-24 22:41:23 +01:00
|
|
|
void Ref() {
|
|
|
|
ref.Increment();
|
|
|
|
}
|
|
|
|
|
2013-01-28 22:54:07 +01:00
|
|
|
void Unref() {
|
2013-01-29 23:20:19 +01:00
|
|
|
if (ref.Decrement())
|
2013-01-28 22:54:07 +01:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2013-11-24 22:41:23 +01:00
|
|
|
long SeekRead(void *ptr, lsn_t start, long int i_size) const {
|
|
|
|
return iso9660_iso_seek_read(iso, ptr, start, i_size);
|
|
|
|
}
|
|
|
|
|
2013-01-29 21:21:07 +01:00
|
|
|
void Visit(const char *path, ArchiveVisitor &visitor);
|
2013-01-29 23:36:58 +01:00
|
|
|
|
|
|
|
virtual void Close() override {
|
|
|
|
Unref();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Visit(ArchiveVisitor &visitor) override;
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
virtual InputStream *OpenStream(const char *path,
|
|
|
|
Mutex &mutex, Cond &cond,
|
|
|
|
Error &error) override;
|
2009-12-16 16:06:16 +01:00
|
|
|
};
|
2008-12-16 21:48:26 +01:00
|
|
|
|
2013-10-17 10:20:57 +02:00
|
|
|
extern const InputPlugin iso9660_input_plugin;
|
2008-12-16 21:48:26 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
static constexpr Domain iso9660_domain("iso9660");
|
2009-11-14 23:53:04 +01:00
|
|
|
|
2008-12-16 21:48:26 +01:00
|
|
|
/* archive open && listing routine */
|
|
|
|
|
2013-01-29 21:21:07 +01:00
|
|
|
inline void
|
|
|
|
Iso9660ArchiveFile::Visit(const char *psz_path, ArchiveVisitor &visitor)
|
2008-12-16 21:48:26 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2013-01-24 19:18:58 +01:00
|
|
|
if (iso9660_stat_s::_STAT_DIR == statbuf->type ) {
|
2008-12-16 21:48:26 +01:00
|
|
|
if (strcmp(statbuf->filename, ".") && strcmp(statbuf->filename, "..")) {
|
|
|
|
strcat(pathname, "/");
|
2013-01-29 21:21:07 +01:00
|
|
|
Visit(pathname, visitor);
|
2008-12-16 21:48:26 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//remove leading /
|
2013-01-29 21:21:07 +01:00
|
|
|
visitor.VisitArchiveEntry(pathname + 1);
|
2008-12-16 21:48:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_cdio_list_free (entlist, true);
|
|
|
|
}
|
|
|
|
|
2013-01-29 23:26:51 +01:00
|
|
|
static ArchiveFile *
|
2014-02-08 13:21:50 +01:00
|
|
|
iso9660_archive_open(Path pathname, Error &error)
|
2008-12-16 21:48:26 +01:00
|
|
|
{
|
|
|
|
/* open archive */
|
2014-02-08 13:21:50 +01:00
|
|
|
auto iso = iso9660_open(pathname.c_str());
|
2013-01-28 22:54:07 +01:00
|
|
|
if (iso == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(iso9660_domain,
|
2014-02-08 13:21:50 +01:00
|
|
|
"Failed to open ISO9660 file %s",
|
|
|
|
pathname.c_str());
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2008-12-16 21:48:26 +01:00
|
|
|
}
|
|
|
|
|
2013-01-29 23:26:51 +01:00
|
|
|
return new Iso9660ArchiveFile(iso);
|
2008-12-16 21:48:26 +01:00
|
|
|
}
|
|
|
|
|
2013-01-29 23:36:58 +01:00
|
|
|
void
|
|
|
|
Iso9660ArchiveFile::Visit(ArchiveVisitor &visitor)
|
2008-12-16 21:48:26 +01:00
|
|
|
{
|
2013-01-29 23:36:58 +01:00
|
|
|
Visit("/", visitor);
|
2008-12-16 21:48:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* single archive handling */
|
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
class Iso9660InputStream final : public InputStream {
|
2013-11-24 22:47:50 +01:00
|
|
|
Iso9660ArchiveFile &archive;
|
2009-12-31 10:33:13 +01:00
|
|
|
|
|
|
|
iso9660_stat_t *statbuf;
|
2013-01-28 22:54:07 +01:00
|
|
|
|
2013-11-24 22:41:23 +01:00
|
|
|
public:
|
2014-05-11 16:02:57 +02:00
|
|
|
Iso9660InputStream(Iso9660ArchiveFile &_archive, const char *_uri,
|
|
|
|
Mutex &_mutex, Cond &_cond,
|
2013-01-28 22:54:07 +01:00
|
|
|
iso9660_stat_t *_statbuf)
|
2014-05-11 16:02:57 +02:00
|
|
|
:InputStream(iso9660_input_plugin, _uri, _mutex, _cond),
|
2013-11-24 22:47:50 +01:00
|
|
|
archive(_archive), statbuf(_statbuf) {
|
2014-05-11 16:02:57 +02:00
|
|
|
size = statbuf->size;
|
|
|
|
SetReady();
|
2013-01-28 22:54:07 +01:00
|
|
|
|
2013-11-24 22:47:50 +01:00
|
|
|
archive.Ref();
|
2013-01-28 22:54:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~Iso9660InputStream() {
|
|
|
|
free(statbuf);
|
2013-11-24 22:47:50 +01:00
|
|
|
archive.Unref();
|
2013-01-28 22:54:07 +01:00
|
|
|
}
|
2013-11-24 22:41:23 +01:00
|
|
|
|
|
|
|
size_t Read(void *ptr, size_t size, Error &error);
|
2009-12-31 10:33:13 +01:00
|
|
|
};
|
|
|
|
|
2013-10-23 22:08:59 +02:00
|
|
|
InputStream *
|
2013-01-29 23:36:58 +01:00
|
|
|
Iso9660ArchiveFile::OpenStream(const char *pathname,
|
|
|
|
Mutex &mutex, Cond &cond,
|
2013-08-10 18:02:44 +02:00
|
|
|
Error &error)
|
2008-12-16 21:48:26 +01:00
|
|
|
{
|
2013-01-29 23:36:58 +01:00
|
|
|
auto statbuf = iso9660_ifs_stat_translate(iso, pathname);
|
2013-01-28 22:54:07 +01:00
|
|
|
if (statbuf == nullptr) {
|
2013-08-10 18:02:44 +02:00
|
|
|
error.Format(iso9660_domain,
|
|
|
|
"not found in the ISO file: %s", pathname);
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2008-12-16 21:48:26 +01:00
|
|
|
}
|
2009-12-31 10:29:17 +01:00
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
return new Iso9660InputStream(*this, pathname, mutex, cond,
|
|
|
|
statbuf);
|
2008-12-16 21:48:26 +01:00
|
|
|
}
|
|
|
|
|
2013-11-24 22:41:23 +01:00
|
|
|
inline size_t
|
2014-05-11 16:02:57 +02:00
|
|
|
Iso9660InputStream::Read(void *ptr, size_t read_size, Error &error)
|
2008-12-16 21:48:26 +01:00
|
|
|
{
|
2013-11-23 18:29:43 +01:00
|
|
|
int readed = 0;
|
2008-12-16 21:48:26 +01:00
|
|
|
int no_blocks, cur_block;
|
2014-05-11 16:02:57 +02:00
|
|
|
size_t left_bytes = statbuf->size - offset;
|
2008-12-16 21:48:26 +01:00
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
if (left_bytes < read_size) {
|
|
|
|
no_blocks = CEILING(left_bytes, ISO_BLOCKSIZE);
|
2008-12-16 21:48:26 +01:00
|
|
|
} else {
|
2014-05-11 16:02:57 +02:00
|
|
|
no_blocks = read_size / ISO_BLOCKSIZE;
|
2008-12-16 21:48:26 +01:00
|
|
|
}
|
|
|
|
|
2013-11-23 18:51:38 +01:00
|
|
|
if (no_blocks == 0)
|
|
|
|
return 0;
|
2008-12-16 21:48:26 +01:00
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
cur_block = offset / ISO_BLOCKSIZE;
|
2008-12-16 21:48:26 +01:00
|
|
|
|
2013-11-24 22:47:50 +01:00
|
|
|
readed = archive.SeekRead(ptr, statbuf->lsn + cur_block,
|
|
|
|
no_blocks);
|
2009-12-31 10:29:17 +01:00
|
|
|
|
2013-11-23 18:51:38 +01:00
|
|
|
if (readed != no_blocks * ISO_BLOCKSIZE) {
|
|
|
|
error.Format(iso9660_domain,
|
|
|
|
"error reading ISO file at lsn %lu",
|
|
|
|
(unsigned long)cur_block);
|
|
|
|
return 0;
|
2008-12-16 21:48:26 +01:00
|
|
|
}
|
2014-05-11 16:02:57 +02:00
|
|
|
if (left_bytes < read_size) {
|
2013-11-23 18:51:38 +01:00
|
|
|
readed = left_bytes;
|
|
|
|
}
|
|
|
|
|
2014-05-11 16:02:57 +02:00
|
|
|
offset += readed;
|
2008-12-16 21:48:26 +01:00
|
|
|
return readed;
|
|
|
|
}
|
|
|
|
|
2013-11-24 22:41:23 +01:00
|
|
|
static size_t
|
|
|
|
iso9660_input_read(InputStream *is, void *ptr, size_t size,
|
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
Iso9660InputStream *iis = (Iso9660InputStream *)is;
|
|
|
|
return iis->Read(ptr, size, error);
|
|
|
|
}
|
|
|
|
|
2008-12-16 21:48:26 +01:00
|
|
|
static bool
|
2013-10-23 22:08:59 +02:00
|
|
|
iso9660_input_eof(InputStream *is)
|
2008-12-16 21:48:26 +01:00
|
|
|
{
|
2009-12-31 10:29:17 +01:00
|
|
|
return is->offset == is->size;
|
2008-12-16 21:48:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* exported structures */
|
|
|
|
|
2009-12-16 16:06:16 +01:00
|
|
|
static const char *const iso9660_archive_extensions[] = {
|
2008-12-16 21:48:26 +01:00
|
|
|
"iso",
|
2013-10-19 18:19:03 +02:00
|
|
|
nullptr
|
2008-12-16 21:48:26 +01:00
|
|
|
};
|
|
|
|
|
2013-10-17 10:20:57 +02:00
|
|
|
const InputPlugin iso9660_input_plugin = {
|
2013-01-24 19:18:58 +01:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
iso9660_input_read,
|
|
|
|
iso9660_input_eof,
|
|
|
|
nullptr,
|
2008-12-16 21:48:26 +01:00
|
|
|
};
|
|
|
|
|
2014-02-08 13:22:13 +01:00
|
|
|
const ArchivePlugin iso9660_archive_plugin = {
|
2013-01-24 19:18:58 +01:00
|
|
|
"iso",
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
iso9660_archive_open,
|
|
|
|
iso9660_archive_extensions,
|
2008-12-16 21:48:26 +01:00
|
|
|
};
|