]> git.tdb.fi Git - libs/core.git/blob - source/io/windows/file.cpp
Actually pass sec_attr to CreateFile
[libs/core.git] / source / io / windows / file.cpp
1 #include <msp/core/systemerror.h>
2 #include <msp/strings/format.h>
3 #include "file.h"
4 #include "handle_private.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace IO {
10
11 void File::platform_init(const string &fn, CreateMode cm)
12 {
13         int flags = 0;
14         int share_flags = 0;
15         int create_flags = OPEN_EXISTING;
16
17         if(mode&M_READ)
18                 flags |= GENERIC_READ;
19
20         if(mode&M_WRITE)
21         {
22                 flags |= GENERIC_WRITE;
23
24                 switch(static_cast<int>(cm))
25                 {
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;
31                 }
32         }
33         else
34                 share_flags = FILE_SHARE_READ;
35
36         SECURITY_ATTRIBUTES sec_attr;
37         sec_attr.nLength = sizeof(SECURITY_ATTRIBUTES);
38         sec_attr.lpSecurityDescriptor = 0;
39         sec_attr.bInheritHandle = !!(mode&M_INHERIT);
40
41         *handle = CreateFile(fn.c_str(), flags, share_flags, 0, create_flags, FILE_ATTRIBUTE_NORMAL, &sec_attr);
42         if(!handle)
43         {
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);
49                 else
50                         throw system_error(format("CreateFile(%s)", fn), err);
51         }
52 }
53
54 void File::sync()
55 {
56 }
57
58 } // namespace IO
59 } // namespace Msp