libupkg: Don't store upkg_priv pointer in struct upkg_file.
authorNick Bowler <nbowler@draconx.ca>
Wed, 16 May 2012 00:09:22 +0000 (20:09 -0400)
committerNick Bowler <nbowler@draconx.ca>
Wed, 16 May 2012 00:09:22 +0000 (20:09 -0400)
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.

src/libupkg.c
src/upkg.h

index 307e1beb03bd73fc774b9e0d1b1b05ab04fd516d..a648bb49824299c90f250b45142823fbc559edf3 100644 (file)
@@ -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;
 }
index 12104cbcb8c4cfac729ff67aa068c6c0144b05d6..51b92f6fc17a0cab69bf1ffbe5bff08f42ac656d 100644 (file)
@@ -89,7 +89,7 @@ struct upkg_file {
 
        int eof;
 
-       struct upkg_priv *pkg;
+       struct upkg *pkg;
        unsigned long base, offset, len;
 };