]> sigrok.org Git - libsigrok.git/blobdiff - session_file.c
gmc-mh-1x-2x: Support for interface SI232-II with driver gmc-mh-2x-bd232.
[libsigrok.git] / session_file.c
index 4e3d05bb395331b5936a8f530d92cc8fc198246a..11adb47f957836f427f1bf5d2128a2d1425650e1 100644 (file)
@@ -81,10 +81,11 @@ SR_PRIV int sr_sessionfile_check(const char *filename)
        zip_fclose(zf);
        s[ret] = 0;
        version = strtoull(s, NULL, 10);
-       if (version != 1) {
+       if (version > 2) {
                sr_dbg("Cannot handle sigrok session file version %d.", version);
                return SR_ERR;
        }
+       sr_spew("Detected sigrok session file version %d.", version);
 
        /* read "metadata" */
        if (zip_stat(archive, "metadata", 0, &zs) == -1) {
@@ -253,11 +254,14 @@ SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
        cnt = 0;
        for (l = sdi->probes; l; l = l->next) {
                probe = l->data;
-               if (probe->enabled) {
-                       if (probe->name)
-                               /* Just borrowing the ptr. */
-                               probe_names[cnt++] = probe->name;
-               }
+               if (probe->type != SR_PROBE_LOGIC)
+                       continue;
+               if (probe->enabled != TRUE)
+                       continue;
+               if (!probe->name)
+                       continue;
+               /* Just borrowing the ptr. */
+               probe_names[cnt++] = probe->name;
        }
 
        if ((ret = sr_session_save_init(filename, samplerate, probe_names)) != SR_OK)
@@ -301,7 +305,7 @@ SR_API int sr_session_save_init(const char *filename, uint64_t samplerate,
                return SR_ERR;
 
        /* "version" */
-       version[0] = '1';
+       version[0] = '2';
        if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
                return SR_ERR;
        if (zip_add(zipfile, "version", versrc) == -1) {
@@ -337,13 +341,18 @@ SR_API int sr_session_save_init(const char *filename, uint64_t samplerate,
 
        fclose(meta);
 
-       if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
+       if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1))) {
+               unlink(metafile);
                return SR_ERR;
-       if (zip_add(zipfile, "metadata", metasrc) == -1)
+       }
+       if (zip_add(zipfile, "metadata", metasrc) == -1) {
+               unlink(metafile);
                return SR_ERR;
+       }
 
        if ((ret = zip_close(zipfile)) == -1) {
                sr_info("error saving zipfile: %s", zip_strerror(zipfile));
+               unlink(metafile);
                return SR_ERR;
        }
 
@@ -408,6 +417,7 @@ SR_API int sr_session_append(const char *filename, unsigned char *buf,
                return SR_ERR;
        }
        g_free(metafile);
+       tmpname[0] = '\0';
        if (!g_key_file_has_key(kf, "device 1", "unitsize", &error)) {
                if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
                        sr_err("Failed to check unitsize key: %s", error ? error->message : "?");
@@ -421,16 +431,21 @@ SR_API int sr_session_append(const char *filename, unsigned char *buf,
                        return SR_ERR;
                if (write(tmpfile, metafile, len) < 0) {
                        sr_dbg("Failed to create new metadata: %s", strerror(errno));
+                       g_free(metafile);
+                       unlink(tmpname);
                        return SR_ERR;
                }
                close(tmpfile);
                if (!(metasrc = zip_source_file(archive, tmpname, 0, -1))) {
-                       g_free(metafile);
                        sr_err("Failed to create zip source for metadata.");
+                       g_free(metafile);
+                       unlink(tmpname);
                        return SR_ERR;
                }
                if (zip_replace(archive, zs.index, metasrc) == -1) {
                        sr_err("Failed to replace metadata file.");
+                       g_free(metafile);
+                       unlink(tmpname);
                        return SR_ERR;
                }
                g_free(metafile);
@@ -448,6 +463,7 @@ SR_API int sr_session_append(const char *filename, unsigned char *buf,
                         * Rename it to "logic-1-1" * and continue with chunk 2. */
                        if (zip_rename(archive, i, "logic-1-1") == -1) {
                                sr_err("Failed to rename 'logic-1' to 'logic-1-1'.");
+                               unlink(tmpname);
                                return SR_ERR;
                        }
                        next_chunk_num = 2;
@@ -459,14 +475,20 @@ SR_API int sr_session_append(const char *filename, unsigned char *buf,
                }
        }
        snprintf(chunkname, 15, "logic-1-%d", next_chunk_num);
-       if (!(logicsrc = zip_source_buffer(archive, buf, units * unitsize, FALSE)))
+       if (!(logicsrc = zip_source_buffer(archive, buf, units * unitsize, FALSE))) {
+               unlink(tmpname);
                return SR_ERR;
-       if (zip_add(archive, chunkname, logicsrc) == -1)
+       }
+       if (zip_add(archive, chunkname, logicsrc) == -1) {
+               unlink(tmpname);
                return SR_ERR;
+       }
        if ((ret = zip_close(archive)) == -1) {
                sr_info("error saving session file: %s", zip_strerror(archive));
+               unlink(tmpname);
                return SR_ERR;
        }
+       unlink(tmpname);
 
        return SR_OK;
 }