Debian Codegraph @

libbrasero-burn/burn-mkisofs-base.c

raw symbols SCIP
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /*
3 * Libbrasero-burn
4 * Copyright (C) Philippe Rouquier 2005-2009 <[email protected]>
5 *
6 * Libbrasero-burn is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * The Libbrasero-burn authors hereby grant permission for non-GPL compatible
12 * GStreamer plugins to be used and distributed together with GStreamer
13 * and Libbrasero-burn. This permission is above and beyond the permissions granted
14 * by the GPL license by which Libbrasero-burn is covered. If you modify this code
15 * you may extend this exception to your version of the code, but you are not
16 * obligated to do so. If you do not wish to do so, delete this exception
17 * statement from your version.
18 *
19 * Libbrasero-burn is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to:
26 * The Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor
28 * Boston, MA 02110-1301, USA.
29 */
30
31 #ifdef HAVE_CONFIG_H
32 # include <config.h>
33 #endif
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <unistd.h>
42
43
44 #include <glib.h>
45 #include <glib-object.h>
46 #include <glib/gi18n-lib.h>
47 #include <glib/gstdio.h>
48
49 #include "burn-basics.h"
50 #include "burn-debug.h"
51 #include "brasero-track.h"
52 #include "burn-mkisofs-base.h"
53
54 struct _BraseroMkisofsBase {
55 const gchar *emptydir;
56 const gchar *videodir;
57
58 gint grafts_fd;
59 gint excluded_fd;
60
61 GHashTable *grafts;
62
63 guint found_video_ts:1;
64 guint use_joliet:1;
65 };
66 typedef struct _BraseroMkisofsBase BraseroMkisofsBase;
67
68 struct _BraseroWriteGraftData {
69 BraseroMkisofsBase *base;
70 GError **error;
71 };
72 typedef struct _BraseroWriteGraftData BraseroWriteGraftData;
73
74 static void
75 brasero_mkisofs_base_clean (BraseroMkisofsBase *base)
76 {
77 /* now we clean base we have the most important :
78 * graft and excluded list, flags and that's what
79 * we're going to use when we'll start the image
80 * creation */
81 if (base->grafts_fd)
82 close (base->grafts_fd);
83 if (base->excluded_fd)
84 close (base->excluded_fd);
85 if (base->grafts) {
86 g_hash_table_destroy (base->grafts);
87 base->grafts = NULL;
88 }
89 }
90
91 static BraseroBurnResult
92 _write_line (int fd, const gchar *filepath, GError **error)
93 {
94 gint len;
95 gint w_len;
96
97 if (lseek (fd, 0, SEEK_CUR)
98 && write (fd, "\n", 1) != 1) {
99 g_set_error (error,
100 BRASERO_BURN_ERROR,
101 BRASERO_BURN_ERROR_GENERAL,
102 "%s",
103 g_strerror (errno));
104 return BRASERO_BURN_ERR;
105 }
106
107 len = strlen (filepath);
108 w_len = write (fd, filepath, len);
109
110 if (w_len != len) {
111 g_set_error (error,
112 BRASERO_BURN_ERROR,
113 BRASERO_BURN_ERROR_GENERAL,
114 "%s",
115 g_strerror (errno));
116 return BRASERO_BURN_ERR;
117 }
118
119 return BRASERO_BURN_OK;
120 }
121
122 static BraseroBurnResult
123 brasero_mkisofs_base_write_excluded (BraseroMkisofsBase *base,
124 const gchar *uri,
125 GError **error)
126 {
127 gint num;
128 gint forbidden;
129 gchar *character;
130 gchar *localpath;
131 BraseroBurnResult result = BRASERO_BURN_OK;
132
133 /* make sure uri is local: otherwise error out */
134 /* FIXME: uri can be path or URI? problem with graft->uri */
135 if (uri && uri [0] == '/') {
136 localpath = g_strdup (uri);
137 }
138 else if (uri && g_str_has_prefix (uri, "file://")) {
139 gchar *unescaped_uri;
140
141 unescaped_uri = g_uri_unescape_string (uri, NULL);
142 localpath = g_filename_from_uri (unescaped_uri, NULL, NULL);
143 g_free (unescaped_uri);
144
145 if (!localpath)
146 localpath = g_filename_from_uri (uri, NULL, NULL);
147 }
148 else {
149 BRASERO_BURN_LOG ("File not stored locally %s", uri);
150 g_set_error (error,
151 BRASERO_BURN_ERROR,
152 BRASERO_BURN_ERROR_FILE_NOT_LOCAL,
153 _("The file is not stored locally"));
154 return BRASERO_BURN_ERR;
155 }
156
157 if (!localpath) {
158 BRASERO_BURN_LOG ("Localpath is NULL");
159 return BRASERO_BURN_ERR;
160 }
161
162 /* we need to escape some characters like []\? since in this file we
163 * can use glob like expressions. */
164 character = localpath;
165 forbidden = 0;
166 num = 0;
167
168 while (character [0]) {
169 if (character [0] == '['
170 || character [0] == ']'
171 || character [0] == '?'
172 || character [0] == '\\')
173 forbidden++;
174
175 num++;
176 character++;
177 }
178
179 if (forbidden) {
180 gchar *tmp;
181 gint i;
182
183 tmp = g_new0 (gchar, num + forbidden + 1);
184 character = tmp;
185
186 for (i = 0; i < num; i++) {
187 if (localpath [i] == '['
188 || localpath [i] == ']'
189 || localpath [i] == '?'
190 || localpath [i] == '\\') {
191 character [i] = '\\';
192 character++;
193 }
194 character [i] = localpath [i];
195 }
196
197 BRASERO_BURN_LOG ("Escaped path %s into %s", localpath, tmp);
198 g_free (localpath);
199 localpath = tmp;
200 }
201
202 /* we just ignore if localpath is NULL:
203 * - it could be a non local whose graft point couldn't be downloaded */
204 if (localpath)
205 result = _write_line (base->excluded_fd, localpath, error);
206
207 g_free (localpath);
208 return result;
209 }
210
211 static gchar *
212 _escape_path (const gchar *str)
213 {
214 gchar *escaped, *d;
215 const gchar *s;
216 gint len;
217
218 s = str;
219 len = 1;
220 while (*s != 0) {
221 if (*s == '\\' || *s == '=') {
222 len++;
223 }
224
225 len++;
226 s++;
227 }
228
229 escaped = g_malloc (len);
230
231 s = str;
232 d = escaped;
233 while (*s != 0) {
234 if (*s == '\\' || *s == '=') {
235 *d++ = '\\';
236 }
237
238 *d++ = *s++;
239 }
240 *d = 0;
241
242 return escaped;
243 }
244
245 static gchar *
246 _build_graft_point (const gchar *uri, const gchar *discpath)
247 {
248 gchar *escaped_discpath;
249 gchar *graft_point;
250 gchar *path;
251
252 if (uri == NULL || discpath == NULL)
253 return NULL;
254
255 /* make up the graft point */
256 if (*uri != '/')
257 path = g_filename_from_uri (uri, NULL, NULL);
258 else
259 path = g_strdup (uri);
260
261 if (discpath) {
262 gchar *escaped_path;
263
264 /* There is a graft because either it's not at the root of the
265 * disc or because its name has changed. */
266 escaped_path = _escape_path (path);
267 g_free (path);
268
269 escaped_discpath = _escape_path (discpath);
270 graft_point = g_strconcat (escaped_discpath,
271 "=",
272 escaped_path,
273 NULL);
274 g_free (escaped_path);
275 g_free (escaped_discpath);
276 }
277 else
278 graft_point = path;
279
280 return graft_point;
281 }
282
283 static BraseroBurnResult
284 brasero_mkisofs_base_write_graft (BraseroMkisofsBase *base,
285 const gchar *uri,
286 const gchar *disc_path,
287 GError **error)
288 {
289 gchar *graft_point;
290 BraseroBurnResult result;
291
292 /* build up graft and write it */
293 graft_point = _build_graft_point (uri, disc_path);
294 if (!graft_point) {
295 g_set_error (error,
296 BRASERO_BURN_ERROR,
297 BRASERO_BURN_ERROR_GENERAL,
298 /* Translators: Error message saying no graft point
299 * is specified. A graft point is the path (on the
300 * disc) where a file from any source will be added
301 * ("grafted") */
302 _("An internal error occurred"));
303 return BRASERO_BURN_ERR;
304 }
305
306 result = _write_line (base->grafts_fd, graft_point, error);
307 g_free (graft_point);
308 if (result != BRASERO_BURN_OK)
309 return result;
310
311 return BRASERO_BURN_OK;
312 }
313
314 static gboolean
315 _foreach_write_grafts (const gchar *uri,
316 GSList *grafts,
317 BraseroWriteGraftData *data)
318 {
319 BraseroBurnResult result;
320 BraseroGraftPt *graft;
321
322 for (; grafts; grafts = grafts->next) {
323 graft = grafts->data;
324
325 if (!graft->path) {
326 result = brasero_mkisofs_base_write_graft (data->base,
327 graft->uri,
328 NULL,
329 data->error);
330 if (result != BRASERO_BURN_OK)
331 return TRUE;
332
333 continue;
334 }
335
336 result = brasero_mkisofs_base_write_graft (data->base,
337 graft->uri,
338 graft->path,
339 data->error);
340 if (result != BRASERO_BURN_OK)
341 return TRUE;
342 }
343
344 return FALSE;
345 }
346
347 static BraseroBurnResult
348 brasero_mkisofs_base_write_grafts (BraseroMkisofsBase *base,
349 GError **error)
350 {
351 BraseroWriteGraftData callback_data;
352 gpointer result;
353
354 callback_data.error = error;
355 callback_data.base = base;
356
357 result = g_hash_table_find (base->grafts,
358 (GHRFunc) _foreach_write_grafts,
359 &callback_data);
360
361 if (result)
362 return BRASERO_BURN_ERR;
363
364 return BRASERO_BURN_OK;
365 }
366
367 static BraseroBurnResult
368 brasero_mkisofs_base_create_video_empty (BraseroMkisofsBase *base,
369 const gchar *disc_path)
370 {
371 gchar *path;
372
373 BRASERO_BURN_LOG ("Creating an empty Video or Audio directory");
374
375 if (base->found_video_ts)
376 return BRASERO_BURN_OK;
377
378 base->found_video_ts = TRUE;
379
380 path = g_build_path (G_DIR_SEPARATOR_S,
381 base->videodir,
382 disc_path,
383 NULL);
384
385 g_mkdir_with_parents (path, S_IRWXU);
386 g_free (path);
387
388 return BRASERO_BURN_OK;
389 }
390
391 static BraseroBurnResult
392 brasero_mkisofs_base_empty_directory (BraseroMkisofsBase *base,
393 const gchar *disc_path,
394 GError **error)
395 {
396 BraseroBurnResult result;
397 gchar *graft_point;
398
399 /* This is a special case when the URI is NULL which can happen mainly
400 * when we have to deal with burn:// uri. */
401 if (base->videodir) {
402 /* try with "VIDEO_TS", "VIDEO_TS/", "VIDEO_TS/" and "/VIDEO_TS/"
403 * to make sure we don't miss one */
404 if (!strcmp (disc_path, "VIDEO_TS")
405 || !strcmp (disc_path, "/VIDEO_TS")
406 || !strcmp (disc_path, "VIDEO_TS/")
407 || !strcmp (disc_path, "/VIDEO_TS/")) {
408 brasero_mkisofs_base_create_video_empty (base, disc_path);
409
410 /* NOTE: joliet cannot be used in this case so that's
411 * perfectly fine to forget about it. */
412 return BRASERO_BURN_OK;
413 }
414 }
415
416 /* Special case for uri = NULL; that is treated as if it were a directory */
417 graft_point = _build_graft_point (base->emptydir, disc_path);
418 result = _write_line (base->grafts_fd, graft_point, error);
419 g_free (graft_point);
420
421 return result;
422 }
423
424 static BraseroBurnResult
425 brasero_mkisofs_base_process_video_graft (BraseroMkisofsBase *base,
426 BraseroGraftPt *graft,
427 GError **error)
428 {
429 gchar *link_path;
430 gchar *path;
431 int res;
432
433 /* Make sure it's a path and not a URI */
434 if (!strncmp (graft->uri, "file:", 5))
435 path = g_filename_from_uri (graft->uri, NULL, NULL);
436 else
437 path = g_strdup (graft->uri);
438
439 if (g_str_has_suffix (path, G_DIR_SEPARATOR_S)) {
440 gchar *tmp;
441
442 tmp = g_strndup (path, strlen (path) - strlen (G_DIR_SEPARATOR_S));
443 g_free (path);
444 path = tmp;
445 }
446
447 link_path = g_build_path (G_DIR_SEPARATOR_S,
448 base->videodir,
449 graft->path,
450 NULL);
451
452 if (g_str_has_suffix (link_path, G_DIR_SEPARATOR_S)) {
453 gchar *tmp;
454
455 tmp = g_strndup (link_path, strlen (link_path) - strlen (G_DIR_SEPARATOR_S));
456 g_free (link_path);
457 link_path = tmp;
458 }
459
460 BRASERO_BURN_LOG ("Linking %s to %s", link_path, path);
461 res = symlink (path, link_path);
462
463 g_free (path);
464 g_free (link_path);
465
466 if (res) {
467 g_set_error (error,
468 BRASERO_BURN_ERROR,
469 BRASERO_BURN_ERROR_GENERAL,
470 "%s",
471 g_strerror (errno));
472 return BRASERO_BURN_ERR;
473 }
474
475 return BRASERO_BURN_OK;
476 }
477
478 static BraseroBurnResult
479 brasero_mkisofs_base_add_graft (BraseroMkisofsBase *base,
480 BraseroGraftPt *graft,
481 GError **error)
482 {
483 GSList *list;
484
485 /* check the file is local */
486 if (graft->uri
487 && graft->uri [0] != '/'
488 && !g_str_has_prefix (graft->uri, "file://")) {
489 /* Error out, files must be local */
490 BRASERO_BURN_LOG ("File not stored locally %s", graft->uri);
491 g_set_error (error,
492 BRASERO_BURN_ERROR,
493 BRASERO_BURN_ERROR_FILE_NOT_LOCAL,
494 _("The file is not stored locally"));
495 return BRASERO_BURN_ERR;
496 }
497
498 /* This is a special case for VIDEO images. Given the tests I performed,
499 * the option --dvd-video requires the parent directory of VIDEO_TS and
500 * AUDIO_TS to be passed. If each of these two directories are passed
501 * as an option it will fail.
502 * One workaround is to create a fake directory for VIDEO_TS and
503 * AUDIO_TS and add hardlinks inside pointing to these two directories.
504 * As this parent directory is a temporary directory it must have been
505 * passed by the calling plugins. */
506 if (base->videodir) {
507 BraseroBurnResult res;
508 gchar *parent;
509
510 /* try with "VIDEO_TS", "VIDEO_TS/", "VIDEO_TS/" and "/VIDEO_TS/"
511 * to make sure we don't miss one */
512 if (!strcmp (graft->path, "VIDEO_TS")
513 || !strcmp (graft->path, "/VIDEO_TS")
514 || !strcmp (graft->path, "VIDEO_TS/")
515 || !strcmp (graft->path, "/VIDEO_TS/")) {
516 res = brasero_mkisofs_base_process_video_graft (base, graft, error);
517 if (res != BRASERO_BURN_OK)
518 return res;
519
520 base->found_video_ts = TRUE;
521 return BRASERO_BURN_OK;
522 }
523
524 if (!strcmp (graft->path, "AUDIO_TS")
525 || !strcmp (graft->path, "/AUDIO_TS")
526 || !strcmp (graft->path, "AUDIO_TS/")
527 || !strcmp (graft->path, "/AUDIO_TS/"))
528 return brasero_mkisofs_base_process_video_graft (base, graft, error);
529
530 /* it could also be a direct child of the VIDEO_TS directory */
531 parent = g_path_get_dirname (graft->path);
532 if (!strcmp (parent, "VIDEO_TS")
533 || !strcmp (parent, "/VIDEO_TS")
534 || !strcmp (parent, "VIDEO_TS/")
535 || !strcmp (parent, "/VIDEO_TS/")) {
536 if (!base->found_video_ts)
537 brasero_mkisofs_base_create_video_empty (base, parent);
538
539 g_free (parent);
540 return brasero_mkisofs_base_process_video_graft (base, graft, error);
541 }
542 g_free (parent);
543 }
544
545 /* add the graft point */
546 list = g_hash_table_lookup (base->grafts, graft->uri);
547 if (list)
548 g_hash_table_steal (base->grafts, graft->uri);
549
550 list = g_slist_prepend (list, graft);
551 g_hash_table_insert (base->grafts, graft->uri, list);
552
553 return BRASERO_BURN_OK;
554 }
555
556 BraseroBurnResult
557 brasero_mkisofs_base_write_to_files (GSList *grafts,
558 GSList *excluded,
559 gboolean use_joliet,
560 const gchar *emptydir,
561 const gchar *videodir,
562 const gchar *grafts_path,
563 const gchar *excluded_path,
564 GError **error)
565 {
566 gchar *uri;
567 BraseroMkisofsBase base;
568 BraseroBurnResult result;
569
570 if (!grafts) {
571 BRASERO_BURN_LOG ("No graft passed");
572 return BRASERO_BURN_ERR;
573 }
574
575 /* initialize base */
576 bzero (&base, sizeof (base));
577
578 base.grafts_fd = open (grafts_path, O_WRONLY|O_TRUNC|O_EXCL);
579 if (base.grafts_fd == -1) {
580 g_set_error (error,
581 BRASERO_BURN_ERROR,
582 BRASERO_BURN_ERROR_GENERAL,
583 "%s",
584 g_strerror (errno));
585 return BRASERO_BURN_ERR;
586 }
587
588 base.excluded_fd = open (excluded_path, O_WRONLY|O_TRUNC|O_EXCL);
589 if (base.excluded_fd == -1) {
590 close (base.excluded_fd);
591 g_set_error (error,
592 BRASERO_BURN_ERROR,
593 BRASERO_BURN_ERROR_GENERAL,
594 "%s",
595 g_strerror (errno));
596 return BRASERO_BURN_ERR;
597 }
598
599 base.use_joliet = use_joliet;
600 base.emptydir = emptydir;
601 base.videodir = videodir;
602
603 base.grafts = g_hash_table_new_full (g_str_hash,
604 g_str_equal,
605 NULL,
606 (GDestroyNotify) g_slist_free);
607
608 /* we analyse the graft points:
609 * first add graft points and excluded. At the same time create a hash
610 * table in which key = uri and value = graft points, a list of all
611 * the uris that have been excluded.
612 * Once finished, for each excluded use the hash to see if there are not
613 * other paths at which the excluded uri must appear. If so, create an
614 * explicit graft point. */
615 for (; grafts; grafts = grafts->next) {
616 BraseroGraftPt *graft;
617
618 graft = grafts->data;
619
620 BRASERO_BURN_LOG ("New graft %s %s", graft->uri, graft->path);
621
622 if (!graft->uri) {
623 result = brasero_mkisofs_base_empty_directory (&base,
624 graft->path,
625 error);
626 if (result != BRASERO_BURN_OK)
627 goto cleanup;
628
629 continue;
630 }
631
632 result = brasero_mkisofs_base_add_graft (&base,
633 graft,
634 error);
635 if (result != BRASERO_BURN_OK)
636 goto cleanup;
637 }
638
639 /* simple check */
640 if (base.videodir && !base.found_video_ts) {
641 g_set_error (error,
642 BRASERO_BURN_ERROR,
643 BRASERO_BURN_ERROR_GENERAL,
644 _("VIDEO_TS directory is missing or invalid"));
645 return BRASERO_BURN_ERR;
646 }
647
648 /* write the grafts list */
649 result = brasero_mkisofs_base_write_grafts (&base, error);
650 if (result != BRASERO_BURN_OK)
651 goto cleanup;
652
653 /* write the global excluded files list */
654 for (; excluded; excluded = excluded->next) {
655 uri = excluded->data;
656 if (!uri) {
657 BRASERO_BURN_LOG ("NULL URI");
658 continue;
659 }
660
661 result = brasero_mkisofs_base_write_excluded (&base,
662 uri,
663 error);
664 if (result != BRASERO_BURN_OK)
665 goto cleanup;
666 }
667
668 brasero_mkisofs_base_clean (&base);
669 return BRASERO_BURN_OK;
670
671
672 cleanup:
673
674 brasero_mkisofs_base_clean (&base);
675 return result;
676 }