snd_input_t *in;
snd_output_t *out;
int stdio;
+ char *nfile = NULL;
err = snd_config_top(&config);
if (err < 0) {
return err;
}
stdio = !strcmp(file, "-");
+ if (!stdio) {
+ nfile = malloc(strlen(file) + 5);
+ if (nfile == NULL) {
+ error("No enough memory...");
+ goto out;
+ }
+ strcpy(nfile, file);
+ strcat(nfile, ".new");
+ }
if (!stdio && (err = snd_input_stdio_open(&in, file, "r")) >= 0) {
err = snd_config_load(config, in);
snd_input_close(in);
#if 0
if (err < 0) {
error("snd_config_load error: %s", snd_strerror(err));
- return err;
+ goto out;
}
#endif
}
if (card < 0) {
if (first) {
if (ignore_nocards) {
- return 0;
+ err = 0;
+ goto out;
} else {
error("No soundcards found...");
- return -ENODEV;
+ err = -ENODEV;
+ goto out;
}
}
break;
}
first = 0;
if ((err = get_controls(card, config)))
- return err;
+ goto out;
}
} else {
int cardno;
cardno = snd_card_get_index(cardname);
if (cardno < 0) {
error("Cannot find soundcard '%s'...", cardname);
- return cardno;
+ err = cardno;
+ goto out;
}
if ((err = get_controls(cardno, config))) {
- return err;
+ goto out;
}
}
- if (stdio)
+ if (stdio) {
err = snd_output_stdio_attach(&out, stdout, 0);
- else
- err = snd_output_stdio_open(&out, file, "w");
+ } else {
+ err = snd_output_stdio_open(&out, nfile, "w");
+ }
if (err < 0) {
error("Cannot open %s for writing: %s", file, snd_strerror(err));
- return -errno;
+ err = -errno;
+ goto out;
}
err = snd_config_save(config, out);
snd_output_close(out);
- if (err < 0)
+ if (err < 0) {
error("snd_config_save: %s", snd_strerror(err));
- return 0;
+ } else {
+ //unlink(file);
+ err = rename(nfile, file);
+ if (err < 0)
+ error("rename failed: %s (%s)", strerror(-err), file);
+ }
+out:
+ free(nfile);
+ snd_config_delete(config);
+ snd_config_update_free_global();
+ return err;
}
int load_state(const char *file, const char *initfile, const char *cardname,
snd_input_close(in);
if (err < 0) {
error("snd_config_load error: %s", snd_strerror(err));
- return err;
+ goto out;
}
} else {
int card, first = 1;
card = snd_card_get_index(cardname);
if (card < 0) {
error("Cannot find soundcard '%s'...", cardname);
- return -ENODEV;
+ err = -ENODEV;
+ goto out;
}
goto single;
} else {
}
if (first)
finalerr = 0; /* no cards, no error code */
- return finalerr;
+ err = finalerr;
+ goto out;
}
if (!cardname) {
if (card < 0) {
if (first) {
if (ignore_nocards) {
- return 0;
+ err = 0;
+ goto out;
} else {
error("No soundcards found...");
- return -ENODEV;
+ err = -ENODEV;
+ goto out;
}
}
break;
cardno = snd_card_get_index(cardname);
if (cardno < 0) {
error("Cannot find soundcard '%s'...", cardname);
- return -ENODEV;
+ err = -ENODEV;
+ goto out;
}
/* do a check if controls matches state file */
if (do_init && set_controls(cardno, config, 0)) {
if ((err = set_controls(cardno, config, 1))) {
initfailed(cardno, "restore", err);
if (!force_restore)
- return err;
+ goto out;
}
}
- return finalerr;
+ err = finalerr;
+out:
+ snd_config_delete(config);
+ snd_config_update_free_global();
+ return err;
}