udf_fs.c 4.97 KB
Newer Older
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
/*
 * This file is part of libbluray
 * Copyright (C) 2015  Petri Hintukainen
 *
 * 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 "udf_fs.h"

#include "file/file.h"
#include "util/macro.h"
#include "util/mutex.h"
#include "util/logging.h"

#include "udfread.h"
#include "blockinput.h"

#include <stdlib.h>
#include <stdio.h>   // SEEK_SET
#include <string.h>
#include <inttypes.h>

/*
 * file access
 */

static void _file_close(BD_FILE_H *file)
{
    if (file) {
        udfread_file_close(file->internal);
        BD_DEBUG(DBG_FILE, "Closed UDF file (%p)\n", (void*)file);
        X_FREE(file);
    }
}

static int64_t _file_seek(BD_FILE_H *file, int64_t offset, int32_t origin)
{
    return udfread_file_seek(file->internal, offset, origin);
}

static int64_t _file_tell(BD_FILE_H *file)
{
    return udfread_file_tell(file->internal);
}

static int64_t _file_read(BD_FILE_H *file, uint8_t *buf, int64_t size)
{
    return udfread_file_read(file->internal, buf, size);
}

BD_FILE_H *udf_file_open(void *udf, const char *filename)
{
    BD_FILE_H *file = calloc(1, sizeof(BD_FILE_H));

    BD_DEBUG(DBG_FILE, "Opening UDF file %s... (%p)\n", filename, (void*)file);

    file->close = _file_close;
    file->seek  = _file_seek;
    file->read  = _file_read;
    file->write = NULL;
    file->tell  = _file_tell;
    file->eof   = NULL;

    file->internal = udfread_file_open(udf, filename);
    if (!file->internal) {
        BD_DEBUG(DBG_FILE, "Error opening file %s!\n", filename);
        X_FREE(file);
    }

    return file;
}

/*
 * directory access
 */

static void _dir_close(BD_DIR_H *dir)
{
    if (dir) {
        udfread_closedir(dir->internal);
        BD_DEBUG(DBG_DIR, "Closed UDF dir (%p)\n", (void*)dir);
        X_FREE(dir);
    }
}

static int _dir_read(BD_DIR_H *dir, BD_DIRENT *entry)
{
    struct udfread_dirent e;

    if (!udfread_readdir(dir->internal, &e)) {
        return -1;
    }

    strncpy(entry->d_name, e.d_name, sizeof(entry->d_name));
    entry->d_name[sizeof(entry->d_name) - 1] = 0;

    return 0;
}

BD_DIR_H *udf_dir_open(void *udf, const char* dirname)
{
    BD_DIR_H *dir = calloc(1, sizeof(BD_DIR_H));

    BD_DEBUG(DBG_DIR, "Opening UDF dir %s... (%p)\n", dirname, (void*)dir);

    dir->close = _dir_close;
    dir->read  = _dir_read;

    dir->internal = udfread_opendir(udf, dirname);
    if (!dir->internal) {
        BD_DEBUG(DBG_DIR, "Error opening %s\n", dirname);
        X_FREE(dir);
    }

    return dir;
}

/*
 * UDF image access
 */

typedef struct {
    struct udfread_block_input i;
    BD_FILE_H *fp;
    BD_MUTEX mutex;
} UDF_BI;

static int _bi_close(struct udfread_block_input *bi_gen)
{
    UDF_BI *bi = (UDF_BI *)bi_gen;
    file_close(bi->fp);
    bd_mutex_destroy(&bi->mutex);
    X_FREE(bi);
    return 0;
}

static uint32_t _bi_size(struct udfread_block_input *bi_gen)
{
    UDF_BI *bi = (UDF_BI *)bi_gen;
    return file_size(bi->fp) / 2048;
}

static int _bi_read(struct udfread_block_input *bi_gen, uint32_t lba, void *buf, uint32_t nblocks, int flags)
{
    (void)flags;
    UDF_BI *bi = (UDF_BI *)bi_gen;
    int got = -1;

    /* seek + read must be atomic */
    bd_mutex_lock(&bi->mutex);

    if (file_seek(bi->fp, SEEK_SET, (int64_t)lba * 2048) >= 0) {
        int64_t bytes = file_read(bi->fp, buf, (int64_t)nblocks * 2048);
        if (bytes >= 2048) {
            got = bytes / 2048;
        }
    }

    bd_mutex_unlock(&bi->mutex);

    return got;
}

static struct udfread_block_input *_block_input(const char *img)
{
    BD_FILE_H *fp = file_open(img, "rb");
    if (fp) {
        UDF_BI *bi = calloc(1, sizeof(*bi));
        if (bi) {
            bi->fp      = fp;
            bi->i.close = _bi_close;
            bi->i.read  = _bi_read;
            bi->i.size  = _bi_size;
            bd_mutex_init(&bi->mutex);
            return &bi->i;
        }
        file_close(fp);
    }
    return NULL;
}


void *udf_image_open(const char *img_path)
{
    udfread *udf = NULL;

    /* app handles file I/O ? */
    if (file_open != file_open_default()) {
        struct udfread_block_input *bi = _block_input(img_path);
        if (bi) {
            udf = udfread_open_input(bi);
            if (!udf) {
                bi->close(bi);
            }
        }
    }

    if (!udf) {
        udf = (void*)udfread_open(img_path);
    }

    return (void*)udf;
}

void udf_image_close(void *udf)
{
    udfread_close(udf);
}