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