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
_file_close(BD_FILE_H * file)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
_file_seek(BD_FILE_H * file,int64_t offset,int32_t origin)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
_file_tell(BD_FILE_H * file)71 static int64_t _file_tell(BD_FILE_H *file)
72 {
73 return _file_seek(file, 0, SEEK_CUR);
74 }
75
76 #if 0
77 static int _file_eof(BD_FILE_H *file)
78 {
79 return feof((FILE *)file->internal);
80 }
81 #endif
82
_file_read(BD_FILE_H * file,uint8_t * buf,int64_t size)83 static int64_t _file_read(BD_FILE_H *file, uint8_t *buf, int64_t size)
84 {
85 ssize_t got, result;
86
87 if (size <= 0 || size >= BD_MAX_SSIZE) {
88 BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %" PRId64 " (%p)\n", size, (void*)file);
89 return 0;
90 }
91
92 for (got = 0; got < (ssize_t)size; got += result) {
93 result = read((int)(intptr_t)file->internal, buf + got, size - got);
94 if (result < 0) {
95 if (errno != EINTR) {
96 BD_DEBUG(DBG_FILE, "read() failed (%p)\n", (void*)file);
97 break;
98 }
99 result = 0;
100 } else if (result == 0) {
101 // hit EOF.
102 break;
103 }
104 }
105 return (int64_t)got;
106 }
107
_file_write(BD_FILE_H * file,const uint8_t * buf,int64_t size)108 static int64_t _file_write(BD_FILE_H *file, const uint8_t *buf, int64_t size)
109 {
110 ssize_t written, result;
111
112 if (size <= 0 || size >= BD_MAX_SSIZE) {
113 if (size == 0) {
114 if (fsync((int)(intptr_t)file->internal)) {
115 BD_DEBUG(DBG_FILE, "fsync() failed (%p)\n", (void*)file);
116 return -1;
117 }
118 return 0;
119 }
120 BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %" PRId64 " (%p)\n", size, (void*)file);
121 return 0;
122 }
123
124 for (written = 0; written < (ssize_t)size; written += result) {
125 result = write((int)(intptr_t)file->internal, buf + written, size - written);
126 if (result < 0) {
127 if (errno != EINTR) {
128 BD_DEBUG(DBG_FILE, "write() failed (%p)\n", (void*)file);
129 break;
130 }
131 result = 0;
132 }
133 }
134 return (int64_t)written;
135 }
136
_file_open(const char * filename,const char * cmode)137 static BD_FILE_H *_file_open(const char* filename, const char *cmode)
138 {
139 BD_FILE_H *file;
140 int fd = -1;
141 int flags = 0;
142 int mode = 0;
143
144 if (strchr(cmode, 'w')) {
145 flags = O_WRONLY | O_CREAT | O_TRUNC;
146 mode = S_IRUSR | S_IWUSR;
147 } else {
148 flags = O_RDONLY;
149 }
150
151 #ifdef O_CLOEXEC
152 flags |= O_CLOEXEC;
153 #endif
154 #ifdef O_BINARY
155 flags |= O_BINARY;
156 #endif
157
158 if ((fd = open(filename, flags, mode)) < 0) {
159 BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
160 return NULL;
161 }
162
163 file = calloc(1, sizeof(BD_FILE_H));
164 if (!file) {
165 close(fd);
166 BD_DEBUG(DBG_FILE, "Error opening file %s (out of memory)\n", filename);
167 return NULL;
168 }
169
170 file->close = _file_close;
171 file->seek = _file_seek;
172 file->read = _file_read;
173 file->write = _file_write;
174 file->tell = _file_tell;
175 //file->eof = file_eof_linux;
176
177 file->internal = (void*)(intptr_t)fd;
178
179 BD_DEBUG(DBG_FILE, "Opened POSIX file %s (%p)\n", filename, (void*)file);
180 return file;
181 }
182
183 BD_FILE_H* (*file_open)(const char* filename, const char *mode) = _file_open;
184
file_open_default(void)185 BD_FILE_OPEN file_open_default(void)
186 {
187 return _file_open;
188 }
189
file_unlink(const char * file)190 int file_unlink(const char *file)
191 {
192 return remove(file);
193 }
194
file_path_exists(const char * path)195 int file_path_exists(const char *path)
196 {
197 struct stat s;
198 return stat(path, &s);
199 }
200
file_mkdir(const char * dir)201 int file_mkdir(const char *dir)
202 {
203 return mkdir(dir, S_IRWXU);
204 }
205