Debian Codegraph @

Xpjproject/pjlib/src/pj/file_io_win32.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <pj/file_io.h>
20 #include <pj/unicode.h>
21 #include <pj/errno.h>
22 #include <pj/assert.h>
23 #include <pj/string.h>
24
25 #if PJ_FILE_IO == PJ_FILE_IO_WIN32
26
27 #include <windows.h>
28
29 #ifndef INVALID_SET_FILE_POINTER
30 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
31 #endif
32
33 static pj_status_t set_file_pointer(pj_oshandle_t fd,
34 pj_off_t offset,
35 pj_off_t* newPos,
36 DWORD dwMoveMethod)
37 {
38 #if defined(PJ_WIN32_WINPHONE8) && PJ_WIN32_WINPHONE8
39 LARGE_INTEGER liDistance, liNewPos;
40
41 liDistance.QuadPart = offset;
42 if (!SetFilePointerEx(fd, liDistance, &liNewPos, dwMoveMethod)) {
43 return PJ_RETURN_OS_ERROR(GetLastError());
44 }
45 *newPos = liNewPos.QuadPart;
46 #else
47 DWORD dwNewPos;
48 LONG hi32;
49
50 hi32 = (LONG)(offset >> 32);
51
52 dwNewPos = SetFilePointer(fd, (long)offset, &hi32, dwMoveMethod);
53 if (dwNewPos == (DWORD)INVALID_SET_FILE_POINTER) {
54 DWORD dwStatus = GetLastError();
55 if (dwStatus != 0)
56 return PJ_RETURN_OS_ERROR(dwStatus);
57 /* dwNewPos actually is not an error. */
58 }
59 *newPos = hi32;
60 *newPos = (*newPos << 32) + dwNewPos;
61 #endif
62
63 return PJ_SUCCESS;
64 }
65
66 /**
67 * Check for end-of-file condition on the specified descriptor.
68 *
69 * @param fd The file descriptor.
70 * @param access The desired access.
71 *
72 * @return Non-zero if file is EOF.
73 */
74 PJ_DECL(pj_bool_t) pj_file_eof(pj_oshandle_t fd,
75 enum pj_file_access access);
76
77
78 PJ_DEF(pj_status_t) pj_file_open( pj_pool_t *pool,
79 const char *pathname,
80 unsigned flags,
81 pj_oshandle_t *fd)
82 {
83 PJ_DECL_UNICODE_TEMP_BUF(wpathname, 256)
84 HANDLE hFile;
85 DWORD dwDesiredAccess = 0;
86 DWORD dwShareMode = 0;
87 DWORD dwCreationDisposition = OPEN_EXISTING;
88 DWORD dwFlagsAndAttributes = 0;
89
90 PJ_UNUSED_ARG(pool);
91
92 PJ_ASSERT_RETURN(pathname!=NULL, PJ_EINVAL);
93
94 if ((flags & PJ_O_WRONLY) == PJ_O_WRONLY) {
95 dwDesiredAccess |= GENERIC_WRITE;
96 if ((flags & PJ_O_APPEND) == PJ_O_APPEND) {
97 #if !defined(PJ_WIN32_WINCE) || !PJ_WIN32_WINCE
98 /* FILE_APPEND_DATA is invalid on WM2003 and WM5, but it seems
99 * to be working on WM6. All are tested on emulator though.
100 * Removing this also seem to work (i.e. data is appended), so
101 * I guess this flag is "optional".
102 * See https://github.com/pjsip/pjproject/issues/825
103 */
104 dwDesiredAccess |= FILE_APPEND_DATA;
105 #endif
106 dwCreationDisposition = OPEN_ALWAYS;
107 } else {
108 dwDesiredAccess &= ~(FILE_APPEND_DATA);
109 dwCreationDisposition = CREATE_ALWAYS;
110 }
111 }
112 if ((flags & PJ_O_RDONLY) == PJ_O_RDONLY) {
113 dwDesiredAccess |= GENERIC_READ;
114 }
115
116 if (dwDesiredAccess == 0) {
117 pj_assert(!"Invalid file open flags");
118 return PJ_EINVAL;
119 }
120
121 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
122
123 if ((flags & PJ_O_SEQUENTIAL) == PJ_O_SEQUENTIAL) {
124 dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
125 }
126 if ((flags & PJ_O_RANDOM) == PJ_O_RANDOM) {
127 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
128 }
129 if ((flags & PJ_O_ASYNC) == PJ_O_ASYNC) {
130 dwFlagsAndAttributes |= FILE_FLAG_OVERLAPPED;
131 }
132 if (!dwFlagsAndAttributes) dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
133
134 #if defined(PJ_WIN32_WINPHONE8) && PJ_WIN32_WINPHONE8
135 hFile = CreateFile2(PJ_STRING_TO_NATIVE(pathname,
136 wpathname, sizeof(wpathname)),
137 dwDesiredAccess, dwShareMode, dwCreationDisposition,
138 NULL);
139 #else
140 hFile = CreateFile(PJ_STRING_TO_NATIVE(pathname,
141 wpathname, sizeof(wpathname)),
142 dwDesiredAccess, dwShareMode, NULL,
143 dwCreationDisposition, dwFlagsAndAttributes, NULL);
144 #endif
145
146 if (hFile == INVALID_HANDLE_VALUE) {
147 DWORD lastErr = GetLastError();
148 *fd = 0;
149 return lastErr == ERROR_FILE_NOT_FOUND ? PJ_ENOTFOUND : PJ_RETURN_OS_ERROR(lastErr);
150 }
151
152 if ((flags & PJ_O_APPEND) == PJ_O_APPEND) {
153 pj_status_t status;
154
155 status = pj_file_setpos(hFile, 0, PJ_SEEK_END);
156 if (status != PJ_SUCCESS) {
157 pj_file_close(hFile);
158 return status;
159 }
160 }
161
162 *fd = hFile;
163 return PJ_SUCCESS;
164 }
165
166 PJ_DEF(pj_status_t) pj_file_close(pj_oshandle_t fd)
167 {
168 if (CloseHandle(fd)==0)
169 return PJ_RETURN_OS_ERROR(GetLastError());
170 return PJ_SUCCESS;
171 }
172
173 PJ_DEF(pj_status_t) pj_file_write( pj_oshandle_t fd,
174 const void *data,
175 pj_ssize_t *size)
176 {
177 BOOL rc;
178 DWORD bytesWritten;
179
180 rc = WriteFile(fd, data, (DWORD)*size, &bytesWritten, NULL);
181 if (!rc) {
182 *size = -1;
183 return PJ_RETURN_OS_ERROR(GetLastError());
184 }
185
186 *size = bytesWritten;
187 return PJ_SUCCESS;
188 }
189
190 PJ_DEF(pj_status_t) pj_file_read( pj_oshandle_t fd,
191 void *data,
192 pj_ssize_t *size)
193 {
194 BOOL rc;
195 DWORD bytesRead;
196
197 rc = ReadFile(fd, data, (DWORD)*size, &bytesRead, NULL);
198 if (!rc) {
199 *size = -1;
200 return PJ_RETURN_OS_ERROR(GetLastError());
201 }
202
203 *size = bytesRead;
204 return PJ_SUCCESS;
205 }
206
207 /*
208 PJ_DEF(pj_bool_t) pj_file_eof(pj_oshandle_t fd, enum pj_file_access access)
209 {
210 BOOL rc;
211 DWORD dummy = 0, bytes;
212 DWORD dwStatus;
213
214 if ((access & PJ_O_RDONLY) == PJ_O_RDONLY) {
215 rc = ReadFile(fd, &dummy, 0, &bytes, NULL);
216 } else if ((access & PJ_O_WRONLY) == PJ_O_WRONLY) {
217 rc = WriteFile(fd, &dummy, 0, &bytes, NULL);
218 } else {
219 pj_assert(!"Invalid access");
220 return PJ_TRUE;
221 }
222
223 dwStatus = GetLastError();
224 if (dwStatus==ERROR_HANDLE_EOF)
225 return PJ_TRUE;
226
227 return 0;
228 }
229 */
230
231 PJ_DEF(pj_status_t) pj_file_setpos( pj_oshandle_t fd,
232 pj_off_t offset,
233 enum pj_file_seek_type whence)
234 {
235 DWORD dwMoveMethod;
236 pj_off_t newPos;
237
238 if (whence == PJ_SEEK_SET)
239 dwMoveMethod = FILE_BEGIN;
240 else if (whence == PJ_SEEK_CUR)
241 dwMoveMethod = FILE_CURRENT;
242 else if (whence == PJ_SEEK_END)
243 dwMoveMethod = FILE_END;
244 else {
245 pj_assert(!"Invalid whence in file_setpos");
246 return PJ_EINVAL;
247 }
248
249 return set_file_pointer(fd, offset, &newPos, dwMoveMethod);
250 }
251
252 PJ_DEF(pj_status_t) pj_file_getpos( pj_oshandle_t fd,
253 pj_off_t *pos)
254 {
255 return set_file_pointer(fd, 0, pos, FILE_CURRENT);
256 }
257
258 PJ_DEF(pj_status_t) pj_file_flush(pj_oshandle_t fd)
259 {
260 BOOL rc;
261
262 rc = FlushFileBuffers(fd);
263
264 if (!rc) {
265 DWORD dwStatus = GetLastError();
266 if (dwStatus != 0)
267 return PJ_RETURN_OS_ERROR(dwStatus);
268 }
269
270 return PJ_SUCCESS;
271 }
272 #endif //PJ_FILE_IO == PJ_FILE_IO_WIN32