/*-------------------------------------------------------------------------
 *
 *
 *
 *-------------------------------------------------------------------------
 */

#ifdef WIN32

#include <pg_config_os.h>
#include <sys/stat.h>
#include <tchar.h>

static ULONGLONG const EpochShift = 116444736000000000; // 134774 days (from 1601-01-01 to 1970-01-01) represented by 100-nanosecond intervals

static time_t cvt_ft2ut ( FILETIME const * ft ) // Converts FILETIME to time_t
{
	ULARGE_INTEGER unified_ft;
    
	unified_ft.LowPart  = ft -> dwLowDateTime;
    unified_ft.HighPart = ft -> dwHighDateTime;
    
	if( unified_ft.QuadPart < EpochShift )
        return -1;

    unified_ft.QuadPart -= EpochShift;
    unified_ft.QuadPart /= 10 * 1000 * 1000;
    
	if ( unified_ft.HighPart != 0 )
        return -1;

    return unified_ft.LowPart;
}

static unsigned short cvt_attr2uxmode ( int attr, const _TCHAR * name )
{
    unsigned short uxmode;
	const _TCHAR * p;

	uxmode = ( unsigned short )( ( attr & FILE_ATTRIBUTE_DIRECTORY ) ? ( _S_IFDIR | _S_IEXEC ) : ( _S_IFREG ) );

    uxmode |= ( attr & FILE_ATTRIBUTE_READONLY ) ? ( _S_IREAD ) : ( _S_IREAD | _S_IWRITE );

	if ( p = _tcsrchr( name, _T( '.' ) ) )
	{
        if ( ! _tcsicmp( p, _T( ".exe" ) ) ||
             ! _tcsicmp( p, _T( ".cmd" ) ) ||
             ! _tcsicmp( p, _T( ".bat" ) ) ||
             ! _tcsicmp( p, _T( ".com" ) ) )
            uxmode |= _S_IEXEC;
    }

    uxmode |= ( uxmode & 0700 ) >> 3;
    uxmode |= ( uxmode & 0700 ) >> 6;

    return uxmode;
}

#ifdef  _UNICODE
#  define _tstat32i64_pg_fixed	_wstat32i64_pg_fixed
#  define _tstat32_pg_fixed		_wstat32_pg_fixed
#else
#  define _tstat32i64_pg_fixed	_stat32i64_pg_fixed
#  define _tstat32_pg_fixed		_stat32_pg_fixed
#endif

#ifdef _USE_INT64
int __cdecl _tstat32i64_pg_fixed ( _TCHAR const * name, struct _stat32i64 * buf )
#else  /* _USE_INT64 */
int __cdecl _tstat32_pg_fixed ( _TCHAR const * name, struct _stat32 * buf )
#endif  /* _USE_INT64 */
{
    WIN32_FILE_ATTRIBUTE_DATA faData;

	if ( NULL == name || NULL == buf )
	{
		errno = EINVAL;
		return -1;
	}

	if ( ! GetFileAttributesEx( name, GetFileExInfoStandard, & faData ) )
	{
		_dosmaperr( GetLastError() );
        return -1;
	}

	memset( buf, 0, sizeof( * buf ) );

	if ( faData.ftLastWriteTime.dwLowDateTime ||
		 faData.ftLastWriteTime.dwHighDateTime )
	{
		buf -> st_mtime = cvt_ft2ut( & faData.ftLastWriteTime );
	}

	if ( faData.ftLastAccessTime.dwLowDateTime ||
		 faData.ftLastAccessTime.dwHighDateTime )
	{
		buf -> st_atime = cvt_ft2ut( & faData.ftLastAccessTime );
	}
	else
	{
		buf -> st_atime = buf -> st_mtime;
	}

	if ( faData.ftCreationTime.dwLowDateTime ||
		 faData.ftCreationTime.dwHighDateTime )
	{
		buf -> st_ctime = cvt_ft2ut( & faData.ftCreationTime );
	}
	else
	{
		buf -> st_ctime = buf -> st_mtime;
	}

	buf -> st_mode  = cvt_attr2uxmode( faData.dwFileAttributes, name );
    buf -> st_nlink = 1;

#ifdef _USE_INT64
    buf -> st_size = ( ( ( __int64 )( faData.nFileSizeHigh ) ) << 32 ) + ( __int64 )( faData.nFileSizeLow );
#else  /* _USE_INT64 */
    buf -> st_size = faData.nFileSizeLow;
#endif  /* _USE_INT64 */

    return 0;
}

#endif // WIN32
