Beispiel #1
0
// check if a file handle is for kernel32.dll
static bool kludge_isKernel32Dll(HANDLE fileHandle, std::string &kernel32Name) {
    static DWORD IndxHigh, IndxLow;
    static bool firstTime = true;
    BY_HANDLE_FILE_INFORMATION info;
    static std::string kernel32Name_;

    if (firstTime) {
       HANDLE kernel32H;
       firstTime = false;
       char sysRootDir[MAX_PATH+1];
       if (GetSystemDirectory(sysRootDir, MAX_PATH) == 0)
          assert(0);
       kernel32Name_ = std::string(sysRootDir) + "\\kernel32.dll";
       kernel32H = CreateFile(kernel32Name_.c_str(), GENERIC_READ, 
                              FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
       assert(kernel32H);
       if (!GetFileInformationByHandle(kernel32H, &info)) {
          printSysError(GetLastError());
          assert(0);
       }
       IndxHigh = info.nFileIndexHigh;
       IndxLow = info.nFileIndexLow;
       CloseHandle(kernel32H);
    }

    if (!GetFileInformationByHandle(fileHandle, &info))
       return false;

    if (info.nFileIndexHigh==IndxHigh && info.nFileIndexLow==IndxLow) {
      kernel32Name = kernel32Name_;
      return true;
    }
    return false;
}
bool LuaProxy::Misc::isSamePath(const std::string first, const std::string second) 
{
    HANDLE hFileFirst = CreateFileA(first.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
    if (hFileFirst == INVALID_HANDLE_VALUE) return false;
    HANDLE hFileSecond = CreateFileA(second.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
    if (hFileSecond == INVALID_HANDLE_VALUE) {
        CloseHandle(hFileFirst);
        return false;
    }

    BY_HANDLE_FILE_INFORMATION fInfoFirst = { 0 };
    BY_HANDLE_FILE_INFORMATION fInfoSecond = { 0 };
    BOOL success = GetFileInformationByHandle(hFileFirst, &fInfoFirst);
    success = success && GetFileInformationByHandle(hFileSecond, &fInfoSecond);
    if (!success) {
        CloseHandle(hFileFirst);
        CloseHandle(hFileSecond);
        return false;
    }

    bool isSame = fInfoFirst.dwVolumeSerialNumber == fInfoSecond.dwVolumeSerialNumber &&
        fInfoFirst.nFileIndexLow == fInfoSecond.nFileIndexLow &&
        fInfoFirst.nFileIndexHigh == fInfoSecond.nFileIndexHigh;

    CloseHandle(hFileFirst);
    CloseHandle(hFileSecond);
    
    return isSame;
}
Beispiel #3
0
// Code adapted from http://stackoverflow.com/questions/562701/best-way-to-determine-if-two-path-reference-to-same-file-in-c-c/562830#562830
// Determine if 2 paths point ot the same file...
bool IsSame(const WCHAR *path1, const WCHAR *path2)
{
    bool isSame = false, needFallback = true;
    HANDLE handle1 = CreateFile(path1, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
    HANDLE handle2 = CreateFile(path2, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);

    if (handle1 != INVALID_HANDLE_VALUE && handle2 != INVALID_HANDLE_VALUE) {
        BY_HANDLE_FILE_INFORMATION fi1, fi2;
        if (GetFileInformationByHandle(handle1, &fi1) && GetFileInformationByHandle(handle2, &fi2)) {
            isSame = fi1.dwVolumeSerialNumber == fi2.dwVolumeSerialNumber &&
                     fi1.nFileIndexHigh == fi2.nFileIndexHigh &&
                     fi1.nFileIndexLow == fi2.nFileIndexLow;
            needFallback = false;
        }
    }

    CloseHandle(handle1);
    CloseHandle(handle2);

    if (!needFallback)
        return isSame;

    ScopedMem<WCHAR> npath1(Normalize(path1));
    ScopedMem<WCHAR> npath2(Normalize(path2));
    // consider the files different, if their paths can't be normalized
    if (!npath1 || !npath2)
        return false;
    return str::EqI(npath1, npath2);
}
int CheckFiles(HANDLE hFile1, HANDLE hFile2)
{

	BY_HANDLE_FILE_INFORMATION info1;
	BY_HANDLE_FILE_INFORMATION info2;
    BOOL r1 = GetFileInformationByHandle(hFile1, &info1);
    BOOL r2 = GetFileInformationByHandle(hFile2, &info2);

	if (!r1 && !r2) {
		return 0;
	}
	else if (r1 && !r2) {
		return 1;
	}
	else if (!r1 && r2) {
		return -1;
	}

	if (info1.dwVolumeSerialNumber != info2.dwVolumeSerialNumber) {
		return info1.dwVolumeSerialNumber - info2.dwVolumeSerialNumber;
	}

	if (info1.nFileIndexHigh != info2.nFileIndexHigh) {
		return info1.nFileIndexHigh - info2.nFileIndexHigh;
	}
	return info1.nFileIndexLow - info2.nFileIndexLow;
}
Beispiel #5
0
/* Return non-zero if F1 and F2 are 2 streams representing the same
   file or pipe or device.  */
int
same_stream (FILE *f1, FILE *f2)
{
  HANDLE fh1 = (HANDLE)_get_osfhandle (fileno (f1));
  HANDLE fh2 = (HANDLE)_get_osfhandle (fileno (f2));

  /* Invalid file descriptors get treated as different streams.  */
  if (fh1 && fh1 != INVALID_HANDLE_VALUE
      && fh2 && fh2 != INVALID_HANDLE_VALUE)
    {
      if (fh1 == fh2)
        return 1;
      else
        {
          DWORD ftyp1 = GetFileType (fh1), ftyp2 = GetFileType (fh2);

          if (ftyp1 != ftyp2
              || ftyp1 == FILE_TYPE_UNKNOWN || ftyp2 == FILE_TYPE_UNKNOWN)
            return 0;
          else if (ftyp1 == FILE_TYPE_CHAR)
            {
              /* For character devices, check if they both refer to a
                 console.  This loses if both handles refer to the
                 null device (FIXME!), but in that case we don't care
                 in the context of Make.  */
              DWORD conmode1, conmode2;

              /* Each process on Windows can have at most 1 console,
                 so if both handles are for the console device, they
                 are the same.  We also compare the console mode to
                 distinguish between stdin and stdout/stderr.  */
              if (GetConsoleMode (fh1, &conmode1)
                  && GetConsoleMode (fh2, &conmode2)
                  && conmode1 == conmode2)
                return 1;
            }
          else
            {
              /* For disk files and pipes, compare their unique
                 attributes.  */
              BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;

              /* Pipes get zero in the volume serial number, but do
                 appear to have meaningful information in file index
                 attributes.  We test file attributes as well, for a
                 good measure.  */
              if (GetFileInformationByHandle (fh1, &bhfi1)
                  && GetFileInformationByHandle (fh2, &bhfi2))
                return (bhfi1.dwVolumeSerialNumber == bhfi2.dwVolumeSerialNumber
                        && bhfi1.nFileIndexLow == bhfi2.nFileIndexLow
                        && bhfi1.nFileIndexHigh == bhfi2.nFileIndexHigh
                        && bhfi1.dwFileAttributes == bhfi2.dwFileAttributes);
            }
        }
    }
  return 0;
}
Beispiel #6
0
bool
tr_sys_path_is_same (const char  * path1,
                     const char  * path2,
                     tr_error   ** error)
{
  bool ret = false;
  wchar_t * wide_path1 = NULL;
  wchar_t * wide_path2 = NULL;
  HANDLE handle1 = INVALID_HANDLE_VALUE;
  HANDLE handle2 = INVALID_HANDLE_VALUE;
  BY_HANDLE_FILE_INFORMATION fi1, fi2;

  assert (path1 != NULL);
  assert (path2 != NULL);

  wide_path1 = tr_win32_utf8_to_native (path1, -1);
  if (wide_path1 == NULL)
    goto fail;

  wide_path2 = tr_win32_utf8_to_native (path2, -1);
  if (wide_path2 == NULL)
    goto fail;

  handle1 = CreateFileW (wide_path1, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  if (handle1 == INVALID_HANDLE_VALUE)
    goto fail;

  handle2 = CreateFileW (wide_path2, 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  if (handle2 == INVALID_HANDLE_VALUE)
    goto fail;

  /* TODO: Use GetFileInformationByHandleEx on >= Server 2012 */

  if (!GetFileInformationByHandle (handle1, &fi1) || !GetFileInformationByHandle (handle2, &fi2))
    goto fail;

  ret = fi1.dwVolumeSerialNumber == fi2.dwVolumeSerialNumber &&
        fi1.nFileIndexHigh == fi2.nFileIndexHigh &&
        fi1.nFileIndexLow  == fi2.nFileIndexLow;

  goto cleanup;

fail:
  set_system_error_if_file_found (error, GetLastError ());

cleanup:
  CloseHandle (handle2);
  CloseHandle (handle1);

  tr_free (wide_path2);
  tr_free (wide_path1);

  return ret;
}
Beispiel #7
0
static bool GetStatByHandle(TSystemFStat& fs, FHANDLE f) {
#ifdef _win_
    return GetFileInformationByHandle(f, &fs);
#else
    return !fstat(f, &fs);
#endif
}
Beispiel #8
0
static INT_PTR CDECL fci_get_open_info( char *name, USHORT *date, USHORT *time,
                                        USHORT *attribs, int *err, void *ptr )
{
    HANDLE handle;
    BY_HANDLE_FILE_INFORMATION info;
    WCHAR *p, *nameW = strdupAtoW( CP_UTF8, name );

    handle = CreateFileW( nameW, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
                          NULL, OPEN_EXISTING, 0, NULL );
    if (handle == INVALID_HANDLE_VALUE)
    {
        *err = GetLastError();
        WINE_ERR( "failed to open %s: error %u\n", wine_dbgstr_w(nameW), *err );
        cab_free( nameW );
        return -1;
    }
    if (!GetFileInformationByHandle( handle, &info ))
    {
        *err = GetLastError();
        CloseHandle( handle );
        cab_free( nameW );
        return -1;
    }
    FileTimeToDosDateTime( &info.ftLastWriteTime, date, time );
    *attribs = info.dwFileAttributes & (_A_RDONLY | _A_HIDDEN | _A_SYSTEM | _A_ARCH);
    for (p = nameW; *p; p++) if (*p >= 0x80) break;
    if (*p) *attribs |= _A_NAME_IS_UTF;
    cab_free( nameW );
    return (INT_PTR)handle;
}
Beispiel #9
0
/**
 * Get two values that (together with the device number), uniquely
 * identify a file. Taken together these values create the windows
 * equivalent of UNIX inodes. If this fails for any reason a 0 is
 * returned, otherwise a 1 is returned. On failure only the st_rdev
 * value is set to zero, normally the st_ino (if this is the result
 * of a call to stat(), may have a sensible value).
 */
int win_get_inodes( const char *fns, ino_t *st_ino, dev_t *st_rdev )
{
    HANDLE fh;
    BY_HANDLE_FILE_INFORMATION info;
    DWORD error;
    int result = 1;

    /* Open file, use FILE_SHARE_READ and FILE_SHARE_WRITE as this
     * seems to be needed for win98 (but not for XP) */
    fh = CreateFile( fns, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
                     NULL, OPEN_EXISTING, 0, NULL );
    if ( fh != INVALID_HANDLE_VALUE ) {

        if ( GetFileInformationByHandle( fh, &info ) ) {
            *st_ino = info.nFileIndexLow;
            *st_rdev = info.nFileIndexHigh;
        }
        else {
            *st_rdev = 0;
            result = 0;
        }
        CloseHandle( fh );
    }
    else {
        *st_rdev = 0;
        result = 0;
    }
    return result;
}
__int64 FileReader::GetFileSize()
{
  CAutoLockFR rLock (&m_accessLock);

	if (m_hFile == INVALID_HANDLE_VALUE) 
  {
    //LogDebug("FileReader::GetFileSize() no open file");
		return -1;
	}
  
  BY_HANDLE_FILE_INFORMATION fileinfo;
		
  //fill the structure with info regarding the file
  if (GetFileInformationByHandle(m_hFile, &fileinfo))
  {	  
    //Extract the file size info
  	LARGE_INTEGER li;
  	li.LowPart  = fileinfo.nFileSizeLow;
  	li.HighPart = fileinfo.nFileSizeHigh;
  	
	  return li.QuadPart;
  }
  
  HRESULT lastErr = HRESULT_FROM_WIN32(GetLastError());	  
	LogDebug("FileReader::GetFileSize() failed. Error 0x%x, %ws, filename = %ws", lastErr, HresultToCString(lastErr), m_pFileName);    
  return -1;
}
Beispiel #11
0
unsigned int TA_FileSize( TA_FileHandle *handle )
{
   #if defined( USE_WIN32_API )
   BY_HANDLE_FILE_INFORMATION bhfi;
   #endif

   TA_FileHandlePriv *fileHandlePriv;
   unsigned int fileSize;

   TA_ASSERT_RET( handle != NULL, 0 );

   fileHandlePriv = (TA_FileHandlePriv *)handle;

   if( fileHandlePriv->streamAccess )
   {
      /* Use the stream instead of the file. */
      fileSize = TA_StreamSizeInByte( fileHandlePriv->stream );
   }
   else
   {
      #if defined( USE_WIN32_API )
         TA_ASSERT_RET( fileHandlePriv->handle != INVALID_HANDLE_VALUE, 0 );
         GetFileInformationByHandle( fileHandlePriv->handle, &bhfi );
         fileSize = bhfi.nFileSizeLow;
      #endif

      #if defined( USE_OSLAYER )
         TA_ASSERT_RET( fileHandlePriv->handle != NULL, 0 );
         fileSize = get_file_size( fileHandlePriv->path );
      #endif
   }

   return fileSize;
}
Beispiel #12
0
static int filefind_index_number_of_links_helper(lua_State* L, struct FileFindInfo* info) {
#if defined(_WIN32)
	HANDLE handle;
	BY_HANDLE_FILE_INFORMATION fileInformation;

	char fullPath[MAX_PATH];
	strcpy(fullPath, info->path);
	strcat(fullPath, info->fd.cFileName);

	handle = CreateFile(fullPath, 0, 0, NULL, OPEN_EXISTING, 0, NULL);
	if (handle == INVALID_HANDLE_VALUE) {
		lua_pushnumber(L, 0);
		return 1;
	}
	if (GetFileInformationByHandle(handle, &fileInformation) == FALSE) {
		CloseHandle(handle);
		lua_pushnumber(L, 0);
		return 1;
	}
	CloseHandle(handle);

	lua_pushnumber(L, fileInformation.nNumberOfLinks);
#else
	lua_pushnumber(L, 0);
#endif
	return 1;
}
bool CRawFile::Stat( struct stat *pFileStats ) const
{
#ifdef _WIN32
    BY_HANDLE_FILE_INFORMATION info;

    if (!GetFileInformationByHandle( m_file, &info ))
        return false;

    pFileStats->st_size = info.nFileSizeLow;
    pFileStats->st_dev = info.nFileIndexLow;
    pFileStats->st_atime = info.ftLastAccessTime.dwLowDateTime;
    pFileStats->st_ctime = info.ftCreationTime.dwLowDateTime;
    pFileStats->st_mtime = info.ftLastWriteTime.dwLowDateTime;
    pFileStats->st_dev = info.dwVolumeSerialNumber;
    pFileStats->st_mode = 0;
    pFileStats->st_nlink = (unsigned short)info.nNumberOfLinks;
    pFileStats->st_rdev = 0;
    pFileStats->st_gid = 0;
    return true;
#elif __linux
    return fstat( fileno( m_file ), pFileStats ) != 0;
#else
    return false;
#endif //OS DEPENDANT CODE
}
Beispiel #14
0
/*************************************************************************
 * IStream_fnStat
 */
static HRESULT WINAPI IStream_fnStat(IStream *iface, STATSTG* lpStat,
                                     DWORD grfStatFlag)
{
    ISHFileStream *This = impl_from_IStream(iface);
    BY_HANDLE_FILE_INFORMATION fi;

    TRACE("(%p,%p,%d)\n", This, lpStat, grfStatFlag);

    if (!grfStatFlag)
        return STG_E_INVALIDPOINTER;

    memset(&fi, 0, sizeof(fi));
    GetFileInformationByHandle(This->hFile, &fi);

    if (grfStatFlag & STATFLAG_NONAME)
      lpStat->pwcsName = NULL;
    else
      lpStat->pwcsName = StrDupW(This->lpszPath);
    lpStat->type = This->type;
    lpStat->cbSize.u.LowPart = fi.nFileSizeLow;
    lpStat->cbSize.u.HighPart = fi.nFileSizeHigh;
    lpStat->mtime = fi.ftLastWriteTime;
    lpStat->ctime = fi.ftCreationTime;
    lpStat->atime = fi.ftLastAccessTime;
    lpStat->grfMode = This->dwMode;
    lpStat->grfLocksSupported = 0;
    memcpy(&lpStat->clsid, &IID_IStream, sizeof(CLSID));
    lpStat->grfStateBits = This->grfStateBits;
    lpStat->reserved = 0;

    return S_OK;
}
    bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info)
    {
        ThreadRestrictions::AssertIOAllowed();
        if(!info)
        {
            return false;
        }

        BY_HANDLE_FILE_INFORMATION file_info;
        if(GetFileInformationByHandle(file, &file_info) == 0)
        {
            return false;
        }

        LARGE_INTEGER size;
        size.HighPart = file_info.nFileSizeHigh;
        size.LowPart = file_info.nFileSizeLow;
        info->size = size.QuadPart;
        info->is_directory =
            (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
        info->last_modified = base::Time::FromFileTime(file_info.ftLastWriteTime);
        info->last_accessed = base::Time::FromFileTime(file_info.ftLastAccessTime);
        info->creation_time = base::Time::FromFileTime(file_info.ftCreationTime);
        return true;
    }
Beispiel #16
0
/**
 * stat a file and return the appropriate object
 * This would better be done on an open file handle
 */
int file_metadata_t::stat(const tstring &fn,
			  file_metadata_t *m,
			  class display &ocb)
{
  struct __stat64 sb;
  if (::TSTAT(fn.c_str(),&sb)) 
  {
    ocb.error_filename(fn,"%s",strerror(errno));
    return -1;
  }
  m->fileid.dev = sb.st_dev;
#ifdef _WIN32
  BY_HANDLE_FILE_INFORMATION fileinfo;
  HANDLE filehandle = CreateFile(fn.c_str(),
				 0,   // desired access
				 FILE_SHARE_READ,
				 NULL,  
				 OPEN_EXISTING,
				 (FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS),
				 NULL);
  // RBF - Error check CreateFile
  (void)GetFileInformationByHandle(filehandle, &fileinfo);
  CloseHandle(filehandle);
  m->fileid.ino = (((uint64_t)fileinfo.nFileIndexHigh)<<32) | (fileinfo.nFileIndexLow);
#else
  m->fileid.ino = sb.st_ino;
#endif
  m->nlink      = sb.st_nlink;
  m->size       = sb.st_size;
  m->ctime      = sb.st_ctime;
  m->mtime      = sb.st_mtime;
  m->atime      = sb.st_atime;

  return 0;
}
Beispiel #17
0
ofint32 
OFFile::type( )
{
    ofint32 t = 0;
#if defined(OFOPSYS_LINUX) || defined(OFOPSYS_SOLARIS) || defined(OFOPSYS_FREEBSD) || defined(OFOPSYS_DARWIN)
    struct stat statbuf;
    ofint32 rc = fstat( m_handle, &statbuf );
    if ( rc == -1 )
        t = -1;
    if ( S_ISDIR(statbuf.st_mode) )
        t = OFFILE_FILETYPE_DIR;
    if ( S_ISREG(statbuf.st_mode) )
        t = OFFILE_FILETYPE_FILE;
#elif defined(OFOPSYS_WIN32) && !defined(__CYGWIN__)
    BY_HANDLE_FILE_INFORMATION fi;
    GetFileInformationByHandle( m_handle, &fi );
    
    if ( fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
        t = OFFILE_FILETYPE_DIR;
    if ( ( fi.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || ( fi.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN ) ||
         ( fi.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) )
        t = OFFILE_FILETYPE_FILE;
#else
#error Undefined platform
#endif
    return t;
}
Beispiel #18
0
int mingw_fstat(int fd, struct stat *buf)
{
	HANDLE fh = (HANDLE)_get_osfhandle(fd);
	BY_HANDLE_FILE_INFORMATION fdata;

	if (fh == INVALID_HANDLE_VALUE) {
		errno = EBADF;
		return -1;
	}
	/* direct non-file handles to MS's fstat() */
	if (GetFileType(fh) != FILE_TYPE_DISK)
		return _fstati64(fd, buf);

	if (GetFileInformationByHandle(fh, &fdata)) {
		buf->st_ino = 0;
		buf->st_gid = 0;
		buf->st_uid = 0;
		buf->st_nlink = 1;
		buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
		buf->st_size = fdata.nFileSizeLow |
			(((off_t)fdata.nFileSizeHigh)<<32);
		buf->st_dev = buf->st_rdev = 0; /* not used by Git */
		buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
		buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
		buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
		return 0;
	}
	errno = EBADF;
	return -1;
}
Beispiel #19
0
LONG gfile_get_length(GFile *gf)
{
    BY_HANDLE_FILE_INFORMATION fi;
    ASSERT(gf != NULL);
    GetFileInformationByHandle((HANDLE)gf->m_hFile, &fi);
    return fi.nFileSizeLow;
}
Beispiel #20
0
static void compute_socketname(char socketname[PATHSZ], struct stat *st)
#endif
{
#ifdef _WIN32
  CHAR user[UNLEN + 1];
  DWORD dwBufSize = UNLEN;
  BY_HANDLE_FILE_INFORMATION info;
  HANDLE hFile = CreateFile(merlin_path, FILE_READ_ATTRIBUTES, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  if (hFile == INVALID_HANDLE_VALUE || !GetFileInformationByHandle(hFile, &info))
    failwith_perror("stat (cannot find ocamlmerlin binary)");
  CloseHandle(hFile);

  if (!GetUserName(user, &dwBufSize))
    user[0] = '\0';
  // @@DRA Need to use Windows API functions to get meaningful values for st_dev and st_ino
  snprintf(eventname, PATHSZ,
      "ocamlmerlin_%s_%lx_%llx",
      user,
      info.dwVolumeSerialNumber,
      ((__int64)info.nFileIndexHigh) << 32 | ((__int64)info.nFileIndexLow));
  snprintf(socketname, PATHSZ,
      "\\\\.\\pipe\\%s", eventname);
#else
  snprintf(socketname, PATHSZ,
      "ocamlmerlin_%llu_%llu_%llu.socket",
      (unsigned long long)getuid(),
      (unsigned long long)st->st_dev,
      (unsigned long long)st->st_ino);
#endif
}
Beispiel #21
0
bool os_fstat(int fd, const char* path, pony_stat_t* p)
{
#if defined(PLATFORM_IS_WINDOWS)
  HANDLE h = (HANDLE)_get_osfhandle(fd);
  BY_HANDLE_FILE_INFORMATION fa;

  if(!GetFileInformationByHandle(h, &fa))
    return false;

  struct __stat64 st;

  if(_fstat64(fd, &st) != 0)
    return false;

  windows_stat(path, p, &st, fa.dwFileAttributes,
    &fa.ftLastAccessTime, &fa.ftLastWriteTime, &fa.ftCreationTime);

  return true;
#elif defined(PLATFORM_IS_POSIX_BASED)
  (void)path;
  struct stat st;

  if(fstat(fd, &st) != 0)
    return false;

  unix_stat(p, &st);
  return true;
#endif
}
Beispiel #22
0
void getfileinfo(void) {
  OFSTRUCT lpReOpenBuff;
  hfFile = (HANDLE)OpenFile(szFileName, &lpReOpenBuff, NULL);

  if (hfFile == (HANDLE)HFILE_ERROR) {
    err_msg();  // exit if error
    exit(0);
  }

  GetFileInformationByHandle(hfFile, &lpFileInformation);
  FileTimeToLocalFileTime(&lpFileInformation.ftLastWriteTime, &lpLastWriteLocalTime);
  FileTimeToSystemTime(&lpLastWriteLocalTime, &lpLastWriteSystemTime);

  GetFileTime(hfFile, &lpCreationTime, &lpLastAccessTime, &lpLastWriteTime);
  FileTimeToSystemTime(&lpCreationTime, &lpSystemTime);
  lpSystemTime.wYear = 2010;
  SystemTimeToFileTime(&lpSystemTime, &lpCreationTime);
  SetFileTime(hfFile, &lpCreationTime, &lpLastAccessTime, &lpLastWriteTime);


  printf ("\n\nFile size: %d", lpFileInformation.nFileSizeLow);
  printf ("\n---LastWriteTime---");
  printtime(lpLastWriteSystemTime);
  printf ("---FileAttributes---\n");
  printattrib();
  CloseHandle(hfFile);
}
Beispiel #23
0
static INT_PTR CDECL get_open_info(char *pszName, USHORT *pdate, USHORT *ptime,
                                   USHORT *pattribs, int *err, void *pv)
{
    BY_HANDLE_FILE_INFORMATION finfo;
    FILETIME filetime;
    HANDLE handle;
    DWORD attrs;
    BOOL res;

    handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);

    ok(handle != INVALID_HANDLE_VALUE, "Failed to CreateFile %s\n", pszName);

    res = GetFileInformationByHandle(handle, &finfo);
    ok(res, "Expected GetFileInformationByHandle to succeed\n");
   
    FileTimeToLocalFileTime(&finfo.ftLastWriteTime, &filetime);
    FileTimeToDosDateTime(&filetime, pdate, ptime);

    attrs = GetFileAttributes(pszName);
    ok(attrs != INVALID_FILE_ATTRIBUTES, "Failed to GetFileAttributes\n");

    return (INT_PTR)handle;
}
Beispiel #24
0
PHP_WIN32_IPC_API key_t
ftok(const char *pathname, int proj_id)
{
    HANDLE fh;
    struct stat st;
    BY_HANDLE_FILE_INFORMATION bhfi;
    key_t ret;

    if (stat(pathname, &st) < 0) {
        return (key_t)-1;
    }

    if ((fh = CreateFile(pathname, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0)) == INVALID_HANDLE_VALUE) {
        return (key_t)-1;
    }

    if (!GetFileInformationByHandle(fh, &bhfi)) {
        CloseHandle(fh);
        return (key_t)-1;
    }

    ret = (key_t) ((proj_id & 0xff) << 24 | (st.st_dev & 0xff) << 16 | ((bhfi.nFileIndexLow | (__int64)bhfi.nFileIndexHigh << 32) & 0xffff));

    CloseHandle(fh);

    return ret;
}
Beispiel #25
0
value onlyWin32_getFileInformationByHandle_ml(value handle_v)
{
  HANDLE handle = (HANDLE)handle_v;
  BY_HANDLE_FILE_INFORMATION fileInfo;
  CAMLparam0 ();
  CAMLlocal1 (v);
  ULARGE_INTEGER size, index;

  if( !GetFileInformationByHandle(handle, &fileInfo) ){
    DWORD err = GetLastError();
    win32_maperr(err);
    uerror("GetFileInformationByHandle", Nothing);
  }

  size.HighPart = fileInfo.nFileSizeHigh;
  size.LowPart = fileInfo.nFileSizeLow;
  index.HighPart = fileInfo.nFileIndexHigh;
  index.LowPart = fileInfo.nFileIndexLow;

  v = caml_alloc (8, 0);
  Store_field(v,0, Val_int(fileInfo.dwFileAttributes));
  Store_field(v, 1,
              caml_copy_double(FileTime_to_POSIX(fileInfo.ftCreationTime)));
  Store_field(v, 2,
              caml_copy_double(FileTime_to_POSIX(fileInfo.ftLastAccessTime)));
  Store_field(v, 3,
              caml_copy_double(FileTime_to_POSIX(fileInfo.ftLastWriteTime)));
  Store_field(v, 4, Val_int(fileInfo.dwVolumeSerialNumber));
  Store_field(v, 5, caml_copy_int64(size.QuadPart));
  Store_field(v, 6, Val_int(fileInfo.nNumberOfLinks));
  Store_field(v, 7, caml_copy_int64(index.QuadPart));

  CAMLreturn (v);
}
Beispiel #26
0
BOOL factor_vm::windows_stat(vm_char *path)
{
	BY_HANDLE_FILE_INFORMATION bhfi;
	HANDLE h = CreateFileW(path,
			GENERIC_READ,
			FILE_SHARE_READ,
			NULL,
			OPEN_EXISTING,
			FILE_FLAG_BACKUP_SEMANTICS,
			NULL);

	if(h == INVALID_HANDLE_VALUE)
	{
		// FindFirstFile is the only call that can stat c:\pagefile.sys
		WIN32_FIND_DATA st;
		HANDLE h;

		if(INVALID_HANDLE_VALUE == (h = FindFirstFile(path, &st)))
			return false;
		FindClose(h);
		return true;
	}
	BOOL ret = GetFileInformationByHandle(h, &bhfi);
	CloseHandle(h);
	return ret;
}
Beispiel #27
0
static FNFCIGETOPENINFO(cb_getopeninfo)
{
    BY_HANDLE_FILE_INFORMATION bhfi;
    FILETIME filetime;
    HANDLE handle;

    /* Need Win32 handle to get time stamps */
    handle = CreateFile(pszName, GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (handle == INVALID_HANDLE_VALUE)
        return -1;

    if (GetFileInformationByHandle(handle, &bhfi) == FALSE)
    {
        CloseHandle(handle);
        return -1;
    }

    FileTimeToLocalFileTime(&bhfi.ftLastWriteTime, &filetime);
    FileTimeToDosDateTime(&filetime, pdate, ptime);

    *pattribs = (int)(bhfi.dwFileAttributes &
        (_A_RDONLY | _A_SYSTEM | _A_HIDDEN | _A_ARCH));

    CloseHandle(handle);

    return _open(pszName, _O_RDONLY | _O_BINARY | O_NOINHERIT);
}
Beispiel #28
0
static long
#else
static long long
#endif
get_inode(struct dirent *entry, const char *dirname)
{
#ifdef __MINGW32__
	char fname[PATH_MAX];
	HANDLE fh;
	BY_HANDLE_FILE_INFORMATION info;

	sprintf(fname, "%s\\%s", dirname, entry->d_name);
	fh = CreateFile(fname, 0, 0, NULL, OPEN_EXISTING,
			FILE_FLAG_BACKUP_SEMANTICS, NULL);
	if (fh == INVALID_HANDLE_VALUE)
		return 0;
	if (GetFileInformationByHandle(fh, &info)) {
		long long inode = info.nFileIndexHigh;

		inode <<= 32;
		inode += info.nFileIndexLow;
		return inode;
	}
	return 0;
#else
	(void) dirname;		/* silence warnings */
	return entry->d_ino;
#endif
}
Beispiel #29
0
static void
GetFileInfo(FD_t fd, int *size, char **ctime, char **mtime, char **atime)
{
#ifdef AFS_NT40_ENV
    BY_HANDLE_FILE_INFORMATION fi;
    if (!GetFileInformationByHandle(fd, &fi)) {
	printf("GetFileInformationByHandle failed, exiting\n");
	exit(1);
    }
    *size = (int)fi.nFileSizeLow;
    *ctime = "N/A";
    *mtime = NT_date(&fi.ftLastWriteTime);
    *atime = NT_date(&fi.ftLastAccessTime);
#else
    struct afs_stat status;
    if (afs_fstat(fd, &status) == -1) {
	printf("fstat failed %d\n", errno);
	exit(1);
    }
    *size = (int)status.st_size;
    *ctime = date(status.st_ctime);
    *mtime = date(status.st_mtime);
    *atime = date(status.st_atime);
#endif
}
Beispiel #30
0
axStatus	axFile::lastWriteTime	( axTimeStamp & t ) {
	if( ! isValid() ) { assert(false);	return axStatus_Std::not_initialized; }
	BY_HANDLE_FILE_INFORMATION info;
	if( ! GetFileInformationByHandle( h_, &info ) ) return axStatus_Std::File_error;
	t.setFILETIME( info.ftLastWriteTime );
	return 0;
}