|
Packit |
5e46da |
/*
|
|
Packit |
5e46da |
* This file is part of libbluray
|
|
Packit |
5e46da |
* Copyright (C) 2009-2010 John Stebbins
|
|
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 |
#include "util/macro.h"
|
|
Packit |
5e46da |
#include "util/logging.h"
|
|
Packit |
5e46da |
#include "util/strutl.h"
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#include <stdlib.h>
|
|
Packit |
5e46da |
#include <string.h>
|
|
Packit |
5e46da |
#if defined(HAVE_DIRENT_H)
|
|
Packit |
5e46da |
# include <dirent.h>
|
|
Packit |
5e46da |
#endif
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__)
|
|
Packit |
5e46da |
# if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 24)
|
|
Packit |
5e46da |
# define USE_READDIR
|
|
Packit |
5e46da |
# include <errno.h>
|
|
Packit |
5e46da |
# endif
|
|
Packit |
5e46da |
#endif
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static void _dir_close_posix(BD_DIR_H *dir)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
if (dir) {
|
|
Packit |
5e46da |
closedir((DIR *)dir->internal);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DEBUG(DBG_DIR, "Closed POSIX dir (%p)\n", (void*)dir);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
X_FREE(dir);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static void _error(const char *msg, int errnum, void *dir)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
char buf[128];
|
|
Packit |
5e46da |
if (strerror_r(errnum, buf, sizeof(buf))) {
|
|
Packit |
5e46da |
strcpy(buf, "?");
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
BD_DEBUG(DBG_DIR | DBG_CRIT, "%s: %d %s (%p)\n", msg, errnum, buf, dir);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int _dir_read_posix(BD_DIR_H *dir, BD_DIRENT *entry)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
struct dirent *p_e;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#ifdef USE_READDIR
|
|
Packit |
5e46da |
errno = 0;
|
|
Packit |
5e46da |
p_e = readdir((DIR*)dir->internal);
|
|
Packit |
5e46da |
if (!p_e && errno) {
|
|
Packit |
5e46da |
_error("Error reading directory", errno, dir);
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
#else /* USE_READDIR */
|
|
Packit |
5e46da |
int result;
|
|
Packit |
5e46da |
struct dirent e;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
result = readdir_r((DIR*)dir->internal, &e, &p_e);
|
|
Packit |
5e46da |
if (result) {
|
|
Packit |
5e46da |
_error("Error reading directory", result, dir);
|
|
Packit |
5e46da |
return -result;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
#endif /* USE_READDIR */
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (p_e == NULL) {
|
|
Packit |
5e46da |
return 1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
strncpy(entry->d_name, p_e->d_name, sizeof(entry->d_name));
|
|
Packit |
5e46da |
entry->d_name[sizeof(entry->d_name) - 1] = 0;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static BD_DIR_H *_dir_open_posix(const char* dirname)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
BD_DIR_H *dir = calloc(1, sizeof(BD_DIR_H));
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DEBUG(DBG_DIR, "Opening POSIX dir %s... (%p)\n", dirname, (void*)dir);
|
|
Packit |
5e46da |
if (!dir) {
|
|
Packit |
5e46da |
return NULL;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
dir->close = _dir_close_posix;
|
|
Packit |
5e46da |
dir->read = _dir_read_posix;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if ((dir->internal = opendir(dirname))) {
|
|
Packit |
5e46da |
return dir;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DEBUG(DBG_DIR, "Error opening dir! (%p)\n", (void*)dir);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
X_FREE(dir);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
return NULL;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DIR_H* (*dir_open)(const char* dirname) = _dir_open_posix;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DIR_OPEN dir_open_default(void)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
return _dir_open_posix;
|
|
Packit |
5e46da |
}
|