-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyio.c
144 lines (125 loc) · 2.78 KB
/
myio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "myio.h"
#include "ngtemplate.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "logging.h"
#ifdef USE_ZZIP
#include "zzip/lib.h"
static ZZIP_DIR *zipfile = NULL;
#endif
#ifdef USE_LIBZIP
#include "zip.h"
static struct zip *zipfile = NULL;
#endif
int io_init(const char *argv0)
{
#ifdef USE_ZZIP
zzip_error_t err;
zipfile = zzip_dir_open(argv0, &err);
#endif
#ifdef USE_LIBZIP
int err;
zipfile = zip_open(argv0, ZIP_CHECKCONS, &err);
#endif
if (!zipfile) {
pid_t pid = getpid();
char b1[64];
char b2[1024];
int k;
sprintf(b1, "/proc/%i/exe", pid);
if ((k = readlink(b1, b2, 1024)) == -1) {
fprintf(stderr, "readlink on %s failed\n", b1);
} else {
b2[k] = 0;
#ifdef USE_ZZIP
zipfile = zzip_dir_open(b2, &err);
#endif
#ifdef USE_LIBZIP
zipfile = zip_open(b2, ZIP_CHECKCONS, &err);
#endif
}
}
if (!zipfile)
{
fprintf(stderr, "Error: failed to open zipfile");
return 1;
}
else
DEBUG("Loaded zip data from %s\n", argv0);
return 0;
}
void io_quit(void)
{
if (zipfile)
#ifdef USE_ZZIP
zzip_dir_close(zipfile);
#endif
#ifdef USE_LIBZIP
zip_close(zipfile);
#endif
zipfile = NULL;
}
static char* load_file_from_zip(const char *filename)
{
if (!zipfile)
{
fprintf(stderr, "Error: %s\n", "Zipfile is invalid");
return NULL;
}
#ifdef USE_ZZIP
ZZIP_FILE *file = zzip_file_open(zipfile, filename, 0);
#endif
#ifdef USE_LIBZIP
struct zip_file *file = zip_fopen(zipfile, filename, 0);
#endif
if (!file)
{
fprintf(stderr, "Error: File not found %s\n", filename);
return NULL;
}
#ifdef USE_ZZIP
ZZIP_STAT stat;
zzip_file_stat(file, &stat);
size_t len = stat.st_size;
#endif
#ifdef USE_LIBZIP
struct zip_stat stat;
zip_stat(zipfile, filename, 0, &stat);
size_t len = stat.size;
#endif
char *buff = malloc(len + 1);
memset(buff, 0, len + 1);
#ifdef USE_ZZIP
zzip_file_read(file, buff, len);
zzip_file_close(file);
#endif
#ifdef USE_LIBZIP
zip_fread(file, buff, len);
zip_fclose(file);
#endif
DEBUG("loaded file: %s [%i bytes]\n", filename, len);
return buff;
}
char* load_file(const char *filename)
{
return load_file_from_zip(filename);
}
int load_template(ngt_template* tpl, const char *filename)
{
char *buff = load_file_from_zip(filename);
if (!buff)
return -1;
if (tpl->template)
free(tpl->template);
tpl->template = buff;
return 0;
}
char* load_template_string(const char *name)
{
char *realname = malloc(strlen(name) + 6);
sprintf(realname, "%s.tmpl", name);
return load_file(realname);
}