archive: use reference counting for archive+input
Make the input_stream implementation hold a reference on the archive_file object. Allow the caller to "close" the archive_file object immediately, no matter if the open_stream() method has succeeded or not.
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
#include "archive/bz2_archive_plugin.h"
|
||||
#include "archive_api.h"
|
||||
#include "input_plugin.h"
|
||||
#include "refcount.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
@@ -40,6 +41,8 @@
|
||||
struct bz2_archive_file {
|
||||
struct archive_file base;
|
||||
|
||||
struct refcount ref;
|
||||
|
||||
char *name;
|
||||
bool reset;
|
||||
struct input_stream istream;
|
||||
@@ -105,6 +108,7 @@ bz2_open(const char *pathname, GError **error_r)
|
||||
|
||||
context = g_malloc(sizeof(*context));
|
||||
archive_file_init(&context->base, &bz2_archive_plugin);
|
||||
refcount_init(&context->ref);
|
||||
|
||||
//open archive
|
||||
if (!input_stream_open(&context->istream, pathname, error_r)) {
|
||||
@@ -149,6 +153,9 @@ bz2_close(struct archive_file *file)
|
||||
{
|
||||
struct bz2_archive_file *context = (struct bz2_archive_file *) file;
|
||||
|
||||
if (!refcount_dec(&context->ref))
|
||||
return;
|
||||
|
||||
g_free(context->name);
|
||||
|
||||
input_stream_close(&context->istream);
|
||||
@@ -180,6 +187,8 @@ bz2_open_stream(struct archive_file *file, struct input_stream *is,
|
||||
|
||||
bis->eof = false;
|
||||
|
||||
refcount_inc(&context->ref);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user