_fixes
This commit is contained in:
@@ -4,31 +4,28 @@
|
||||
|
||||
#include "UnixFileSystem.h"
|
||||
#include "Engine/Platform/File.h"
|
||||
#include "Engine/Platform/StringUtils.h"
|
||||
#include "Engine/Core/Types/String.h"
|
||||
#include "Engine/Core/Types/StringBuilder.h"
|
||||
#include "Engine/Core/Types/StringView.h"
|
||||
#include "Engine/Core/Types/TimeSpan.h"
|
||||
#include "Engine/Core/Collections/Array.h"
|
||||
#include "Engine/Core/Math/Math.h"
|
||||
#include "Engine/Core/Log.h"
|
||||
#include "Engine/Utilities/StringConverter.h"
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sendfile.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <cerrno>
|
||||
#include <dirent.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#if PLATFORM_MAC || PLATFORM_IOS
|
||||
typedef StringAsANSI<> UnixString;
|
||||
#else
|
||||
typedef StringAsUTF8<> UnixString;
|
||||
#endif
|
||||
|
||||
const DateTime UnixEpoch(1970, 1, 1);
|
||||
|
||||
bool UnixFileSystem::CreateDirectory(const StringView& path)
|
||||
{
|
||||
const StringAsUTF8<> pathAnsi(*path, path.Length());
|
||||
const UnixString pathAnsi(*path, path.Length());
|
||||
|
||||
// Skip if already exists
|
||||
struct stat fileInfo;
|
||||
@@ -119,7 +116,7 @@ bool DeleteUnixPathTree(const char* path)
|
||||
|
||||
bool UnixFileSystem::DeleteDirectory(const String& path, bool deleteContents)
|
||||
{
|
||||
const StringAsUTF8<> pathANSI(*path, path.Length());
|
||||
const UnixString pathANSI(*path, path.Length());
|
||||
if (deleteContents)
|
||||
{
|
||||
return DeleteUnixPathTree(pathANSI.Get());
|
||||
@@ -133,7 +130,7 @@ bool UnixFileSystem::DeleteDirectory(const String& path, bool deleteContents)
|
||||
bool UnixFileSystem::DirectoryExists(const StringView& path)
|
||||
{
|
||||
struct stat fileInfo;
|
||||
const StringAsUTF8<> pathANSI(*path, path.Length());
|
||||
const UnixString pathANSI(*path, path.Length());
|
||||
if (stat(pathANSI.Get(), &fileInfo) != -1)
|
||||
{
|
||||
return S_ISDIR(fileInfo.st_mode);
|
||||
@@ -143,8 +140,8 @@ bool UnixFileSystem::DirectoryExists(const StringView& path)
|
||||
|
||||
bool UnixFileSystem::DirectoryGetFiles(Array<String>& results, const String& path, const Char* searchPattern, DirectorySearchOption option)
|
||||
{
|
||||
const StringAsUTF8<> pathANSI(*path, path.Length());
|
||||
const StringAsUTF8<> searchPatternANSI(searchPattern);
|
||||
const UnixString pathANSI(*path, path.Length());
|
||||
const UnixString searchPatternANSI(searchPattern);
|
||||
|
||||
// Check if use only top directory
|
||||
if (option == DirectorySearchOption::TopDirectoryOnly)
|
||||
@@ -158,7 +155,7 @@ bool UnixFileSystem::GetChildDirectories(Array<String>& results, const String& p
|
||||
DIR* dir;
|
||||
struct stat statPath, statEntry;
|
||||
struct dirent* entry;
|
||||
const StringAsUTF8<> pathANSI(*path, path.Length());
|
||||
const UnixString pathANSI(*path, path.Length());
|
||||
const char* pathStr = pathANSI.Get();
|
||||
|
||||
// Stat for the path
|
||||
@@ -214,7 +211,7 @@ bool UnixFileSystem::GetChildDirectories(Array<String>& results, const String& p
|
||||
bool UnixFileSystem::FileExists(const StringView& path)
|
||||
{
|
||||
struct stat fileInfo;
|
||||
const StringAsUTF8<> pathANSI(*path, path.Length());
|
||||
const UnixString pathANSI(*path, path.Length());
|
||||
if (stat(pathANSI.Get(), &fileInfo) != -1)
|
||||
{
|
||||
return S_ISREG(fileInfo.st_mode);
|
||||
@@ -224,7 +221,7 @@ bool UnixFileSystem::FileExists(const StringView& path)
|
||||
|
||||
bool UnixFileSystem::DeleteFile(const StringView& path)
|
||||
{
|
||||
const StringAsUTF8<> pathANSI(*path, path.Length());
|
||||
const UnixString pathANSI(*path, path.Length());
|
||||
return unlink(pathANSI.Get()) != 0;
|
||||
}
|
||||
|
||||
@@ -232,7 +229,7 @@ uint64 UnixFileSystem::GetFileSize(const StringView& path)
|
||||
{
|
||||
struct stat fileInfo;
|
||||
fileInfo.st_size = 0;
|
||||
const StringAsUTF8<> pathANSI(*path, path.Length());
|
||||
const UnixString pathANSI(*path, path.Length());
|
||||
if (stat(pathANSI.Get(), &fileInfo) != -1)
|
||||
{
|
||||
// Check for directories
|
||||
@@ -246,7 +243,7 @@ uint64 UnixFileSystem::GetFileSize(const StringView& path)
|
||||
|
||||
bool UnixFileSystem::IsReadOnly(const StringView& path)
|
||||
{
|
||||
const StringAsUTF8<> pathANSI(*path, path.Length());
|
||||
const UnixString pathANSI(*path, path.Length());
|
||||
if (access(pathANSI.Get(), W_OK) == -1)
|
||||
{
|
||||
return errno == EACCES;
|
||||
@@ -256,7 +253,7 @@ bool UnixFileSystem::IsReadOnly(const StringView& path)
|
||||
|
||||
bool UnixFileSystem::SetReadOnly(const StringView& path, bool isReadOnly)
|
||||
{
|
||||
const StringAsUTF8<> pathANSI(*path, path.Length());
|
||||
const UnixString pathANSI(*path, path.Length());
|
||||
struct stat fileInfo;
|
||||
if (stat(pathANSI.Get(), &fileInfo) != -1)
|
||||
{
|
||||
@@ -283,15 +280,15 @@ bool UnixFileSystem::MoveFile(const StringView& dst, const StringView& src, bool
|
||||
|
||||
if (overwrite)
|
||||
{
|
||||
unlink(StringAsUTF8<>(*dst, dst.Length()).Get());
|
||||
unlink(UnixString(*dst, dst.Length()).Get());
|
||||
}
|
||||
if (rename(StringAsUTF8<>(*src, src.Length()).Get(), StringAsUTF8<>(*dst, dst.Length()).Get()) != 0)
|
||||
if (rename(UnixString(*src, src.Length()).Get(), UnixString(*dst, dst.Length()).Get()) != 0)
|
||||
{
|
||||
if (errno == EXDEV)
|
||||
{
|
||||
if (!CopyFile(dst, src))
|
||||
{
|
||||
unlink(StringAsUTF8<>(*src, src.Length()).Get());
|
||||
unlink(UnixString(*src, src.Length()).Get());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -300,78 +297,6 @@ bool UnixFileSystem::MoveFile(const StringView& dst, const StringView& src, bool
|
||||
return false;
|
||||
}
|
||||
|
||||
bool UnixFileSystem::CopyFile(const StringView& dst, const StringView& src)
|
||||
{
|
||||
const StringAsUTF8<> srcANSI(*src, src.Length());
|
||||
const StringAsUTF8<> dstANSI(*dst, dst.Length());
|
||||
|
||||
int srcFile, dstFile;
|
||||
char buffer[4096];
|
||||
ssize_t readSize;
|
||||
int cachedError;
|
||||
|
||||
srcFile = open(srcANSI.Get(), O_RDONLY);
|
||||
if (srcFile < 0)
|
||||
return true;
|
||||
dstFile = open(dstANSI.Get(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
||||
if (dstFile < 0)
|
||||
goto out_error;
|
||||
|
||||
// first try the kernel method
|
||||
struct stat statBuf;
|
||||
fstat(srcFile, &statBuf);
|
||||
readSize = 1;
|
||||
while (readSize > 0)
|
||||
{
|
||||
readSize = sendfile(dstFile, srcFile, 0, statBuf.st_size);
|
||||
}
|
||||
// sendfile could fail for example if the input file is not nmap'able
|
||||
// in this case we fall back to the read/write loop
|
||||
if (readSize < 0)
|
||||
{
|
||||
while (readSize = read(srcFile, buffer, sizeof(buffer)), readSize > 0)
|
||||
{
|
||||
char* ptr = buffer;
|
||||
ssize_t writeSize;
|
||||
|
||||
do
|
||||
{
|
||||
writeSize = write(dstFile, ptr, readSize);
|
||||
if (writeSize >= 0)
|
||||
{
|
||||
readSize -= writeSize;
|
||||
ptr += writeSize;
|
||||
}
|
||||
else if (errno != EINTR)
|
||||
{
|
||||
goto out_error;
|
||||
}
|
||||
} while (readSize > 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (readSize == 0)
|
||||
{
|
||||
if (close(dstFile) < 0)
|
||||
{
|
||||
dstFile = -1;
|
||||
goto out_error;
|
||||
}
|
||||
close(srcFile);
|
||||
|
||||
// Success
|
||||
return false;
|
||||
}
|
||||
|
||||
out_error:
|
||||
cachedError = errno;
|
||||
close(srcFile);
|
||||
if (dstFile >= 0)
|
||||
close(dstFile);
|
||||
errno = cachedError;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UnixFileSystem::getFilesFromDirectoryTop(Array<String>& results, const char* path, const char* searchPattern)
|
||||
{
|
||||
size_t pathLength;
|
||||
@@ -532,7 +457,7 @@ bool UnixFileSystem::getFilesFromDirectoryAll(Array<String>& results, const char
|
||||
DateTime UnixFileSystem::GetFileLastEditTime(const StringView& path)
|
||||
{
|
||||
struct stat fileInfo;
|
||||
const StringAsUTF8<> pathANSI(*path, path.Length());
|
||||
const UnixString pathANSI(*path, path.Length());
|
||||
if (stat(pathANSI.Get(), &fileInfo) == -1)
|
||||
{
|
||||
return DateTime::MinValue();
|
||||
|
||||
Reference in New Issue
Block a user