struct upkg_private {
const struct upkg_file_ops *fops;
+ int (*dtor)(void *handle);
void *f;
struct upkg_file *last_file;
return feof(handle);
}
+static int file_close(void *handle)
+{
+ return fclose(handle);
+}
+
const struct upkg_file_ops upkg_default_fops = {
.read = file_read,
.seek = file_seek,
return -1;
}
-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;
return NULL;
}
pkg->priv->fops = fops;
+ pkg->priv->dtor = destructor;
pkg->priv->f = f;
if (pkg_init_guid(pkg) != 0) {
return NULL;
}
- pkg = upkg_open(f, &upkg_default_fops);
+ pkg = upkg_open(f, &upkg_default_fops, file_close);
if (!pkg) {
fclose(f);
}
{
int rc = 0;
- 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);