| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <io.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <unistd.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <string.h> |
| 25 |
#include <errno.h> |
| 26 |
|
| 27 |
#include <sys/stat.h> |
| 28 |
#include <fcntl.h> |
| 29 |
|
| 30 |
|
| 31 |
#undef stat |
| 32 |
|
| 33 |
#include <windows.h> |
| 34 |
#include "../msvc/contrib/win32_cs.h" |
| 35 |
|
| 36 |
#include <dvdread/dvd_filesystem.h> |
| 37 |
#include "filesystem.h" |
| 38 |
|
| 39 |
|
| 40 |
static void *file_open_default(dvd_reader_filesystem_h *fs, const char* filename) |
| 41 |
{ |
| 42 |
if (!fs) |
| 43 |
return NULL; |
| 44 |
|
| 45 |
int *handle; |
| 46 |
int fd; |
| 47 |
wchar_t *wpath; |
| 48 |
|
| 49 |
wpath = _utf8_to_wchar(filename); |
| 50 |
if (!wpath) { |
| 51 |
return NULL; |
| 52 |
} |
| 53 |
|
| 54 |
fd = _wopen(wpath, O_RDONLY | O_BINARY); |
| 55 |
free(wpath); |
| 56 |
if (fd < 0) { |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
|
| 60 |
handle = malloc(sizeof(*handle)); |
| 61 |
if (!handle) { |
| 62 |
close(fd); |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
*handle = fd; |
| 66 |
|
| 67 |
return handle; |
| 68 |
} |
| 69 |
|
| 70 |
static ssize_t file_read_default(void *file, char *buf, size_t size) |
| 71 |
{ |
| 72 |
if (size == 0) { |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
return read(*(int*)file, buf, size); |
| 77 |
} |
| 78 |
|
| 79 |
static off64_t file_seek_default(void *file, off64_t offset, int whence) |
| 80 |
{ |
| 81 |
return _lseeki64(*(int*)file, offset, whence); |
| 82 |
} |
| 83 |
|
| 84 |
static int file_close_default(void *file) |
| 85 |
{ |
| 86 |
if (file) { |
| 87 |
int ret = close(*(int*)file); |
| 88 |
free(file); |
| 89 |
return ret; |
| 90 |
} |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
typedef struct { |
| 95 |
intptr_t handle; |
| 96 |
struct _wfinddata_t went; |
| 97 |
} win32_dir_t; |
| 98 |
|
| 99 |
static void *dir_open_default(dvd_reader_filesystem_h *fs, const char* dirname) |
| 100 |
{ |
| 101 |
if (!fs) |
| 102 |
return NULL; |
| 103 |
|
| 104 |
char *filespec; |
| 105 |
wchar_t *wfilespec; |
| 106 |
win32_dir_t *d = calloc(1, sizeof(*d)); |
| 107 |
|
| 108 |
if (!d) { |
| 109 |
return NULL; |
| 110 |
} |
| 111 |
|
| 112 |
filespec = malloc(strlen(dirname) + 3); |
| 113 |
if (!filespec) { |
| 114 |
goto fail; |
| 115 |
} |
| 116 |
sprintf(filespec, "%s\\*", dirname); |
| 117 |
|
| 118 |
wfilespec = _utf8_to_wchar(filespec); |
| 119 |
free(filespec); |
| 120 |
if (!wfilespec) { |
| 121 |
goto fail; |
| 122 |
} |
| 123 |
|
| 124 |
d->handle = _wfindfirst(wfilespec, &d->went); |
| 125 |
free(wfilespec); |
| 126 |
if (d->handle != -1) { |
| 127 |
return d; |
| 128 |
} |
| 129 |
|
| 130 |
fail: |
| 131 |
free(d); |
| 132 |
return NULL; |
| 133 |
} |
| 134 |
|
| 135 |
static int dir_read_default(void *dir, dvd_dirent_t *entry) |
| 136 |
{ |
| 137 |
win32_dir_t *wdir = (win32_dir_t*)dir; |
| 138 |
if (wdir->went.name[0]) { |
| 139 |
if (!WideCharToMultiByte(CP_UTF8, 0, wdir->went.name, -1, entry->d_name, sizeof(entry->d_name), NULL, NULL)) |
| 140 |
entry->d_name[0] = 0; |
| 141 |
wdir->went.name[0] = 0; |
| 142 |
_wfindnext(wdir->handle, &wdir->went); |
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
return 1; |
| 147 |
} |
| 148 |
|
| 149 |
static void dir_close_default(void *dir) |
| 150 |
{ |
| 151 |
if (dir) { |
| 152 |
win32_dir_t *wdir = (win32_dir_t*)dir; |
| 153 |
_findclose(wdir->handle); |
| 154 |
free(wdir); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
static int stat_default(dvd_reader_filesystem_h *fs, const char *path, dvdstat_t* statbuf) |
| 159 |
{ |
| 160 |
if (!fs) |
| 161 |
return -1; |
| 162 |
|
| 163 |
struct _stat64 win32statbuf; |
| 164 |
|
| 165 |
wchar_t *wpath, *it; |
| 166 |
|
| 167 |
wpath = _utf8_to_wchar(path); |
| 168 |
if (!wpath) { |
| 169 |
return -1; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
for (it = wpath; *it; it++) |
| 174 |
if ((*it == '\\' || *it == '/') && *(it+1) == 0) |
| 175 |
*it = 0; |
| 176 |
|
| 177 |
int ret = _wstat64(wpath, &win32statbuf); |
| 178 |
free(wpath); |
| 179 |
if (ret == 0) { |
| 180 |
statbuf->size = win32statbuf.st_size; |
| 181 |
statbuf->st_mode = win32statbuf.st_mode; |
| 182 |
} |
| 183 |
return ret; |
| 184 |
} |
| 185 |
|
| 186 |
static void default_filesystem_close(dvd_reader_filesystem_h *fs) { |
| 187 |
free(fs); |
| 188 |
} |
| 189 |
|
| 190 |
dvd_reader_filesystem_h* InitInternalFilesystem(void) { |
| 191 |
dvd_reader_filesystem_h* fs = calloc( 1, sizeof(dvd_reader_filesystem_h)); |
| 192 |
if (!fs) { |
| 193 |
return NULL; |
| 194 |
} |
| 195 |
fs->close = default_filesystem_close; |
| 196 |
fs->stat = stat_default; |
| 197 |
fs->dir_open = dir_open_default; |
| 198 |
fs->dir_read = dir_read_default; |
| 199 |
fs->dir_close = dir_close_default; |
| 200 |
fs->file_open = file_open_default; |
| 201 |
fs->file_read = file_read_default; |
| 202 |
fs->file_seek = file_seek_default; |
| 203 |
fs->file_close = file_close_default; |
| 204 |
return fs; |
| 205 |
} |