From: Nick Bowler Date: Wed, 16 May 2012 00:09:22 +0000 (-0400) Subject: libupkg: Don't store upkg_priv pointer in struct upkg_file. X-Git-Url: https://git.draconx.ca/gitweb/upkg.git/commitdiff_plain/195935b16f1a2966e50007309f0e68275dc8f2b5 libupkg: Don't store upkg_priv pointer in struct upkg_file. If this is going to be stored in a public struct, make it actually useful to users outside of libupkg by giving them a pointer type that can actually be used. --- diff --git a/src/libupkg.c b/src/libupkg.c index 307e1be..a648bb4 100644 --- a/src/libupkg.c +++ b/src/libupkg.c @@ -588,7 +588,7 @@ struct upkg_file *upkg_export_open(struct upkg *pub, unsigned long idx) return NULL; *f = (struct upkg_file) { - .pkg = pkg, + .pkg = pub, .base = pkg->exports[idx].offset, .len = pkg->exports[idx].size, .name = pkg->exports[idx].pub.name, @@ -599,8 +599,10 @@ struct upkg_file *upkg_export_open(struct upkg *pub, unsigned long idx) void upkg_export_close(struct upkg_file *f) { - if (f->pkg->last_file == f) - f->pkg->last_file = NULL; + struct upkg_priv *pkg = (struct upkg_priv *)f->pkg; + + if (pkg->last_file == f) + pkg->last_file = NULL; free(f); } @@ -611,7 +613,8 @@ long upkg_export_tell(struct upkg_file *f) int upkg_export_seek(struct upkg_file *f, long offset, int whence) { - const struct upkg_file_ops *fops = f->pkg->fops; + struct upkg_priv *pkg = (struct upkg_priv *)f->pkg; + const struct upkg_file_ops *fops = pkg->fops; int rc = EOF; switch (whence) { @@ -620,23 +623,23 @@ int upkg_export_seek(struct upkg_file *f, long offset, int whence) case SEEK_SET: if (offset < 0 || offset > f->len) return EOF; - rc = fops->seek(f->pkg->f, f->base + offset, SEEK_SET); + rc = fops->seek(pkg->f, f->base + offset, SEEK_SET); break; case SEEK_END: offset = -offset; if (offset < 0 || offset > f->len) return EOF; offset = f->len - offset; - rc = fops->seek(f->pkg->f, f->base + offset, SEEK_SET); + rc = fops->seek(pkg->f, f->base + offset, SEEK_SET); break; } if (rc == 0) { - f->pkg->last_file = f; + pkg->last_file = f; f->offset = offset; f->eof = 0; - } else if (f->pkg->last_file == f) { - f->pkg->last_file = NULL; + } else if (pkg->last_file == f) { + pkg->last_file = NULL; } return rc; @@ -644,7 +647,8 @@ int upkg_export_seek(struct upkg_file *f, long offset, int whence) size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n) { - const struct upkg_file_ops *fops = f->pkg->fops; + struct upkg_priv *pkg = (struct upkg_priv *)f->pkg; + const struct upkg_file_ops *fops = pkg->fops; size_t want = MIN(n, f->len - f->offset); size_t rc; @@ -652,15 +656,15 @@ size_t upkg_export_read(struct upkg_file *f, void *buf, size_t n) return 0; } - if (f != f->pkg->last_file) { - if (fops->seek(f->pkg->f, f->base + f->offset, SEEK_SET)) + if (f != pkg->last_file) { + if (fops->seek(pkg->f, f->base + f->offset, SEEK_SET)) return 0; } - rc = fops->read(buf, want, f->pkg->f); + rc = fops->read(buf, want, pkg->f); f->offset += rc; - if (want < n || (rc < want && fops->eof(f->pkg->f))) + if (want < n || (rc < want && fops->eof(pkg->f))) f->eof = 1; return rc; } diff --git a/src/upkg.h b/src/upkg.h index 12104cb..51b92f6 100644 --- a/src/upkg.h +++ b/src/upkg.h @@ -89,7 +89,7 @@ struct upkg_file { int eof; - struct upkg_priv *pkg; + struct upkg *pkg; unsigned long base, offset, len; };