| 1 |
/* |
| 2 |
* This file is part of libbluray |
| 3 |
* Copyright (C) 2009-2010 Obliter0n |
| 4 |
* Copyright (C) 2009-2010 John Stebbins |
| 5 |
* Copyright (C) 2010-2015 Petri Hintukainen |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library. If not, see |
| 19 |
* <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#if HAVE_CONFIG_H |
| 23 |
#include "config.h" |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "file.h" |
| 27 |
#include "util/macro.h" |
| 28 |
#include "util/logging.h" |
| 29 |
|
| 30 |
#include <errno.h> |
| 31 |
#include <inttypes.h> |
| 32 |
#include <stdio.h> // remove() |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
#include <unistd.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <sys/stat.h> |
| 39 |
#include <fcntl.h> |
| 40 |
|
| 41 |
#ifdef __ANDROID__ |
| 42 |
# undef lseek |
| 43 |
# define lseek lseek64 |
| 44 |
# undef off_t |
| 45 |
# define off_t off64_t |
| 46 |
#endif |
| 47 |
|
| 48 |
static void _file_close(BD_FILE_H *file) |
| 49 |
{ |
| 50 |
if (file) { |
| 51 |
if (close((int)(intptr_t)file->internal)) { |
| 52 |
BD_DEBUG(DBG_CRIT | DBG_FILE, "Error closing POSIX file (%p)\n", (void*)file); |
| 53 |
} |
| 54 |
|
| 55 |
BD_DEBUG(DBG_FILE, "Closed POSIX file (%p)\n", (void*)file); |
| 56 |
|
| 57 |
X_FREE(file); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
static int64_t _file_seek(BD_FILE_H *file, int64_t offset, int32_t origin) |
| 62 |
{ |
| 63 |
off_t result = lseek((int)(intptr_t)file->internal, offset, origin); |
| 64 |
if (result == (off_t)-1) { |
| 65 |
BD_DEBUG(DBG_FILE, "lseek() failed (%p)\n", (void*)file); |
| 66 |
return -1; |
| 67 |
} |
| 68 |
return (int64_t)result; |
| 69 |
} |
| 70 |
|
| 71 |
static int64_t _file_tell(BD_FILE_H *file) |
| 72 |
{ |
| 73 |
return _file_seek(file, 0, SEEK_CUR); |
| 74 |
} |
| 75 |
|
| 76 |
static int64_t _file_read(BD_FILE_H *file, uint8_t *buf, int64_t size) |
| 77 |
{ |
| 78 |
ssize_t got, result; |
| 79 |
|
| 80 |
if (size <= 0 || size >= BD_MAX_SSIZE) { |
| 81 |
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %" PRId64 " (%p)\n", size, (void*)file); |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
for (got = 0; got < (ssize_t)size; got += result) { |
| 86 |
result = read((int)(intptr_t)file->internal, buf + got, size - got); |
| 87 |
if (result < 0) { |
| 88 |
if (errno != EINTR) { |
| 89 |
BD_DEBUG(DBG_FILE, "read() failed (%p)\n", (void*)file); |
| 90 |
break; |
| 91 |
} |
| 92 |
result = 0; |
| 93 |
} else if (result == 0) { |
| 94 |
// hit EOF. |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
return (int64_t)got; |
| 99 |
} |
| 100 |
|
| 101 |
static int64_t _file_write(BD_FILE_H *file, const uint8_t *buf, int64_t size) |
| 102 |
{ |
| 103 |
ssize_t written, result; |
| 104 |
|
| 105 |
if (size <= 0 || size >= BD_MAX_SSIZE) { |
| 106 |
if (size == 0) { |
| 107 |
if (fsync((int)(intptr_t)file->internal)) { |
| 108 |
BD_DEBUG(DBG_FILE, "fsync() failed (%p)\n", (void*)file); |
| 109 |
return -1; |
| 110 |
} |
| 111 |
return 0; |
| 112 |
} |
| 113 |
BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %" PRId64 " (%p)\n", size, (void*)file); |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
for (written = 0; written < (ssize_t)size; written += result) { |
| 118 |
result = write((int)(intptr_t)file->internal, buf + written, size - written); |
| 119 |
if (result < 0) { |
| 120 |
if (errno != EINTR) { |
| 121 |
BD_DEBUG(DBG_FILE, "write() failed (%p)\n", (void*)file); |
| 122 |
break; |
| 123 |
} |
| 124 |
result = 0; |
| 125 |
} |
| 126 |
} |
| 127 |
return (int64_t)written; |
| 128 |
} |
| 129 |
|
| 130 |
static BD_FILE_H *_file_open(void *handle, const char* filename) |
| 131 |
{ |
| 132 |
BD_FILE_H *file; |
| 133 |
int fd = -1; |
| 134 |
int flags = 0; |
| 135 |
int mode = 0; |
| 136 |
(void)handle; |
| 137 |
|
| 138 |
flags = O_RDONLY; |
| 139 |
|
| 140 |
#ifdef O_CLOEXEC |
| 141 |
flags |= O_CLOEXEC; |
| 142 |
#endif |
| 143 |
#ifdef O_BINARY |
| 144 |
flags |= O_BINARY; |
| 145 |
#endif |
| 146 |
|
| 147 |
fd = open(filename, flags, mode); |
| 148 |
if (fd < 0) { |
| 149 |
BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename); |
| 150 |
return NULL; |
| 151 |
} |
| 152 |
|
| 153 |
file = calloc(1, sizeof(BD_FILE_H)); |
| 154 |
if (!file) { |
| 155 |
close(fd); |
| 156 |
BD_DEBUG(DBG_FILE, "Error opening file %s (out of memory)\n", filename); |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
file->close = _file_close; |
| 161 |
file->seek = _file_seek; |
| 162 |
file->read = _file_read; |
| 163 |
file->write = _file_write; |
| 164 |
file->tell = _file_tell; |
| 165 |
|
| 166 |
file->internal = (void*)(intptr_t)fd; |
| 167 |
|
| 168 |
BD_DEBUG(DBG_FILE, "Opened POSIX file %s (%p)\n", filename, (void*)file); |
| 169 |
return file; |
| 170 |
} |
| 171 |
|
| 172 |
BD_FILE_H* (*file_open)(void *handle, const char* filename) = _file_open; |
| 173 |
|
| 174 |
BD_FILE_OPEN file_open_default(void) |
| 175 |
{ |
| 176 |
return _file_open; |
| 177 |
} |
| 178 |
|
| 179 |
int file_path_exists(const char *path) |
| 180 |
{ |
| 181 |
struct stat s; |
| 182 |
return stat(path, &s); |
| 183 |
} |
| 184 |
|
| 185 |
int file_mkdir(const char *dir) |
| 186 |
{ |
| 187 |
return mkdir(dir, S_IRWXU); |
| 188 |
} |