root/daemon/cmd_save_studio.c @ 09cb302c01901f7fda8ddafef92d30a38a5d73c6

Revision 09cb302c01901f7fda8ddafef92d30a38a5d73c6, 11.5 KB (checked in by Nedko Arnaudov <nedko@…>, 3 years ago)

daemon: fix save of studios with rooms

  • Property mode set to 100644
Line 
1/* -*- Mode: C ; c-basic-offset: 2 -*- */
2/*
3 * LADI Session Handler (ladish)
4 *
5 * Copyright (C) 2009,2010 Nedko Arnaudov <nedko@arnaudov.name>
6 *
7 **************************************************************************
8 * This file contains implementation of the "save studio" command
9 **************************************************************************
10 *
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27#include <sys/types.h>
28#include <signal.h>
29
30#include "common.h"
31
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <fcntl.h>
35#include <unistd.h>
36
37#include "escape.h"
38#include "studio_internal.h"
39#include "cmd.h"
40#include "../proxies/notify_proxy.h"
41#include "save.h"
42
43#define STUDIO_HEADER_TEXT BASE_NAME " Studio configuration.\n"
44
45bool
46write_jack_parameter(
47  int fd,
48  int indent,
49  struct jack_conf_parameter * parameter_ptr)
50{
51  const char * src;
52  char * dst;
53  char path[JACK_CONF_MAX_ADDRESS_SIZE * 3]; /* encode each char in three bytes (percent encoding) */
54  const char * content;
55  char valbuf[100];
56
57  /* compose the parameter path, percent-encode "bad" chars */
58  src = parameter_ptr->address;
59  dst = path;
60  do
61  {
62    *dst++ = '/';
63    escape(&src, &dst);
64    src++;
65  }
66  while (*src != 0);
67  *dst = 0;
68
69  if (!ladish_write_indented_string(fd, indent, "<parameter path=\""))
70  {
71    return false;
72  }
73
74  if (!ladish_write_string(fd, path))
75  {
76    return false;
77  }
78
79  if (!ladish_write_string(fd, "\">"))
80  {
81    return false;
82  }
83
84  switch (parameter_ptr->parameter.type)
85  {
86  case jack_boolean:
87    content = parameter_ptr->parameter.value.boolean ? "true" : "false";
88    log_debug("%s value is %s (boolean)", path, content);
89    break;
90  case jack_string:
91    content = parameter_ptr->parameter.value.string;
92    log_debug("%s value is %s (string)", path, content);
93    break;
94  case jack_byte:
95    valbuf[0] = (char)parameter_ptr->parameter.value.byte;
96    valbuf[1] = 0;
97    content = valbuf;
98    log_debug("%s value is %u/%c (byte/char)", path, parameter_ptr->parameter.value.byte, (char)parameter_ptr->parameter.value.byte);
99    break;
100  case jack_uint32:
101    snprintf(valbuf, sizeof(valbuf), "%" PRIu32, parameter_ptr->parameter.value.uint32);
102    content = valbuf;
103    log_debug("%s value is %s (uint32)", path, content);
104    break;
105  case jack_int32:
106    snprintf(valbuf, sizeof(valbuf), "%" PRIi32, parameter_ptr->parameter.value.int32);
107    content = valbuf;
108    log_debug("%s value is %s (int32)", path, content);
109    break;
110  default:
111    log_error("unknown jack parameter_ptr->parameter type %d (%s)", (int)parameter_ptr->parameter.type, path);
112    return false;
113  }
114
115  if (!ladish_write_string(fd, content))
116  {
117    return false;
118  }
119
120  if (!ladish_write_string(fd, "</parameter>\n"))
121  {
122    return false;
123  }
124
125  return true;
126}
127
128#define fd (((struct ladish_write_context *)context)->fd)
129#define indent (((struct ladish_write_context *)context)->indent)
130
131static bool save_studio_room(void * context, ladish_room_handle room)
132{
133  uuid_t uuid;
134  char str[37];
135
136  log_info("saving room '%s'", ladish_room_get_name(room));
137
138  if (!ladish_write_indented_string(fd, indent, "<room name=\""))
139  {
140    return false;
141  }
142
143  if (!ladish_write_string(fd, ladish_room_get_name(room)))
144  {
145    return false;
146  }
147
148  if (!ladish_write_string(fd, "\" uuid=\""))
149  {
150    return false;
151  }
152
153  ladish_room_get_uuid(room, uuid);
154  uuid_unparse(uuid, str);
155
156  if (!ladish_write_string(fd, str))
157  {
158    return false;
159  }
160
161  if (!ladish_write_string(fd, "\">\n"))
162  {
163    return false;
164  }
165
166  if (!ladish_write_room_link_ports(fd, indent + 1, room))
167  {
168    log_error("ladish_write_room_link_ports() failed");
169    return false;
170  }
171
172  if (!ladish_write_indented_string(fd, indent, " </room>\n"))
173  {
174    return false;
175  }
176
177  return true;
178}
179
180#undef indent
181#undef fd
182
183struct ladish_command_save_studio
184{
185  struct ladish_command command;
186  char * studio_name;
187};
188
189#define cmd_ptr ((struct ladish_command_save_studio *)command_context)
190
191static bool run(void * command_context)
192{
193  struct list_head * node_ptr;
194  struct jack_conf_parameter * parameter_ptr;
195  int fd;
196  time_t timestamp;
197  char timestamp_str[26];
198  bool ret;
199  char * filename;              /* filename */
200  char * bak_filename;          /* filename of the backup file */
201  char * old_filename;          /* filename where studio was persisted before save */
202  struct stat st;
203  struct ladish_write_context save_context;
204
205  ASSERT(cmd_ptr->command.state == LADISH_COMMAND_STATE_PENDING);
206
207  time(&timestamp);
208  ctime_r(&timestamp, timestamp_str);
209  timestamp_str[24] = 0;
210
211  ret = false;
212
213  ladish_app_supervisor_save_L1(g_studio.app_supervisor);
214
215  if (!ladish_studio_is_started())
216  {
217    log_error("Cannot save not-started studio");
218    ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Cannot save not-started studio", NULL);
219    goto exit;
220  }
221
222  if (!ladish_studio_compose_filename(cmd_ptr->studio_name, &filename, &bak_filename))
223  {
224    log_error("failed to compose studio filename");
225    goto exit;
226  }
227
228  if (g_studio.filename == NULL)
229  {
230    /* saving studio for first time */
231    g_studio.filename = filename;
232    free(bak_filename);
233    bak_filename = NULL;
234    old_filename = NULL;
235  }
236  else if (strcmp(g_studio.filename, filename) == 0)
237  {
238    /* saving already persisted studio that was not renamed */
239    old_filename = filename;
240  }
241  else if (strcmp(cmd_ptr->studio_name, g_studio.name) == 0)
242  {
243    /* saving renamed studio */
244    old_filename = g_studio.filename;
245    g_studio.filename = filename;
246  }
247  else
248  {
249    /* saving studio copy (save as) */
250    old_filename = filename;
251    g_studio.filename = filename;
252  }
253
254  filename = NULL;
255  ASSERT(g_studio.filename != NULL);
256  ASSERT(g_studio.filename != bak_filename);
257
258  if (bak_filename != NULL)
259  {
260    ASSERT(old_filename != NULL);
261
262    if (stat(old_filename, &st) == 0) /* if old filename does not exist, rename with fail */
263    {
264      if (rename(old_filename, bak_filename) != 0)
265      {
266        log_error("rename(%s, %s) failed: %d (%s)", old_filename, bak_filename, errno, strerror(errno));
267        goto free_filenames;
268      }
269    }
270    else
271    {
272      /* mark that there is no backup file */
273      free(bak_filename);
274      bak_filename = NULL;
275    }
276  }
277
278  log_info("saving studio... (%s)", g_studio.filename);
279
280  fd = open(g_studio.filename, O_WRONLY | O_TRUNC | O_CREAT, 0666);
281  if (fd == -1)
282  {
283    log_error("open(%s) failed: %d (%s)", g_studio.filename, errno, strerror(errno));
284    goto rename_back;
285  }
286
287  if (!ladish_write_string(fd, "<?xml version=\"1.0\"?>\n"))
288  {
289    goto close;
290  }
291
292  if (!ladish_write_string(fd, "<!--\n"))
293  {
294    goto close;
295  }
296
297  if (!ladish_write_string(fd, STUDIO_HEADER_TEXT))
298  {
299    goto close;
300  }
301
302  if (!ladish_write_string(fd, "-->\n"))
303  {
304    goto close;
305  }
306
307  if (!ladish_write_string(fd, "<!-- "))
308  {
309    goto close;
310  }
311
312  if (!ladish_write_string(fd, timestamp_str))
313  {
314    goto close;
315  }
316
317  if (!ladish_write_string(fd, " -->\n"))
318  {
319    goto close;
320  }
321
322  if (!ladish_write_string(fd, "<studio>\n"))
323  {
324    goto close;
325  }
326
327  if (!ladish_write_indented_string(fd, 1, "<jack>\n"))
328  {
329    goto close;
330  }
331
332  if (!ladish_write_indented_string(fd, 2, "<conf>\n"))
333  {
334    goto close;
335  }
336
337  list_for_each(node_ptr, &g_studio.jack_params)
338  {
339    parameter_ptr = list_entry(node_ptr, struct jack_conf_parameter, leaves);
340
341    if (!write_jack_parameter(fd, 3, parameter_ptr))
342    {
343      goto close;
344    }
345  }
346
347  if (!ladish_write_indented_string(fd, 2, "</conf>\n"))
348  {
349    goto close;
350  }
351
352  if (!ladish_write_jgraph(fd, 2, ladish_studio_get_studio_graph()))
353  {
354    log_error("ladish_write_jgraph() failed for studio graph");
355    goto close;
356  }
357
358  if (!ladish_write_indented_string(fd, 1, "</jack>\n"))
359  {
360    goto close;
361  }
362
363  if (ladish_studio_has_rooms())
364  {
365    if (!ladish_write_indented_string(fd, 1, "<rooms>\n"))
366    {
367      goto close;
368    }
369
370    save_context.indent = 2;
371    save_context.fd = fd;
372
373    if (!ladish_studio_iterate_rooms(&save_context, save_studio_room))
374    {
375      log_error("ladish_studio_iterate_rooms() failed");
376      goto close;
377    }
378
379    if (!ladish_write_indented_string(fd, 1, "</rooms>\n"))
380    {
381      goto close;
382    }
383  }
384
385  if (!ladish_write_vgraph(fd, 1, g_studio.studio_graph, g_studio.app_supervisor))
386  {
387    log_error("ladish_write_vgraph() failed for studio");
388    goto close;
389  }
390
391  if (!ladish_write_dict(fd, 1, ladish_graph_get_dict(g_studio.studio_graph)))
392  {
393    goto close;
394  }
395
396  if (!ladish_write_string(fd, "</studio>\n"))
397  {
398    goto close;
399  }
400
401  log_info("studio saved. (%s)", g_studio.filename);
402  g_studio.persisted = true;
403  g_studio.automatic = false;   /* even if it was automatic, it is not anymore because it is saved */
404
405  cmd_ptr->command.state = LADISH_COMMAND_STATE_DONE;
406
407  ret = true;
408
409  if (old_filename == g_studio.filename && strcmp(g_studio.name, cmd_ptr->studio_name) != 0)
410  {
411    free(g_studio.name);
412    g_studio.name = cmd_ptr->studio_name;
413    cmd_ptr->studio_name = NULL;
414    ladish_studio_emit_renamed();
415  }
416
417close:
418  close(fd);
419
420rename_back:
421  if (!ret && bak_filename != NULL)
422  {
423    /* save failed - try to rename the backup file back */
424    ASSERT(old_filename != NULL);
425    if (rename(bak_filename, old_filename) != 0)
426    {
427      log_error("rename(%s, %s) failed: %d (%s)", bak_filename, g_studio.filename, errno, strerror(errno));
428    }
429  }
430
431free_filenames:
432  if (bak_filename != NULL)
433  {
434    free(bak_filename);
435  }
436
437  if (old_filename != NULL && old_filename != g_studio.filename)
438  {
439    free(old_filename);
440  }
441
442  ASSERT(filename == NULL);
443  ASSERT(g_studio.filename != NULL);
444
445exit:
446  if (!ret)
447  {
448    ladish_notify_simple(LADISH_NOTIFY_URGENCY_HIGH, "Studio save failed", "Please inspect the ladishd log (~/.ladish/ladish.log) for more info");
449  }
450
451  return ret;
452}
453
454static void destructor(void * command_context)
455{
456  log_info("save studio command destructor");
457  if (cmd_ptr->studio_name != NULL)
458  {
459    free(cmd_ptr->studio_name);
460  }
461}
462
463#undef cmd_ptr
464
465bool ladish_command_save_studio(void * call_ptr, struct ladish_cqueue * queue_ptr, const char * new_studio_name)
466{
467  struct ladish_command_save_studio * cmd_ptr;
468  char * studio_name_dup;
469
470  studio_name_dup = strdup(new_studio_name);
471  if (studio_name_dup == NULL)
472  {
473    lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "strdup('%s') failed.", new_studio_name);
474    goto fail;
475  }
476
477  cmd_ptr = ladish_command_new(sizeof(struct ladish_command_save_studio));
478  if (cmd_ptr == NULL)
479  {
480    log_error("ladish_command_new() failed.");
481    goto fail_free_name;
482  }
483
484  cmd_ptr->command.run = run;
485  cmd_ptr->command.destructor = destructor;
486  cmd_ptr->studio_name = studio_name_dup;
487
488  if (!ladish_cqueue_add_command(queue_ptr, &cmd_ptr->command))
489  {
490    lash_dbus_error(call_ptr, LASH_DBUS_ERROR_GENERIC, "ladish_cqueue_add_command() failed.");
491    goto fail_destroy_command;
492  }
493
494  return true;
495
496fail_destroy_command:
497  free(cmd_ptr);
498fail_free_name:
499  free(studio_name_dup);
500fail:
501  return false;
502}
Note: See TracBrowser for help on using the browser.