packio
changeset 1:a5728bc6a02f tip
moving along
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 09 Jan 2015 02:31:29 +0200 |
parents | a71bd70c1014 |
children | |
files | include/packio.h src/archive.c src/archive.h src/archive_flat.h src/logger.c src/logger.h src/packio.c src/packio_types.h src/pathmap.c src/vnode.h |
diffstat | 10 files changed, 204 insertions(+), 3 deletions(-) [+] |
line diff
1.1 --- a/include/packio.h Sat Jan 03 15:29:05 2015 +0200 1.2 +++ b/include/packio.h Fri Jan 09 02:31:29 2015 +0200 1.3 @@ -20,7 +20,7 @@ 1.4 1.5 extern int pkio_errno; 1.6 1.7 -int pkio_fopen(const char *path); 1.8 +PKIO_FILE *pkio_fopen(const char *path, const char *mode); 1.9 int pkio_fclose(PKIO_FILE *fp); 1.10 1.11 int pkio_fseek(PKIO_FILE *fp, long offset, int whence);
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/archive.c Fri Jan 09 02:31:29 2015 +0200 2.3 @@ -0,0 +1,60 @@ 2.4 +#include <stdio.h> 2.5 +#include <string.h> 2.6 +#include <errno.h> 2.7 +#include "archive.h" 2.8 +#include "logger.h" 2.9 + 2.10 +static struct archive_module *modlist; 2.11 + 2.12 +int pkio_reg_archive_module(struct archive_module *m) 2.13 +{ 2.14 + m->next = modlist; 2.15 + modlist = m; 2.16 + return 0; 2.17 +} 2.18 + 2.19 +struct archive *pkio_load_archive(const char *fname) 2.20 +{ 2.21 + FILE *fp; 2.22 + struct archive *ar; 2.23 + 2.24 + if(!modlist) { 2.25 + pkio_logmsg("failed to load archive, no archive modules found\n"); 2.26 + return 0; 2.27 + } 2.28 + 2.29 + if(!(fp = fopen(fname, "rb"))) { 2.30 + pkio_logmsg("failed to load archive: %s: %s\n", fname, strerror(errno)); 2.31 + return 0; 2.32 + } 2.33 + 2.34 + if(!(ar = pkio_load_archive_file(fp))) { 2.35 + pkio_logmsg("failed to load archive: %s\n", fname); 2.36 + fclose(fp); 2.37 + return 0; 2.38 + } 2.39 + return ar; 2.40 +} 2.41 + 2.42 +struct archive *pkio_load_archive_file(FILE *fp) 2.43 +{ 2.44 + struct archive *ar; 2.45 + struct archive_module *mod; 2.46 + 2.47 + if(!modlist) { 2.48 + pkio_logmsg("failed to load archive, no archive modules found\n"); 2.49 + return 0; 2.50 + } 2.51 + 2.52 + /* try all the archive modules in turn */ 2.53 + mod = modlist; 2.54 + while(mod) { 2.55 + if((ar = mod->load_archive(fp, mod->data))) { 2.56 + ar->module = mod; 2.57 + return ar; 2.58 + } 2.59 + mod = mod->next; 2.60 + } 2.61 + 2.62 + return 0; 2.63 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/archive.h Fri Jan 09 02:31:29 2015 +0200 3.3 @@ -0,0 +1,42 @@ 3.4 +#ifndef ARCHIVE_H_ 3.5 +#define ARCHIVE_H_ 3.6 + 3.7 +typedef void AR_FILE; 3.8 + 3.9 +struct archive; 3.10 + 3.11 +struct archive_module { 3.12 + char *name; 3.13 + void *data; 3.14 + 3.15 + struct archive *(*load_archive)(FILE *fp, void *data); 3.16 + int (*close_archive)(struct archive *ar, void *data); 3.17 + 3.18 + AR_FILE *(*open)(struct archive *ar, const char *path, void *data); 3.19 + int (*close)(struct archive *ar, AR_FILE *fp, void *data); 3.20 + int (*read)(struct archive *ar, AR_FILE *fp, void *buf, int sz, void *data); 3.21 + int (*write)(struct archive *ar, AR_FILE *fp, void *buf, int sz, void *data); 3.22 + int (*seek)(struct archive *ar, AR_FILE *fp, int offs, int whence, void *data); 3.23 + 3.24 + struct archive_module *next; 3.25 +}; 3.26 + 3.27 +struct archive { 3.28 + FILE *fp; 3.29 + struct archive_module *module; 3.30 +}; 3.31 + 3.32 +int pkio_reg_archive_module(struct archive_module *m); 3.33 + 3.34 +struct archive *pkio_load_archive(const char *fname); 3.35 +struct archive *pkio_load_archive_file(FILE *fp); 3.36 +void pkio_close_archive(struct archive *ar); 3.37 + 3.38 +/* these map onto the function pointers used by the archive module */ 3.39 +AR_FILE *pkio_ar_open(struct archive *ar, const char *path); 3.40 +int pkio_ar_close(struct archive *ar, AR_FILE *fp); 3.41 +int pkio_ar_read(struct archive *ar, AR_FILE *fp, void *buf, int sz); 3.42 +int pkio_ar_write(struct archive *ar, AR_FILE *fp, void *buf, int sz); 3.43 +int pkio_ar_seek(struct archive *ar, AR_FILE *fp, int offs, int whence); 3.44 + 3.45 +#endif /* ARCHIVE_H_ */
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/archive_flat.h Fri Jan 09 02:31:29 2015 +0200 4.3 @@ -0,0 +1,33 @@ 4.4 +#include <stdio.h> 4.5 +#include <stdlib.h> 4.6 +#include "archive.h" 4.7 + 4.8 + 4.9 +static struct archive *load_archive(FILE *fp, void *data) 4.10 +{ 4.11 +} 4.12 + 4.13 +static int close_archive(struct archive *ar, void *data) 4.14 +{ 4.15 +} 4.16 + 4.17 + 4.18 +static AR_FILE *open_file(struct archive *ar, const char *path, void *data) 4.19 +{ 4.20 +} 4.21 + 4.22 +static int close_file(struct archive *ar, AR_FILE *fp, void *data) 4.23 +{ 4.24 +} 4.25 + 4.26 +static int read_file(struct archive *ar, AR_FILE *fp, void *buf, int sz, void *data) 4.27 +{ 4.28 +} 4.29 + 4.30 +static int write_file(struct archive *ar, AR_FILE *fp, void *buf, int sz, void *data) 4.31 +{ 4.32 +} 4.33 + 4.34 +static int seek_file(struct archive *ar, AR_FILE *fp, int offs, int whence, void *data) 4.35 +{ 4.36 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/logger.c Fri Jan 09 02:31:29 2015 +0200 5.3 @@ -0,0 +1,15 @@ 5.4 +#include <stdio.h> 5.5 +#include <stdlib.h> 5.6 +#include <stdarg.h> 5.7 + 5.8 +#define PREFIX "packio: " 5.9 + 5.10 +void pkio_logmsg(const char *fmt, ...) 5.11 +{ 5.12 + va_list ap; 5.13 + 5.14 + va_start(ap, fmt); 5.15 + fputs(PREFIX, stderr); 5.16 + vfprintf(stderr, fmt, ap); 5.17 + va_end(ap); 5.18 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/logger.h Fri Jan 09 02:31:29 2015 +0200 6.3 @@ -0,0 +1,6 @@ 6.4 +#ifndef LOGGER_H_ 6.5 +#define LOGGER_H_ 6.6 + 6.7 +void pkio_logmsg(const char *fmt, ...); 6.8 + 6.9 +#endif /* LOGGER_H_ */
7.1 --- a/src/packio.c Sat Jan 03 15:29:05 2015 +0200 7.2 +++ b/src/packio.c Fri Jan 09 02:31:29 2015 +0200 7.3 @@ -1,6 +1,25 @@ 7.4 #include <stdio.h> 7.5 +#include <stdlib.h> 7.6 #include "packio.h" 7.7 +#include "packio_types.h" 7.8 +#include "logger.h" 7.9 +#include "pathmap.h" 7.10 7.11 -int pkio_fopen(const char *path) 7.12 + 7.13 +PKIO_FILE *pkio_fopen(const char *path, const char *mode) 7.14 { 7.15 + struct pkio_file *file; 7.16 + FILE *fp; 7.17 + const char *fname = pkio_pathmap(path); 7.18 + 7.19 + if((fp = fopen(fname, mode))) { 7.20 + if(!(file = calloc(1, sizeof *file))) { 7.21 + pkio_logmsg("%s: failed to allocate file structure\n", __FUNCTION__); 7.22 + return 0; 7.23 + } 7.24 + file->fp = fp; 7.25 + file->in_archive = 0; 7.26 + } 7.27 + 7.28 + return 0; /* TODO */ 7.29 }
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/src/packio_types.h Fri Jan 09 02:31:29 2015 +0200 8.3 @@ -0,0 +1,9 @@ 8.4 +#ifndef PACKIO_TYPES_H_ 8.5 +#define PACKIO_TYPES_H_ 8.6 + 8.7 +struct pkio_file { 8.8 + int in_archive; 8.9 + FILE *fp; 8.10 +}; 8.11 + 8.12 +#endif /* PACKIO_TYPES_H_ */
9.1 --- a/src/pathmap.c Sat Jan 03 15:29:05 2015 +0200 9.2 +++ b/src/pathmap.c Fri Jan 09 02:31:29 2015 +0200 9.3 @@ -14,7 +14,7 @@ 9.4 9.5 if(!map) { 9.6 if(!(map = rb_create(RB_KEY_STRING))) { 9.7 - fprintf(stderr, "packio: failed to create path mapping tree\n"); 9.8 + fprintf(stderr, "packio: failed to create the path mapping tree\n"); 9.9 return -1; 9.10 } 9.11 rb_set_delete_func(map, map_entry_destructor, 0);
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/src/vnode.h Fri Jan 09 02:31:29 2015 +0200 10.3 @@ -0,0 +1,17 @@ 10.4 +#ifndef VNODE_H_ 10.5 +#define VNODE_H_ 10.6 + 10.7 +enum vnode_type { 10.8 + VNODE_FILE, 10.9 + VNODE_DIR 10.10 +}; 10.11 + 10.12 +struct vnode { 10.13 + enum vnode_type type; 10.14 + 10.15 + struct vnode *next, *prev; 10.16 + struct vnode *parent, *child; 10.17 +}; 10.18 + 10.19 + 10.20 +#endif /* VNODE_H_ */