This fixes mistake where fclose was called unconditionally in
upkg_close.
struct upkg_private {
const struct upkg_file_ops *fops;
struct upkg_private {
const struct upkg_file_ops *fops;
+ int (*dtor)(void *handle);
void *f;
struct upkg_file *last_file;
void *f;
struct upkg_file *last_file;
+static int file_close(void *handle)
+{
+ return fclose(handle);
+}
+
const struct upkg_file_ops upkg_default_fops = {
.read = file_read,
.seek = file_seek,
const struct upkg_file_ops upkg_default_fops = {
.read = file_read,
.seek = file_seek,
-struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops)
+struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops,
+ int (*destructor)(void *handle))
{
unsigned char hdr_buf[UPKG_HDR_SIZE];
struct upkg *pkg;
{
unsigned char hdr_buf[UPKG_HDR_SIZE];
struct upkg *pkg;
return NULL;
}
pkg->priv->fops = fops;
return NULL;
}
pkg->priv->fops = fops;
+ pkg->priv->dtor = destructor;
pkg->priv->f = f;
if (pkg_init_guid(pkg) != 0) {
pkg->priv->f = f;
if (pkg_init_guid(pkg) != 0) {
- pkg = upkg_open(f, &upkg_default_fops);
+ pkg = upkg_open(f, &upkg_default_fops, file_close);
- if (pkg->priv->f) {
- rc = fclose(pkg->priv->f);
+ if (pkg->priv->dtor) {
+ rc = pkg->priv->dtor(pkg->priv->f);
+ }
- for (unsigned i = 0; i < pkg->name_count; i++) {
- free(pkg->priv->names[i].name);
- }
+ for (unsigned i = 0; i < pkg->name_count; i++) {
+ free(pkg->priv->names[i].name);
}
free(pkg->priv->imports);
}
free(pkg->priv->imports);
/* Default I/O operations for ordinary files. */
extern const struct upkg_file_ops upkg_default_fops;
/* Default I/O operations for ordinary files. */
extern const struct upkg_file_ops upkg_default_fops;
-struct upkg *upkg_open(void *handle, const struct upkg_file_ops *fops);
+struct upkg *upkg_open(void *handle, const struct upkg_file_ops *fops,
+ int (*destructor)(void *handle));
struct upkg *upkg_fopen(const char *path);
int upkg_close(struct upkg *pkg);
struct upkg *upkg_fopen(const char *path);
int upkg_close(struct upkg *pkg);