src/osd/windows/winfile.c
| r9730 | r9731 | |
| 42 | 42 | // standard windows headers |
| 43 | 43 | #define WIN32_LEAN_AND_MEAN |
| 44 | 44 | #include <windows.h> |
| 45 | | #include <winsock.h> |
| 46 | 45 | #include <winioctl.h> |
| 47 | 46 | #include <tchar.h> |
| 48 | 47 | #include <stdlib.h> |
| r9730 | r9731 | |
| 60 | 59 | // TYPE DEFINITIONS |
| 61 | 60 | //============================================================ |
| 62 | 61 | |
| 63 | | enum _osd_file_kind |
| 64 | | { |
| 65 | | regular_file, |
| 66 | | socket_file |
| 67 | | }; |
| 68 | | |
| 69 | 62 | struct _osd_file |
| 70 | 63 | { |
| 71 | | _osd_file_kind file_kind; |
| 72 | 64 | HANDLE handle; |
| 73 | | int sockfd; |
| 74 | 65 | TCHAR filename[1]; |
| 75 | 66 | }; |
| 76 | 67 | |
| r9730 | r9731 | |
| 85 | 76 | |
| 86 | 77 | |
| 87 | 78 | //============================================================ |
| 88 | | // GLOBALS |
| 89 | | //============================================================ |
| 90 | | |
| 91 | | static int socket_count = 0; |
| 92 | | |
| 93 | | |
| 94 | | //============================================================ |
| 95 | 79 | // INLINE FUNCTIONS |
| 96 | 80 | //============================================================ |
| 97 | 81 | |
| r9730 | r9731 | |
| 101 | 85 | } |
| 102 | 86 | |
| 103 | 87 | |
| 104 | | INLINE int is_path_to_socket(const char *path) |
| 105 | | { |
| 106 | | return (_strnicmp(path, "\\\\.\\socket", 10) == 0); |
| 107 | | } |
| 108 | 88 | |
| 109 | | |
| 110 | | |
| 111 | 89 | //============================================================ |
| 112 | 90 | // osd_open |
| 113 | 91 | //============================================================ |
| r9730 | r9731 | |
| 143 | 121 | *dst++ = *src;//(*src == '/') ? '\\' : *src; |
| 144 | 122 | *dst++ = 0; |
| 145 | 123 | |
| 146 | | // attempt to open a socket connection |
| 147 | | if (is_path_to_socket(path)) |
| 148 | | { |
| 149 | | WSADATA wsaData; |
| 150 | | char hostname[256]; |
| 151 | | struct hostent *localhost; |
| 152 | | struct sockaddr_in sai; |
| 153 | | int flag = 1; |
| 154 | | int port; |
| 155 | | |
| 156 | | (*file)->file_kind = socket_file; |
| 157 | | |
| 158 | | if( socket_count == 0 ) |
| 159 | | { |
| 160 | | if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) |
| 161 | | { |
| 162 | | filerr = FILERR_INVALID_ACCESS; |
| 163 | | goto error; |
| 164 | | } |
| 165 | | } |
| 166 | | |
| 167 | | socket_count++; |
| 168 | | |
| 169 | | sscanf( path+strlen("\\\\.\\socket"), ":%255[^:]:%d", hostname, &port ); |
| 170 | | |
| 171 | | if (((*file)->sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) |
| 172 | | { |
| 173 | | filerr = FILERR_SOCKET_ERROR; |
| 174 | | goto error; |
| 175 | | } |
| 176 | | |
| 177 | | if (setsockopt((*file)->sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(flag)) == -1) |
| 178 | | { |
| 179 | | filerr = FILERR_SOCKET_ERROR; |
| 180 | | goto error; |
| 181 | | } |
| 182 | | |
| 183 | | localhost = gethostbyname(hostname); |
| 184 | | |
| 185 | | memset(&sai, 0, sizeof(sai)); |
| 186 | | sai.sin_family = AF_INET; |
| 187 | | sai.sin_port = htons(port); |
| 188 | | sai.sin_addr = *((struct in_addr *)localhost->h_addr); |
| 189 | | |
| 190 | | if (connect((*file)->sockfd, (struct sockaddr *)&sai, sizeof(struct sockaddr)) == -1) |
| 191 | | { |
| 192 | | filerr = FILERR_SOCKET_ERROR; |
| 193 | | goto error; |
| 194 | | } |
| 195 | | |
| 196 | | *filesize = 0; |
| 197 | | |
| 198 | | filerr = FILERR_NONE; |
| 199 | | goto error; |
| 200 | | } |
| 201 | | else |
| 202 | | { |
| 203 | | (*file)->file_kind = regular_file; |
| 204 | | } |
| 205 | | |
| 206 | 124 | // select the file open modes |
| 207 | 125 | if (openflags & OPEN_FLAG_WRITE) |
| 208 | 126 | { |
| r9730 | r9731 | |
| 275 | 193 | |
| 276 | 194 | file_error osd_read(osd_file *file, void *buffer, UINT64 offset, UINT32 length, UINT32 *actual) |
| 277 | 195 | { |
| 278 | | if (file->file_kind == regular_file) |
| 279 | | { |
| 280 | | LONG upper = offset >> 32; |
| 281 | | DWORD result; |
| 196 | LONG upper = offset >> 32; |
| 197 | DWORD result; |
| 282 | 198 | |
| 283 | | // attempt to set the file pointer |
| 284 | | result = SetFilePointer(file->handle, (UINT32)offset, &upper, FILE_BEGIN); |
| 285 | | if (result == INVALID_SET_FILE_POINTER) |
| 286 | | { |
| 287 | | DWORD error = GetLastError(); |
| 288 | | if (error != NO_ERROR) |
| 289 | | return win_error_to_file_error(error); |
| 290 | | } |
| 291 | | |
| 292 | | // then perform the read |
| 293 | | if (!ReadFile(file->handle, buffer, length, &result, NULL)) |
| 294 | | return win_error_to_file_error(GetLastError()); |
| 295 | | if (actual != NULL) |
| 296 | | *actual = result; |
| 297 | | return FILERR_NONE; |
| 298 | | } |
| 299 | | else if (file->file_kind == socket_file) |
| 199 | // attempt to set the file pointer |
| 200 | result = SetFilePointer(file->handle, (UINT32)offset, &upper, FILE_BEGIN); |
| 201 | if (result == INVALID_SET_FILE_POINTER) |
| 300 | 202 | { |
| 301 | | ssize_t result; |
| 302 | | struct timeval timeout; |
| 303 | | fd_set readfds; |
| 304 | | |
| 305 | | FD_ZERO(&readfds); |
| 306 | | FD_SET(file->sockfd, &readfds); |
| 307 | | timeout.tv_sec = timeout.tv_usec = 0; |
| 308 | | |
| 309 | | if (select(file->sockfd + 1, &readfds, NULL, NULL, &timeout) < 0) |
| 310 | | { |
| 311 | | return win_error_to_file_error(GetLastError()); |
| 312 | | } |
| 313 | | else if (FD_ISSET(file->sockfd, &readfds)) |
| 314 | | { |
| 315 | | result = recv(file->sockfd, (char *)buffer, length, 0); |
| 316 | | } |
| 317 | | else |
| 318 | | return FILERR_FAILURE; |
| 319 | | |
| 320 | | if (result < 0) |
| 321 | | return win_error_to_file_error(GetLastError()); |
| 322 | | |
| 323 | | if (actual != NULL ) |
| 324 | | *actual = result; |
| 325 | | |
| 326 | | return FILERR_NONE; |
| 203 | DWORD error = GetLastError(); |
| 204 | if (error != NO_ERROR) |
| 205 | return win_error_to_file_error(error); |
| 327 | 206 | } |
| 328 | | else |
| 329 | | { |
| 330 | | return FILERR_INVALID_ACCESS; |
| 331 | | } |
| 207 | |
| 208 | // then perform the read |
| 209 | if (!ReadFile(file->handle, buffer, length, &result, NULL)) |
| 210 | return win_error_to_file_error(GetLastError()); |
| 211 | if (actual != NULL) |
| 212 | *actual = result; |
| 213 | return FILERR_NONE; |
| 332 | 214 | } |
| 333 | 215 | |
| 334 | 216 | |
| r9730 | r9731 | |
| 338 | 220 | |
| 339 | 221 | file_error osd_write(osd_file *file, const void *buffer, UINT64 offset, UINT32 length, UINT32 *actual) |
| 340 | 222 | { |
| 341 | | if (file->file_kind == regular_file) |
| 342 | | { |
| 343 | | LONG upper = offset >> 32; |
| 344 | | DWORD result; |
| 223 | LONG upper = offset >> 32; |
| 224 | DWORD result; |
| 345 | 225 | |
| 346 | | // attempt to set the file pointer |
| 347 | | result = SetFilePointer(file->handle, (UINT32)offset, &upper, FILE_BEGIN); |
| 348 | | if (result == INVALID_SET_FILE_POINTER) |
| 349 | | { |
| 350 | | DWORD error = GetLastError(); |
| 351 | | if (error != NO_ERROR) |
| 352 | | return win_error_to_file_error(error); |
| 353 | | } |
| 354 | | |
| 355 | | // then perform the read |
| 356 | | if (!WriteFile(file->handle, buffer, length, &result, NULL)) |
| 357 | | return win_error_to_file_error(GetLastError()); |
| 358 | | if (actual != NULL) |
| 359 | | *actual = result; |
| 360 | | return FILERR_NONE; |
| 361 | | } |
| 362 | | else if (file->file_kind == socket_file) |
| 226 | // attempt to set the file pointer |
| 227 | result = SetFilePointer(file->handle, (UINT32)offset, &upper, FILE_BEGIN); |
| 228 | if (result == INVALID_SET_FILE_POINTER) |
| 363 | 229 | { |
| 364 | | UINT32 result; |
| 365 | | |
| 366 | | result = send(file->sockfd, (const char *)buffer, length, 0); |
| 367 | | |
| 368 | | if (result < 0) |
| 369 | | return win_error_to_file_error(GetLastError()); |
| 370 | | |
| 371 | | if (actual != NULL ) |
| 372 | | *actual = result; |
| 373 | | |
| 374 | | return FILERR_NONE; |
| 230 | DWORD error = GetLastError(); |
| 231 | if (error != NO_ERROR) |
| 232 | return win_error_to_file_error(error); |
| 375 | 233 | } |
| 376 | | else |
| 377 | | { |
| 378 | | return FILERR_INVALID_ACCESS; |
| 379 | | } |
| 234 | |
| 235 | // then perform the read |
| 236 | if (!WriteFile(file->handle, buffer, length, &result, NULL)) |
| 237 | return win_error_to_file_error(GetLastError()); |
| 238 | if (actual != NULL) |
| 239 | *actual = result; |
| 240 | return FILERR_NONE; |
| 380 | 241 | } |
| 381 | 242 | |
| 382 | 243 | |
| r9730 | r9731 | |
| 387 | 248 | file_error osd_close(osd_file *file) |
| 388 | 249 | { |
| 389 | 250 | // close the file handle and free the file structure |
| 390 | | if (file->file_kind == regular_file) |
| 391 | | { |
| 392 | | CloseHandle(file->handle); |
| 393 | | } |
| 394 | | else if (file->file_kind == socket_file) |
| 395 | | { |
| 396 | | closesocket(file->sockfd); |
| 397 | | socket_count--; |
| 398 | | |
| 399 | | if( socket_count == 0 ) |
| 400 | | { |
| 401 | | WSACleanup(); |
| 402 | | } |
| 403 | | } |
| 404 | | else |
| 405 | | { |
| 406 | | } |
| 407 | | |
| 251 | CloseHandle(file->handle); |
| 408 | 252 | free(file); |
| 409 | 253 | return FILERR_NONE; |
| 410 | 254 | } |