Debian Codegraph @

src/file/file_posix.c

raw symbols SCIP
1 /*
2 * This file is part of libdvdread
3 * Copyright (C) 2022 VideoLAN
4 *
5 * This file 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 file 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 #include "config.h"
21
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27
28
29 #include <sys/stat.h>
30 #include <fcntl.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 # endif
39 #endif
40
41 #include <dvdread/dvd_filesystem.h>
42
43 #include "filesystem.h"
44
45 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
46 defined(__NetBSD__) || defined(__DragonFly__) || \
47 (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS >= 64)
48 /* off_t is already 64 bit; lseek handles off64_t offsets */
49 #else
50 # undef lseek
51 # define lseek lseek64
52 # undef off_t
53 # define off_t off64_t
54 #endif
55
56
57 static void *file_open_default(dvd_reader_filesystem_h *fs, const char* filename)
58 {
59 if (!fs)
60 return NULL;
61
62 int *handle;
63 int fd;
64 int flags = 0;
65 int mode = 0;
66
67 #if defined(__OS2__) // not posix but kept here for legacy compatibility reasons
68 flags = O_RDONLY | O_BINARY;
69 #else
70 flags = O_RDONLY;
71 #endif
72
73 #ifdef O_CLOEXEC
74 flags |= O_CLOEXEC;
75 #endif
76 #ifdef O_BINARY
77 flags |= O_BINARY;
78 #endif
79
80 if ((fd = open(filename, flags, mode)) < 0) {
81 return NULL;
82 }
83
84 handle = malloc(sizeof(*handle));
85 if (!handle) {
86 close(fd);
87 return NULL;
88 }
89 *handle = fd;
90
91 return handle;
92 }
93
94 static ssize_t file_read_default(void *file, char *buf, size_t size)
95 {
96 if (size == 0) {
97 return 0;
98 }
99
100 return read(*(int*)file, buf, size);
101 }
102
103 static off64_t file_seek_default(void *file, off64_t offset, int whence)
104 {
105 return lseek(*(int*)file, offset, whence);
106 }
107
108 static int file_close_default(void *file)
109 {
110 if (file) {
111 int ret = close(*(int*)file);
112 free(file);
113 return ret;
114 }
115 return 0;
116 }
117
118 static void *dir_open_default(dvd_reader_filesystem_h *fs, const char* dirname)
119 {
120 if (!fs)
121 return NULL;
122
123 return opendir(dirname);
124 }
125
126 static int dir_read_default(void *dir, dvd_dirent_t *entry)
127 {
128 struct dirent *p_e;
129
130 #ifdef USE_READDIR
131 errno = 0;
132 p_e = readdir((DIR*)dir);
133 if (!p_e && errno) {
134 return -errno;
135 }
136 #else /* USE_READDIR */
137 int result;
138 struct dirent e;
139
140 result = readdir_r((DIR*)dir, &e, &p_e);
141 if (result) {
142 return -result;
143 }
144 #endif /* USE_READDIR */
145
146 if (p_e == NULL) {
147 return 1;
148 }
149 strncpy(entry->d_name, p_e->d_name, sizeof(entry->d_name));
150 entry->d_name[sizeof(entry->d_name) - 1] = 0;
151
152 return 0;
153 }
154
155 static void dir_close_default(void *dir)
156 {
157 if (dir)
158 closedir((DIR *)dir);
159 }
160
161 static int stat_default(dvd_reader_filesystem_h *fs, const char *path, dvdstat_t* statbuf)
162 {
163 if (!fs)
164 return -1;
165
166 struct stat posixstatbuf;
167 int ret = stat(path, &posixstatbuf);
168 if (ret == 0) {
169 statbuf->size = posixstatbuf.st_size;
170 statbuf->st_mode = posixstatbuf.st_mode;
171 }
172 return ret;
173 }
174
175 static void default_filesystem_close(dvd_reader_filesystem_h *fs) {
176 free(fs);
177 }
178
179 dvd_reader_filesystem_h* InitInternalFilesystem(void) {
180 dvd_reader_filesystem_h* fs = calloc( 1, sizeof(dvd_reader_filesystem_h));
181 if (!fs) {
182 return NULL;
183 }
184 fs->close = default_filesystem_close;
185 fs->stat = stat_default;
186 fs->dir_open = dir_open_default;
187 fs->dir_read = dir_read_default;
188 fs->dir_close = dir_close_default;
189 fs->file_open = file_open_default;
190 fs->file_read = file_read_default;
191 fs->file_seek = file_seek_default;
192 fs->file_close = file_close_default;
193 return fs;
194 }