Blame src/file/file.c

Packit 5e46da
/*
Packit 5e46da
 * This file is part of libbluray
Packit 5e46da
 * Copyright (C) 2014  Petri Hintukainen
Packit 5e46da
 *
Packit 5e46da
 * This library is free software; you can redistribute it and/or
Packit 5e46da
 * modify it under the terms of the GNU Lesser General Public
Packit 5e46da
 * License as published by the Free Software Foundation; either
Packit 5e46da
 * version 2.1 of the License, or (at your option) any later version.
Packit 5e46da
 *
Packit 5e46da
 * This library is distributed in the hope that it will be useful,
Packit 5e46da
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 5e46da
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 5e46da
 * Lesser General Public License for more details.
Packit 5e46da
 *
Packit 5e46da
 * You should have received a copy of the GNU Lesser General Public
Packit 5e46da
 * License along with this library. If not, see
Packit 5e46da
 * <http://www.gnu.org/licenses/>.
Packit 5e46da
 */
Packit 5e46da
Packit 5e46da
#if HAVE_CONFIG_H
Packit 5e46da
#include "config.h"
Packit 5e46da
#endif
Packit 5e46da
Packit 5e46da
#include "file.h"
Packit 5e46da
Packit 5e46da
#include "util/logging.h"
Packit 5e46da
#include "util/macro.h"
Packit 5e46da
#include "util/strutl.h"
Packit 5e46da
Packit 5e46da
#include <stdio.h>  // SEEK_*
Packit 5e46da
#include <string.h> // strchr
Packit 5e46da
Packit 5e46da
Packit 5e46da
int64_t file_size(BD_FILE_H *fp)
Packit 5e46da
{
Packit 5e46da
    int64_t pos    = file_tell(fp);
Packit 5e46da
    int64_t res1   = file_seek(fp, 0, SEEK_END);
Packit 5e46da
    int64_t length = file_tell(fp);
Packit 5e46da
    int64_t res2   = file_seek(fp, pos, SEEK_SET);
Packit 5e46da
Packit 5e46da
    if (res1 < 0 || res2 < 0 || pos < 0 || length < 0) {
Packit 5e46da
        return -1;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    return length;
Packit 5e46da
}
Packit 5e46da
Packit 5e46da
int file_mkdirs(const char *path)
Packit 5e46da
{
Packit 5e46da
    int result = 0;
Packit 5e46da
    char *dir = str_dup(path);
Packit 5e46da
    char *end = dir;
Packit 5e46da
    char *p;
Packit 5e46da
Packit 5e46da
    if (!dir) {
Packit 5e46da
        return -1;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    /* strip file name */
Packit 5e46da
    if (!(end = strrchr(end, DIR_SEP_CHAR))) {
Packit 5e46da
        X_FREE(dir);
Packit 5e46da
        return -1;
Packit 5e46da
    }
Packit 5e46da
    *end = 0;
Packit 5e46da
Packit 5e46da
    /* tokenize, stop to first existing dir */
Packit 5e46da
    while ((p = strrchr(dir, DIR_SEP_CHAR))) {
Packit 5e46da
        if (!file_path_exists(dir)) {
Packit 5e46da
            break;
Packit 5e46da
        }
Packit 5e46da
        *p = 0;
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    /* create missing dirs */
Packit 5e46da
    p = dir;
Packit 5e46da
    while (p < end) {
Packit 5e46da
Packit 5e46da
        /* concatenate next non-existing dir */
Packit 5e46da
        while (*p) p++;
Packit 5e46da
        if (p >= end) break;
Packit 5e46da
        *p = DIR_SEP_CHAR;
Packit 5e46da
Packit 5e46da
        result = file_mkdir(dir);
Packit 5e46da
        if (result < 0) {
Packit 5e46da
            BD_DEBUG(DBG_FILE | DBG_CRIT, "Error creating directory %s\n", dir);
Packit 5e46da
            break;
Packit 5e46da
        }
Packit 5e46da
        BD_DEBUG(DBG_FILE, "  created directory %s\n", dir);
Packit 5e46da
    }
Packit 5e46da
Packit 5e46da
    X_FREE(dir);
Packit 5e46da
    return result;
Packit 5e46da
}