1/* 2 * libwebsockets - small server side websockets and web server implementation 3 * 4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to 8 * deal in the Software without restriction, including without limitation the 9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 * sell copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 * IN THE SOFTWARE. 23 */ 24 25#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS 26#define _WINSOCK_DEPRECATED_NO_WARNINGS 27#endif 28#include "private-lib-core.h" 29 30int lws_plat_apply_FD_CLOEXEC(int n) 31{ 32 return 0; 33} 34 35lws_fop_fd_t 36_lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename, 37 const char *vpath, lws_fop_flags_t *flags) 38{ 39 HANDLE ret; 40 WCHAR buf[MAX_PATH]; 41 lws_fop_fd_t fop_fd; 42 LARGE_INTEGER llFileSize = {0}; 43 44 MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, LWS_ARRAY_SIZE(buf)); 45 if (((*flags) & 7) == _O_RDONLY) 46 ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 47 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); 48 else 49 ret = CreateFileW(buf, GENERIC_WRITE, 0, NULL, 50 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 51 52 if (ret == NULL) 53 goto bail; 54 55 fop_fd = malloc(sizeof(*fop_fd)); 56 if (!fop_fd) 57 goto bail; 58 59 fop_fd->fops = fops; 60#if defined(__MINGW32__) 61 /* we use filesystem_priv */ 62 fop_fd->fd = (int)(intptr_t)ret; 63#else 64 fop_fd->fd = ret; 65#endif 66 fop_fd->filesystem_priv = ret; 67 fop_fd->flags = *flags; 68 fop_fd->len = GetFileSize(ret, NULL); 69 if(GetFileSizeEx(ret, &llFileSize)) 70 fop_fd->len = llFileSize.QuadPart; 71 72 fop_fd->pos = 0; 73 74 return fop_fd; 75 76bail: 77 return NULL; 78} 79 80int 81_lws_plat_file_close(lws_fop_fd_t *fop_fd) 82{ 83 HANDLE fd = (*fop_fd)->filesystem_priv; 84 85 free(*fop_fd); 86 *fop_fd = NULL; 87 88 CloseHandle((HANDLE)fd); 89 90 return 0; 91} 92 93lws_fileofs_t 94_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset) 95{ 96 LARGE_INTEGER l; 97 98 l.QuadPart = offset; 99 if (!SetFilePointerEx((HANDLE)fop_fd->filesystem_priv, l, NULL, FILE_CURRENT)) 100 { 101 lwsl_err("error seeking from cur %ld, offset %ld\n", (long)fop_fd->pos, (long)offset); 102 return -1; 103 } 104 105 LARGE_INTEGER zero; 106 zero.QuadPart = 0; 107 LARGE_INTEGER newPos; 108 if (!SetFilePointerEx((HANDLE)fop_fd->filesystem_priv, zero, &newPos, FILE_CURRENT)) 109 { 110 lwsl_err("error seeking from cur %ld, offset %ld\n", (long)fop_fd->pos, (long)offset); 111 return -1; 112 } 113 fop_fd->pos = newPos.QuadPart; 114 115 return newPos.QuadPart; 116} 117 118int 119_lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount, 120 uint8_t *buf, lws_filepos_t len) 121{ 122 DWORD _amount; 123 124 if (!ReadFile((HANDLE)fop_fd->filesystem_priv, buf, (DWORD)len, &_amount, NULL)) { 125 *amount = 0; 126 127 return 1; 128 } 129 130 fop_fd->pos += _amount; 131 *amount = (unsigned long)_amount; 132 133 return 0; 134} 135 136int 137_lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount, 138 uint8_t* buf, lws_filepos_t len) 139{ 140 DWORD _amount; 141 142 if (!WriteFile((HANDLE)fop_fd->filesystem_priv, buf, (DWORD)len, &_amount, NULL)) { 143 *amount = 0; 144 145 return 1; 146 } 147 148 fop_fd->pos += _amount; 149 *amount = (unsigned long)_amount; 150 151 return 0; 152} 153 154 155int 156lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf, 157 size_t len) 158{ 159 int n; 160 161 n = (int)write(fd, buf, (unsigned int)len); 162 163 lseek(fd, 0, SEEK_SET); 164 165 return (size_t)n != len; 166} 167 168int 169lws_plat_write_file(const char *filename, void *buf, size_t len) 170{ 171 int m, fd; 172 173 fd = lws_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); 174 175 if (fd == -1) 176 return -1; 177 178 m = (int)write(fd, buf, (unsigned int)len); 179 close(fd); 180 181 return (size_t)m != len; 182} 183 184int 185lws_plat_read_file(const char *filename, void *buf, size_t len) 186{ 187 int n, fd = lws_open(filename, O_RDONLY); 188 if (fd == -1) 189 return -1; 190 191 n = (int)read(fd, buf, (unsigned int)len); 192 close(fd); 193 194 return n; 195} 196 197