summaryrefslogtreecommitdiff
path: root/ACE/ace/OS_NS_sys_stat.inl
blob: 321f2caf7aefc1e55ded8f79dde8b26d01948502 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// -*- C++ -*-
//
// $Id$

#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_fcntl.h"
#include "ace/OS_NS_errno.h"
#include "ace/OS_NS_macros.h"

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE_OS
{

  ACE_INLINE ACE_HANDLE
  creat (const ACE_TCHAR *filename, mode_t mode)
  {
    ACE_OS_TRACE ("ACE_OS::creat");
#if defined (ACE_WIN32)
    return ACE_OS::open (filename, O_CREAT|O_TRUNC|O_WRONLY, mode);
#else
    ACE_OSCALL_RETURN (::creat (ACE_TEXT_ALWAYS_CHAR (filename), mode),
                       ACE_HANDLE, ACE_INVALID_HANDLE);
#endif /* ACE_WIN32 */
  }

#if !defined (ACE_WIN32)

  ACE_INLINE int
  fstat (ACE_HANDLE handle, ACE_stat *stp)
  {
    ACE_OS_TRACE ("ACE_OS::fstat");
#if defined (ACE_HAS_X86_STAT_MACROS)
    // Solaris for intel uses an macro for fstat(), this is a wrapper
    // for _fxstat() use of the macro.
    // causes compile and runtime problems.
    ACE_OSCALL_RETURN (::_fxstat (_STAT_VER, handle, stp), int, -1);
#elif defined (ACE_WIN32)
    ACE_OSCALL_RETURN (::_fstat (handle, stp), int, -1);
#else
# if defined (ACE_OPENVMS)
    ::fsync(handle);
# endif
    ACE_OSCALL_RETURN (::fstat (handle, stp), int, -1);
# endif /* !ACE_HAS_X86_STAT_MACROS */
  }

#else /* ACE_WIN32 */

  ACE_INLINE int
  fstat (ACE_HANDLE handle, ACE_stat *stp)
  {
    ACE_OS_TRACE ("ACE_OS::fstat");
# if 1
    BY_HANDLE_FILE_INFORMATION fdata;

    if (::GetFileInformationByHandle (handle, &fdata) == FALSE)
      {
        ACE_OS::set_errno_to_last_error ();
        return -1;
      }
    else if (fdata.nFileSizeHigh != 0)
      {
        errno = EINVAL;
        return -1;
      }
    else
      {
        stp->st_size = fdata.nFileSizeLow;
        stp->st_atime = ACE_Time_Value (fdata.ftLastAccessTime).sec ();
        stp->st_mtime = ACE_Time_Value (fdata.ftLastWriteTime).sec ();
        stp->st_ctime = ACE_Time_Value (fdata.ftCreationTime).sec ();
        stp->st_nlink = static_cast<short> (fdata.nNumberOfLinks);
        stp->st_dev = stp->st_rdev = 0; // No equivalent conversion.
        stp->st_mode = S_IXOTH | S_IROTH |
          (fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0 : S_IWOTH) |
          (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? S_IFDIR : S_IFREG);
      }
    return 0;
# else /* 1 */
    // This implementation close the handle.
    int retval = -1;
    int fd = ::_open_osfhandle ((long) handle, 0);
    if (fd != -1)
      retval = ::_fstat (fd, stp);

    ::_close (fd);
    // Remember to close the file handle.
    return retval;
# endif /* 1 */
  }

#endif /* WIN32 */

  // This function returns the number of bytes in the file referenced by
  // FD.

  ACE_INLINE ACE_OFF_T
  filesize (ACE_HANDLE handle)
  {
    ACE_OS_TRACE ("ACE_OS::filesize");
#if defined (ACE_WIN32)
# if defined (_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
    LARGE_INTEGER size;
    return
      (::GetFileSizeEx (handle, &size)
       ? size.QuadPart
       : (ACE_OS::set_errno_to_last_error (), -1));
# else
    DWORD const size = ::GetFileSize (handle, 0);
    return
      (size != INVALID_FILE_SIZE
       ? static_cast<ACE_OFF_T> (size)
       : (ACE_OS::set_errno_to_last_error (), -1));
# endif  /* _FILE_OFFSET_BITS == 64 */
#else /* !ACE_WIN32 */
    ACE_stat sb;
    return ACE_OS::fstat (handle, &sb) == -1 ? -1 : sb.st_size;
#endif
  }

  ACE_INLINE ACE_OFF_T
  filesize (const ACE_TCHAR *filename)
  {
    ACE_OS_TRACE ("ACE_OS::filesize");

    ACE_HANDLE const h = ACE_OS::open (filename, O_RDONLY);
    if (h != ACE_INVALID_HANDLE)
      {
        ACE_OFF_T size = ACE_OS::filesize (h);
        ACE_OS::close (h);
        return size;
      }
    else
      return -1;
  }

  ACE_INLINE int
  lstat (const char *file, ACE_stat *stp)
  {
    ACE_OS_TRACE ("ACE_OS::lstat");
# if defined (ACE_LACKS_LSTAT)
    return ACE_OS::stat (file, stp);
# elif defined (ACE_HAS_X86_STAT_MACROS)
    // Solaris for intel uses an macro for lstat(), this macro is a
    // wrapper for _lxstat().
    ACE_OSCALL_RETURN (::_lxstat (_STAT_VER, file, stp), int, -1);
# else /* !ACE_HAS_X86_STAT_MACROS */
    ACE_OSCALL_RETURN (::lstat (file, stp), int, -1);
# endif /* ACE_LACKS_LSTAT */
  }

#if defined (ACE_HAS_WCHAR)
  ACE_INLINE int
  lstat (const wchar_t *file, ACE_stat *stp)
  {
    ACE_OS_TRACE ("ACE_OS::lstat");
# if defined (ACE_LACKS_LSTAT)
    return ACE_OS::stat (file, stp);
# else
    return ACE_OS::lstat (ACE_Wide_To_Ascii (file).char_rep (), stp);
# endif /* ACE_LACKS_LSTAT */
  }
#endif /* ACE_HAS_WCHAR */

  ACE_INLINE int
  mkdir (const char *path, mode_t mode)
  {
#if defined (ACE_WIN32) && defined (__IBMCPP__) && (__IBMCPP__ >= 400)
    ACE_UNUSED_ARG (mode);
    ACE_OSCALL_RETURN (::_mkdir (const_cast <char *> (path)), int, -1);
#elif defined (ACE_HAS_WINCE)
    ACE_UNUSED_ARG (mode);
    ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::CreateDirectory (ACE_TEXT_CHAR_TO_TCHAR (path), 0),
                                            ace_result_),
                          int, -1);
#elif defined (ACE_MKDIR_LACKS_MODE)
    ACE_UNUSED_ARG (mode);
    ACE_OSCALL_RETURN (::mkdir (path), int, -1);
#else
    ACE_OSCALL_RETURN (::mkdir (path, mode), int, -1);
#endif
  }

#if defined (ACE_HAS_WCHAR)

  ACE_INLINE int
  mkdir (const wchar_t *path, mode_t mode)
  {
#if defined (ACE_HAS_WINCE)
    ACE_UNUSED_ARG (mode);
    ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (CreateDirectoryW (path, 0),
                                            ace_result_),
                          int, -1);
#elif defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
    ACE_UNUSED_ARG (mode);
    ACE_OSCALL_RETURN (::_wmkdir (path), int, -1);
#else
    return ACE_OS::mkdir (ACE_Wide_To_Ascii (path).char_rep (), mode);
#endif /* ACE_HAS_WINCE */
  }

#endif /* ACE_HAS_WCHAR */

  ACE_INLINE int
  mkfifo (const ACE_TCHAR *file, mode_t mode)
  {
    ACE_OS_TRACE ("ACE_OS::mkfifo");
#if defined (ACE_LACKS_MKFIFO)
    ACE_UNUSED_ARG (file);
    ACE_UNUSED_ARG (mode);
    ACE_NOTSUP_RETURN (-1);
#else
    ACE_OSCALL_RETURN (::mkfifo (ACE_TEXT_ALWAYS_CHAR (file), mode), int, -1);
#endif /* ACE_LACKS_MKFIFO */
  }

  ACE_INLINE int
  stat (const char *file, ACE_stat *stp)
  {
    ACE_OS_TRACE ("ACE_OS::stat");
#if defined (ACE_HAS_NONCONST_STAT)
    ACE_OSCALL_RETURN (::stat (const_cast <char *> (file), stp), int, -1);
#elif defined (ACE_HAS_WINCE)
    ACE_TEXT_WIN32_FIND_DATA fdata;

    HANDLE fhandle;

    fhandle = ::FindFirstFile (ACE_TEXT_CHAR_TO_TCHAR (file), &fdata);
    if (fhandle == INVALID_HANDLE_VALUE)
      {
        ACE_OS::set_errno_to_last_error ();
        return -1;
      }
    else if (fdata.nFileSizeHigh != 0)
      {
        errno = EINVAL;
        return -1;
      }
    else
      {
        stp->st_mode = static_cast<unsigned short>(fdata.dwFileAttributes);
        stp->st_size = fdata.nFileSizeLow;
        stp->st_atime = ACE_Time_Value (fdata.ftLastAccessTime);
        stp->st_mtime = ACE_Time_Value (fdata.ftLastWriteTime);
      }
    return 0;
#elif defined (ACE_HAS_X86_STAT_MACROS)
    // Solaris for intel uses an macro for stat(), this macro is a
    // wrapper for _xstat().
    ACE_OSCALL_RETURN (::_xstat (_STAT_VER, file, stp), int, -1);
#else
    ACE_OSCALL_RETURN (ACE_STAT_FUNC_NAME (file, stp), int, -1);
#endif /* ACE_HAS_NONCONST_STAT */
  }

#if defined (ACE_HAS_WCHAR)
  ACE_INLINE int
  stat (const wchar_t *file, ACE_stat *stp)
  {
    ACE_OS_TRACE ("ACE_OS::stat");
#if defined (ACE_HAS_WINCE)
    WIN32_FIND_DATAW fdata;

    HANDLE fhandle;

    fhandle = ::FindFirstFileW (file, &fdata);
    if (fhandle == INVALID_HANDLE_VALUE)
      {
        ACE_OS::set_errno_to_last_error ();
        return -1;
      }
    else if (fdata.nFileSizeHigh != 0)
      {
        errno = EINVAL;
        return -1;
      }
    else
      {
        stp->st_mode = static_cast<unsigned short>(fdata.dwFileAttributes);
        stp->st_size = fdata.nFileSizeLow;
        stp->st_atime = ACE_Time_Value (fdata.ftLastAccessTime);
        stp->st_mtime = ACE_Time_Value (fdata.ftLastWriteTime);
      }
    return 0;
#elif defined (__BORLANDC__) \
      || (defined (_MSC_VER) && _MSC_VER >= 1300) \
      || defined (__MINGW32__)
    ACE_OSCALL_RETURN (ACE_WSTAT_FUNC_NAME (file, stp), int, -1);
#else /* ACE_HAS_WINCE */
    ACE_Wide_To_Ascii nfile (file);
    return ACE_OS::stat (nfile.char_rep (), stp);
#endif /* ACE_HAS_WINCE */
  }
#endif /* ACE_HAS_WCHAR */

  ACE_INLINE mode_t
  umask (mode_t cmask)
  {
    ACE_OS_TRACE ("ACE_OS::umask");
# if defined (ACE_LACKS_UMASK)
    ACE_UNUSED_ARG (cmask);
    ACE_NOTSUP_RETURN ((mode_t)-1);
# elif defined (ACE_WIN32) && !defined (__BORLANDC__)
    ACE_OSCALL_RETURN (::_umask (cmask), mode_t, -1);
# else
    return ::umask (cmask); // This call shouldn't fail...
# endif /* ACE_LACKS_UMASK */
  }

} // ACE_OS namespace

ACE_END_VERSIONED_NAMESPACE_DECL