disc.c 11.2 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 46
    BD_MUTEX  ovl_mutex;     /* protect access to overlay root */

    char     *disc_root;     /* disc filesystem root (if disc is mounted) */
    char     *disc_device;   /* disc device (if using real device) */
    char     *overlay_root;  /* overlay filesystem root (if set) */
npzacs's avatar
npzacs committed
47 48

    BD_DEC   *dec;
49 50 51 52 53

    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 *);
54 55

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

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

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

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

    return fp;
}

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

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

    return dp;
}

npzacs's avatar
npzacs committed
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
 * 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);
        fp = file_open_default()(abs_path, "rb");
        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
203 204 205 206 207 208
 */

BD_DISC *disc_open(const char *device_path,
                   struct bd_enc_info *enc_info,
                   const char *keyfile_path,
                   void *regs, void *psr_read, void *psr_write)
Petri Hintukainen's avatar
Petri Hintukainen committed
209 210
{
    BD_DISC *p = calloc(1, sizeof(BD_DISC));
211

Petri Hintukainen's avatar
Petri Hintukainen committed
212 213 214 215 216 217 218
    if (p) {

        char *disc_root = mount_get_mountpoint(device_path);

        p->disc_device = str_dup(device_path);

        /* make sure path ends to slash */
npzacs's avatar
npzacs committed
219
        if (disc_root[0] && disc_root[strlen(disc_root) - 1] == DIR_SEP_CHAR) {
Petri Hintukainen's avatar
Petri Hintukainen committed
220 221 222 223 224
            p->disc_root = disc_root;
        } else {
            p->disc_root = str_printf("%s%c", disc_root, DIR_SEP_CHAR);
            X_FREE(disc_root);
        }
npzacs's avatar
npzacs committed
225

226 227
        bd_mutex_init(&p->ovl_mutex);

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        /* 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;

#ifdef ENABLE_UDF
        /* check if disc root directory can be opened. If not, treat it as device/image file. */
        BD_DIR_H *dp_img = dir_open(device_path);
        if (!dp_img) {
            void *udf = udf_image_open(device_path);
            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;

246 247
                p->udf_volid = udf_volume_id(udf);

248 249 250 251 252
                /* root not accessible with stdio */
                X_FREE(p->disc_root);
            }
        } else {
            dir_close(dp_img);
253
            BD_DEBUG(DBG_FILE, "%s does not seem to be image file or device node\n", device_path);
254 255 256
        }
#endif

257
        struct dec_dev dev = { p->fs_handle, p->pf_file_open_bdrom, p, (file_openFp)disc_open_path, p->disc_root, p->disc_device };
npzacs's avatar
npzacs committed
258
        p->dec = dec_init(&dev, enc_info, keyfile_path, regs, psr_read, psr_write);
Petri Hintukainen's avatar
Petri Hintukainen committed
259 260 261 262 263 264 265 266 267
    }

    return p;
}

void disc_close(BD_DISC **pp)
{
    if (pp && *pp) {
        BD_DISC *p = *pp;
npzacs's avatar
npzacs committed
268 269 270

        dec_close(&p->dec);

271 272 273 274
        if (p->pf_fs_close) {
            p->pf_fs_close(p->fs_handle);
        }

275 276
        bd_mutex_destroy(&p->ovl_mutex);

Petri Hintukainen's avatar
Petri Hintukainen committed
277 278 279 280 281 282 283 284 285 286
        X_FREE(p->disc_root);
        X_FREE(p->disc_device);
        X_FREE(*pp);
    }
}

/*
 *
 */

287
const char *disc_root(BD_DISC *p)
Petri Hintukainen's avatar
Petri Hintukainen committed
288 289 290 291
{
    return p->disc_root;
}

292 293 294 295 296
const char *disc_volume_id(BD_DISC *p)
{
    return p ? p->udf_volid : NULL;
}

297 298 299 300 301
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);
}

302 303 304
/*
 * VFS
 */
Petri Hintukainen's avatar
Petri Hintukainen committed
305 306 307 308 309

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

310 311
    /* search file from overlay */
    fp = _overlay_open_path(p, rel_path);
Petri Hintukainen's avatar
Petri Hintukainen committed
312

313
    /* if not found, try BD-ROM */
Petri Hintukainen's avatar
Petri Hintukainen committed
314
    if (!fp) {
315
        fp = p->pf_file_open_bdrom(p->fs_handle, rel_path);
Petri Hintukainen's avatar
Petri Hintukainen committed
316

317 318 319 320
        if (!fp) {
            BD_DEBUG(DBG_FILE | DBG_CRIT, "error opening file %s\n", rel_path);
        }
    }
Petri Hintukainen's avatar
Petri Hintukainen committed
321 322 323 324 325 326 327 328 329

    return fp;
}

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

330 331
    path = str_printf("%s" DIR_SEP "%s", dir, file);
    fp = disc_open_path(p, path);
Petri Hintukainen's avatar
Petri Hintukainen committed
332 333 334 335 336 337 338
    X_FREE(path);

    return fp;
}

BD_DIR_H *disc_open_dir(BD_DISC *p, const char *dir)
{
339 340
    BD_DIR_H *dp_rom;
    BD_DIR_H *dp_ovl;
Petri Hintukainen's avatar
Petri Hintukainen committed
341

342
    dp_rom = p->pf_dir_open_bdrom(p->fs_handle, dir);
343
    dp_ovl = _overlay_open_dir(p, dir);
Petri Hintukainen's avatar
Petri Hintukainen committed
344

345 346 347 348 349 350 351 352
    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
353 354
    }

355
    return _combine_dirs(dp_ovl, dp_rom);
Petri Hintukainen's avatar
Petri Hintukainen committed
356 357
}

358
size_t disc_read_file(BD_DISC *disc, const char *dir, const char *file,
Petri Hintukainen's avatar
Petri Hintukainen committed
359 360 361 362 363 364 365 366 367
                       uint8_t **data)
{
    BD_FILE_H *fp;
    int64_t    size;

    *data = NULL;

    fp = disc_open_file(disc, dir, file);
    if (!fp) {
368
        return 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
369 370 371
    }

    size = file_size(fp);
372 373
    if (size > 0 && size < BD_MAX_SSIZE) {
        *data = malloc((size_t)size);
Petri Hintukainen's avatar
Petri Hintukainen committed
374 375 376 377 378
        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);
379
                size = 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
380 381
            }
        } else {
382
          size = 0;
Petri Hintukainen's avatar
Petri Hintukainen committed
383 384
        }
    }
385 386 387
    else {
      size = 0;
    }
Petri Hintukainen's avatar
Petri Hintukainen committed
388 389

    file_close(fp);
390
    return (size_t)size;
Petri Hintukainen's avatar
Petri Hintukainen committed
391
}
npzacs's avatar
npzacs committed
392 393

/*
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
 * 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);
}

409 410 411 412 413 414 415 416 417 418 419 420 421
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;
    }

422 423 424
    /* make sure path exists */
    file_mkdirs(cache_path);

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
    /* output file in local filesystem */
    fp_out = file_open_default()(cache_path, "wb");
    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;
}

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

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;
        }
    }
}