Debian Codegraph @

src/file/file_win32.c

raw
1 /*
2 * This file is part of libbluray
3 * Copyright (C) 2009-2010 Obliter0n
4 * Copyright (C) 2009-2010 John Stebbins
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
21 #if HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #if defined(__MINGW32__)
26 /* ftello64() and fseeko64() prototypes from stdio.h */
27 # undef __STRICT_ANSI__
28 #endif
29
30 #include "file.h"
31 #include "util/macro.h"
32 #include "util/logging.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <inttypes.h>
37
38 #include <windows.h>
39
40 static void _file_close(BD_FILE_H *file)
41 {
42 if (file) {
43 if (fclose((FILE *)file->internal)) {
44 BD_DEBUG(DBG_FILE | DBG_CRIT, "Error closing WIN32 file (%p)\n", (void*)file);
45 }
46
47 BD_DEBUG(DBG_FILE, "Closed WIN32 file (%p)\n", (void*)file);
48
49 X_FREE(file);
50 }
51 }
52
53 static int64_t _file_seek(BD_FILE_H *file, int64_t offset, int32_t origin)
54 {
55 #if defined(__MINGW32__)
56 return fseeko64((FILE *)file->internal, offset, origin);
57 #else
58 return _fseeki64((FILE *)file->internal, offset, origin);
59 #endif
60 }
61
62 static int64_t _file_tell(BD_FILE_H *file)
63 {
64 #if defined(__MINGW32__)
65 return ftello64((FILE *)file->internal);
66 #else
67 return _ftelli64((FILE *)file->internal);
68 #endif
69 }
70
71 static int64_t _file_read(BD_FILE_H *file, uint8_t *buf, int64_t size)
72 {
73 if (size > 0 && size < BD_MAX_SSIZE) {
74 return (int64_t)fread(buf, 1, (size_t)size, (FILE *)file->internal);
75 }
76
77 BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid read of size %" PRId64 " (%p)\n", size, (void*)file);
78 return 0;
79 }
80
81 static int64_t _file_write(BD_FILE_H *file, const uint8_t *buf, int64_t size)
82 {
83 if (size > 0 && size < BD_MAX_SSIZE) {
84 return (int64_t)fwrite(buf, 1, (size_t)size, (FILE *)file->internal);
85 }
86
87 if (size == 0) {
88 if (fflush((FILE *)file->internal)) {
89 BD_DEBUG(DBG_FILE, "fflush() failed (%p)\n", (void*)file);
90 return -1;
91 }
92 return 0;
93 }
94
95 BD_DEBUG(DBG_FILE | DBG_CRIT, "Ignoring invalid write of size %" PRId64 " (%p)\n", size, (void*)file);
96 return 0;
97 }
98
99 static BD_FILE_H *_file_open(void *handle, const char* filename)
100 {
101 BD_FILE_H *file;
102 FILE *fp;
103 wchar_t wfilename[MAX_PATH], wmode[8];
104 const char *mode = "rb";
105 (void)handle;
106
107 if (!MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, filename, -1, wfilename, MAX_PATH) ||
108 !MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mode, -1, wmode, 8)) {
109
110 BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
111 return NULL;
112 }
113
114 fp = _wfopen(wfilename, wmode);
115 if (!fp) {
116 BD_DEBUG(DBG_FILE, "Error opening file %s\n", filename);
117 return NULL;
118 }
119
120 file = calloc(1, sizeof(BD_FILE_H));
121 if (!file) {
122 BD_DEBUG(DBG_FILE | DBG_CRIT, "Error opening file %s (out of memory)\n", filename);
123 fclose(fp);
124 return NULL;
125 }
126
127 file->internal = fp;
128 file->close = _file_close;
129 file->seek = _file_seek;
130 file->read = _file_read;
131 file->write = _file_write;
132 file->tell = _file_tell;
133
134 BD_DEBUG(DBG_FILE, "Opened WIN32 file %s (%p)\n", filename, (void*)file);
135 return file;
136 }
137
138 BD_FILE_H* (*file_open)(void *handle, const char* filename) = _file_open;
139
140 BD_FILE_OPEN file_open_default(void)
141 {
142 return _file_open;
143 }
144
145 int file_path_exists(const char *path)
146 {
147 wchar_t wpath[MAX_PATH];
148
149 if (!MultiByteToWideChar(CP_UTF8, 0, path, -1, wpath, MAX_PATH)) {
150 return -1;
151 }
152
153 DWORD dwAttrib = GetFileAttributesW(wpath);
154 if (dwAttrib != INVALID_FILE_ATTRIBUTES) {
155 return 0;
156 }
157 return -1;
158 }
159
160 int file_mkdir(const char *dir)
161 {
162 wchar_t wdir[MAX_PATH];
163
164 if (!MultiByteToWideChar(CP_UTF8, 0, dir, -1, wdir, MAX_PATH)) {
165 return -1;
166 }
167 if (!CreateDirectoryW(wdir, NULL))
168 return -1;
169 return 0;
170 }