Debian Codegraph @

src/utilities/ntrace/os/win32/FileObject.cpp

raw
1 /*
2 * PROGRAM: Client/Server Common Code
3 * MODULE: FileObject.cpp
4 * DESCRIPTION: Wrapper class for platform IO
5 *
6 * The contents of this file are subject to the Initial
7 * Developer's Public License Version 1.0 (the "License");
8 * you may not use this file except in compliance with the
9 * License. You may obtain a copy of the License at
10 * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
11 *
12 * Software distributed under the License is distributed AS IS,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
14 * See the License for the specific language governing rights
15 * and limitations under the License.
16 *
17 * The Original Code was created by Nickolay Samofatov
18 * for the Firebird Open Source RDBMS project.
19 *
20 * Copyright (c) 2004 Nickolay Samofatov <[email protected]>
21 * and all contributors signed below.
22 *
23 * All Rights Reserved.
24 * Contributor(s): ______________________________________.
25 *
26 */
27
28 #include "firebird.h"
29 #include "../FileObject.h"
30
31 using namespace Firebird;
32 Firebird::Mutex open_mutex;
33
34 void FileObject::open(int flags, int pflags)
35 {
36 MutexLockGuard guard(open_mutex, FB_FUNCTION);
37 DWORD flagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
38 DWORD filecreate = 0;
39 DWORD desiredAccess = 0;
40
41 switch (flags & (fo_rdonly | fo_wronly | fo_rdwr))
42 {
43 case fo_rdonly:
44 desiredAccess = GENERIC_READ;
45 break;
46 case fo_wronly:
47 desiredAccess = GENERIC_WRITE;
48 break;
49 default:
50 desiredAccess = GENERIC_READ | GENERIC_WRITE;
51 }
52
53 if (flags & fo_append)
54 {
55 // This is going to be tricky. Need to use global named mutex to achieve
56 // multi-process happiness
57 string temp(filename.c_str());
58 for (string::size_type i = 0; i < temp.length(); i++)
59 {
60 switch (temp[i])
61 {
62 case '\\':
63 case '/':
64 case ':':
65 temp[i] = '_';
66 }
67 }
68
69 temp.append("_mutex");
70 append_mutex = CreateMutex(NULL, FALSE, temp.c_str());
71 if (append_mutex == NULL)
72 {
73 append_mutex = INVALID_HANDLE_VALUE;
74 system_call_failed::raise("CreateMutex");
75 }
76 }
77
78 // decode open/create method flags
79 switch ( flags & (fo_creat | fo_excl | fo_trunc) )
80 {
81 case 0:
82 case fo_excl: // ignore EXCL w/o CREAT
83 filecreate = OPEN_EXISTING;
84 break;
85
86 case fo_creat:
87 filecreate = OPEN_ALWAYS;
88 break;
89
90 case fo_creat | fo_excl:
91 case fo_creat | fo_trunc | fo_excl:
92 filecreate = CREATE_NEW;
93 break;
94
95 case fo_trunc:
96 case fo_trunc | fo_excl: // ignore EXCL w/o CREAT
97 filecreate = TRUNCATE_EXISTING;
98 break;
99
100 case fo_creat | fo_trunc:
101 filecreate = CREATE_ALWAYS;
102 break;
103
104 default:
105 // this can't happen ... all cases are covered
106 fb_assert(false);
107 }
108
109 if (flags & fo_temporary)
110 flagsAndAttributes |= FILE_FLAG_DELETE_ON_CLOSE;
111
112 if (flags & fo_short_lived)
113 flagsAndAttributes |= FILE_ATTRIBUTE_TEMPORARY;
114
115 if (flags & fo_sequential)
116 flagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
117
118 if (flags & fo_random)
119 flagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
120
121 file = CreateFile(filename.c_str(),
122 desiredAccess,
123 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
124 NULL,
125 filecreate,
126 flagsAndAttributes, NULL);
127
128 if (file == INVALID_HANDLE_VALUE)
129 fatal_exception::raiseFmt("Error (%d) opening file: %s", GetLastError(), filename.c_str());
130 }
131
132 FileObject::~FileObject()
133 {
134 CloseHandle(file);
135 CloseHandle(append_mutex);
136 }
137
138 UINT64 FileObject::size()
139 {
140 UINT64 nFileLen = 0;
141 if (file != INVALID_HANDLE_VALUE)
142 {
143 LARGE_INTEGER size;
144 if (GetFileSizeEx(file, &size))
145 nFileLen = size.QuadPart;
146 }
147 return nFileLen;
148 }
149
150 void FileObject::reopen()
151 {
152 CloseHandle(file);
153 if (append_mutex != INVALID_HANDLE_VALUE)
154 {
155 ReleaseMutex(append_mutex);
156 CloseHandle(append_mutex);
157 }
158 open(fo_rdwr | fo_append | fo_creat, 0);
159 FlushFileBuffers(file);
160 }
161
162 size_t FileObject::blockRead(void* buffer, size_t bytesToRead)
163 {
164 DWORD bytesDone;
165 if (!ReadFile(file, buffer, bytesToRead, &bytesDone, NULL))
166 {
167 fatal_exception::raiseFmt("IO error (%d) reading file: %s",
168 GetLastError(),
169 filename.c_str());
170 }
171
172 return bytesDone;
173 }
174
175 void FileObject::blockWrite(const void* buffer, size_t bytesToWrite)
176 {
177 if (append_mutex != INVALID_HANDLE_VALUE)
178 {
179 if (WaitForSingleObject(append_mutex, INFINITE) != WAIT_OBJECT_0)
180 system_call_failed::raise("WaitForSingleObject");
181 seek(0, so_from_end);
182 }
183
184 DWORD bytesDone;
185 if (!WriteFile(file, buffer, bytesToWrite, &bytesDone, NULL) ||
186 bytesDone != bytesToWrite)
187 {
188 if (append_mutex != INVALID_HANDLE_VALUE)
189 ReleaseMutex(append_mutex);
190 fatal_exception::raiseFmt("IO error (%d) writing file: %s",
191 GetLastError(),
192 filename.c_str());
193 }
194
195 if (append_mutex != INVALID_HANDLE_VALUE)
196 ReleaseMutex(append_mutex);
197 }
198
199 // Write data to header only if file is empty
200 void FileObject::writeHeader(const void* buffer, size_t bytesToWrite)
201 {
202 if (append_mutex != INVALID_HANDLE_VALUE)
203 {
204 if (WaitForSingleObject(append_mutex, INFINITE) != WAIT_OBJECT_0)
205 system_call_failed::raise("WaitForSingleObject");
206 }
207
208 if (seek(0, so_from_end) != 0)
209 return;
210
211 DWORD bytesDone;
212 if (!WriteFile(file, buffer, bytesToWrite, &bytesDone, NULL) ||
213 bytesDone != bytesToWrite)
214 {
215 if (append_mutex != INVALID_HANDLE_VALUE)
216 ReleaseMutex(append_mutex);
217
218 fatal_exception::raiseFmt("IO error (%d) writing file: %s",
219 GetLastError(),
220 filename.c_str());
221 }
222
223 if (append_mutex != INVALID_HANDLE_VALUE)
224 ReleaseMutex(append_mutex);
225 }
226
227 bool FileObject::renameFile(const Firebird::PathName new_filename)
228 {
229 if (append_mutex != INVALID_HANDLE_VALUE)
230 {
231 if (WaitForSingleObject(append_mutex, INFINITE) != WAIT_OBJECT_0)
232 system_call_failed::raise("WaitForSingleObject");
233 }
234
235 if (!MoveFile(filename.c_str(), new_filename.c_str()))
236 {
237 DWORD rename_err = GetLastError();
238 if (rename_err == ERROR_ALREADY_EXISTS || rename_err == ERROR_FILE_NOT_FOUND)
239 {
240 // Another process renames our file just now. Open it again.
241 reopen();
242 return false;
243 }
244
245 if (append_mutex != INVALID_HANDLE_VALUE)
246 ReleaseMutex(append_mutex);
247
248 fatal_exception::raiseFmt("IO error (%d) renaming file: %s",
249 rename_err,
250 filename.c_str());
251 }
252 else
253 reopen();
254
255 return true;
256 }
257
258 SINT64 FileObject::seek(SINT64 newOffset, SeekOrigin origin)
259 {
260 LARGE_INTEGER offset;
261 offset.QuadPart = newOffset;
262 DWORD moveMethod;
263
264 switch(origin)
265 {
266 case so_from_beginning:
267 moveMethod = FILE_BEGIN;
268 break;
269 case so_from_current:
270 moveMethod = FILE_CURRENT;
271 break;
272 case so_from_end:
273 moveMethod = FILE_END;
274 break;
275 }
276
277 DWORD error;
278 if ((offset.LowPart = SetFilePointer(file, offset.LowPart,
279 &offset.HighPart, moveMethod)) == INVALID_SET_FILE_POINTER &&
280 (error = GetLastError()) != NO_ERROR)
281 {
282 fatal_exception::raiseFmt("IO error (%d) seeking file: %s",
283 error,
284 filename.c_str());
285 }
286
287 return offset.QuadPart;
288 }