- GSList *l, *d;
- struct device *device;
- struct datastore *ds;
- struct zip *zipfile;
- struct zip_source *src;
- int bufcnt, devcnt, tmpfile, ret, error;
- char version[1], rawname[16], metafile[32], *buf;
-
- /* Quietly delete it first, libzip wants replace ops otherwise. */
- unlink(filename);
-
- if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
- return SIGROK_ERR;
-
- /* Version */
- version[0] = '1';
- if (!(src = zip_source_buffer(zipfile, version, 1, 0)))
- return SIGROK_ERR;
- if (zip_add(zipfile, "version", src) == -1) {
- g_message("error saving version into zipfile: %s",
- zip_strerror(zipfile));
- return SIGROK_ERR;
- }
-
- /* Metadata */
- strcpy(metafile, "sigrok-meta-XXXXXX");
- if ((tmpfile = g_mkstemp(metafile)) == -1)
- return SIGROK_ERR;
- close(tmpfile);
- make_metadata(metafile);
- if (!(src = zip_source_file(zipfile, metafile, 0, -1)))
- return SIGROK_ERR;
- if (zip_add(zipfile, "metadata", src) == -1)
- return SIGROK_ERR;
- unlink(metafile);
-
- /* Raw */
- devcnt = 1;
- for (l = session->devices; l; l = l->next) {
- device = l->data;
- ds = device->datastore;
- if (ds) {
- buf = malloc(ds->num_units * ds->ds_unitsize +
- DATASTORE_CHUNKSIZE);
- bufcnt = 0;
- for (d = ds->chunklist; d; d = d->next) {
- memcpy(buf + bufcnt, d->data,
- DATASTORE_CHUNKSIZE);
- bufcnt += DATASTORE_CHUNKSIZE;
- }
- if (!(src = zip_source_buffer(zipfile, buf,
- ds->num_units * ds->ds_unitsize, TRUE)))
- return SIGROK_ERR;
- snprintf(rawname, 15, "raw-%d", devcnt);
- if (zip_add(zipfile, rawname, src) == -1)
- return SIGROK_ERR;
- }
- devcnt++;
- }
-
- if ((ret = zip_close(zipfile)) == -1) {
- g_message("error saving zipfile: %s", zip_strerror(zipfile));
- return SIGROK_ERR;
+ struct source *new_sources;
+ int old, new;
+
+ if (!sources)
+ return;
+
+ new_sources = calloc(1, sizeof(struct source) * num_sources);
+ for (old = 0, new = 0; old < num_sources; old++)
+ if (sources[old].fd != fd)
+ memcpy(&new_sources[new++], &sources[old],
+ sizeof(struct source));
+
+ if (old != new) {
+ free(sources);
+ sources = new_sources;
+ num_sources--;
+ } else {
+ /* Target fd was not found. */
+ free(new_sources);