burn-volume.c 8.3 KB
Newer Older
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
/*
3
 * Libbrasero-media
4
 * Copyright (C) Philippe Rouquier 2005-2009 <bonfire-app@wanadoo.fr>
5
 *
6
 * Libbrasero-media is free software; you can redistribute it and/or modify
7 8 9
 * 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.
10
 *
11
 * The Libbrasero-media authors hereby grant permission for non-GPL compatible
12
 * GStreamer plugins to be used and distributed together with GStreamer
13 14
 * and Libbrasero-media. This permission is above and beyond the permissions granted
 * by the GPL license by which Libbrasero-media is covered. If you modify this code
15 16 17 18
 * you may extend this exception to your version of the code, but you are not
 * obligated to do so. If you do not wish to do so, delete this exception
 * statement from your version.
 * 
19
 * Libbrasero-media is distributed in the hope that it will be useful,
20 21 22 23 24 25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to:
26 27 28
 * 	The Free Software Foundation, Inc.,
 * 	51 Franklin Street, Fifth Floor
 * 	Boston, MA  02110-1301, USA.
29 30 31 32 33 34 35 36 37
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <errno.h>
#include <string.h>
#include <stdio.h>
38
#include <unistd.h>
39 40 41 42

#include <glib.h>
#include <glib/gi18n-lib.h>

43
#include "burn-volume-source.h"
44 45
#include "burn-volume.h"
#include "burn-iso9660.h"
46
#include "brasero-media.h"
47
#include "brasero-media-private.h"
48
#include "brasero-units.h"
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

struct _BraseroTagDesc {
	guint16 id;
	guint16 version;
	guchar checksum;
	guchar reserved;
	guint16 serial;
	guint16 crc;
	guint16 crc_len;
	guint32 location;
};
typedef struct _BraseroTagDesc BraseroTagDesc;

struct _BraseroAnchorDesc {
	BraseroTagDesc tag;

	guchar main_extent		[8];
	guchar reserve_extent		[8];
};
typedef struct _BraseroAnchorDesc BraseroAnchorDesc;

#define SYSTEM_AREA_SECTORS		16
#define ANCHOR_AREA_SECTORS		256


void
brasero_volume_file_free (BraseroVolFile *file)
{
	if (!file)
		return;

	if (file->isdir) {
		GList *iter;

83 84 85
		if (file->isdir_loaded) {
			for (iter = file->specific.dir.children; iter; iter = iter->next)
				brasero_volume_file_free (iter->data);
86

87 88
			g_list_free (file->specific.dir.children);
		}
89
	}
90 91 92 93 94 95
	else {
		g_slist_foreach (file->specific.file.extents,
				 (GFunc) g_free,
				 NULL);
		g_slist_free (file->specific.file.extents);
	}
96

97
	g_free (file->rr_name);
98 99 100 101 102
	g_free (file->name);
	g_free (file);
}

static gboolean
103
brasero_volume_get_primary_from_file (BraseroVolSrc *vol,
104 105 106
				      gchar *primary_vol,
				      GError **error)
{
107
	BraseroVolDesc *desc;
108 109

	/* skip the first 16 blocks */
110
	if (BRASERO_VOL_SRC_SEEK (vol, SYSTEM_AREA_SECTORS, SEEK_CUR, error) == -1)
111 112
		return FALSE;

113
	if (!BRASERO_VOL_SRC_READ (vol, primary_vol, 1, error))
114 115 116
		return FALSE;

	/* make a few checks to ensure this is an ECMA volume */
117 118 119 120 121 122 123 124
	desc = (BraseroVolDesc *) primary_vol;
	if (memcmp (desc->id, "CD001", 5)
	&&  memcmp (desc->id, "BEA01", 5)
	&&  memcmp (desc->id, "BOOT2", 5)
	&&  memcmp (desc->id, "CDW02", 5)
	&&  memcmp (desc->id, "NSR02", 5)	/* usually UDF */
	&&  memcmp (desc->id, "NSR03", 5)	/* usually UDF */
	&&  memcmp (desc->id, "TEA01", 5)) {
125
		g_set_error (error,
126 127
			     BRASERO_MEDIA_ERROR,
			     BRASERO_MEDIA_ERROR_IMAGE_INVALID,
128
			     _("It does not appear to be a valid ISO image"));
129
		BRASERO_MEDIA_LOG ("Wrong volume descriptor, got %.5s", desc->id);
130 131 132 133 134 135
		return FALSE;
	}

	return TRUE;
}

136
gboolean
137 138 139 140
brasero_volume_get_size (BraseroVolSrc *vol,
			 gint64 block,
			 gint64 *nb_blocks,
			 GError **error)
141 142 143 144
{
	gboolean result;
	gchar buffer [ISO9660_BLOCK_SIZE];

145
	if (block && BRASERO_VOL_SRC_SEEK (vol, block, SEEK_SET, error) == -1)
146 147
		return FALSE;

148
	result = brasero_volume_get_primary_from_file (vol, buffer, error);
149 150
	if (!result)
		return FALSE;
151 152 153 154 155

	if (!brasero_iso9660_is_primary_descriptor (buffer, error))
		return FALSE;

	return brasero_iso9660_get_size (buffer, nb_blocks, error);
156 157 158
}

BraseroVolFile *
159
brasero_volume_get_files (BraseroVolSrc *vol,
160
			  gint64 block,
161
			  gchar **label,
162
			  gint64 *nb_blocks,
163
			  gint64 *data_blocks,
164 165 166 167
			  GError **error)
{
	gchar buffer [ISO9660_BLOCK_SIZE];

168
	if (BRASERO_VOL_SRC_SEEK (vol, block, SEEK_SET, error) == -1)
169
		return FALSE;
170

171
	if (!brasero_volume_get_primary_from_file (vol, buffer, error))
172 173
		return NULL;

174
	if (!brasero_iso9660_is_primary_descriptor (buffer, error))
175 176 177
		return NULL;

	if (label
178
	&& !brasero_iso9660_get_label (buffer, label, error))
179 180 181
		return NULL;

	if (nb_blocks
182
	&& !brasero_iso9660_get_size (buffer, nb_blocks, error))
183 184
		return NULL;

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
	return brasero_iso9660_get_contents (vol,
					     buffer,
					     data_blocks,
					     error);
}

GList *
brasero_volume_load_directory_contents (BraseroVolSrc *vol,
					gint64 session_block,
					gint64 block,
					GError **error)
{
	gchar buffer [ISO9660_BLOCK_SIZE];

	if (BRASERO_VOL_SRC_SEEK (vol, session_block, SEEK_SET, error) == -1)
		return FALSE;

	if (!brasero_volume_get_primary_from_file (vol, buffer, error))
		return NULL;

	if (!brasero_iso9660_is_primary_descriptor (buffer, error))
		return NULL;

	return brasero_iso9660_get_directory_contents (vol,
						       buffer,
						       block,
						       error);
212
}
213

214
BraseroVolFile *
215
brasero_volume_get_file (BraseroVolSrc *vol,
216 217 218 219 220 221
			 const gchar *path,
			 gint64 volume_start_block,
			 GError **error)
{
	gchar buffer [ISO9660_BLOCK_SIZE];

222
	if (BRASERO_VOL_SRC_SEEK (vol, volume_start_block, SEEK_SET, error) == -1)
223 224
		return NULL;

225
	if (!brasero_volume_get_primary_from_file (vol, buffer, error))
226 227
		return NULL;

228
	if (!brasero_iso9660_is_primary_descriptor (buffer, error))
229 230
		return NULL;

231
	return brasero_iso9660_get_file (vol, path, buffer, error);
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
gchar *
brasero_volume_file_to_path (BraseroVolFile *file)
{
	GString *path;
	BraseroVolFile *parent;
	GSList *components = NULL, *iter, *next;

	if (!file)
		return NULL;

	/* make a list of all the components of the path by going up to root */
	parent = file->parent;
	while (parent && parent->name) {
		components = g_slist_prepend (components, BRASERO_VOLUME_FILE_NAME (parent));
		parent = parent->parent;
	}

	if (!components)
		return NULL;

	path = g_string_new (NULL);
	for (iter = components; iter; iter = next) {
		gchar *name;

		name = iter->data;
		next = iter->next;
		components = g_slist_remove (components, name);

		g_string_append_c (path, G_DIR_SEPARATOR);
		g_string_append (path, name);
	}

	g_slist_free (components);
	return g_string_free (path, FALSE);
}

BraseroVolFile *
brasero_volume_file_from_path (const gchar *ptr,
			       BraseroVolFile *parent)
{
	GList *iter;
	gchar *next;
	gint len;

	/* first determine the name of the directory / file to look for */
	if (!ptr || ptr [0] != '/' || !parent)
		return NULL;

	ptr ++;
	next = g_utf8_strchr (ptr, -1, G_DIR_SEPARATOR);
	if (!next)
		len = strlen (ptr);
	else
		len = next - ptr;

	for (iter = parent->specific.dir.children; iter; iter = iter->next) {
		BraseroVolFile *file;

		file = iter->data;
		if (!strncmp (ptr, BRASERO_VOLUME_FILE_NAME (file), len)) {
			/* we've found it seek for the next if any */
			if (!next)
				return file;

			ptr = next;
			return brasero_volume_file_from_path (ptr, file);
		}
	}

	return NULL;
}

gint64
brasero_volume_file_size (BraseroVolFile *file)
{
	GList *iter;
	gint64 size = 0;

312 313 314 315 316 317 318 319 320
	if (!file->isdir) {
		GSList *extents;

		for (extents = file->specific.file.extents; extents; extents = extents->next) {
			BraseroVolFileExtent *extent;

			extent = extents->data;
			size += extent->size;
		}
321
		return BRASERO_BYTES_TO_SECTORS (size, 2048);
322
	}
323 324 325 326 327 328 329

	for (iter = file->specific.dir.children; iter; iter = iter->next) {
		file = iter->data;

		if (file->isdir)
			size += brasero_volume_file_size (file);
		else
330
			size += BRASERO_BYTES_TO_SECTORS (file->specific.file.size_bytes, 2048);
331 332 333 334
	}

	return size;
}
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

BraseroVolFile *
brasero_volume_file_merge (BraseroVolFile *file1,
			   BraseroVolFile *file2)
{
	file1->specific.file.size_bytes += file2->specific.file.size_bytes;
	file1->specific.file.extents = g_slist_concat (file1->specific.file.extents,
							     file2->specific.file.extents);

	file2->specific.file.extents = NULL;
	brasero_volume_file_free (file2);

	return file1;
}