dir_posix.c 2.09 KB
Newer Older
1 2 3 4
/*
 * This file is part of libbluray
 * Copyright (C) 2009-2010  John Stebbins
 *
gates's avatar
gates committed
5 6 7 8
 * 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.
9
 *
gates's avatar
gates committed
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
gates's avatar
gates committed
12 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
14
 *
gates's avatar
gates committed
15 16 17
 * 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/>.
18 19
 */

20 21 22
#if HAVE_CONFIG_H
#include "config.h"
#endif
john's avatar
john committed
23

24
#include "file.h"
25 26
#include "util/macro.h"
#include "util/logging.h"
27
#include "util/strutl.h"
28

29
#include <stdlib.h>
30
#include <string.h>
31 32 33
#if defined(HAVE_DIRENT_H)
#   include <dirent.h>
#endif
john's avatar
john committed
34

35
static void _dir_close_posix(BD_DIR_H *dir)
john's avatar
john committed
36 37 38 39
{
    if (dir) {
        closedir((DIR *)dir->internal);

40
        BD_DEBUG(DBG_DIR, "Closed POSIX dir (%p)\n", (void*)dir);
john's avatar
john committed
41 42 43 44 45

        X_FREE(dir);
    }
}

46
static int _dir_read_posix(BD_DIR_H *dir, BD_DIRENT *entry)
john's avatar
john committed
47 48
{
    struct dirent e, *p_e;
49
    int result;
john's avatar
john committed
50 51 52 53 54 55 56

    result = readdir_r((DIR*)dir->internal, &e, &p_e);
    if (result) {
        return -result;
    } else if (p_e == NULL) {
        return 1;
    }
Petri Hintukainen's avatar
Petri Hintukainen committed
57
    strncpy(entry->d_name, e.d_name, sizeof(entry->d_name));
58
    entry->d_name[sizeof(entry->d_name) - 1] = 0;
59

john's avatar
john committed
60 61 62
    return 0;
}

63
static BD_DIR_H *_dir_open_posix(const char* dirname)
john's avatar
john committed
64
{
Petri Hintukainen's avatar
Petri Hintukainen committed
65
    BD_DIR_H *dir = calloc(1, sizeof(BD_DIR_H));
john's avatar
john committed
66

67
    BD_DEBUG(DBG_DIR, "Opening POSIX dir %s... (%p)\n", dirname, (void*)dir);
68 69
    dir->close = _dir_close_posix;
    dir->read = _dir_read_posix;
john's avatar
john committed
70

71
    if ((dir->internal = opendir(dirname))) {
john's avatar
john committed
72 73 74
        return dir;
    }

75
    BD_DEBUG(DBG_DIR, "Error opening dir! (%p)\n", (void*)dir);
john's avatar
john committed
76 77 78 79 80

    X_FREE(dir);

    return NULL;
}
81

82
BD_DIR_H* (*dir_open)(const char* dirname) = _dir_open_posix;
83 84 85 86 87

BD_DIR_OPEN dir_open_default(void)
{
    return _dir_open_posix;
}