1 #include "ace/OS_NS_fcntl.h"
3 #if !defined (ACE_HAS_INLINED_OSCALLS)
4 # include "ace/OS_NS_fcntl.inl"
5 #endif /* ACE_HAS_INLINED_OSCALLS */
7 #include "ace/OS_NS_stdio.h"
8 #include "ace/OS_NS_Thread.h"
9 #include "ace/OS_NS_macros.h"
10 #include "ace/Object_Manager_Base.h"
12 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
15 ACE_OS::open (const char *filename
,
18 LPSECURITY_ATTRIBUTES sa
)
20 ACE_OS_TRACE ("ACE_OS::open");
22 #if defined (ACE_WIN32)
23 DWORD access
= GENERIC_READ
;
24 if (ACE_BIT_ENABLED (mode
, O_WRONLY
))
25 access
= GENERIC_WRITE
;
26 else if (ACE_BIT_ENABLED (mode
, O_RDWR
))
27 access
= GENERIC_READ
| GENERIC_WRITE
;
29 DWORD creation
= OPEN_EXISTING
;
31 if ((mode
& (_O_CREAT
| _O_EXCL
)) == (_O_CREAT
| _O_EXCL
))
32 creation
= CREATE_NEW
;
33 else if ((mode
& (_O_CREAT
| _O_TRUNC
)) == (_O_CREAT
| _O_TRUNC
))
34 creation
= CREATE_ALWAYS
;
35 else if (ACE_BIT_ENABLED (mode
, _O_CREAT
))
36 creation
= OPEN_ALWAYS
;
37 else if (ACE_BIT_ENABLED (mode
, _O_TRUNC
))
38 creation
= TRUNCATE_EXISTING
;
42 if (ACE_BIT_ENABLED (mode
, _O_TEMPORARY
))
43 flags
|= FILE_FLAG_DELETE_ON_CLOSE
| FILE_ATTRIBUTE_TEMPORARY
;
45 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_WRITE_THROUGH
))
46 flags
|= FILE_FLAG_WRITE_THROUGH
;
47 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_OVERLAPPED
))
48 flags
|= FILE_FLAG_OVERLAPPED
;
49 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_NO_BUFFERING
))
50 flags
|= FILE_FLAG_NO_BUFFERING
;
51 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_RANDOM_ACCESS
))
52 flags
|= FILE_FLAG_RANDOM_ACCESS
;
53 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_SEQUENTIAL_SCAN
))
54 flags
|= FILE_FLAG_SEQUENTIAL_SCAN
;
55 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_DELETE_ON_CLOSE
))
56 flags
|= FILE_FLAG_DELETE_ON_CLOSE
;
57 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_BACKUP_SEMANTICS
))
58 flags
|= FILE_FLAG_BACKUP_SEMANTICS
;
59 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_POSIX_SEMANTICS
))
60 flags
|= FILE_FLAG_POSIX_SEMANTICS
;
62 ACE_MT (ACE_thread_mutex_t
*ace_os_monitor_lock
= 0;)
64 if (ACE_BIT_ENABLED (mode
, _O_APPEND
))
68 ace_os_monitor_lock
= static_cast <ACE_thread_mutex_t
*> (
69 ACE_OS_Object_Manager::preallocated_object
[
70 ACE_OS_Object_Manager::ACE_OS_MONITOR_LOCK
]);
71 ACE_OS::thread_mutex_lock (ace_os_monitor_lock
);
75 DWORD shared_mode
= perms
;
76 SECURITY_ATTRIBUTES sa_buffer
;
77 SECURITY_DESCRIPTOR sd_buffer
;
79 ACE_HANDLE h
= ::CreateFileA (filename
,
82 ACE_OS::default_win32_security_attributes_r
83 (sa
, &sa_buffer
, &sd_buffer
),
88 if (ACE_BIT_ENABLED (mode
, _O_APPEND
))
91 if (h
!= ACE_INVALID_HANDLE
92 && ::SetFilePointer (h
,
95 FILE_END
) == INVALID_SET_FILE_POINTER
96 && GetLastError () != NO_ERROR
)
98 ACE_MT (ACE_OS::thread_mutex_unlock (ace_os_monitor_lock
);)
99 ACE_FAIL_RETURN (ACE_INVALID_HANDLE
);
102 ACE_MT (ACE_OS::thread_mutex_unlock (ace_os_monitor_lock
);)
105 if (h
== ACE_INVALID_HANDLE
)
109 #elif defined (INTEGRITY)
111 if(!strcmp(filename
,ACE_DEV_NULL
)) {
112 return ::AllocateNullConsoleDescriptor();
115 return ::open (filename
, mode
, perms
);
117 #elif defined (ACE_MQX)
118 ACE_UNUSED_ARG (perms
);
120 return MQX_Filesystem::inst ().open (filename
, mode
);
123 return ::open (filename
, mode
, perms
);
124 #endif /* ACE_WIN32 */
127 #if defined (ACE_HAS_WCHAR)
129 ACE_OS::open (const wchar_t *filename
,
132 LPSECURITY_ATTRIBUTES sa
)
134 #if defined (ACE_WIN32)
135 // @@ (brunsch) Yuck, maybe there is a way to combine the code
136 // here with the char version
138 DWORD access
= GENERIC_READ
;
139 if (ACE_BIT_ENABLED (mode
, O_WRONLY
))
140 access
= GENERIC_WRITE
;
141 else if (ACE_BIT_ENABLED (mode
, O_RDWR
))
142 access
= GENERIC_READ
| GENERIC_WRITE
;
144 DWORD creation
= OPEN_EXISTING
;
146 if ((mode
& (_O_CREAT
| _O_EXCL
)) == (_O_CREAT
| _O_EXCL
))
147 creation
= CREATE_NEW
;
148 else if ((mode
& (_O_CREAT
| _O_TRUNC
)) == (_O_CREAT
| _O_TRUNC
))
149 creation
= CREATE_ALWAYS
;
150 else if (ACE_BIT_ENABLED (mode
, _O_CREAT
))
151 creation
= OPEN_ALWAYS
;
152 else if (ACE_BIT_ENABLED (mode
, _O_TRUNC
))
153 creation
= TRUNCATE_EXISTING
;
157 if (ACE_BIT_ENABLED (mode
, _O_TEMPORARY
))
158 flags
|= FILE_FLAG_DELETE_ON_CLOSE
| FILE_ATTRIBUTE_TEMPORARY
;
160 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_WRITE_THROUGH
))
161 flags
|= FILE_FLAG_WRITE_THROUGH
;
162 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_OVERLAPPED
))
163 flags
|= FILE_FLAG_OVERLAPPED
;
164 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_NO_BUFFERING
))
165 flags
|= FILE_FLAG_NO_BUFFERING
;
166 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_RANDOM_ACCESS
))
167 flags
|= FILE_FLAG_RANDOM_ACCESS
;
168 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_SEQUENTIAL_SCAN
))
169 flags
|= FILE_FLAG_SEQUENTIAL_SCAN
;
170 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_DELETE_ON_CLOSE
))
171 flags
|= FILE_FLAG_DELETE_ON_CLOSE
;
172 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_BACKUP_SEMANTICS
))
173 flags
|= FILE_FLAG_BACKUP_SEMANTICS
;
174 if (ACE_BIT_ENABLED (mode
, FILE_FLAG_POSIX_SEMANTICS
))
175 flags
|= FILE_FLAG_POSIX_SEMANTICS
;
177 ACE_MT (ACE_thread_mutex_t
*ace_os_monitor_lock
= 0;)
179 if (ACE_BIT_ENABLED (mode
, _O_APPEND
))
183 ace_os_monitor_lock
= static_cast <ACE_thread_mutex_t
*> (
184 ACE_OS_Object_Manager::preallocated_object
[
185 ACE_OS_Object_Manager::ACE_OS_MONITOR_LOCK
]);
186 ACE_OS::thread_mutex_lock (ace_os_monitor_lock
);
190 DWORD shared_mode
= perms
;
191 SECURITY_ATTRIBUTES sa_buffer
;
192 SECURITY_DESCRIPTOR sd_buffer
;
194 ACE_HANDLE h
= ::CreateFileW (filename
,
197 ACE_OS::default_win32_security_attributes_r
198 (sa
, &sa_buffer
, &sd_buffer
),
203 if (ACE_BIT_ENABLED (mode
, _O_APPEND
))
206 if (h
!= ACE_INVALID_HANDLE
207 && ::SetFilePointer (h
,
210 FILE_END
) == INVALID_SET_FILE_POINTER
211 && GetLastError () != NO_ERROR
)
213 ACE_MT (ACE_OS::thread_mutex_unlock (ace_os_monitor_lock
);)
214 ACE_FAIL_RETURN (ACE_INVALID_HANDLE
);
217 ACE_MT (ACE_OS::thread_mutex_unlock (ace_os_monitor_lock
);)
220 if (h
== ACE_INVALID_HANDLE
)
224 #else /* ACE_WIN32 */
225 // Just emulate with ascii version
226 return ACE_OS::open (ACE_Wide_To_Ascii (filename
).char_rep (),
230 #endif /* ACE_WIN32 */
232 #endif /* ACE_HAS_WCHAR */
234 ACE_END_VERSIONED_NAMESPACE_DECL