Skip to content

Commit

Permalink
Get rid of a bunch of more uses of stat64 on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Sep 23, 2015
1 parent cf63ec6 commit 58d3137
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Common/ChunkFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ CChunkFileReader::Error CChunkFileReader::LoadFile(const std::string& _rFilename
}

// Check file size
const u64 fileSize = File::GetSize(_rFilename);
const u64 fileSize = File::GetFileSize(_rFilename);
static const u64 headerSize = sizeof(SChunkHeader);
if (fileSize < headerSize)
{
Expand Down
2 changes: 0 additions & 2 deletions Common/CommonFuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ inline u64 __rotr64(u64 x, unsigned int shift){
#define fseeko _fseeki64
#define ftello _ftelli64
#define atoll _atoi64
#define stat64 _stat64
#define fstat64 _fstat64
#define fileno _fileno
#ifndef _XBOX
#if _M_IX86
Expand Down
85 changes: 39 additions & 46 deletions Common/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,33 +154,31 @@ bool Exists(const std::string &filename) {
#endif
}

// Returns true if stat represents a directory
bool IsDirectory(const struct stat64 &file_info)
{
return S_ISDIR(file_info.st_mode);
}

// Returns true if filename is a directory
bool IsDirectory(const std::string &filename)
{
std::string fn = filename;
StripTailDirSlashes(fn);

struct stat64 file_info;
#if defined(_WIN32) && defined(UNICODE)
#if defined(_WIN32)
std::wstring copy = ConvertUTF8ToWString(fn);
int result = _wstat64(copy.c_str(), &file_info);
DWORD result = GetFileAttributes(copy.c_str());
if (result == INVALID_FILE_ATTRIBUTES) {
WARN_LOG(COMMON, "GetFileAttributes failed on %s: %08x", fn.c_str(), GetLastError());
return false;
}
return (result & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY;
#else
std::string copy(fn);
struct stat64 file_info;
int result = stat64(copy.c_str(), &file_info);
#endif
if (result < 0) {
WARN_LOG(COMMON, "IsDirectory: stat failed on %s: %s",
fn.c_str(), GetLastErrorMsg());
return false;
}

return IsDirectory(file_info);
return S_ISDIR(file_info.st_mode);
#endif
}

// Deletes a given filename, return true on success
Expand Down Expand Up @@ -423,72 +421,67 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename)
#endif
}

bool GetModifTime(const std::string &filename, tm &return_time)
{
bool GetModifTime(const std::string &filename, tm &return_time) {
memset(&return_time, 0, sizeof(return_time));
if (!Exists(filename))
{
if (!Exists(filename)) {
WARN_LOG(COMMON, "GetCreateTime: failed %s: No such file", filename.c_str());

This comment has been minimized.

Copy link
@unknownbrackets

unknownbrackets Sep 23, 2015

Collaborator

Btw, these are wrong.

-[Unknown]

This comment has been minimized.

Copy link
@hrydgard

hrydgard Sep 23, 2015

Author Owner

This is gone, please review the latest commits.

return false;
}

if (IsDirectory(filename))
{
if (IsDirectory(filename)) {
WARN_LOG(COMMON, "GetCreateTime: failed %s: is a directory", filename.c_str());
return false;
}

#ifdef _WIN32
struct _stat64 buf;
// TODO: Find a Win32 way

This comment has been minimized.

Copy link
@unknownbrackets

unknownbrackets Sep 23, 2015

Collaborator

GetFileAttributesEx() - ftLastWriteTime?

-[Unknown]

This comment has been minimized.

Copy link
@hrydgard

hrydgard Sep 23, 2015

Author Owner

@unknownbrackets , yes, in later commits I changed it.

This comment has been minimized.

Copy link
@unknownbrackets

unknownbrackets Sep 23, 2015

Collaborator

Erp, sorry.

-[Unknown]

if (_wstat64(ConvertUTF8ToWString(filename.c_str()).c_str(), &buf) == 0) {
INFO_LOG(COMMON, "GetCreateTime: %s: %lld", filename.c_str(), (long long)buf.st_mtime);
localtime_r((time_t*)&buf.st_mtime, &return_time);
return true;
}
#else
struct stat64 buf;
if (stat64(filename.c_str(), &buf) == 0)
{
if (stat64(ConvertUTF8ToWString(filename.c_str()).c_str(), &buf) == 0) {
INFO_LOG(COMMON, "GetCreateTime: %s: %lld", filename.c_str(), (long long)buf.st_mtime);
localtime_r((time_t*)&buf.st_mtime, &return_time);
return true;
}

#endif
ERROR_LOG(COMMON, "GetCreateTime: Stat failed %s: %s", filename.c_str(), GetLastErrorMsg());
return false;
}

// Returns the size of filename (64bit)
u64 GetSize(const std::string &filename)
{
struct stat64 file_info;
// Returns the size of file (64bit)
// TODO: Add a way to return an error.
u64 GetFileSize(const std::string &filename) {
#if defined(_WIN32) && defined(UNICODE)
int result = _wstat64(ConvertUTF8ToWString(filename).c_str(), &file_info);
WIN32_FILE_ATTRIBUTE_DATA attr;
if (!GetFileAttributesEx(ConvertUTF8ToWString(filename).c_str(), GetFileExInfoStandard, &attr))
return 0;
if (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
return 0;
return ((u64)attr.nFileSizeHigh << 32) | (u64)attr.nFileSizeLow;
#else
struct stat64 file_info;
int result = stat64(filename.c_str(), &file_info);
#endif
if (result != 0)
{
if (result != 0) {
WARN_LOG(COMMON, "GetSize: failed %s: No such file", filename.c_str());
return 0;
}

if (IsDirectory(file_info))
{
if (IsDirectory(file_info)) {
WARN_LOG(COMMON, "GetSize: failed %s: is a directory", filename.c_str());
return 0;
}

DEBUG_LOG(COMMON, "GetSize: %s: %lld", filename.c_str(), (long long)file_info.st_size);
return file_info.st_size;
}

// Overloaded GetSize, accepts file descriptor
u64 GetSize(const int fd)
{
struct stat64 buf;
if (fstat64(fd, &buf) != 0) {
ERROR_LOG(COMMON, "GetSize: stat failed %i: %s",
fd, GetLastErrorMsg());
return 0;
}
return buf.st_size;
#endif
}

// Overloaded GetSize, accepts FILE*
u64 GetSize(FILE *f)
u64 GetFileSize(FILE *f)
{
// can't use off_t here because it can be 32-bit
u64 pos = ftello(f);
Expand Down Expand Up @@ -780,7 +773,7 @@ void IOFile::SetHandle(std::FILE* file)
u64 IOFile::GetSize()
{
if (IsOpen())
return File::GetSize(m_file);
return File::GetFileSize(m_file);
else
return 0;
}
Expand Down
7 changes: 2 additions & 5 deletions Common/FileUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ bool IsDirectory(const std::string &filename);
bool GetModifTime(const std::string &filename, tm &return_time);

// Returns the size of filename (64bit)
u64 GetSize(const std::string &filename);

// Overloaded GetSize, accepts file descriptor
u64 GetSize(const int fd);
u64 GetFileSize(const std::string &filename);

// Overloaded GetSize, accepts FILE*
u64 GetSize(FILE *f);
u64 GetFileSize(FILE *f);

// Returns true if successful, or path already exists.
bool CreateDir(const std::string &filename);
Expand Down
21 changes: 11 additions & 10 deletions Core/FileSystems/DirectoryFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) {
x.name = filename;

std::string fullName = GetLocalPath(filename);
if (! File::Exists(fullName)) {
if (!File::Exists(fullName)) {
#if HOST_IS_CASE_SENSITIVE
if (! FixPathCase(basePath,filename, FPC_FILE_MUST_EXIST))
return x;
Expand All @@ -675,21 +675,23 @@ PSPFileInfo DirectoryFileSystem::GetFileInfo(std::string filename) {
x.type = File::IsDirectory(fullName) ? FILETYPE_DIRECTORY : FILETYPE_NORMAL;
x.exists = true;

if (x.type != FILETYPE_DIRECTORY)
{
if (x.type != FILETYPE_DIRECTORY) {
#ifdef _WIN32
struct _stat64i32 s;
_wstat64i32(ConvertUTF8ToWString(fullName).c_str(), &s);
// TODO: Find a Win32 way to get the atime, ctime etc.
if (_wstat64i32(ConvertUTF8ToWString(fullName).c_str(), &s) != 0) {
ERROR_LOG(FILESYS, "DirectoryFileSystem::GetFileInfo: _wstat64i32 failed: %s", fullName.c_str());
}
#else
struct stat s;
stat(fullName.c_str(), &s);
#endif

x.size = File::GetSize(fullName);
x.size = File::GetFileSize(fullName);
x.access = s.st_mode & 0x1FF;
localtime_r((time_t*)&s.st_atime,&x.atime);
localtime_r((time_t*)&s.st_ctime,&x.ctime);
localtime_r((time_t*)&s.st_mtime,&x.mtime);
localtime_r((time_t*)&s.st_atime, &x.atime);
localtime_r((time_t*)&s.st_ctime, &x.ctime);
localtime_r((time_t*)&s.st_mtime, &x.mtime);
}

return x;
Expand All @@ -703,8 +705,7 @@ bool DirectoryFileSystem::GetHostPath(const std::string &inpath, std::string &ou
#ifdef _WIN32
#define FILETIME_FROM_UNIX_EPOCH_US 11644473600000000ULL

static void tmFromFiletime(tm &dest, FILETIME &src)
{
static void tmFromFiletime(tm &dest, FILETIME &src) {
u64 from_1601_us = (((u64) src.dwHighDateTime << 32ULL) + (u64) src.dwLowDateTime) / 10ULL;
u64 from_1970_us = from_1601_us - FILETIME_FROM_UNIX_EPOCH_US;

Expand Down
14 changes: 11 additions & 3 deletions Core/FileSystems/VirtualDiscFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void VirtualDiscFileSystem::LoadFileListIndex() {
ERROR_LOG(FILESYS, "Unable to open virtual file: %s", entry.fileName.c_str());
}
} else {
entry.totalSize = File::GetSize(GetLocalPath(entry.fileName));
entry.totalSize = File::GetFileSize(GetLocalPath(entry.fileName));
}

// Try to keep currentBlockIndex sane, in case there are other files.
Expand Down Expand Up @@ -272,7 +272,7 @@ int VirtualDiscFileSystem::getFileListIndex(std::string& fileName)

FileListEntry entry = {""};
entry.fileName = fileName;
entry.totalSize = File::GetSize(fullName);
entry.totalSize = File::GetFileSize(fullName);
entry.firstBlock = currentBlockIndex;
currentBlockIndex += (entry.totalSize+2047)/2048;

Expand Down Expand Up @@ -595,10 +595,18 @@ PSPFileInfo VirtualDiscFileSystem::GetFileInfo(std::string filename) {
}

if (x.type != FILETYPE_DIRECTORY) {
#ifdef _WIN32
struct _stat64i32 s;
// TODO: Find a Win32 way to get the atime, ctime etc.
if (_wstat64i32(ConvertUTF8ToWString(fullName).c_str(), &s) != 0) {
ERROR_LOG(FILESYS, "DirectoryFileSystem::GetFileInfo: _wstat64i32 failed: %s", fullName.c_str());
}
#else
struct stat s;
stat(fullName.c_str(), &s);
#endif

x.size = File::GetSize(fullName);
x.size = File::GetFileSize(fullName);

x.startSector = fileList[fileIndex].firstBlock;
x.numSectors = (x.size+2047)/2048;
Expand Down
1 change: 1 addition & 0 deletions Core/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ void InitSysDirectories() {
if (!File::Exists(g_Config.memStickDirectory)) {
if (!File::CreateDir(g_Config.memStickDirectory))
g_Config.memStickDirectory = myDocsPath;
INFO_LOG(COMMON, "Memstick directory not present, creating at '%s'", g_Config.memStickDirectory.c_str());
}

const std::string testFile = g_Config.memStickDirectory + "/_writable_test.$$$";
Expand Down
22 changes: 0 additions & 22 deletions ext/native/gfx_es2/glsl_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,6 @@ GLSLProgram *glsl_create_source(const char *vshader_src, const char *fshader_src
return program;
}

bool glsl_up_to_date(GLSLProgram *program) {
struct stat vs, fs;
stat(program->vshader_filename, &vs);
stat(program->fshader_filename, &fs);
if ((time_t)vs.st_mtime != program->vshader_mtime ||
(time_t)fs.st_mtime != program->fshader_mtime) {
return false;
} else {
return true;
}
}

void glsl_refresh() {
ILOG("glsl_refresh()");
for (std::set<GLSLProgram *>::const_iterator iter = active_programs.begin();
iter != active_programs.end(); ++iter) {
if (!glsl_up_to_date(*iter)) {
glsl_recompile(*iter);
}
}
}

// Not wanting to change ReadLocalFile semantics.
// Needs to use delete [], not delete like auto_ptr, and can't use unique_ptr because of Symbian.
struct AutoCharArrayBuf {
Expand Down
5 changes: 0 additions & 5 deletions ext/native/gfx_es2/glsl_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,3 @@ void glsl_bind(const GLSLProgram *program);
void glsl_unbind();
int glsl_attrib_loc(const GLSLProgram *program, const char *name);
int glsl_uniform_loc(const GLSLProgram *program, const char *name);

// Expensive, try to only call this once per second or so and only when developing.
// fstat-s all the source files of all the shaders to see if they
// should be recompiled, and recompiles them if so.
void glsl_refresh();

0 comments on commit 58d3137

Please sign in to comment.