disc.c 11.6 KB
Newer Older
Petri Hintukainen's avatar
Petri Hintukainen committed
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
/*
 * This file is part of libbluray
 * Copyright (C) 2014  Petri Hintukainen <phintuka@users.sourceforge.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif

#include "disc.h"

npzacs's avatar
npzacs committed
26 27
#include "dec.h"

Petri Hintukainen's avatar
Petri Hintukainen committed
28 29
#include "util/logging.h"
#include "util/macro.h"
30
#include "util/mutex.h"
Petri Hintukainen's avatar
Petri Hintukainen committed
31 32 33 34 35 36
#include "util/strutl.h"
#include "file/file.h"
#include "file/mount.h"

#include <string.h>

37 38 39 40
#ifdef ENABLE_UDF
#include "udf_fs.h"
#endif

Petri Hintukainen's avatar
Petri Hintukainen committed
41
struct bd_disc {
42 43 44 45
    BD_MUTEX  ovl_mutex;     /* protect access to overlay root */

    char     *disc_root;     /* disc filesystem root (if disc is mounted) */
    char     *overlay_root;  /* overlay filesystem root (if set) */
npzacs's avatar
npzacs committed
46 47

    BD_DEC   *dec;
48 49 50 51 52

    void         *fs_handle;
    BD_FILE_H * (*pf_file_open_bdrom)(void *, const char *);
    BD_DIR_H *  (*pf_dir_open_bdrom)(void *, const char *);
    void        (*pf_fs_close)(void *);
53 54

    const char   *udf_volid;
Petri Hintukainen's avatar
Petri Hintukainen committed
55 56
};

57 58 59 60 61
/*
 * BD-ROM filesystem
 */

static BD_FILE_H *_bdrom_open_path(void *p, const char *rel_path)
npzacs's avatar
npzacs committed
62
{
63
    BD_DISC *disc = (BD_DISC *)p;
npzacs's avatar
npzacs committed
64
    BD_FILE_H *fp;
65
    char *abs_path;
npzacs's avatar
npzacs committed
66

67 68 69 70 71 72 73
    abs_path = str_printf("%s%s", disc->disc_root, rel_path);
    fp = file_open(abs_path, "rb");
    X_FREE(abs_path);

    return fp;
}

74
static BD_DIR_H *_bdrom_open_dir(void *p, const char *dir)
75
{
76
    BD_DISC *disc = (BD_DISC *)p;
77 78 79
    BD_DIR_H *dp;
    char *path;

80
    path = str_printf("%s%s", disc->disc_root, dir);
81 82 83 84 85 86
    dp = dir_open(path);
    X_FREE(path);

    return dp;
}

npzacs's avatar
npzacs committed
87
/*
88 89 90 91 92 93 94 95 96 97 98
 * overlay filesystem
 */

static BD_FILE_H *_overlay_open_path(BD_DISC *p, const char *rel_path)
{
    BD_FILE_H *fp = NULL;

    bd_mutex_lock(&p->ovl_mutex);

    if (p->overlay_root) {
        char *abs_path = str_printf("%s%s", p->overlay_root, rel_path);
99
        fp = file_open(abs_path, "rb");
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
        X_FREE(abs_path);
    }

    bd_mutex_unlock(&p->ovl_mutex);

    return fp;
}

static BD_DIR_H *_overlay_open_dir(BD_DISC *p, const char *dir)
{
    BD_DIR_H *dp = NULL;

    bd_mutex_lock(&p->ovl_mutex);

    if (p->overlay_root) {
        char *abs_path = str_printf("%s%s", p->disc_root, dir);
        dp = dir_open_default()(abs_path);
        X_FREE(abs_path);
    }

    bd_mutex_unlock(&p->ovl_mutex);

    return dp;
}

/*
 * directory combining
 */

typedef struct {
    unsigned int count;
    unsigned int pos;
    BD_DIRENT    entry[1]; /* VLA */
} COMB_DIR;

static void _comb_dir_close(BD_DIR_H *dp)
{
    X_FREE(dp->internal);
    X_FREE(dp);
}

static int _comb_dir_read(BD_DIR_H *dp, BD_DIRENT *entry)
{
    COMB_DIR *priv = (COMB_DIR *)dp->internal;
    if (priv->pos < priv->count) {
        strcpy(entry->d_name, priv->entry[priv->pos++].d_name);
        return 0;
    }
    return 1;
}

static void _comb_dir_append(BD_DIR_H *dp, BD_DIRENT *entry)
{
    COMB_DIR *priv = (COMB_DIR *)dp->internal;
    unsigned int i;

    if (!priv) {
        return;
    }

    /* no duplicates */
    for (i = 0; i < priv->count; i++) {
        if (!strcmp(priv->entry[i].d_name, entry->d_name)) {
            return;
        }
    }

    /* append */
    priv = realloc(priv, sizeof(*priv) + priv->count * sizeof(BD_DIRENT));
    if (!priv) {
        return;
    }
    strcpy(priv->entry[priv->count].d_name, entry->d_name);
    priv->count++;
    dp->internal = (void*)priv;
}

static BD_DIR_H *_combine_dirs(BD_DIR_H *ovl, BD_DIR_H *rom)
{
    BD_DIR_H *dp = calloc(1, sizeof(BD_DIR_H));
    BD_DIRENT entry;

    if (dp) {
        dp->read     = _comb_dir_read;
        dp->close    = _comb_dir_close;
        dp->internal = calloc(1, sizeof(COMB_DIR));

        while (!dir_read(ovl, &entry)) {
            _comb_dir_append(dp, &entry);
        }
        while (!dir_read(rom, &entry)) {
            _comb_dir_append(dp, &entry);
        }
    }
    dir_close(ovl);
    dir_close(rom);

    return dp;
}

/*
 * disc open / close
npzacs's avatar
npzacs committed
202 203
 */

204
static BD_DISC *_disc_init()
Petri Hintukainen's avatar
Petri Hintukainen committed
205 206 207
{
    BD_DISC *p = calloc(1, sizeof(BD_DISC));
    if (p) {
208 209 210 211 212 213 214 215 216
        bd_mutex_init(&p->ovl_mutex);

        /* default file access functions */
        p->fs_handle          = (void*)p;
        p->pf_file_open_bdrom = _bdrom_open_path;
        p->pf_dir_open_bdrom  = _bdrom_open_dir;
    }
    return p;
}
Petri Hintukainen's avatar
Petri Hintukainen committed
217

218 219 220
static void _set_paths(BD_DISC *p, const char *device_path)
{
    if (device_path) {
Petri Hintukainen's avatar
Petri Hintukainen committed
221 222 223
        char *disc_root = mount_get_mountpoint(device_path);

        /* make sure path ends to slash */
224
        if (!disc_root || (disc_root[0] && disc_root[strlen(disc_root) - 1] == DIR_SEP_CHAR)) {
Petri Hintukainen's avatar
Petri Hintukainen committed
225 226 227 228 229
            p->disc_root = disc_root;
        } else {
            p->disc_root = str_printf("%s%c", disc_root, DIR_SEP_CHAR);
            X_FREE(disc_root);
        }
230 231
    }
}
npzacs's avatar
npzacs committed
232

233
BD_DISC *disc_open(const char *device_path,
Petri Hintukainen's avatar
Petri Hintukainen committed
234 235
                   void *read_blocks_handle,
                   int (*read_blocks)(void *handle, void *buf, int lba, int num_blocks),
236 237 238 239 240
                   struct bd_enc_info *enc_info,
                   const char *keyfile_path,
                   void *regs, void *psr_read, void *psr_write)
{
    BD_DISC *p = _disc_init();
241

242 243
    if (p) {
        _set_paths(p, device_path);
244 245 246

#ifdef ENABLE_UDF
        /* check if disc root directory can be opened. If not, treat it as device/image file. */
Petri Hintukainen's avatar
Petri Hintukainen committed
247
        BD_DIR_H *dp_img = device_path ? dir_open(device_path) : NULL;
248
        if (!dp_img) {
Petri Hintukainen's avatar
Petri Hintukainen committed
249
            void *udf = udf_image_open(device_path, read_blocks_handle, read_blocks);
250 251 252 253 254 255 256 257
            if (!udf) {
                BD_DEBUG(DBG_FILE | DBG_CRIT, "failed opening UDF image %s\n", device_path);
            } else {
                p->fs_handle          = udf;
                p->pf_fs_close        = udf_image_close;
                p->pf_file_open_bdrom = udf_file_open;
                p->pf_dir_open_bdrom  = udf_dir_open;

258 259
                p->udf_volid = udf_volume_id(udf);

260 261 262 263 264
                /* root not accessible with stdio */
                X_FREE(p->disc_root);
            }
        } else {
            dir_close(dp_img);
265
            BD_DEBUG(DBG_FILE, "%s does not seem to be image file or device node\n", device_path);
266
        }
267 268 269
#else
        (void)read_blocks_handle;
        (void)read_blocks;
270 271
#endif

272
        struct dec_dev dev = { p->fs_handle, p->pf_file_open_bdrom, p, (file_openFp)disc_open_path, p->disc_root, device_path };
npzacs's avatar
npzacs committed
273
        p->dec = dec_init(&dev, enc_info, keyfile_path, regs, psr_read, psr_write);
Petri Hintukainen's avatar
Petri Hintukainen committed
274 275 276 277 278 279 280 281 282
    }

    return p;
}

void disc_close(BD_DISC **pp)
{
    if (pp && *pp) {
        BD_DISC *p = *pp;
npzacs's avatar
npzacs committed
283 284 285

        dec_close(&p->dec);

286 287 288 289
        if (p->pf_fs_close) {
            p->pf_fs_close(p->fs_handle);
        }

290 291
        bd_mutex_destroy(&p->ovl_mutex);

Petri Hintukainen's avatar
Petri Hintukainen committed
292 293 294 295 296 297 298 299 300
        X_FREE(p->disc_root);
        X_FREE(*pp);
    }
}

/*
 *
 */

301
const char *disc_root(BD_DISC *p)
Petri Hintukainen's avatar
Petri Hintukainen committed
302 303 304 305
{
    return p->disc_root;
}

306 307 308 309 310
const char *disc_volume_id(BD_DISC *p)
{
    return p ? p->udf_volid : NULL;
}

311 312 313 314 315
BD_DIR_H *disc_open_bdrom_dir(BD_DISC *p, const char *rel_path)
{
    return p->pf_dir_open_bdrom(p->fs_handle, rel_path);
}

316 317 318
/*
 * VFS
 */
Petri Hintukainen's avatar
Petri Hintukainen committed
319 320 321 322 323

BD_FILE_H *disc_open_path(BD_DISC *p, const char *rel_path)
{
    BD_FILE_H *fp;

324 325
    /* search file from overlay */
    fp = _overlay_open_path(p, rel_path);
Petri Hintukainen's avatar
Petri Hintukainen committed
326

327
    /* if not found, try BD-ROM */
Petri Hintukainen's avatar
Petri Hintukainen committed
328
    if (!fp) {
329
        fp = p->pf_file_open_bdrom(p->fs_handle, rel_path);
Petri Hintukainen's avatar
Petri Hintukainen committed
330

331 332 333 334
        if (!fp) {
            BD_DEBUG(DBG_FILE | DBG_CRIT, "error opening file %s\n", rel_path);
        }
    }
Petri Hintukainen's avatar
Petri Hintukainen committed
335 336 337 338 339 340 341 342 343

    return fp;
}

BD_FILE_H *disc_open_file(BD_DISC *p, const char *dir, const char *file)
{
    BD_FILE_H *fp;
    char *path;

344 345
    path = str_printf("%s" DIR_SEP "%s", dir, file);
    fp = disc_open_path(p, path);
Petri Hintukainen's avatar
Petri Hintukainen committed
346 347 348 349 350 351 352
    X_FREE(path);

    return fp;
}

BD_DIR_H *disc_open_dir(BD_DISC *p, const char *dir)
{
353 354
    BD_DIR_H *dp_rom;
    BD_DIR_H *dp_ovl;
Petri Hintukainen's avatar
Petri Hintukainen committed
355

356
    dp_rom = p->pf_dir_open_bdrom(p->fs_handle, dir);
357
    dp_ovl = _overlay_open_dir(p, dir);
Petri Hintukainen's avatar
Petri Hintukainen committed
358

359 360 361 362 363 364 365 366
    if (!dp_ovl) {
        if (!dp_rom) {
            BD_DEBUG(DBG_FILE, "error opening dir %s\n", dir);
        }
        return dp_rom;
    }
    if (!dp_rom) {
        return dp_ovl;
Petri Hintukainen's avatar
Petri Hintukainen committed
367 368
    }

369
    return _combine_dirs(dp_ovl, dp_rom);
Petri Hintukainen's avatar
Petri Hintukainen committed
370 371
}

372
size_t disc_read_file(BD_DISC *disc, const char *dir, const char *file,
Petri Hintukainen's avatar
Petri Hintukainen committed
373 374 375 376 377 378 379 380 381
                       uint8_t **data)
{
    BD_FILE_H *fp;
    int64_t    size;

    *data = NULL;

    fp = disc_open_file(disc, dir, file);
    if (!fp) {
382
        return 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
383 384 385
    }

    size = file_size(fp);
386 387
    if (size > 0 && size < BD_MAX_SSIZE) {
        *data = malloc((size_t)size);
Petri Hintukainen's avatar
Petri Hintukainen committed
388 389 390 391 392
        if (*data) {
            int64_t got = file_read(fp, *data, size);
            if (got != size) {
                BD_DEBUG(DBG_FILE | DBG_CRIT, "Error reading file %s from %s\n", file, dir);
                X_FREE(*data);
393
                size = 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
394 395
            }
        } else {
396
          size = 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
397 398
        }
    }
399 400 401
    else {
      size = 0;
    }
Petri Hintukainen's avatar
Petri Hintukainen committed
402 403

    file_close(fp);
404
    return (size_t)size;
Petri Hintukainen's avatar
Petri Hintukainen committed
405
}
npzacs's avatar
npzacs committed
406 407

/*
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
 * filesystem update
 */

void disc_update(BD_DISC *p, const char *overlay_root)
{
    bd_mutex_lock(&p->ovl_mutex);

    X_FREE(p->overlay_root);
    if (overlay_root) {
        p->overlay_root = str_dup(overlay_root);
    }

    bd_mutex_unlock(&p->ovl_mutex);
}

423 424 425 426 427 428 429 430 431 432 433 434 435
int disc_cache_bdrom_file(BD_DISC *p, const char *rel_path, const char *cache_path)
{
    BD_FILE_H *fp_in;
    BD_FILE_H *fp_out;
    int64_t    got;

    /* input file from BD-ROM */
    fp_in = p->pf_file_open_bdrom(p->fs_handle, rel_path);
    if (!fp_in) {
        BD_DEBUG(DBG_FILE | DBG_CRIT, "error caching file %s (does not exist ?)\n", rel_path);
        return -1;
    }

436 437 438
    /* make sure path exists */
    file_mkdirs(cache_path);

439
    /* output file in local filesystem */
440
    fp_out = file_open(cache_path, "wb");
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
    if (!fp_out) {
        BD_DEBUG(DBG_FILE | DBG_CRIT, "error creating cache file %s\n", cache_path);
        file_close(fp_in);
        return -1;
    }

    while (1) {
        uint8_t buf[16*2048];
        got = file_read(fp_in, buf, sizeof(buf));
        if (got <= 0) {
            break;
        }
        if (fp_out->write(fp_out, buf, got) != got) {
            BD_DEBUG(DBG_FILE | DBG_CRIT, "error caching file %s\n", rel_path);
            file_close(fp_out);
            file_close(fp_in);
            file_unlink(cache_path);
            return -1;
        }
    }
    BD_DEBUG(DBG_FILE, "cached %s to %s\n", rel_path, cache_path);

    file_close(fp_out);
    file_close(fp_in);
    return 0;
}

468 469
/*
 * streams
npzacs's avatar
npzacs committed
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
 */

BD_FILE_H *disc_open_stream(BD_DISC *disc, const char *file)
{
  BD_FILE_H *fp = disc_open_file(disc, "BDMV" DIR_SEP "STREAM", file);
  if (!fp) {
      return NULL;
  }

  if (disc->dec) {
      BD_FILE_H *st = dec_open_stream(disc->dec, fp, atoi(file));
      if (st) {
          return st;
      }
  }

  return fp;
}

const uint8_t *disc_get_data(BD_DISC *disc, int type)
{
    if (disc->dec) {
        return dec_data(disc->dec, type);
    }
    return NULL;
}

void disc_event(BD_DISC *disc, uint32_t event, uint32_t param)
{
    if (disc->dec) {
        switch (event) {
            case DISC_EVENT_START:
                dec_start(disc->dec, param);
                return;
            case DISC_EVENT_TITLE:
                dec_title(disc->dec, param);
                return;
            case DISC_EVENT_APPLICATION:
                dec_application(disc->dec, param);
                return;
        }
    }
}