]> sigrok.org Git - libsigrok.git/blame - src/output/srzip.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / output / srzip.c
CommitLineData
3250d8c7
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdlib.h>
21#include <unistd.h>
22#include <string.h>
23#include <errno.h>
24#include <glib.h>
25#include <glib/gstdio.h>
26#include <zip.h>
c1aae900 27#include <libsigrok/libsigrok.h>
3250d8c7
BV
28#include "libsigrok-internal.h"
29
30#define LOG_PREFIX "output/srzip"
31
32struct out_context {
33 gboolean zip_created;
34 uint64_t samplerate;
35 char *filename;
36};
37
38static int init(struct sr_output *o, GHashTable *options)
39{
40 struct out_context *outc;
41
37875f75
SA
42 (void)options;
43
37875f75
SA
44 if (strlen(o->filename) == 0) {
45 sr_info("srzip output module requires a file name, cannot save.");
3250d8c7 46 return SR_ERR_ARG;
37875f75 47 }
7df31ae8
SA
48
49 outc = g_malloc0(sizeof(struct out_context));
37875f75 50 outc->filename = g_strdup(o->filename);
7df31ae8 51 o->priv = outc;
3250d8c7
BV
52
53 return SR_OK;
54}
55
56static int zip_create(const struct sr_output *o)
57{
58 struct out_context *outc;
59 struct sr_channel *ch;
60 FILE *meta;
61 struct zip *zipfile;
62 struct zip_source *versrc, *metasrc;
63 GVariant *gvar;
64 GSList *l;
65 int tmpfile, ret;
66 char version[1], metafile[32], *s;
67
68 outc = o->priv;
69 if (outc->samplerate == 0) {
70 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
71 &gvar) == SR_OK) {
72 outc->samplerate = g_variant_get_uint64(gvar);
73 g_variant_unref(gvar);
74 }
75 }
76
77 /* Quietly delete it first, libzip wants replace ops otherwise. */
78 unlink(outc->filename);
79 if (!(zipfile = zip_open(outc->filename, ZIP_CREATE, &ret)))
80 return SR_ERR;
81
82 /* "version" */
83 version[0] = '2';
84 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
85 return SR_ERR;
86 if (zip_add(zipfile, "version", versrc) == -1) {
87 sr_info("Error saving version into zipfile: %s.",
88 zip_strerror(zipfile));
89 return SR_ERR;
90 }
91
92 /* init "metadata" */
93 strcpy(metafile, "sigrok-meta-XXXXXX");
94 if ((tmpfile = g_mkstemp(metafile)) == -1)
95 return SR_ERR;
96 close(tmpfile);
97 meta = g_fopen(metafile, "wb");
98 fprintf(meta, "[global]\n");
99 fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
100 fprintf(meta, "[device 1]\ncapturefile = logic-1\n");
101 fprintf(meta, "total probes = %d\n", g_slist_length(o->sdi->channels));
102 s = sr_samplerate_string(outc->samplerate);
103 fprintf(meta, "samplerate = %s\n", s);
104 g_free(s);
105
106 for (l = o->sdi->channels; l; l = l->next) {
107 ch = l->data;
108 if (ch->type != SR_CHANNEL_LOGIC)
109 continue;
5e1b68c6
AJ
110 if (!ch->enabled)
111 continue;
3250d8c7
BV
112 fprintf(meta, "probe%d = %s\n", ch->index + 1, ch->name);
113 }
114 fclose(meta);
115
116 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1))) {
117 unlink(metafile);
118 return SR_ERR;
119 }
120 if (zip_add(zipfile, "metadata", metasrc) == -1) {
121 unlink(metafile);
122 return SR_ERR;
123 }
124
125 if ((ret = zip_close(zipfile)) == -1) {
126 sr_info("Error saving zipfile: %s.", zip_strerror(zipfile));
127 unlink(metafile);
128 return SR_ERR;
129 }
130
131 unlink(metafile);
132
133 return SR_OK;
134}
135
136static int zip_append(const struct sr_output *o, unsigned char *buf,
137 int unitsize, int length)
138{
139 struct out_context *outc;
140 struct zip *archive;
141 struct zip_source *logicsrc;
142 zip_int64_t num_files;
143 struct zip_file *zf;
144 struct zip_stat zs;
145 struct zip_source *metasrc;
146 GKeyFile *kf;
147 GError *error;
148 gsize len;
149 int chunk_num, next_chunk_num, tmpfile, ret, i;
150 const char *entry_name;
151 char *metafile, tmpname[32], chunkname[16];
152
153 outc = o->priv;
154 if (!(archive = zip_open(outc->filename, 0, &ret)))
155 return SR_ERR;
156
157 if (zip_stat(archive, "metadata", 0, &zs) == -1)
158 return SR_ERR;
159
160 metafile = g_malloc(zs.size);
161 zf = zip_fopen_index(archive, zs.index, 0);
162 zip_fread(zf, metafile, zs.size);
163 zip_fclose(zf);
164
165 /*
166 * If the file was only initialized but doesn't yet have any
167 * data it in, it won't have a unitsize field in metadata yet.
168 */
169 error = NULL;
170 kf = g_key_file_new();
171 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, &error)) {
172 sr_err("Failed to parse metadata: %s.", error->message);
173 return SR_ERR;
174 }
175 g_free(metafile);
176 tmpname[0] = '\0';
177 if (!g_key_file_has_key(kf, "device 1", "unitsize", &error)) {
178 if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
179 sr_err("Failed to check unitsize key: %s", error ? error->message : "?");
180 return SR_ERR;
181 }
182 /* Add unitsize field. */
183 g_key_file_set_integer(kf, "device 1", "unitsize", unitsize);
184 metafile = g_key_file_to_data(kf, &len, &error);
185 strcpy(tmpname, "sigrok-meta-XXXXXX");
186 if ((tmpfile = g_mkstemp(tmpname)) == -1)
187 return SR_ERR;
188 if (write(tmpfile, metafile, len) < 0) {
189 sr_dbg("Failed to create new metadata: %s", strerror(errno));
190 g_free(metafile);
191 unlink(tmpname);
192 return SR_ERR;
193 }
194 close(tmpfile);
195 if (!(metasrc = zip_source_file(archive, tmpname, 0, -1))) {
196 sr_err("Failed to create zip source for metadata.");
197 g_free(metafile);
198 unlink(tmpname);
199 return SR_ERR;
200 }
201 if (zip_replace(archive, zs.index, metasrc) == -1) {
202 sr_err("Failed to replace metadata file.");
203 g_free(metafile);
204 unlink(tmpname);
205 return SR_ERR;
206 }
207 g_free(metafile);
208 }
209 g_key_file_free(kf);
210
211 next_chunk_num = 1;
212 num_files = zip_get_num_entries(archive, 0);
213 for (i = 0; i < num_files; i++) {
214 entry_name = zip_get_name(archive, i, 0);
215 if (strncmp(entry_name, "logic-1", 7))
216 continue;
217 if (strlen(entry_name) == 7) {
218 /* This file has no extra chunks, just a single "logic-1".
219 * Rename it to "logic-1-1" * and continue with chunk 2. */
220 if (zip_rename(archive, i, "logic-1-1") == -1) {
221 sr_err("Failed to rename 'logic-1' to 'logic-1-1'.");
222 unlink(tmpname);
223 return SR_ERR;
224 }
225 next_chunk_num = 2;
226 break;
227 } else if (strlen(entry_name) > 8 && entry_name[7] == '-') {
228 chunk_num = strtoull(entry_name + 8, NULL, 10);
229 if (chunk_num >= next_chunk_num)
230 next_chunk_num = chunk_num + 1;
231 }
232 }
233 snprintf(chunkname, 15, "logic-1-%d", next_chunk_num);
234 if (!(logicsrc = zip_source_buffer(archive, buf, length, FALSE))) {
235 unlink(tmpname);
236 return SR_ERR;
237 }
238 if (zip_add(archive, chunkname, logicsrc) == -1) {
239 unlink(tmpname);
240 return SR_ERR;
241 }
242 if ((ret = zip_close(archive)) == -1) {
243 sr_info("error saving session file: %s", zip_strerror(archive));
244 unlink(tmpname);
245 return SR_ERR;
246 }
247 unlink(tmpname);
248
249 return SR_OK;
250}
251
252static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
253 GString **out)
254{
255 struct out_context *outc;
256 const struct sr_datafeed_meta *meta;
257 const struct sr_datafeed_logic *logic;
258 const struct sr_config *src;
259 GSList *l;
260
261 int ret;
262
263 *out = NULL;
264 if (!o || !o->sdi || !(outc = o->priv))
265 return SR_ERR_ARG;
266
267 switch (packet->type) {
268 case SR_DF_META:
269 meta = packet->payload;
270 for (l = meta->config; l; l = l->next) {
271 src = l->data;
272 if (src->key != SR_CONF_SAMPLERATE)
273 continue;
274 outc->samplerate = g_variant_get_uint64(src->data);
275 }
276 break;
277 case SR_DF_LOGIC:
278 if (!outc->zip_created) {
279 if ((ret = zip_create(o)) != SR_OK)
280 return ret;
281 outc->zip_created = TRUE;
282 }
283 logic = packet->payload;
284 ret = zip_append(o, logic->data, logic->unitsize, logic->length);
285 break;
286 }
287
288 return SR_OK;
289}
290
291static int cleanup(struct sr_output *o)
292{
293 struct out_context *outc;
294
295 outc = o->priv;
296 g_free(outc->filename);
297 g_free(outc);
298 o->priv = NULL;
299
300 return SR_OK;
301}
302
303static struct sr_option options[] = {
3250d8c7
BV
304 ALL_ZERO
305};
306
307static const struct sr_option *get_options(void)
308{
309 if (!options[0].def)
310 options[0].def = g_variant_ref_sink(g_variant_new_string(""));
311
312 return options;
313}
314
315SR_PRIV struct sr_output_module output_srzip = {
316 .id = "srzip",
317 .name = "srzip",
318 .desc = "srzip session file",
8a174d23 319 .exts = (const char*[]){"sr", NULL},
3cd4b381 320 .flags = SR_OUTPUT_INTERNAL_IO_HANDLING,
3250d8c7
BV
321 .options = get_options,
322 .init = init,
323 .receive = receive,
324 .cleanup = cleanup,
325};