이미 열린 파일은 닫고, 파일을 새로 연다.
82{
85
87#ifdef __WINNT__
88 __fileType = UNKNOWN;
91
92 FileType fileType = UNKNOWN;
93
94 HANDLE
handle = CreateFileW(_path, GENERIC_READ, 0,
NULL, OPEN_EXISTING, 0,
NULL);
96 DWORD dw;
97 DCB dcb;
99 fileType = PIPE;
100 else if (GetConsoleMode(
handle, &dw))
101 fileType = CONSOLE;
102 else if (GetCommState(
handle, &dcb))
103 fileType = COMM;
104 else
105 fileType = REGULAR;
106
109 }
110
111 DWORD dwDesiredAccess = GENERIC_READ;
112 DWORD dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
113 SECURITY_ATTRIBUTES securityAttributes = {
sizeof(SECURITY_ATTRIBUTES),
NULL,
TRUE };
114 DWORD dwCreationDisposition = OPEN_EXISTING;
115 DWORD dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
116
117 switch (_oflags & (READONLY | WRITEONLY | READWRITE)) {
118 default:
119
120 case READONLY:
121 dwDesiredAccess = GENERIC_READ;
122 break;
123 case WRITEONLY:
124 if (_oflags & APPEND)
125 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
126 else
127 dwDesiredAccess = GENERIC_WRITE;
128 break;
129 case READWRITE:
130 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
131 break;
132 }
133
134 if (_oflags & EXCLUSIVE) {
135 dwShareMode = 0;
136 }
137
138 switch (_oflags & (CREATE | EXCLUSIVE | TRUNCATE)) {
139 default:
140 case EXCLUSIVE:
141 dwCreationDisposition = OPEN_EXISTING;
142 break;
143 case CREATE:
144 case CREATE | TRUNCATE:
145 dwCreationDisposition = OPEN_ALWAYS;
146 break;
147 case CREATE | EXCLUSIVE:
148 case CREATE | EXCLUSIVE | TRUNCATE:
149 dwCreationDisposition = CREATE_NEW;
150 break;
151 case TRUNCATE:
152 case TRUNCATE | EXCLUSIVE:
153 dwCreationDisposition = TRUNCATE_EXISTING;
154 break;
155 }
156
157 if (_oflags & NONBLOCK)
158 dwFlagsAndAttributes |= FILE_FLAG_OVERLAPPED;
159
160 if (_oflags & SYNC)
161 dwFlagsAndAttributes |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
162
163 if (fileType == CONSOLE || fileType == PIPE) {
164
165 handle = CreateFileW(_path, dwDesiredAccess, dwShareMode, &securityAttributes,
166 OPEN_EXISTING, dwFlagsAndAttributes,
NULL);
167 }
168 else if (fileType == COMM) {
169 handle = CreateFileW(_path, dwDesiredAccess, dwShareMode, &securityAttributes,
170 OPEN_EXISTING, dwFlagsAndAttributes,
NULL);
171 }
172 else {
173
174 switch (dwDesiredAccess & (GENERIC_READ | GENERIC_WRITE)) {
175 case GENERIC_READ:
176 dwFlagsAndAttributes |= FILE_FLAG_SEQUENTIAL_SCAN;
177 break;
178 case GENERIC_READ | GENERIC_WRITE:
179 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
180 break;
181 }
182
183 if ((_oflags & TRUNCATE) && ((dwDesiredAccess & GENERIC_WRITE) == GENERIC_WRITE))
184 handle = CreateFileW(_path, dwDesiredAccess, dwShareMode,
185 &securityAttributes, TRUNCATE_EXISTING, dwFlagsAndAttributes,
NULL);
186
188 handle = CreateFileW(_path, dwDesiredAccess, dwShareMode,
189 &securityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
NULL);
190
192 && (_oflags & APPEND) && ((dwDesiredAccess & GENERIC_READ) == GENERIC_READ)) {
193 fileType = REGULAR;
194 if (SetFilePointer(
handle, 0,
NULL, FILE_END) == INVALID_SET_FILE_POINTER) {
195 DWORD dwSaveError = GetLastError();
198 SetLastError(dwSaveError);
199 }
200 }
201 }
202
203
204 HANDLE readEvent =
NULL, writeEvent =
NULL;
206 __DCL_ASSERT(dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED);
207 if (_oflags & (WRITEONLY | READWRITE))
209
210
211 if (_oflags & (READONLY | READWRITE) || writeEvent ==
NULL)
213
214 if (readEvent ==
NULL && writeEvent ==
NULL) {
215 DWORD dwSaveError = GetLastError();
216 if (readEvent)
217 CloseHandle(readEvent);
218 if (writeEvent)
219 CloseHandle(writeEvent);
220
223 SetLastError(dwSaveError);
224 }
225 }
226
228 throw new IOException(_path, GetLastError());
229
230 __fileType = fileType;
231 __readEvent = readEvent;
232 __writeEvent = writeEvent;
233#else
234 int oflags = 0;
235 if (_oflags & READONLY)
236 oflags |= O_RDONLY;
237 if (_oflags & WRITEONLY)
238 oflags |= O_WRONLY;
239 if (_oflags & READWRITE)
240 oflags |= O_RDWR;
241 if (_oflags & CREATE)
242 oflags |= O_CREAT;
243 if (_oflags & EXCLUSIVE)
244 oflags |= O_EXCL;
245 if (_oflags & NOCTTY)
246 oflags |= O_NOCTTY;
247 if (_oflags & APPEND)
248 oflags |= O_APPEND;
249 if (_oflags & TRUNCATE)
250 oflags |= O_TRUNC;
251 if (_oflags & NONBLOCK)
252 oflags |= O_NONBLOCK;
253 if (_oflags & SYNC)
254 oflags |= O_SYNC;
255
258 throw new IOException(_path, errno);
259#endif
262}
DCLCAPI int __open(const String &_path, int _oflags,...)