|
Packit |
5e46da |
/*
|
|
Packit |
5e46da |
* This file is part of libbluray
|
|
Packit |
5e46da |
* Copyright (C) 2009-2010 Obliter0n
|
|
Packit |
5e46da |
* Copyright (C) 2009-2010 John Stebbins
|
|
Packit |
5e46da |
* Copyright (C) 2010-2015 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 |
#include "util/macro.h"
|
|
Packit |
5e46da |
#include "util/logging.h"
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#include <errno.h>
|
|
Packit |
5e46da |
#include <inttypes.h>
|
|
Packit |
5e46da |
#include <stdio.h> // remove()
|
|
Packit |
5e46da |
#include <stdlib.h>
|
|
Packit |
5e46da |
#include <string.h>
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#include <unistd.h>
|
|
Packit |
5e46da |
#include <sys/types.h>
|
|
Packit |
5e46da |
#include <sys/stat.h>
|
|
Packit |
5e46da |
#include <fcntl.h>
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#ifdef __ANDROID__
|
|
Packit |
5e46da |
# undef lseek
|
|
Packit |
5e46da |
# define lseek lseek64
|
|
Packit |
5e46da |
# undef off_t
|
|
Packit |
5e46da |
# define off_t off64_t
|
|
Packit |
5e46da |
#endif
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static void _file_close(BD_FILE_H *file)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
if (file) {
|
|
Packit |
5e46da |
if (close((int)(intptr_t)file->internal)) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_CRIT | DBG_FILE, "Error closing POSIX file (%p)\n", (void*)file);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DEBUG(DBG_FILE, "Closed POSIX file (%p)\n", (void*)file);
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
X_FREE(file);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int64_t _file_seek(BD_FILE_H *file, int64_t offset, int32_t origin)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
off_t result = lseek((int)(intptr_t)file->internal, offset, origin);
|
|
Packit |
5e46da |
if (result == (off_t)-1) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_FILE, "lseek() failed (%p)\n", (void*)file);
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
return (int64_t)result;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int64_t _file_tell(BD_FILE_H *file)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
return _file_seek(file, 0, SEEK_CUR);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#if 0
|
|
Packit |
5e46da |
static int _file_eof(BD_FILE_H *file)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
return feof((FILE *)file->internal);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
#endif
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int64_t _file_read(BD_FILE_H *file, uint8_t *buf, int64_t size)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
ssize_t got, result;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (size <= 0 || size >= BD_MAX_SSIZE) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %"PRId64" (%p)\n", size, (void*)file);
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
for (got = 0; got < (ssize_t)size; got += result) {
|
|
Packit |
5e46da |
result = read((int)(intptr_t)file->internal, buf + got, size - got);
|
|
Packit |
5e46da |
if (result < 0) {
|
|
Packit |
5e46da |
if (errno != EINTR) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_FILE, "read() failed (%p)\n", (void*)file);
|
|
Packit |
5e46da |
break;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
result = 0;
|
|
Packit |
5e46da |
} else if (result == 0) {
|
|
Packit |
5e46da |
// hit EOF.
|
|
Packit |
5e46da |
break;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
return (int64_t)got;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static int64_t _file_write(BD_FILE_H *file, const uint8_t *buf, int64_t size)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
ssize_t written, result;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (size <= 0 || size >= BD_MAX_SSIZE) {
|
|
Packit |
5e46da |
if (size == 0) {
|
|
Packit |
5e46da |
if (fsync((int)(intptr_t)file->internal)) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_FILE, "fsync() failed (%p)\n", (void*)file);
|
|
Packit |
5e46da |
return -1;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %"PRId64" (%p)\n", size, (void*)file);
|
|
Packit |
5e46da |
return 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
for (written = 0; written < (ssize_t)size; written += result) {
|
|
Packit |
5e46da |
result = write((int)(intptr_t)file->internal, buf + written, size - written);
|
|
Packit |
5e46da |
if (result < 0) {
|
|
Packit |
5e46da |
if (errno != EINTR) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_FILE, "write() failed (%p)\n", (void*)file);
|
|
Packit |
5e46da |
break;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
result = 0;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
return (int64_t)written;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
static BD_FILE_H *_file_open(const char* filename, const char *cmode)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
BD_FILE_H *file;
|
|
Packit |
5e46da |
int fd = -1;
|
|
Packit |
5e46da |
int flags = 0;
|
|
Packit |
5e46da |
int mode = 0;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if (strchr(cmode, 'w')) {
|
|
Packit |
5e46da |
flags = O_WRONLY | O_CREAT | O_TRUNC;
|
|
Packit |
5e46da |
mode = S_IRUSR | S_IWUSR;
|
|
Packit |
5e46da |
} else {
|
|
Packit |
5e46da |
flags = O_RDONLY;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
#ifdef O_CLOEXEC
|
|
Packit |
5e46da |
flags |= O_CLOEXEC;
|
|
Packit |
5e46da |
#endif
|
|
Packit |
5e46da |
#ifdef O_BINARY
|
|
Packit |
5e46da |
flags |= O_BINARY;
|
|
Packit |
5e46da |
#endif
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
if ((fd = open(filename, flags, mode)) < 0) {
|
|
Packit |
5e46da |
BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
|
|
Packit |
5e46da |
return NULL;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
file = calloc(1, sizeof(BD_FILE_H));
|
|
Packit |
5e46da |
if (!file) {
|
|
Packit |
5e46da |
close(fd);
|
|
Packit |
5e46da |
BD_DEBUG(DBG_FILE, "Error opening file %s (out of memory)\n", filename);
|
|
Packit |
5e46da |
return NULL;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
file->close = _file_close;
|
|
Packit |
5e46da |
file->seek = _file_seek;
|
|
Packit |
5e46da |
file->read = _file_read;
|
|
Packit |
5e46da |
file->write = _file_write;
|
|
Packit |
5e46da |
file->tell = _file_tell;
|
|
Packit |
5e46da |
//file->eof = file_eof_linux;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
file->internal = (void*)(intptr_t)fd;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_DEBUG(DBG_FILE, "Opened POSIX file %s (%p)\n", filename, (void*)file);
|
|
Packit |
5e46da |
return file;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_FILE_H* (*file_open)(const char* filename, const char *mode) = _file_open;
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
BD_FILE_OPEN file_open_default(void)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
return _file_open;
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
int file_unlink(const char *file)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
return remove(file);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
int file_path_exists(const char *path)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
struct stat s;
|
|
Packit |
5e46da |
return stat(path, &s);
|
|
Packit |
5e46da |
}
|
|
Packit |
5e46da |
|
|
Packit |
5e46da |
int file_mkdir(const char *dir)
|
|
Packit |
5e46da |
{
|
|
Packit |
5e46da |
return mkdir(dir, S_IRWXU);
|
|
Packit |
5e46da |
}
|