# HG changeset patch # User John Tsiombikas # Date 1420763489 -7200 # Node ID a5728bc6a02f637c1282eb8d0000d5293d502f1b # Parent a71bd70c10141b230013263f3c3ab359366cba07 moving along diff -r a71bd70c1014 -r a5728bc6a02f include/packio.h --- a/include/packio.h Sat Jan 03 15:29:05 2015 +0200 +++ b/include/packio.h Fri Jan 09 02:31:29 2015 +0200 @@ -20,7 +20,7 @@ extern int pkio_errno; -int pkio_fopen(const char *path); +PKIO_FILE *pkio_fopen(const char *path, const char *mode); int pkio_fclose(PKIO_FILE *fp); int pkio_fseek(PKIO_FILE *fp, long offset, int whence); diff -r a71bd70c1014 -r a5728bc6a02f src/archive.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/archive.c Fri Jan 09 02:31:29 2015 +0200 @@ -0,0 +1,60 @@ +#include +#include +#include +#include "archive.h" +#include "logger.h" + +static struct archive_module *modlist; + +int pkio_reg_archive_module(struct archive_module *m) +{ + m->next = modlist; + modlist = m; + return 0; +} + +struct archive *pkio_load_archive(const char *fname) +{ + FILE *fp; + struct archive *ar; + + if(!modlist) { + pkio_logmsg("failed to load archive, no archive modules found\n"); + return 0; + } + + if(!(fp = fopen(fname, "rb"))) { + pkio_logmsg("failed to load archive: %s: %s\n", fname, strerror(errno)); + return 0; + } + + if(!(ar = pkio_load_archive_file(fp))) { + pkio_logmsg("failed to load archive: %s\n", fname); + fclose(fp); + return 0; + } + return ar; +} + +struct archive *pkio_load_archive_file(FILE *fp) +{ + struct archive *ar; + struct archive_module *mod; + + if(!modlist) { + pkio_logmsg("failed to load archive, no archive modules found\n"); + return 0; + } + + /* try all the archive modules in turn */ + mod = modlist; + while(mod) { + if((ar = mod->load_archive(fp, mod->data))) { + ar->module = mod; + return ar; + } + mod = mod->next; + } + + return 0; +} diff -r a71bd70c1014 -r a5728bc6a02f src/archive.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/archive.h Fri Jan 09 02:31:29 2015 +0200 @@ -0,0 +1,42 @@ +#ifndef ARCHIVE_H_ +#define ARCHIVE_H_ + +typedef void AR_FILE; + +struct archive; + +struct archive_module { + char *name; + void *data; + + struct archive *(*load_archive)(FILE *fp, void *data); + int (*close_archive)(struct archive *ar, void *data); + + AR_FILE *(*open)(struct archive *ar, const char *path, void *data); + int (*close)(struct archive *ar, AR_FILE *fp, void *data); + int (*read)(struct archive *ar, AR_FILE *fp, void *buf, int sz, void *data); + int (*write)(struct archive *ar, AR_FILE *fp, void *buf, int sz, void *data); + int (*seek)(struct archive *ar, AR_FILE *fp, int offs, int whence, void *data); + + struct archive_module *next; +}; + +struct archive { + FILE *fp; + struct archive_module *module; +}; + +int pkio_reg_archive_module(struct archive_module *m); + +struct archive *pkio_load_archive(const char *fname); +struct archive *pkio_load_archive_file(FILE *fp); +void pkio_close_archive(struct archive *ar); + +/* these map onto the function pointers used by the archive module */ +AR_FILE *pkio_ar_open(struct archive *ar, const char *path); +int pkio_ar_close(struct archive *ar, AR_FILE *fp); +int pkio_ar_read(struct archive *ar, AR_FILE *fp, void *buf, int sz); +int pkio_ar_write(struct archive *ar, AR_FILE *fp, void *buf, int sz); +int pkio_ar_seek(struct archive *ar, AR_FILE *fp, int offs, int whence); + +#endif /* ARCHIVE_H_ */ diff -r a71bd70c1014 -r a5728bc6a02f src/archive_flat.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/archive_flat.h Fri Jan 09 02:31:29 2015 +0200 @@ -0,0 +1,33 @@ +#include +#include +#include "archive.h" + + +static struct archive *load_archive(FILE *fp, void *data) +{ +} + +static int close_archive(struct archive *ar, void *data) +{ +} + + +static AR_FILE *open_file(struct archive *ar, const char *path, void *data) +{ +} + +static int close_file(struct archive *ar, AR_FILE *fp, void *data) +{ +} + +static int read_file(struct archive *ar, AR_FILE *fp, void *buf, int sz, void *data) +{ +} + +static int write_file(struct archive *ar, AR_FILE *fp, void *buf, int sz, void *data) +{ +} + +static int seek_file(struct archive *ar, AR_FILE *fp, int offs, int whence, void *data) +{ +} diff -r a71bd70c1014 -r a5728bc6a02f src/logger.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/logger.c Fri Jan 09 02:31:29 2015 +0200 @@ -0,0 +1,15 @@ +#include +#include +#include + +#define PREFIX "packio: " + +void pkio_logmsg(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + fputs(PREFIX, stderr); + vfprintf(stderr, fmt, ap); + va_end(ap); +} diff -r a71bd70c1014 -r a5728bc6a02f src/logger.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/logger.h Fri Jan 09 02:31:29 2015 +0200 @@ -0,0 +1,6 @@ +#ifndef LOGGER_H_ +#define LOGGER_H_ + +void pkio_logmsg(const char *fmt, ...); + +#endif /* LOGGER_H_ */ diff -r a71bd70c1014 -r a5728bc6a02f src/packio.c --- a/src/packio.c Sat Jan 03 15:29:05 2015 +0200 +++ b/src/packio.c Fri Jan 09 02:31:29 2015 +0200 @@ -1,6 +1,25 @@ #include +#include #include "packio.h" +#include "packio_types.h" +#include "logger.h" +#include "pathmap.h" -int pkio_fopen(const char *path) + +PKIO_FILE *pkio_fopen(const char *path, const char *mode) { + struct pkio_file *file; + FILE *fp; + const char *fname = pkio_pathmap(path); + + if((fp = fopen(fname, mode))) { + if(!(file = calloc(1, sizeof *file))) { + pkio_logmsg("%s: failed to allocate file structure\n", __FUNCTION__); + return 0; + } + file->fp = fp; + file->in_archive = 0; + } + + return 0; /* TODO */ } diff -r a71bd70c1014 -r a5728bc6a02f src/packio_types.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/packio_types.h Fri Jan 09 02:31:29 2015 +0200 @@ -0,0 +1,9 @@ +#ifndef PACKIO_TYPES_H_ +#define PACKIO_TYPES_H_ + +struct pkio_file { + int in_archive; + FILE *fp; +}; + +#endif /* PACKIO_TYPES_H_ */ diff -r a71bd70c1014 -r a5728bc6a02f src/pathmap.c --- a/src/pathmap.c Sat Jan 03 15:29:05 2015 +0200 +++ b/src/pathmap.c Fri Jan 09 02:31:29 2015 +0200 @@ -14,7 +14,7 @@ if(!map) { if(!(map = rb_create(RB_KEY_STRING))) { - fprintf(stderr, "packio: failed to create path mapping tree\n"); + fprintf(stderr, "packio: failed to create the path mapping tree\n"); return -1; } rb_set_delete_func(map, map_entry_destructor, 0); diff -r a71bd70c1014 -r a5728bc6a02f src/vnode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vnode.h Fri Jan 09 02:31:29 2015 +0200 @@ -0,0 +1,17 @@ +#ifndef VNODE_H_ +#define VNODE_H_ + +enum vnode_type { + VNODE_FILE, + VNODE_DIR +}; + +struct vnode { + enum vnode_type type; + + struct vnode *next, *prev; + struct vnode *parent, *child; +}; + + +#endif /* VNODE_H_ */