| 1 |
/* |
| 2 |
* This file is part of libbluray |
| 3 |
* Copyright (C) 2009-2010 John Stebbins |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library. If not, see |
| 17 |
* <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
#if HAVE_CONFIG_H |
| 21 |
#include "config.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
#include "file.h" |
| 25 |
#include "util/macro.h" |
| 26 |
#include "util/logging.h" |
| 27 |
#include "util/strutl.h" |
| 28 |
|
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#if defined(HAVE_DIRENT_H) |
| 32 |
# include <dirent.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) |
| 36 |
# if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 24) |
| 37 |
# define USE_READDIR |
| 38 |
# include <errno.h> |
| 39 |
# endif |
| 40 |
#endif |
| 41 |
|
| 42 |
static void _dir_close_posix(BD_DIR_H *dir) |
| 43 |
{ |
| 44 |
if (dir) { |
| 45 |
closedir((DIR *)dir->internal); |
| 46 |
|
| 47 |
BD_DEBUG(DBG_DIR, "Closed POSIX dir (%p)\n", (void*)dir); |
| 48 |
|
| 49 |
X_FREE(dir); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static void _error(const char *msg, int errnum, void *dir) |
| 54 |
{ |
| 55 |
char buf[128]; |
| 56 |
if (strerror_r(errnum, buf, sizeof(buf))) { |
| 57 |
strcpy(buf, "?"); |
| 58 |
} |
| 59 |
BD_DEBUG(DBG_DIR | DBG_CRIT, "%s: %d %s (%p)\n", msg, errnum, buf, dir); |
| 60 |
} |
| 61 |
|
| 62 |
static int _dir_read_posix(BD_DIR_H *dir, BD_DIRENT *entry) |
| 63 |
{ |
| 64 |
struct dirent *p_e; |
| 65 |
|
| 66 |
#ifdef USE_READDIR |
| 67 |
errno = 0; |
| 68 |
p_e = readdir((DIR*)dir->internal); |
| 69 |
if (!p_e && errno) { |
| 70 |
_error("Error reading directory", errno, dir); |
| 71 |
return -1; |
| 72 |
} |
| 73 |
#else /* USE_READDIR */ |
| 74 |
int result; |
| 75 |
struct dirent e; |
| 76 |
|
| 77 |
result = readdir_r((DIR*)dir->internal, &e, &p_e); |
| 78 |
if (result) { |
| 79 |
_error("Error reading directory", result, dir); |
| 80 |
return -result; |
| 81 |
} |
| 82 |
#endif /* USE_READDIR */ |
| 83 |
|
| 84 |
if (p_e == NULL) { |
| 85 |
return 1; |
| 86 |
} |
| 87 |
strncpy(entry->d_name, p_e->d_name, sizeof(entry->d_name)); |
| 88 |
entry->d_name[sizeof(entry->d_name) - 1] = 0; |
| 89 |
|
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
static BD_DIR_H *_dir_open_posix(const char* dirname) |
| 94 |
{ |
| 95 |
BD_DIR_H *dir = calloc(1, sizeof(BD_DIR_H)); |
| 96 |
|
| 97 |
if (!dir) { |
| 98 |
return NULL; |
| 99 |
} |
| 100 |
|
| 101 |
dir->close = _dir_close_posix; |
| 102 |
dir->read = _dir_read_posix; |
| 103 |
|
| 104 |
if ((dir->internal = opendir(dirname))) { |
| 105 |
BD_DEBUG(DBG_DIR, "Opened POSIX dir %s (%p)\n", dirname, (void*)dir); |
| 106 |
return dir; |
| 107 |
} |
| 108 |
|
| 109 |
BD_DEBUG(DBG_DIR, "Error opening dir %s\n", dirname); |
| 110 |
|
| 111 |
X_FREE(dir); |
| 112 |
|
| 113 |
return NULL; |
| 114 |
} |
| 115 |
|
| 116 |
BD_DIR_OPEN dir_open_default(void) |
| 117 |
{ |
| 118 |
return _dir_open_posix; |
| 119 |
} |