ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

stat64.c
Go to the documentation of this file.
00001 #include <precomp.h>
00002 #include <tchar.h>
00003 #include <direct.h>
00004 
00005 HANDLE fdtoh(int fd);
00006 
00007 #define ALL_S_IREAD  (_S_IREAD  | (_S_IREAD  >> 3) | (_S_IREAD  >> 6))
00008 #define ALL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
00009 #define ALL_S_IEXEC  (_S_IEXEC  | (_S_IEXEC  >> 3) | (_S_IEXEC  >> 6))
00010 
00011 #ifdef UNICODE
00012 #define TCHAR4 ULONGLONG
00013 #else
00014 #define TCHAR4 ULONG
00015 #endif
00016 
00017 #define TCSIZE sizeof(_TCHAR)
00018 #define TOULL(x) ((TCHAR4)(x))
00019 
00020 #define EXE ((TOULL('e')<<(2*TCSIZE)) | (TOULL('x')<<TCSIZE) | (TOULL('e')))
00021 #define BAT ((TOULL('b')<<(2*TCSIZE)) | (TOULL('a')<<TCSIZE) | (TOULL('t')))
00022 #define CMD ((TOULL('c')<<(2*TCSIZE)) | (TOULL('m')<<TCSIZE) | (TOULL('d')))
00023 #define COM ((TOULL('c')<<(2*TCSIZE)) | (TOULL('o')<<TCSIZE) | (TOULL('m')))
00024 
00025 int CDECL _tstat64(const _TCHAR *path, struct __stat64 *buf)
00026 {
00027   DWORD dw;
00028   WIN32_FILE_ATTRIBUTE_DATA hfi;
00029   unsigned short mode = ALL_S_IREAD;
00030   size_t plen;
00031 
00032   TRACE(":file (%s) buf(%p)\n",path,buf);
00033 
00034   if (!GetFileAttributesEx(path, GetFileExInfoStandard, &hfi))
00035   {
00036       TRACE("failed (%d)\n",GetLastError());
00037       _dosmaperr(ERROR_FILE_NOT_FOUND);
00038       return -1;
00039   }
00040 
00041   memset(buf,0,sizeof(struct __stat64));
00042 
00043   /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
00044      Bon 011120: This FIXME seems incorrect
00045                  Also a letter as first char isn't enough to be classified
00046          as a drive letter
00047   */
00048   if (isalpha(*path)&& (*(path+1)==':'))
00049     buf->st_dev = buf->st_rdev = _totupper(*path) - 'A'; /* drive num */
00050   else
00051     buf->st_dev = buf->st_rdev = _getdrive() - 1;
00052 
00053   plen = _tcslen(path);
00054 
00055   /* Dir, or regular file? */
00056   if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
00057       (path[plen-1] == '\\'))
00058     mode |= (_S_IFDIR | ALL_S_IEXEC);
00059   else
00060   {
00061     mode |= _S_IFREG;
00062     /* executable? */
00063     if (plen > 6 && path[plen-4] == '.')  /* shortest exe: "\x.exe" */
00064     {
00065       TCHAR4 ext = _totlower(path[plen-1]) | (_totlower(path[plen-2]) << TCSIZE) |
00066                                  (_totlower(path[plen-3]) << (2*TCSIZE));
00067       if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
00068           mode |= ALL_S_IEXEC;
00069     }
00070   }
00071 
00072   if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
00073     mode |= ALL_S_IWRITE;
00074 
00075   buf->st_mode  = mode;
00076   buf->st_nlink = 1;
00077   buf->st_size  = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
00078   RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
00079   buf->st_atime = dw;
00080   RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
00081   buf->st_mtime = buf->st_ctime = dw;
00082   TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
00083         (long)(buf->st_size >> 32),(long)buf->st_size,
00084         (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
00085   return 0;
00086 }
00087 
00088 #ifndef _UNICODE
00089 
00090 int CDECL _fstat64(int fd, struct __stat64* buf)
00091 {
00092   DWORD dw;
00093   DWORD type;
00094   BY_HANDLE_FILE_INFORMATION hfi;
00095   HANDLE hand = fdtoh(fd);
00096 
00097   TRACE(":fd (%d) stat (%p)\n",fd,buf);
00098   if (hand == INVALID_HANDLE_VALUE)
00099     return -1;
00100 
00101   if (!buf)
00102   {
00103     WARN(":failed-NULL buf\n");
00104     _dosmaperr(ERROR_INVALID_PARAMETER);
00105     return -1;
00106   }
00107 
00108   memset(&hfi, 0, sizeof(hfi));
00109   memset(buf, 0, sizeof(struct __stat64));
00110   type = GetFileType(hand);
00111   if (type == FILE_TYPE_PIPE)
00112   {
00113     buf->st_dev = buf->st_rdev = fd;
00114     buf->st_mode = _S_IFIFO;
00115     buf->st_nlink = 1;
00116   }
00117   else if (type == FILE_TYPE_CHAR)
00118   {
00119     buf->st_dev = buf->st_rdev = fd;
00120     buf->st_mode = _S_IFCHR;
00121     buf->st_nlink = 1;
00122   }
00123   else /* FILE_TYPE_DISK etc. */
00124   {
00125     if (!GetFileInformationByHandle(hand, &hfi))
00126     {
00127       WARN(":failed-last error (%d)\n",GetLastError());
00128       _dosmaperr(ERROR_INVALID_PARAMETER);
00129       return -1;
00130     }
00131     buf->st_mode = _S_IFREG | ALL_S_IREAD;
00132     if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
00133       buf->st_mode |= ALL_S_IWRITE;
00134     buf->st_size  = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
00135     RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
00136     buf->st_atime = dw;
00137     RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
00138     buf->st_mtime = buf->st_ctime = dw;
00139     buf->st_nlink = (short)hfi.nNumberOfLinks;
00140   }
00141   TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
00142    buf->st_mode);
00143   return 0;
00144 }
00145 
00146 #endif

Generated on Sun May 27 2012 04:36:37 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.