input/iso9660: add constructor/destructor
This commit is contained in:
@@ -34,6 +34,8 @@
|
|||||||
#include <cdio/iso9660.h>
|
#include <cdio/iso9660.h>
|
||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define CEILING(x, y) ((x+(y-1))/y)
|
#define CEILING(x, y) ((x+(y-1))/y)
|
||||||
@@ -46,6 +48,29 @@ struct Iso9660ArchiveFile {
|
|||||||
iso9660_t *iso;
|
iso9660_t *iso;
|
||||||
GSList *list;
|
GSList *list;
|
||||||
GSList *iter;
|
GSList *iter;
|
||||||
|
|
||||||
|
Iso9660ArchiveFile(iso9660_t *_iso)
|
||||||
|
:iso(_iso), list(nullptr) {
|
||||||
|
archive_file_init(&base, &iso9660_archive_plugin);
|
||||||
|
refcount_init(&ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
~Iso9660ArchiveFile() {
|
||||||
|
//free list
|
||||||
|
for (GSList *tmp = list; tmp != NULL; tmp = g_slist_next(tmp))
|
||||||
|
g_free(tmp->data);
|
||||||
|
g_slist_free(list);
|
||||||
|
|
||||||
|
//close archive
|
||||||
|
iso9660_close(iso);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Unref() {
|
||||||
|
if (refcount_dec(&ref))
|
||||||
|
delete this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CollectRecursive(const char *path);
|
||||||
};
|
};
|
||||||
|
|
||||||
extern const struct input_plugin iso9660_input_plugin;
|
extern const struct input_plugin iso9660_input_plugin;
|
||||||
@@ -58,10 +83,9 @@ iso9660_quark(void)
|
|||||||
|
|
||||||
/* archive open && listing routine */
|
/* archive open && listing routine */
|
||||||
|
|
||||||
static void
|
void
|
||||||
listdir_recur(const char *psz_path, Iso9660ArchiveFile *context)
|
Iso9660ArchiveFile::CollectRecursive(const char *psz_path)
|
||||||
{
|
{
|
||||||
iso9660_t *iso = context->iso;
|
|
||||||
CdioList_t *entlist;
|
CdioList_t *entlist;
|
||||||
CdioListNode_t *entnode;
|
CdioListNode_t *entnode;
|
||||||
iso9660_stat_t *statbuf;
|
iso9660_stat_t *statbuf;
|
||||||
@@ -81,12 +105,11 @@ listdir_recur(const char *psz_path, Iso9660ArchiveFile *context)
|
|||||||
if (iso9660_stat_s::_STAT_DIR == statbuf->type ) {
|
if (iso9660_stat_s::_STAT_DIR == statbuf->type ) {
|
||||||
if (strcmp(statbuf->filename, ".") && strcmp(statbuf->filename, "..")) {
|
if (strcmp(statbuf->filename, ".") && strcmp(statbuf->filename, "..")) {
|
||||||
strcat(pathname, "/");
|
strcat(pathname, "/");
|
||||||
listdir_recur(pathname, context);
|
CollectRecursive(pathname);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//remove leading /
|
//remove leading /
|
||||||
context->list = g_slist_prepend( context->list,
|
list = g_slist_prepend(list, g_strdup(pathname + 1));
|
||||||
g_strdup(pathname + 1));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_cdio_list_free (entlist, true);
|
_cdio_list_free (entlist, true);
|
||||||
@@ -95,25 +118,17 @@ listdir_recur(const char *psz_path, Iso9660ArchiveFile *context)
|
|||||||
static struct archive_file *
|
static struct archive_file *
|
||||||
iso9660_archive_open(const char *pathname, GError **error_r)
|
iso9660_archive_open(const char *pathname, GError **error_r)
|
||||||
{
|
{
|
||||||
Iso9660ArchiveFile *context =
|
|
||||||
g_new(Iso9660ArchiveFile, 1);
|
|
||||||
|
|
||||||
archive_file_init(&context->base, &iso9660_archive_plugin);
|
|
||||||
refcount_init(&context->ref);
|
|
||||||
|
|
||||||
context->list = NULL;
|
|
||||||
|
|
||||||
/* open archive */
|
/* open archive */
|
||||||
context->iso = iso9660_open (pathname);
|
auto iso = iso9660_open(pathname);
|
||||||
if (context->iso == NULL) {
|
if (iso == nullptr) {
|
||||||
g_set_error(error_r, iso9660_quark(), 0,
|
g_set_error(error_r, iso9660_quark(), 0,
|
||||||
"Failed to open ISO9660 file %s", pathname);
|
"Failed to open ISO9660 file %s", pathname);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
listdir_recur("/", context);
|
Iso9660ArchiveFile *archive = new Iso9660ArchiveFile(iso);
|
||||||
|
archive->CollectRecursive("/");
|
||||||
return &context->base;
|
return &archive->base;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -146,21 +161,8 @@ iso9660_archive_close(struct archive_file *file)
|
|||||||
{
|
{
|
||||||
Iso9660ArchiveFile *context =
|
Iso9660ArchiveFile *context =
|
||||||
(Iso9660ArchiveFile *)file;
|
(Iso9660ArchiveFile *)file;
|
||||||
GSList *tmp;
|
|
||||||
|
|
||||||
if (!refcount_dec(&context->ref))
|
context->Unref();
|
||||||
return;
|
|
||||||
|
|
||||||
if (context->list) {
|
|
||||||
//free list
|
|
||||||
for (tmp = context->list; tmp != NULL; tmp = g_slist_next(tmp))
|
|
||||||
g_free(tmp->data);
|
|
||||||
g_slist_free(context->list);
|
|
||||||
}
|
|
||||||
//close archive
|
|
||||||
iso9660_close(context->iso);
|
|
||||||
|
|
||||||
g_free(context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* single archive handling */
|
/* single archive handling */
|
||||||
@@ -172,6 +174,26 @@ struct Iso9660InputStream {
|
|||||||
|
|
||||||
iso9660_stat_t *statbuf;
|
iso9660_stat_t *statbuf;
|
||||||
size_t max_blocks;
|
size_t max_blocks;
|
||||||
|
|
||||||
|
Iso9660InputStream(Iso9660ArchiveFile &_archive, const char *uri,
|
||||||
|
Mutex &mutex, Cond &cond,
|
||||||
|
iso9660_stat_t *_statbuf)
|
||||||
|
:archive(&_archive), statbuf(_statbuf),
|
||||||
|
max_blocks(CEILING(statbuf->size, ISO_BLOCKSIZE)) {
|
||||||
|
input_stream_init(&base, &iso9660_input_plugin, uri,
|
||||||
|
mutex, cond);
|
||||||
|
|
||||||
|
base.ready = true;
|
||||||
|
base.size = statbuf->size;
|
||||||
|
|
||||||
|
refcount_inc(&archive->ref);
|
||||||
|
}
|
||||||
|
|
||||||
|
~Iso9660InputStream() {
|
||||||
|
free(statbuf);
|
||||||
|
archive->Unref();
|
||||||
|
input_stream_deinit(&base);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct input_stream *
|
static struct input_stream *
|
||||||
@@ -181,31 +203,17 @@ iso9660_archive_open_stream(struct archive_file *file, const char *pathname,
|
|||||||
{
|
{
|
||||||
Iso9660ArchiveFile *context =
|
Iso9660ArchiveFile *context =
|
||||||
(Iso9660ArchiveFile *)file;
|
(Iso9660ArchiveFile *)file;
|
||||||
Iso9660InputStream *iis;
|
|
||||||
|
|
||||||
iis = g_new(Iso9660InputStream, 1);
|
auto statbuf = iso9660_ifs_stat_translate(context->iso, pathname);
|
||||||
input_stream_init(&iis->base, &iso9660_input_plugin, pathname,
|
if (statbuf == nullptr) {
|
||||||
mutex, cond);
|
|
||||||
|
|
||||||
iis->archive = context;
|
|
||||||
iis->statbuf = iso9660_ifs_stat_translate(context->iso, pathname);
|
|
||||||
if (iis->statbuf == NULL) {
|
|
||||||
g_free(iis);
|
|
||||||
g_set_error(error_r, iso9660_quark(), 0,
|
g_set_error(error_r, iso9660_quark(), 0,
|
||||||
"not found in the ISO file: %s", pathname);
|
"not found in the ISO file: %s", pathname);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
iis->base.ready = true;
|
Iso9660InputStream *iis =
|
||||||
//we are not seekable
|
new Iso9660InputStream(*context, pathname, mutex, cond,
|
||||||
iis->base.seekable = false;
|
statbuf);
|
||||||
|
|
||||||
iis->base.size = iis->statbuf->size;
|
|
||||||
|
|
||||||
iis->max_blocks = CEILING(iis->statbuf->size, ISO_BLOCKSIZE);
|
|
||||||
|
|
||||||
refcount_inc(&context->ref);
|
|
||||||
|
|
||||||
return &iis->base;
|
return &iis->base;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -214,12 +222,7 @@ iso9660_input_close(struct input_stream *is)
|
|||||||
{
|
{
|
||||||
Iso9660InputStream *iis = (Iso9660InputStream *)is;
|
Iso9660InputStream *iis = (Iso9660InputStream *)is;
|
||||||
|
|
||||||
g_free(iis->statbuf);
|
delete iis;
|
||||||
|
|
||||||
iso9660_archive_close(&iis->archive->base);
|
|
||||||
|
|
||||||
input_stream_deinit(&iis->base);
|
|
||||||
g_free(iis);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user