00001
00002
00003
00004
00005 #include "ace/OS_NS_unistd.h"
00006 #include "ace/OS_NS_fcntl.h"
00007 #include "ace/OS_NS_errno.h"
00008 #include "ace/OS_NS_macros.h"
00009
00010 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00011
00012 namespace ACE_OS
00013 {
00014
00015 ACE_INLINE ACE_HANDLE
00016 creat (const ACE_TCHAR *filename, mode_t mode)
00017 {
00018 ACE_OS_TRACE ("ACE_OS::creat");
00019 #if defined (ACE_WIN32)
00020 return ACE_OS::open (filename, O_CREAT|O_TRUNC|O_WRONLY, mode);
00021 #else
00022 ACE_OSCALL_RETURN (::creat (ACE_TEXT_ALWAYS_CHAR (filename), mode),
00023 ACE_HANDLE, ACE_INVALID_HANDLE);
00024 #endif
00025 }
00026
00027 #if !defined (ACE_WIN32)
00028
00029 ACE_INLINE int
00030 fstat (ACE_HANDLE handle, ACE_stat *stp)
00031 {
00032 ACE_OS_TRACE ("ACE_OS::fstat");
00033 #if defined (ACE_HAS_X86_STAT_MACROS)
00034
00035
00036
00037 ACE_OSCALL_RETURN (::_fxstat (_STAT_VER, handle, stp), int, -1);
00038 #elif defined (ACE_WIN32)
00039 ACE_OSCALL_RETURN (::_fstat (handle, stp), int, -1);
00040 #else
00041 # if defined (ACE_OPENVMS)
00042 ::fsync(handle);
00043 # endif
00044 ACE_OSCALL_RETURN (::fstat (handle, stp), int, -1);
00045 # endif
00046 }
00047
00048 #else
00049
00050 ACE_INLINE int
00051 fstat (ACE_HANDLE handle, ACE_stat *stp)
00052 {
00053 ACE_OS_TRACE ("ACE_OS::fstat");
00054 # if 1
00055 BY_HANDLE_FILE_INFORMATION fdata;
00056
00057 if (::GetFileInformationByHandle (handle, &fdata) == FALSE)
00058 {
00059 ACE_OS::set_errno_to_last_error ();
00060 return -1;
00061 }
00062 else if (fdata.nFileSizeHigh != 0)
00063 {
00064 errno = EINVAL;
00065 return -1;
00066 }
00067 else
00068 {
00069 stp->st_size = fdata.nFileSizeLow;
00070 stp->st_atime = ACE_Time_Value (fdata.ftLastAccessTime).sec ();
00071 stp->st_mtime = ACE_Time_Value (fdata.ftLastWriteTime).sec ();
00072 stp->st_ctime = ACE_Time_Value (fdata.ftCreationTime).sec ();
00073 stp->st_nlink = static_cast<short> (fdata.nNumberOfLinks);
00074 stp->st_dev = stp->st_rdev = 0;
00075 stp->st_mode = S_IXOTH | S_IROTH |
00076 (fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0 : S_IWOTH) |
00077 (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? S_IFDIR : S_IFREG);
00078 }
00079 return 0;
00080 # else
00081
00082 int retval = -1;
00083 int fd = ::_open_osfhandle ((long) handle, 0);
00084 if (fd != -1)
00085 retval = ::_fstat (fd, stp);
00086
00087 ::_close (fd);
00088
00089 return retval;
00090 # endif
00091 }
00092
00093 #endif
00094
00095
00096
00097
00098 ACE_INLINE ACE_OFF_T
00099 filesize (ACE_HANDLE handle)
00100 {
00101 ACE_OS_TRACE ("ACE_OS::filesize");
00102 #if defined (ACE_WIN32)
00103 # if defined (_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
00104 LARGE_INTEGER size;
00105 return
00106 (::GetFileSizeEx (handle, &size)
00107 ? size.QuadPart
00108 : (ACE_OS::set_errno_to_last_error (), -1));
00109 # else
00110 DWORD const size = ::GetFileSize (handle, 0);
00111 return
00112 (size != INVALID_FILE_SIZE
00113 ? static_cast<ACE_OFF_T> (size)
00114 : (ACE_OS::set_errno_to_last_error (), -1));
00115 # endif
00116 #else
00117 ACE_stat sb;
00118 return ACE_OS::fstat (handle, &sb) == -1 ?
00119 static_cast<ACE_OFF_T> (-1) : sb.st_size;
00120 #endif
00121 }
00122
00123 ACE_INLINE ACE_OFF_T
00124 filesize (const ACE_TCHAR *filename)
00125 {
00126 ACE_OS_TRACE ("ACE_OS::filesize");
00127
00128 ACE_HANDLE const h = ACE_OS::open (filename, O_RDONLY);
00129 if (h != ACE_INVALID_HANDLE)
00130 {
00131 ACE_OFF_T size = ACE_OS::filesize (h);
00132 ACE_OS::close (h);
00133 return size;
00134 }
00135 else
00136 return -1;
00137 }
00138
00139 ACE_INLINE int
00140 lstat (const char *file, ACE_stat *stp)
00141 {
00142 ACE_OS_TRACE ("ACE_OS::lstat");
00143 # if defined (ACE_LACKS_LSTAT)
00144 return ACE_OS::stat (file, stp);
00145 # elif defined (ACE_HAS_X86_STAT_MACROS)
00146
00147
00148 ACE_OSCALL_RETURN (::_lxstat (_STAT_VER, file, stp), int, -1);
00149 # else
00150 ACE_OSCALL_RETURN (::lstat (file, stp), int, -1);
00151 # endif
00152 }
00153
00154 #if defined (ACE_HAS_WCHAR)
00155 ACE_INLINE int
00156 lstat (const wchar_t *file, ACE_stat *stp)
00157 {
00158 ACE_OS_TRACE ("ACE_OS::lstat");
00159 # if defined (ACE_LACKS_LSTAT)
00160 return ACE_OS::stat (file, stp);
00161 # else
00162 return ACE_OS::lstat (ACE_Wide_To_Ascii (file).char_rep (), stp);
00163 # endif
00164 }
00165 #endif
00166
00167 ACE_INLINE int
00168 mkdir (const char *path, mode_t mode)
00169 {
00170 #if defined (ACE_HAS_WINCE)
00171 ACE_UNUSED_ARG (mode);
00172 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::CreateDirectory (ACE_TEXT_CHAR_TO_TCHAR (path), 0),
00173 ace_result_),
00174 int, -1);
00175 #elif defined (ACE_MKDIR_LACKS_MODE)
00176 ACE_UNUSED_ARG (mode);
00177 ACE_OSCALL_RETURN (::mkdir (path), int, -1);
00178 #else
00179 ACE_OSCALL_RETURN (::mkdir (path, mode), int, -1);
00180 #endif
00181 }
00182
00183 #if defined (ACE_HAS_WCHAR)
00184
00185 ACE_INLINE int
00186 mkdir (const wchar_t *path, mode_t mode)
00187 {
00188 #if defined (ACE_HAS_WINCE)
00189 ACE_UNUSED_ARG (mode);
00190 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (CreateDirectoryW (path, 0),
00191 ace_result_),
00192 int, -1);
00193 #elif defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
00194 ACE_UNUSED_ARG (mode);
00195 ACE_OSCALL_RETURN (::_wmkdir (path), int, -1);
00196 #else
00197 return ACE_OS::mkdir (ACE_Wide_To_Ascii (path).char_rep (), mode);
00198 #endif
00199 }
00200
00201 #endif
00202
00203 ACE_INLINE int
00204 mkfifo (const ACE_TCHAR *file, mode_t mode)
00205 {
00206 ACE_OS_TRACE ("ACE_OS::mkfifo");
00207 #if defined (ACE_LACKS_MKFIFO)
00208 ACE_UNUSED_ARG (file);
00209 ACE_UNUSED_ARG (mode);
00210 ACE_NOTSUP_RETURN (-1);
00211 #else
00212 ACE_OSCALL_RETURN (::mkfifo (ACE_TEXT_ALWAYS_CHAR (file), mode), int, -1);
00213 #endif
00214 }
00215
00216 ACE_INLINE int
00217 stat (const char *file, ACE_stat *stp)
00218 {
00219 ACE_OS_TRACE ("ACE_OS::stat");
00220 #if defined (ACE_HAS_NONCONST_STAT)
00221 ACE_OSCALL_RETURN (::stat (const_cast <char *> (file), stp), int, -1);
00222 #elif defined (ACE_HAS_WINCE)
00223 ACE_TEXT_WIN32_FIND_DATA fdata;
00224
00225 HANDLE fhandle;
00226
00227 fhandle = ::FindFirstFile (ACE_TEXT_CHAR_TO_TCHAR (file), &fdata);
00228 if (fhandle == INVALID_HANDLE_VALUE)
00229 {
00230 ACE_OS::set_errno_to_last_error ();
00231 return -1;
00232 }
00233 else if (fdata.nFileSizeHigh != 0)
00234 {
00235 errno = EINVAL;
00236 return -1;
00237 }
00238 else
00239 {
00240 stp->st_mode = static_cast<unsigned short>(fdata.dwFileAttributes);
00241 stp->st_size = fdata.nFileSizeLow;
00242 stp->st_atime = ACE_Time_Value (fdata.ftLastAccessTime);
00243 stp->st_mtime = ACE_Time_Value (fdata.ftLastWriteTime);
00244 }
00245 return 0;
00246 #elif defined (ACE_HAS_X86_STAT_MACROS)
00247
00248
00249 ACE_OSCALL_RETURN (::_xstat (_STAT_VER, file, stp), int, -1);
00250 #else
00251 ACE_OSCALL_RETURN (ACE_STAT_FUNC_NAME (file, stp), int, -1);
00252 #endif
00253 }
00254
00255 #if defined (ACE_HAS_WCHAR)
00256 ACE_INLINE int
00257 stat (const wchar_t *file, ACE_stat *stp)
00258 {
00259 ACE_OS_TRACE ("ACE_OS::stat");
00260 #if defined (ACE_HAS_WINCE)
00261 WIN32_FIND_DATAW fdata;
00262
00263 HANDLE fhandle;
00264
00265 fhandle = ::FindFirstFileW (file, &fdata);
00266 if (fhandle == INVALID_HANDLE_VALUE)
00267 {
00268 ACE_OS::set_errno_to_last_error ();
00269 return -1;
00270 }
00271 else if (fdata.nFileSizeHigh != 0)
00272 {
00273 errno = EINVAL;
00274 return -1;
00275 }
00276 else
00277 {
00278 stp->st_mode = static_cast<unsigned short>(fdata.dwFileAttributes);
00279 stp->st_size = fdata.nFileSizeLow;
00280 stp->st_atime = ACE_Time_Value (fdata.ftLastAccessTime);
00281 stp->st_mtime = ACE_Time_Value (fdata.ftLastWriteTime);
00282 }
00283 return 0;
00284 #elif defined (__BORLANDC__) \
00285 || (defined (_MSC_VER) && _MSC_VER >= 1300) \
00286 || defined (__MINGW32__)
00287 ACE_OSCALL_RETURN (ACE_WSTAT_FUNC_NAME (file, stp), int, -1);
00288 #else
00289 ACE_Wide_To_Ascii nfile (file);
00290 return ACE_OS::stat (nfile.char_rep (), stp);
00291 #endif
00292 }
00293 #endif
00294
00295 ACE_INLINE mode_t
00296 umask (mode_t cmask)
00297 {
00298 ACE_OS_TRACE ("ACE_OS::umask");
00299 # if defined (ACE_LACKS_UMASK)
00300 ACE_UNUSED_ARG (cmask);
00301 ACE_NOTSUP_RETURN ((mode_t)-1);
00302 # elif defined (ACE_HAS_TR24731_2005_CRT)
00303 mode_t old_mode;
00304 ACE_SECURECRTCALL (_umask_s (cmask, &old_mode), mode_t, -1, old_mode);
00305 return old_mode;
00306 # elif defined (ACE_WIN32) && !defined (__BORLANDC__)
00307 ACE_OSCALL_RETURN (::_umask (cmask), mode_t, -1);
00308 # else
00309 return ::umask (cmask);
00310 # endif
00311 }
00312
00313 }
00314
00315 ACE_END_VERSIONED_NAMESPACE_DECL