OS_NS_sys_stat.inl

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // $Id: OS_NS_sys_stat.inl 80826 2008-03-04 14:51:23Z wotte $
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 /* ACE_WIN32 */
00025   }
00026 
00027   ACE_INLINE int
00028   fstat (ACE_HANDLE handle, ACE_stat *stp)
00029   {
00030     ACE_OS_TRACE ("ACE_OS::fstat");
00031 #if defined (ACE_HAS_X86_STAT_MACROS)
00032     // Solaris for intel uses an macro for fstat(), this is a wrapper
00033     // for _fxstat() use of the macro.
00034     // causes compile and runtime problems.
00035     ACE_OSCALL_RETURN (::_fxstat (_STAT_VER, handle, stp), int, -1);
00036 #elif defined (ACE_WIN32)
00037     BY_HANDLE_FILE_INFORMATION fdata;
00038 
00039     if (::GetFileInformationByHandle (handle, &fdata) == FALSE)
00040       {
00041         ACE_OS::set_errno_to_last_error ();
00042         return -1;
00043       }
00044     else if (fdata.nFileSizeHigh != 0)
00045       {
00046         errno = EINVAL;
00047         return -1;
00048       }
00049     else
00050       {
00051         stp->st_size = fdata.nFileSizeLow;
00052         stp->st_atime = ACE_Time_Value (fdata.ftLastAccessTime).sec ();
00053         stp->st_mtime = ACE_Time_Value (fdata.ftLastWriteTime).sec ();
00054         stp->st_ctime = ACE_Time_Value (fdata.ftCreationTime).sec ();
00055         stp->st_nlink = static_cast<short> (fdata.nNumberOfLinks);
00056         stp->st_dev = stp->st_rdev = 0; // No equivalent conversion.
00057         stp->st_mode = S_IXOTH | S_IROTH |
00058           (fdata.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? 0 : S_IWOTH) |
00059           (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? S_IFDIR : S_IFREG);
00060       }
00061     return 0;
00062 #else
00063 # if defined (ACE_OPENVMS)
00064     //FUZZ: disable check_for_lack_ACE_OS
00065     ::fsync(handle);
00066     //FUZZ: enable check_for_lack_ACE_OS
00067  #endif
00068     ACE_OSCALL_RETURN (::fstat (handle, stp), int, -1);
00069 # endif /* !ACE_HAS_X86_STAT_MACROS */
00070   }
00071 
00072   // This function returns the number of bytes in the file referenced by
00073   // FD.
00074 
00075   ACE_INLINE ACE_OFF_T
00076   filesize (ACE_HANDLE handle)
00077   {
00078     ACE_OS_TRACE ("ACE_OS::filesize");
00079 #if defined (ACE_WIN32)
00080 # if defined (_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
00081     LARGE_INTEGER size;
00082     return
00083       (::GetFileSizeEx (handle, &size)
00084        ? size.QuadPart
00085        : (ACE_OS::set_errno_to_last_error (), -1));
00086 # else
00087     DWORD const size = ::GetFileSize (handle, 0);
00088     return
00089       (size != INVALID_FILE_SIZE
00090        ? static_cast<ACE_OFF_T> (size)
00091        : (ACE_OS::set_errno_to_last_error (), -1));
00092 # endif  /* _FILE_OFFSET_BITS == 64 */
00093 #else /* !ACE_WIN32 */
00094     ACE_stat sb;
00095     return ACE_OS::fstat (handle, &sb) == -1 ?
00096                     static_cast<ACE_OFF_T> (-1) : sb.st_size;
00097 #endif
00098   }
00099 
00100   ACE_INLINE ACE_OFF_T
00101   filesize (const ACE_TCHAR *filename)
00102   {
00103     ACE_OS_TRACE ("ACE_OS::filesize");
00104 
00105     ACE_HANDLE const h = ACE_OS::open (filename, O_RDONLY);
00106     if (h != ACE_INVALID_HANDLE)
00107       {
00108         ACE_OFF_T size = ACE_OS::filesize (h);
00109         ACE_OS::close (h);
00110         return size;
00111       }
00112     else
00113       return -1;
00114   }
00115 
00116   ACE_INLINE int
00117   lstat (const char *file, ACE_stat *stp)
00118   {
00119     ACE_OS_TRACE ("ACE_OS::lstat");
00120 # if defined (ACE_LACKS_LSTAT)
00121     return ACE_OS::stat (file, stp);
00122 # elif defined (ACE_HAS_X86_STAT_MACROS)
00123     // Solaris for intel uses an macro for lstat(), this macro is a
00124     // wrapper for _lxstat().
00125     ACE_OSCALL_RETURN (::_lxstat (_STAT_VER, file, stp), int, -1);
00126 # else /* !ACE_HAS_X86_STAT_MACROS */
00127     ACE_OSCALL_RETURN (::lstat (file, stp), int, -1);
00128 # endif /* ACE_LACKS_LSTAT */
00129   }
00130 
00131 #if defined (ACE_HAS_WCHAR)
00132   ACE_INLINE int
00133   lstat (const wchar_t *file, ACE_stat *stp)
00134   {
00135     ACE_OS_TRACE ("ACE_OS::lstat");
00136 # if defined (ACE_LACKS_LSTAT)
00137     return ACE_OS::stat (file, stp);
00138 # else
00139     return ACE_OS::lstat (ACE_Wide_To_Ascii (file).char_rep (), stp);
00140 # endif /* ACE_LACKS_LSTAT */
00141   }
00142 #endif /* ACE_HAS_WCHAR */
00143 
00144   ACE_INLINE int
00145   mkdir (const char *path, mode_t mode)
00146   {
00147 #if defined (ACE_HAS_WINCE)
00148     ACE_UNUSED_ARG (mode);
00149     ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::CreateDirectory (ACE_TEXT_CHAR_TO_TCHAR (path), 0),
00150                                             ace_result_),
00151                           int, -1);
00152 #elif defined (ACE_MKDIR_LACKS_MODE)
00153     ACE_UNUSED_ARG (mode);
00154     ACE_OSCALL_RETURN (::mkdir (path), int, -1);
00155 #else
00156     ACE_OSCALL_RETURN (::mkdir (path, mode), int, -1);
00157 #endif
00158   }
00159 
00160 #if defined (ACE_HAS_WCHAR)
00161 
00162   ACE_INLINE int
00163   mkdir (const wchar_t *path, mode_t mode)
00164   {
00165 #if defined (ACE_HAS_WINCE)
00166     ACE_UNUSED_ARG (mode);
00167     ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (CreateDirectoryW (path, 0),
00168                                             ace_result_),
00169                           int, -1);
00170 #elif defined (ACE_WIN32) && defined (ACE_USES_WCHAR)
00171     ACE_UNUSED_ARG (mode);
00172     ACE_OSCALL_RETURN (::_wmkdir (path), int, -1);
00173 #else
00174     return ACE_OS::mkdir (ACE_Wide_To_Ascii (path).char_rep (), mode);
00175 #endif /* ACE_HAS_WINCE */
00176   }
00177 
00178 #endif /* ACE_HAS_WCHAR */
00179 
00180   ACE_INLINE int
00181   mkfifo (const ACE_TCHAR *file, mode_t mode)
00182   {
00183     ACE_OS_TRACE ("ACE_OS::mkfifo");
00184 #if defined (ACE_LACKS_MKFIFO)
00185     ACE_UNUSED_ARG (file);
00186     ACE_UNUSED_ARG (mode);
00187     ACE_NOTSUP_RETURN (-1);
00188 #else
00189     ACE_OSCALL_RETURN (::mkfifo (ACE_TEXT_ALWAYS_CHAR (file), mode), int, -1);
00190 #endif /* ACE_LACKS_MKFIFO */
00191   }
00192 
00193   ACE_INLINE int
00194   stat (const char *file, ACE_stat *stp)
00195   {
00196     ACE_OS_TRACE ("ACE_OS::stat");
00197 #if defined (ACE_HAS_NONCONST_STAT)
00198     ACE_OSCALL_RETURN (::stat (const_cast <char *> (file), stp), int, -1);
00199 #elif defined (ACE_HAS_WINCE)
00200     ACE_TEXT_WIN32_FIND_DATA fdata;
00201 
00202     HANDLE fhandle;
00203 
00204     fhandle = ::FindFirstFile (ACE_TEXT_CHAR_TO_TCHAR (file), &fdata);
00205     if (fhandle == INVALID_HANDLE_VALUE)
00206       {
00207         ACE_OS::set_errno_to_last_error ();
00208         return -1;
00209       }
00210     else if (fdata.nFileSizeHigh != 0)
00211       {
00212         errno = EINVAL;
00213         return -1;
00214       }
00215     else
00216       {
00217         stp->st_mode = static_cast<unsigned short>(fdata.dwFileAttributes);
00218         stp->st_size = fdata.nFileSizeLow;
00219         stp->st_atime = ACE_Time_Value (fdata.ftLastAccessTime);
00220         stp->st_mtime = ACE_Time_Value (fdata.ftLastWriteTime);
00221       }
00222     return 0;
00223 #elif defined (ACE_HAS_X86_STAT_MACROS)
00224     // Solaris for intel uses an macro for stat(), this macro is a
00225     // wrapper for _xstat().
00226     ACE_OSCALL_RETURN (::_xstat (_STAT_VER, file, stp), int, -1);
00227 #else
00228     ACE_OSCALL_RETURN (ACE_STAT_FUNC_NAME (file, stp), int, -1);
00229 #endif /* ACE_HAS_NONCONST_STAT */
00230   }
00231 
00232 #if defined (ACE_HAS_WCHAR)
00233   ACE_INLINE int
00234   stat (const wchar_t *file, ACE_stat *stp)
00235   {
00236     ACE_OS_TRACE ("ACE_OS::stat");
00237 #if defined (ACE_HAS_WINCE)
00238     WIN32_FIND_DATAW fdata;
00239 
00240     HANDLE fhandle;
00241 
00242     fhandle = ::FindFirstFileW (file, &fdata);
00243     if (fhandle == INVALID_HANDLE_VALUE)
00244       {
00245         ACE_OS::set_errno_to_last_error ();
00246         return -1;
00247       }
00248     else if (fdata.nFileSizeHigh != 0)
00249       {
00250         errno = EINVAL;
00251         return -1;
00252       }
00253     else
00254       {
00255         stp->st_mode = static_cast<unsigned short>(fdata.dwFileAttributes);
00256         stp->st_size = fdata.nFileSizeLow;
00257         stp->st_atime = ACE_Time_Value (fdata.ftLastAccessTime);
00258         stp->st_mtime = ACE_Time_Value (fdata.ftLastWriteTime);
00259       }
00260     return 0;
00261 #elif defined (__BORLANDC__) \
00262       || (defined (_MSC_VER) && _MSC_VER >= 1300) \
00263       || defined (__MINGW32__)
00264     ACE_OSCALL_RETURN (ACE_WSTAT_FUNC_NAME (file, stp), int, -1);
00265 #else /* ACE_HAS_WINCE */
00266     ACE_Wide_To_Ascii nfile (file);
00267     return ACE_OS::stat (nfile.char_rep (), stp);
00268 #endif /* ACE_HAS_WINCE */
00269   }
00270 #endif /* ACE_HAS_WCHAR */
00271 
00272   ACE_INLINE mode_t
00273   umask (mode_t cmask)
00274   {
00275     ACE_OS_TRACE ("ACE_OS::umask");
00276 # if defined (ACE_LACKS_UMASK)
00277     ACE_UNUSED_ARG (cmask);
00278     ACE_NOTSUP_RETURN ((mode_t)-1);
00279 # elif defined (ACE_HAS_TR24731_2005_CRT)
00280     mode_t old_mode;
00281     ACE_SECURECRTCALL (_umask_s (cmask, &old_mode), mode_t, -1, old_mode);
00282     return old_mode;
00283 # elif defined (ACE_WIN32) && !defined (__BORLANDC__)
00284     ACE_OSCALL_RETURN (::_umask (cmask), mode_t, -1);
00285 # else
00286     return ::umask (cmask); // This call shouldn't fail...
00287 # endif /* ACE_LACKS_UMASK */
00288   }
00289 
00290 } // ACE_OS namespace
00291 
00292 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:18:41 2010 for ACE by  doxygen 1.4.7