Skip to content

Commit

Permalink
fs:: const renaming, fs::g_tls_error stub
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekotekina committed Aug 24, 2015
1 parent c2897cd commit 73b1087
Show file tree
Hide file tree
Showing 33 changed files with 197 additions and 122 deletions.
120 changes: 90 additions & 30 deletions Utilities/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ bool truncate_file(const std::string& file, u64 length)

#endif

thread_local fse fs::g_tls_error = fse::ok;

bool fs::stat(const std::string& path, stat_t& info)
{
g_tls_error = fse::ok;

#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA attrs;
if (!GetFileAttributesExW(to_wchar(path).get(), GetFileExInfoStandard, &attrs))
Expand Down Expand Up @@ -149,6 +153,8 @@ bool fs::stat(const std::string& path, stat_t& info)

bool fs::exists(const std::string& path)
{
g_tls_error = fse::ok;

#ifdef _WIN32
return GetFileAttributesW(to_wchar(path).get()) != 0xFFFFFFFF;
#else
Expand All @@ -159,6 +165,8 @@ bool fs::exists(const std::string& path)

bool fs::is_file(const std::string& file)
{
g_tls_error = fse::ok;

#ifdef _WIN32
DWORD attrs;
if ((attrs = GetFileAttributesW(to_wchar(file).get())) == INVALID_FILE_ATTRIBUTES)
Expand All @@ -180,6 +188,8 @@ bool fs::is_file(const std::string& file)

bool fs::is_dir(const std::string& dir)
{
g_tls_error = fse::ok;

#ifdef _WIN32
DWORD attrs;
if ((attrs = GetFileAttributesW(to_wchar(dir).get())) == INVALID_FILE_ATTRIBUTES)
Expand All @@ -201,6 +211,8 @@ bool fs::is_dir(const std::string& dir)

bool fs::create_dir(const std::string& dir)
{
g_tls_error = fse::ok;

#ifdef _WIN32
if (!CreateDirectoryW(to_wchar(dir).get(), NULL))
#else
Expand All @@ -216,6 +228,8 @@ bool fs::create_dir(const std::string& dir)

bool fs::create_path(const std::string& path)
{
g_tls_error = fse::ok;

size_t start = 0;

while (true)
Expand Down Expand Up @@ -258,6 +272,8 @@ bool fs::create_path(const std::string& path)

bool fs::remove_dir(const std::string& dir)
{
g_tls_error = fse::ok;

#ifdef _WIN32
if (!RemoveDirectoryW(to_wchar(dir).get()))
#else
Expand All @@ -273,6 +289,8 @@ bool fs::remove_dir(const std::string& dir)

bool fs::rename(const std::string& from, const std::string& to)
{
g_tls_error = fse::ok;

#ifdef _WIN32
if (!MoveFileW(to_wchar(from).get(), to_wchar(to).get()))
#else
Expand Down Expand Up @@ -324,6 +342,8 @@ int OSCopyFile(const char* source, const char* destination, bool overwrite)

bool fs::copy_file(const std::string& from, const std::string& to, bool overwrite)
{
g_tls_error = fse::ok;

#ifdef _WIN32
if (!CopyFileW(to_wchar(from).get(), to_wchar(to).get(), !overwrite))
#else
Expand All @@ -339,6 +359,8 @@ bool fs::copy_file(const std::string& from, const std::string& to, bool overwrit

bool fs::remove_file(const std::string& file)
{
g_tls_error = fse::ok;

#ifdef _WIN32
if (!DeleteFileW(to_wchar(file).get()))
#else
Expand All @@ -354,6 +376,8 @@ bool fs::remove_file(const std::string& file)

bool fs::truncate_file(const std::string& file, u64 length)
{
g_tls_error = fse::ok;

#ifdef _WIN32
if (!::truncate_file(file, length))
#else
Expand Down Expand Up @@ -383,62 +407,64 @@ bool fs::file::open(const std::string& filename, u32 mode)
{
this->close();

g_tls_error = fse::ok;

#ifdef _WIN32
DWORD access = 0;
switch (mode & (o_read | o_write | o_append))
{
case o_read: access |= GENERIC_READ; break;
case o_read | o_append: access |= GENERIC_READ; break;
case o_write: access |= GENERIC_WRITE; break;
case o_write | o_append: access |= FILE_APPEND_DATA; break;
case o_read | o_write: access |= GENERIC_READ | GENERIC_WRITE; break;
case o_read | o_write | o_append: access |= GENERIC_READ | FILE_APPEND_DATA; break;
switch (mode & (fom::read | fom::write | fom::append))
{
case fom::read: access |= GENERIC_READ; break;
case fom::read | fom::append: access |= GENERIC_READ; break;
case fom::write: access |= GENERIC_WRITE; break;
case fom::write | fom::append: access |= FILE_APPEND_DATA; break;
case fom::read | fom::write: access |= GENERIC_READ | GENERIC_WRITE; break;
case fom::read | fom::write | fom::append: access |= GENERIC_READ | FILE_APPEND_DATA; break;
default:
{
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither o_read nor o_write specified (0x%x)", filename, mode);
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither fom::read nor fom::write specified (0x%x)", filename, mode);
return false;
}
}

DWORD disp = 0;
switch (mode & (o_create | o_trunc | o_excl))
switch (mode & (fom::create | fom::trunc | fom::excl))
{
case 0: disp = OPEN_EXISTING; break;
case o_create: disp = OPEN_ALWAYS; break;
case o_trunc: disp = TRUNCATE_EXISTING; break;
case o_create | o_trunc: disp = CREATE_ALWAYS; break;
case o_create | o_excl: disp = CREATE_NEW; break;
case o_create | o_excl | o_trunc: disp = CREATE_NEW; break;
case fom::create: disp = OPEN_ALWAYS; break;
case fom::trunc: disp = TRUNCATE_EXISTING; break;
case fom::create | fom::trunc: disp = CREATE_ALWAYS; break;
case fom::create | fom::excl: disp = CREATE_NEW; break;
case fom::create | fom::excl | fom::trunc: disp = CREATE_NEW; break;
}

if (!disp || (mode & ~(o_read | o_write | o_append | o_create | o_trunc | o_excl)))
if (!disp || (mode & ~(fom::read | fom::write | fom::append | fom::create | fom::trunc | fom::excl)))
{
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: unknown mode specified (0x%x)", filename, mode);
return false;
}

m_fd = (intptr_t)CreateFileW(to_wchar(filename).get(), access, FILE_SHARE_READ, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
m_fd = (intptr_t)CreateFileW(to_wchar(filename).get(), access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
#else
int flags = 0;

switch (mode & (o_read | o_write))
switch (mode & (fom::read | fom::write))
{
case o_read: flags |= O_RDONLY; break;
case o_write: flags |= O_WRONLY; break;
case o_read | o_write: flags |= O_RDWR; break;
case fom::read: flags |= O_RDONLY; break;
case fom::write: flags |= O_WRONLY; break;
case fom::read | fom::write: flags |= O_RDWR; break;
default:
{
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither o_read nor o_write specified (0x%x)", filename, mode);
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither fom::read nor fom::write specified (0x%x)", filename, mode);
return false;
}
}

if (mode & o_append) flags |= O_APPEND;
if (mode & o_create) flags |= O_CREAT;
if (mode & o_trunc) flags |= O_TRUNC;
if (mode & o_excl) flags |= O_EXCL;
if (mode & fom::append) flags |= O_APPEND;
if (mode & fom::create) flags |= O_CREAT;
if (mode & fom::trunc) flags |= O_TRUNC;
if (mode & fom::excl) flags |= O_EXCL;

if (((mode & o_excl) && !(mode & o_create)) || (mode & ~(o_read | o_write | o_append | o_create | o_trunc | o_excl)))
if (((mode & fom::excl) && !(mode & fom::create)) || (mode & ~(fom::read | fom::write | fom::append | fom::create | fom::trunc | fom::excl)))
{
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: unknown mode specified (0x%x)", filename, mode);
return false;
Expand All @@ -458,6 +484,8 @@ bool fs::file::open(const std::string& filename, u32 mode)

bool fs::file::trunc(u64 size) const
{
g_tls_error = fse::ok;

#ifdef _WIN32
LARGE_INTEGER old, pos;

Expand All @@ -479,6 +507,8 @@ bool fs::file::trunc(u64 size) const

bool fs::file::stat(stat_t& info) const
{
g_tls_error = fse::ok;

#ifdef _WIN32
FILE_BASIC_INFO basic_info;

Expand Down Expand Up @@ -513,6 +543,8 @@ bool fs::file::stat(stat_t& info) const

bool fs::file::close()
{
g_tls_error = fse::ok;

if (m_fd == null)
{
return false;
Expand All @@ -530,6 +562,8 @@ bool fs::file::close()

u64 fs::file::read(void* buffer, u64 count) const
{
g_tls_error = fse::ok;

const int size = count <= INT_MAX ? static_cast<int>(count) : throw EXCEPTION("Invalid count (0x%llx)", count);

#ifdef _WIN32
Expand All @@ -547,6 +581,8 @@ u64 fs::file::read(void* buffer, u64 count) const

u64 fs::file::write(const void* buffer, u64 count) const
{
g_tls_error = fse::ok;

const int size = count <= INT_MAX ? static_cast<int>(count) : throw EXCEPTION("Invalid count (0x%llx)", count);

#ifdef _WIN32
Expand All @@ -562,27 +598,41 @@ u64 fs::file::write(const void* buffer, u64 count) const
#endif
}

u64 fs::file::seek(u64 offset, u32 mode) const
u64 fs::file::seek(s64 offset, fsm seek_mode) const
{
assert(mode < 3);
g_tls_error = fse::ok;

#ifdef _WIN32
LARGE_INTEGER pos;
pos.QuadPart = offset;

const DWORD mode =
seek_mode == fsm::begin ? FILE_BEGIN :
seek_mode == fsm::cur ? FILE_CURRENT :
seek_mode == fsm::end ? FILE_END :
throw EXCEPTION("Unknown seek_mode (0x%x)", seek_mode);

if (!SetFilePointerEx((HANDLE)m_fd, pos, &pos, mode))
{
return -1;
}

return pos.QuadPart;
#else
return ::lseek(m_fd, offset, mode);
const int whence =
seek_mode == fsm::begin ? SEEK_SET :
seek_mode == fsm::cur ? SEEK_CUR :
seek_mode == fsm::end ? SEEK_END :
throw EXCEPTION("Unknown seek_mode (0x%x)", seek_mode);

return ::lseek(m_fd, offset, whence);
#endif
}

u64 fs::file::size() const
{
g_tls_error = fse::ok;

#ifdef _WIN32
LARGE_INTEGER size;
if (!GetFileSizeEx((HANDLE)m_fd, &size))
Expand Down Expand Up @@ -625,6 +675,8 @@ void fs::dir::import(handle_type dd, const std::string& path)
#endif
}

g_tls_error = fse::ok;

m_dd = dd;

#ifdef _WIN32
Expand All @@ -646,6 +698,8 @@ bool fs::dir::open(const std::string& dirname)
#endif
}

g_tls_error = fse::ok;

m_dd = null;

m_path.reset();
Expand All @@ -667,6 +721,8 @@ bool fs::dir::open(const std::string& dirname)

bool fs::dir::close()
{
g_tls_error = fse::ok;

if (m_dd == null)
{
if (m_path)
Expand Down Expand Up @@ -703,6 +759,8 @@ bool fs::dir::get_first(std::string& name, stat_t& info)
#endif
}

g_tls_error = fse::ok;

m_dd = null;

if (!m_path)
Expand Down Expand Up @@ -739,6 +797,8 @@ bool fs::dir::get_first(std::string& name, stat_t& info)

bool fs::dir::get_next(std::string& name, stat_t& info)
{
g_tls_error = fse::ok;

if (m_dd == null)
{
return false;
Expand Down
Loading

0 comments on commit 73b1087

Please sign in to comment.