1 #include <msp/core/systemerror.h>
2 #include <msp/strings/format.h>
4 #include "handle_private.h"
11 void File::platform_init(const string &fn, CreateMode cm)
15 int create_flags = OPEN_EXISTING;
18 flags |= GENERIC_READ;
22 flags |= GENERIC_WRITE;
24 switch(static_cast<int>(cm))
26 case C_NONE: create_flags = OPEN_EXISTING; break;
27 case C_CREATE: create_flags = OPEN_ALWAYS; break;
28 case C_TRUNCATE: create_flags = TRUNCATE_EXISTING; break;
29 case C_OVERWRITE: create_flags = CREATE_ALWAYS; break;
30 case C_NEW: create_flags = CREATE_NEW; break;
34 share_flags = FILE_SHARE_READ;
36 SECURITY_ATTRIBUTES sec_attr;
37 sec_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
38 sec_attr.lpSecurityDescriptor = 0;
39 sec_attr.bInheritHandle = !!(mode&M_INHERIT);
41 *handle = CreateFile(fn.c_str(), flags, share_flags, 0, create_flags, FILE_ATTRIBUTE_NORMAL, &sec_attr);
44 int err = GetLastError();
45 if(err==ERROR_FILE_NOT_FOUND)
46 throw file_not_found(fn);
47 else if(err==ERROR_FILE_EXISTS)
48 throw file_already_exists(fn);
50 throw system_error(format("CreateFile(%s)", fn), err);