1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-22 18:53:28 +01:00
rpcs3/Utilities/File.cpp

775 lines
16 KiB
C++
Raw Normal View History

#include "stdafx.h"
2014-08-22 16:21:55 +02:00
#include "Log.h"
2015-04-24 23:38:11 +02:00
#include "File.h"
2014-07-11 19:06:59 +02:00
#ifdef _WIN32
#include <Windows.h>
2015-04-15 16:27:37 +02:00
#define GET_API_ERROR static_cast<u64>(GetLastError())
2015-04-25 21:15:53 +02:00
static_assert(fs::file::null == intptr_t(INVALID_HANDLE_VALUE) && fs::dir::null == fs::file::null, "Check fs::file::null definition");
2015-04-21 21:35:11 +02:00
std::unique_ptr<wchar_t[]> ConvertUTF8ToWChar(const std::string& source)
2015-04-13 20:10:31 +02:00
{
2015-04-15 16:27:37 +02:00
const size_t length = source.size() + 1; // size + null terminator
2015-04-15 20:33:44 +02:00
const int size = source.size() < INT_MAX ? static_cast<int>(length) : throw std::length_error(__FUNCTION__);
2015-04-15 16:27:37 +02:00
2015-04-21 21:35:11 +02:00
std::unique_ptr<wchar_t[]> buffer(new wchar_t[length]); // allocate buffer assuming that length is the max possible size
2015-04-15 16:27:37 +02:00
if (!MultiByteToWideChar(CP_UTF8, 0, source.c_str(), size, buffer.get(), size))
{
2015-04-18 15:38:42 +02:00
LOG_ERROR(GENERAL, "ConvertUTF8ToWChar(source='%s') failed: 0x%llx", source, GET_API_ERROR);
2014-07-11 19:06:59 +02:00
}
2015-04-15 16:27:37 +02:00
return buffer;
}
2015-04-25 21:15:53 +02:00
std::string ConvertWCharToUTF8(const wchar_t* source)
{
const int size = WideCharToMultiByte(CP_UTF8, 0, source, -1 /* NTS */, NULL, 0, NULL, NULL); // size
if (size < 1) throw std::length_error(__FUNCTION__); // ???
std::string result;
result.resize(size - 1);
if (!WideCharToMultiByte(CP_UTF8, 0, source, -1 /* NTS */, &result.front(), size, NULL, NULL))
{
LOG_ERROR(GENERAL, "ConvertWCharToUTF8() failed: 0x%llx", GET_API_ERROR);
}
return result;
}
2015-04-19 21:25:04 +02:00
time_t to_time_t(const ULARGE_INTEGER& ft)
2015-04-19 19:57:04 +02:00
{
return ft.QuadPart / 10000000ULL - 11644473600ULL;
}
2015-04-19 21:25:04 +02:00
time_t to_time_t(const LARGE_INTEGER& ft)
{
ULARGE_INTEGER v;
v.LowPart = ft.LowPart;
v.HighPart = ft.HighPart;
return to_time_t(v);
}
2015-04-15 16:27:37 +02:00
time_t to_time_t(const FILETIME& ft)
{
2015-04-19 21:25:04 +02:00
ULARGE_INTEGER v;
2015-04-15 16:27:37 +02:00
v.LowPart = ft.dwLowDateTime;
v.HighPart = ft.dwHighDateTime;
2015-04-19 19:57:04 +02:00
return to_time_t(v);
2014-07-11 19:06:59 +02:00
}
2015-04-15 16:27:37 +02:00
2015-04-25 21:15:53 +02:00
bool truncate_file(const std::string& file, u64 length)
2015-04-18 15:38:42 +02:00
{
2015-04-19 15:19:24 +02:00
// open the file
2015-04-18 15:38:42 +02:00
const auto handle = CreateFileW(ConvertUTF8ToWChar(file).get(), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE)
{
return false;
}
LARGE_INTEGER distance;
distance.QuadPart = length;
2015-04-19 15:19:24 +02:00
// seek and truncate
if (!SetFilePointerEx(handle, distance, NULL, FILE_BEGIN) || !SetEndOfFile(handle))
2015-04-18 15:38:42 +02:00
{
2015-04-19 15:19:24 +02:00
const auto error = GetLastError();
2015-04-18 15:38:42 +02:00
CloseHandle(handle);
2015-04-19 15:19:24 +02:00
SetLastError(error);
2015-04-18 15:38:42 +02:00
return false;
}
return CloseHandle(handle);
}
2015-04-13 16:46:10 +02:00
#else
2015-04-19 18:02:35 +02:00
#include <sys/stat.h>
#include <sys/types.h>
2015-04-25 22:21:06 +02:00
#include <dirent.h>
2015-04-13 16:46:10 +02:00
#include <fcntl.h>
#include <unistd.h>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <copyfile.h>
#else
#include <sys/sendfile.h>
#endif
2015-04-15 16:27:37 +02:00
#include "errno.h"
2015-04-13 16:46:10 +02:00
#define GET_API_ERROR static_cast<u64>(errno)
2015-04-15 16:27:37 +02:00
2014-10-24 15:24:09 +02:00
#endif
2015-04-24 23:38:11 +02:00
bool fs::stat(const std::string& path, stat_t& info)
2015-04-13 20:10:31 +02:00
{
#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA attrs;
2015-04-15 16:27:37 +02:00
if (!GetFileAttributesExW(ConvertUTF8ToWChar(path).get(), GetFileExInfoStandard, &attrs))
{
return false;
}
2015-04-15 16:27:37 +02:00
2015-04-24 23:38:11 +02:00
info.is_directory = (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
info.is_writable = (attrs.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
2015-04-25 21:15:53 +02:00
info.size = (u64)attrs.nFileSizeLow | ((u64)attrs.nFileSizeHigh << 32);
2015-04-15 16:27:37 +02:00
info.atime = to_time_t(attrs.ftLastAccessTime);
info.mtime = to_time_t(attrs.ftLastWriteTime);
info.ctime = to_time_t(attrs.ftCreationTime);
#else
struct stat64 file_info;
2015-04-15 17:12:10 +02:00
if (stat64(path.c_str(), &file_info) < 0)
2015-04-15 16:27:37 +02:00
{
return false;
}
2015-04-24 23:38:11 +02:00
info.is_directory = S_ISDIR(file_info.st_mode);
info.is_writable = file_info.st_mode & 0200; // HACK: approximation
2015-04-15 16:27:37 +02:00
info.size = file_info.st_size;
info.atime = file_info.st_atime;
info.mtime = file_info.st_mtime;
info.ctime = file_info.st_ctime;
#endif
2015-04-25 21:15:53 +02:00
return true;
}
2015-04-24 23:38:11 +02:00
bool fs::exists(const std::string& path)
2015-04-20 17:53:31 +02:00
{
#ifdef _WIN32
return GetFileAttributesW(ConvertUTF8ToWChar(path).get()) != 0xFFFFFFFF;
#else
struct stat buffer;
return stat(path.c_str(), &buffer) == 0;
#endif
}
2015-04-24 23:38:11 +02:00
bool fs::is_file(const std::string& file)
2015-04-20 17:53:31 +02:00
{
#ifdef _WIN32
DWORD attrs;
if ((attrs = GetFileAttributesW(ConvertUTF8ToWChar(file).get())) == INVALID_FILE_ATTRIBUTES)
{
return false;
}
return (attrs & FILE_ATTRIBUTE_DIRECTORY) == 0;
#else
struct stat64 file_info;
if (stat64(file.c_str(), &file_info) < 0)
{
return false;
}
return !S_ISDIR(file_info.st_mode);
#endif
}
2015-04-24 23:38:11 +02:00
bool fs::is_dir(const std::string& dir)
2015-04-15 16:27:37 +02:00
{
#ifdef _WIN32
DWORD attrs;
if ((attrs = GetFileAttributesW(ConvertUTF8ToWChar(dir).get())) == INVALID_FILE_ATTRIBUTES)
{
return false;
}
return (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;
#else
struct stat64 file_info;
2015-04-15 17:12:10 +02:00
if (stat64(dir.c_str(), &file_info) < 0)
2015-04-15 16:27:37 +02:00
{
return false;
}
return S_ISDIR(file_info.st_mode);
#endif
}
2015-04-24 23:38:11 +02:00
bool fs::create_dir(const std::string& dir)
{
2014-08-29 15:06:58 +02:00
#ifdef _WIN32
2015-04-18 15:38:42 +02:00
if (!CreateDirectoryW(ConvertUTF8ToWChar(dir).get(), NULL))
2014-08-29 15:06:58 +02:00
#else
2015-04-18 15:38:42 +02:00
if (mkdir(dir.c_str(), 0777))
2014-08-29 15:06:58 +02:00
#endif
2015-04-18 15:38:42 +02:00
{
2015-04-20 03:54:19 +02:00
LOG_WARNING(GENERAL, "Error creating directory '%s': 0x%llx", dir, GET_API_ERROR);
2015-04-18 15:38:42 +02:00
return false;
}
return true;
}
2015-04-24 23:38:11 +02:00
bool fs::create_path(const std::string& path)
{
2015-04-20 17:53:31 +02:00
size_t start = 0;
while (true)
{
// maybe it could be more optimal if goes from the end recursively
size_t pos = path.find_first_of("/\\", start);
2015-04-20 17:53:31 +02:00
if (pos == std::string::npos)
{
pos = path.length();
2015-04-20 17:53:31 +02:00
}
std::string dir = path.substr(0, pos);
start = ++pos;
2015-04-20 17:53:31 +02:00
if (dir.size() == 0)
{
continue;
}
2015-04-20 17:53:31 +02:00
2015-04-24 23:38:11 +02:00
if (!is_dir(dir))
2015-04-20 17:53:31 +02:00
{
// if doesn't exist or not a dir
2015-04-24 23:38:11 +02:00
if (!create_dir(dir))
2015-04-20 17:53:31 +02:00
{
// if creating failed
return false;
}
}
2014-08-05 14:14:08 +02:00
if (pos >= path.length())
2015-04-20 17:53:31 +02:00
{
break;
}
}
2015-04-20 17:53:31 +02:00
return true;
}
2015-04-24 23:38:11 +02:00
bool fs::remove_dir(const std::string& dir)
{
#ifdef _WIN32
2015-04-17 06:37:13 +02:00
if (!RemoveDirectoryW(ConvertUTF8ToWChar(dir).get()))
2014-10-24 15:24:09 +02:00
#else
2015-03-31 11:20:25 +02:00
if (rmdir(dir.c_str()))
2014-10-24 15:24:09 +02:00
#endif
{
2015-04-20 03:54:19 +02:00
LOG_WARNING(GENERAL, "Error deleting directory '%s': 0x%llx", dir, GET_API_ERROR);
return false;
}
2015-04-13 16:46:10 +02:00
return true;
}
2015-04-24 23:38:11 +02:00
bool fs::rename(const std::string& from, const std::string& to)
{
// TODO: Deal with case-sensitivity
#ifdef _WIN32
2015-04-17 06:37:13 +02:00
if (!MoveFileW(ConvertUTF8ToWChar(from).get(), ConvertUTF8ToWChar(to).get()))
#else
2015-04-13 16:46:10 +02:00
if (rename(from.c_str(), to.c_str()))
#endif
{
2015-04-20 03:54:19 +02:00
LOG_WARNING(GENERAL, "Error renaming '%s' to '%s': 0x%llx", from, to, GET_API_ERROR);
2015-04-13 16:46:10 +02:00
return false;
}
return true;
}
#ifndef _WIN32
int OSCopyFile(const char* source, const char* destination, bool overwrite)
{
/* This function was taken from http://stackoverflow.com/questions/2180079/how-can-i-copy-a-file-on-unix-using-c */
int input, output;
if ((input = open(source, O_RDONLY)) == -1)
{
return -1;
}
2015-04-13 21:06:01 +02:00
if ((output = open(destination, O_WRONLY | O_CREAT | (overwrite ? O_TRUNC : O_EXCL), 0666)) == -1)
2015-04-13 16:46:10 +02:00
{
close(input);
return -1;
}
//Here we use kernel-space copying for performance reasons
#if defined(__APPLE__) || defined(__FreeBSD__)
//fcopyfile works on FreeBSD and OS X 10.5+
int result = fcopyfile(input, output, 0, COPYFILE_ALL);
#else
//sendfile will work with non-socket output (i.e. regular file) on Linux 2.6.33+
off_t bytesCopied = 0;
struct stat fileinfo = { 0 };
fstat(input, &fileinfo);
2015-04-13 20:35:44 +02:00
int result = sendfile(output, input, &bytesCopied, fileinfo.st_size) == -1 ? -1 : 0;
2015-04-13 16:46:10 +02:00
#endif
close(input);
close(output);
return result;
}
#endif
2015-04-24 23:38:11 +02:00
bool fs::copy_file(const std::string& from, const std::string& to, bool overwrite)
2015-04-13 16:46:10 +02:00
{
#ifdef _WIN32
2015-04-17 06:37:13 +02:00
if (!CopyFileW(ConvertUTF8ToWChar(from).get(), ConvertUTF8ToWChar(to).get(), !overwrite))
2015-04-13 16:46:10 +02:00
#else
if (OSCopyFile(from.c_str(), to.c_str(), overwrite))
#endif
2015-03-16 17:20:02 +01:00
{
2015-04-20 03:54:19 +02:00
LOG_WARNING(GENERAL, "Error copying '%s' to '%s': 0x%llx", from, to, GET_API_ERROR);
2015-03-16 17:20:02 +01:00
return false;
}
2015-04-13 16:46:10 +02:00
2015-03-16 17:20:02 +01:00
return true;
}
2015-04-24 23:38:11 +02:00
bool fs::remove_file(const std::string& file)
{
#ifdef _WIN32
2015-04-17 06:37:13 +02:00
if (!DeleteFileW(ConvertUTF8ToWChar(file).get()))
#else
2015-03-31 11:20:25 +02:00
if (unlink(file.c_str()))
2014-10-24 15:24:09 +02:00
#endif
{
2015-04-20 03:54:19 +02:00
LOG_WARNING(GENERAL, "Error deleting file '%s': 0x%llx", file, GET_API_ERROR);
2014-08-04 01:52:36 +02:00
return false;
}
2015-04-18 15:38:42 +02:00
return true;
}
2015-04-25 21:15:53 +02:00
bool fs::truncate_file(const std::string& file, u64 length)
2015-04-18 15:38:42 +02:00
{
#ifdef _WIN32
2015-04-24 23:38:11 +02:00
if (!::truncate_file(file, length))
2015-04-18 15:38:42 +02:00
#else
2015-04-19 15:19:24 +02:00
if (truncate64(file.c_str(), length))
2015-04-18 15:38:42 +02:00
#endif
{
2015-04-20 03:54:19 +02:00
LOG_WARNING(GENERAL, "Error resizing file '%s' to 0x%llx: 0x%llx", file, length, GET_API_ERROR);
2015-04-18 15:38:42 +02:00
return false;
}
2014-08-04 01:52:36 +02:00
return true;
}
2015-04-24 23:38:11 +02:00
fs::file::~file()
{
2015-04-25 21:15:53 +02:00
if (m_fd != null)
{
2015-04-19 15:19:24 +02:00
#ifdef _WIN32
2015-04-25 21:15:53 +02:00
CloseHandle((HANDLE)m_fd);
2015-04-19 15:19:24 +02:00
#else
2015-04-25 22:24:45 +02:00
::close(m_fd);
2015-04-19 15:19:24 +02:00
#endif
2015-04-25 21:15:53 +02:00
}
2015-04-19 18:02:35 +02:00
}
2015-04-24 23:38:11 +02:00
bool fs::file::open(const std::string& filename, u32 mode)
{
2015-04-24 23:38:11 +02:00
this->~file();
2015-04-19 15:19:24 +02:00
#ifdef _WIN32
DWORD access = 0;
2015-04-24 16:06:30 +02:00
switch (mode & (o_read | o_write | o_append))
2015-04-19 18:02:35 +02:00
{
case o_read: access |= GENERIC_READ; break;
2015-04-24 16:06:30 +02:00
case o_read | o_append: access |= GENERIC_READ; break;
2015-04-19 18:02:35 +02:00
case o_write: access |= GENERIC_WRITE; break;
2015-04-24 16:06:30 +02:00
case o_write | o_append: access |= FILE_APPEND_DATA; break;
2015-04-19 18:02:35 +02:00
case o_read | o_write: access |= GENERIC_READ | GENERIC_WRITE; break;
2015-04-24 16:06:30 +02:00
case o_read | o_write | o_append: access |= GENERIC_READ | FILE_APPEND_DATA; break;
2015-04-19 18:02:35 +02:00
default:
{
2015-04-24 23:38:11 +02:00
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither o_read nor o_write specified (0x%x)", filename, mode);
2015-04-19 18:02:35 +02:00
return false;
}
}
2015-04-19 15:19:24 +02:00
DWORD disp = 0;
switch (mode & (o_create | o_trunc | o_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;
2015-04-20 17:53:31 +02:00
case o_create | o_excl | o_trunc: disp = CREATE_NEW; break;
2015-04-19 18:02:35 +02:00
}
2015-04-24 16:06:30 +02:00
if (!disp || (mode & ~(o_read | o_write | o_append | o_create | o_trunc | o_excl)))
2015-04-19 15:19:24 +02:00
{
2015-04-24 23:38:11 +02:00
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: unknown mode specified (0x%x)", filename, mode);
2015-04-19 15:19:24 +02:00
return false;
}
2015-04-25 21:15:53 +02:00
m_fd = (intptr_t)CreateFileW(ConvertUTF8ToWChar(filename).get(), access, FILE_SHARE_READ, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
2015-04-19 15:19:24 +02:00
#else
int flags = 0;
2015-04-19 18:02:35 +02:00
switch (mode & (o_read | o_write))
{
case o_read: flags |= O_RDONLY; break;
case o_write: flags |= O_WRONLY; break;
2015-04-19 18:02:35 +02:00
case o_read | o_write: flags |= O_RDWR; break;
default:
{
2015-04-24 23:38:11 +02:00
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: neither o_read nor o_write specified (0x%x)", filename, mode);
2015-04-19 18:02:35 +02:00
return false;
}
}
2015-04-24 16:06:30 +02:00
if (mode & o_append) flags |= O_APPEND;
2015-04-19 15:19:24 +02:00
if (mode & o_create) flags |= O_CREAT;
if (mode & o_trunc) flags |= O_TRUNC;
if (mode & o_excl) flags |= O_EXCL;
2015-04-24 16:06:30 +02:00
if (((mode & o_excl) && !(mode & o_create)) || (mode & ~(o_read | o_write | o_append | o_create | o_trunc | o_excl)))
2015-04-19 18:02:35 +02:00
{
2015-04-24 23:38:11 +02:00
LOG_ERROR(GENERAL, "fs::file::open('%s') failed: unknown mode specified (0x%x)", filename, mode);
2015-04-19 18:02:35 +02:00
return false;
}
2015-04-25 21:15:53 +02:00
m_fd = ::open(filename.c_str(), flags, 0666);
2015-04-19 15:19:24 +02:00
#endif
2015-04-25 21:15:53 +02:00
if (m_fd == null)
2015-04-20 03:54:19 +02:00
{
2015-04-24 23:38:11 +02:00
LOG_WARNING(GENERAL, "fs::file::open('%s', 0x%x) failed: error 0x%llx", filename, mode, GET_API_ERROR);
2015-04-20 03:54:19 +02:00
return false;
}
2015-04-20 03:54:19 +02:00
return true;
}
2015-04-24 23:38:11 +02:00
bool fs::file::trunc(u64 size) const
{
2015-04-19 15:19:24 +02:00
#ifdef _WIN32
LARGE_INTEGER old, pos;
2015-04-19 15:19:24 +02:00
pos.QuadPart = 0;
2015-04-25 21:15:53 +02:00
SetFilePointerEx((HANDLE)m_fd, pos, &old, FILE_CURRENT); // get old position
2015-04-19 15:19:24 +02:00
pos.QuadPart = size;
2015-04-25 21:15:53 +02:00
SetFilePointerEx((HANDLE)m_fd, pos, NULL, FILE_BEGIN); // set new position
2015-04-19 15:19:24 +02:00
2015-04-25 21:15:53 +02:00
SetEndOfFile((HANDLE)m_fd); // change file size
2015-04-19 19:57:04 +02:00
2015-04-25 21:15:53 +02:00
SetFilePointerEx((HANDLE)m_fd, old, NULL, FILE_BEGIN); // restore position
2015-04-19 15:19:24 +02:00
return true; // TODO
#else
2015-04-25 21:15:53 +02:00
return !ftruncate64(m_fd, size);
2015-04-19 15:19:24 +02:00
#endif
}
2015-04-24 23:38:11 +02:00
bool fs::file::stat(stat_t& info) const
2015-04-19 19:57:04 +02:00
{
#ifdef _WIN32
FILE_BASIC_INFO basic_info;
2015-04-25 21:15:53 +02:00
if (!GetFileInformationByHandleEx((HANDLE)m_fd, FileBasicInfo, &basic_info, sizeof(FILE_BASIC_INFO)))
2015-04-19 19:57:04 +02:00
{
return false;
}
2015-04-24 23:38:11 +02:00
info.is_directory = (basic_info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
info.is_writable = (basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
2015-04-19 19:57:04 +02:00
info.size = this->size();
info.atime = to_time_t(basic_info.LastAccessTime);
info.mtime = to_time_t(basic_info.ChangeTime);
info.ctime = to_time_t(basic_info.CreationTime);
#else
struct stat64 file_info;
2015-04-25 21:15:53 +02:00
if (fstat64(m_fd, &file_info) < 0)
2015-04-19 19:57:04 +02:00
{
return false;
}
2015-04-24 23:38:11 +02:00
info.is_directory = S_ISDIR(file_info.st_mode);
info.is_writable = file_info.st_mode & 0200; // HACK: approximation
2015-04-19 19:57:04 +02:00
info.size = file_info.st_size;
info.atime = file_info.st_atime;
info.mtime = file_info.st_mtime;
info.ctime = file_info.st_ctime;
#endif
2015-04-25 21:15:53 +02:00
2015-04-19 19:57:04 +02:00
return true;
}
2015-04-24 23:38:11 +02:00
bool fs::file::close()
{
2015-04-25 21:15:53 +02:00
if (m_fd == null)
2015-04-19 15:19:24 +02:00
{
2015-04-25 21:15:53 +02:00
return false;
2015-04-19 15:19:24 +02:00
}
2015-04-24 16:06:30 +02:00
2015-04-25 21:15:53 +02:00
auto fd = m_fd;
m_fd = null;
2015-04-24 16:06:30 +02:00
2015-04-25 21:15:53 +02:00
#ifdef _WIN32
return CloseHandle((HANDLE)fd);
#else
return !::close(fd);
2015-04-19 15:19:24 +02:00
#endif
}
2015-04-24 23:38:11 +02:00
u64 fs::file::read(void* buffer, u64 count) const
{
2015-04-19 15:19:24 +02:00
#ifdef _WIN32
DWORD nread;
2015-04-25 21:15:53 +02:00
if (!ReadFile((HANDLE)m_fd, buffer, count, &nread, NULL))
2015-04-19 15:19:24 +02:00
{
return -1;
}
return nread;
#else
2015-04-25 21:15:53 +02:00
return ::read(m_fd, buffer, count);
2015-04-19 15:19:24 +02:00
#endif
}
2015-04-24 23:38:11 +02:00
u64 fs::file::write(const void* buffer, u64 count) const
{
2015-04-19 15:19:24 +02:00
#ifdef _WIN32
DWORD nwritten;
2015-04-25 21:15:53 +02:00
if (!WriteFile((HANDLE)m_fd, buffer, count, &nwritten, NULL))
2015-04-19 15:19:24 +02:00
{
return -1;
}
return nwritten;
#else
2015-04-25 21:15:53 +02:00
return ::write(m_fd, buffer, count);
2015-04-19 15:19:24 +02:00
#endif
}
2015-04-24 23:38:11 +02:00
u64 fs::file::seek(u64 offset, u32 mode) const
{
2015-04-19 15:19:24 +02:00
assert(mode < 3);
#ifdef _WIN32
LARGE_INTEGER pos;
pos.QuadPart = offset;
2015-04-25 21:15:53 +02:00
if (!SetFilePointerEx((HANDLE)m_fd, pos, &pos, mode))
2015-04-19 15:19:24 +02:00
{
return -1;
}
return pos.QuadPart;
#else
2015-04-25 21:15:53 +02:00
return lseek64(m_fd, offset, mode);
2015-04-19 15:19:24 +02:00
#endif
}
2015-04-24 23:38:11 +02:00
u64 fs::file::size() const
{
2015-04-19 15:19:24 +02:00
#ifdef _WIN32
LARGE_INTEGER size;
2015-04-25 21:15:53 +02:00
if (!GetFileSizeEx((HANDLE)m_fd, &size))
2015-04-19 15:19:24 +02:00
{
return -1;
}
return size.QuadPart;
#else
struct stat64 file_info;
2015-04-25 21:15:53 +02:00
if (fstat64(m_fd, &file_info) < 0)
2015-04-19 15:19:24 +02:00
{
return -1;
}
return file_info.st_size;
#endif
}
2015-04-25 21:15:53 +02:00
fs::dir::~dir()
{
2015-04-25 21:15:53 +02:00
if (m_dd != null)
{
#ifdef _WIN32
FindClose((HANDLE)m_dd);
#else
2015-04-25 22:10:39 +02:00
::closedir((DIR*)m_dd);
2015-04-25 21:15:53 +02:00
#endif
}
}
2015-04-25 21:15:53 +02:00
void fs::dir::import(handle_type dd, const std::string& path)
{
2015-04-25 21:15:53 +02:00
if (m_dd != null)
{
#ifdef _WIN32
FindClose((HANDLE)m_dd);
#else
2015-04-25 22:10:39 +02:00
::closedir((DIR*)m_dd);
2015-04-25 21:15:53 +02:00
#endif
}
2015-04-25 21:15:53 +02:00
m_dd = dd;
#ifdef _WIN32
m_path = ConvertUTF8ToWChar(path);
#else
m_path.reset(new char[path.size() + 1]);
memcpy(m_path.get(), path.c_str(), path.size() + 1);
#endif
}
2015-04-25 21:15:53 +02:00
bool fs::dir::open(const std::string& dirname)
{
2015-04-25 21:15:53 +02:00
if (m_dd != null)
{
#ifdef _WIN32
FindClose((HANDLE)m_dd);
#else
2015-04-25 22:10:39 +02:00
::closedir((DIR*)m_dd);
2015-04-25 21:15:53 +02:00
#endif
}
m_dd = null;
m_path.reset();
if (!is_dir(dirname))
{
return false;
}
#ifdef _WIN32
m_path = ConvertUTF8ToWChar(dirname + "/*");
#else
m_path.reset(new char[dirname.size() + 1]);
memcpy(m_path.get(), dirname.c_str(), dirname.size() + 1);
#endif
return true;
}
2015-04-25 21:15:53 +02:00
bool fs::dir::close()
2014-08-06 00:34:26 +02:00
{
2015-04-25 21:15:53 +02:00
if (m_dd == null)
{
if (m_path)
{
m_path.reset();
return true;
}
else
{
return false;
}
}
auto dd = m_dd;
m_dd = null;
m_path.reset();
#ifdef _WIN32
return FindClose((HANDLE)dd);
#else
2015-04-25 22:10:39 +02:00
return !::closedir((DIR*)dd);
2015-04-25 21:15:53 +02:00
#endif
2014-08-06 00:34:26 +02:00
}
2015-04-25 21:15:53 +02:00
bool fs::dir::get_first(std::string& name, stat_t& info)
{
2015-04-25 21:15:53 +02:00
if (m_dd != null) // close previous handle
{
#ifdef _WIN32
FindClose((HANDLE)m_dd);
#else
2015-04-25 22:10:39 +02:00
::closedir((DIR*)m_dd);
2015-04-25 21:15:53 +02:00
#endif
}
m_dd = null;
if (!m_path)
{
return false;
}
#ifdef _WIN32
WIN32_FIND_DATAW found;
m_dd = (intptr_t)FindFirstFileW(m_path.get(), &found);
2015-04-25 22:55:15 +02:00
if (m_dd == null)
{
return false;
}
name = ConvertWCharToUTF8(found.cFileName);
info.is_directory = (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
info.is_writable = (found.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
info.size = ((u64)found.nFileSizeHigh << 32) | (u64)found.nFileSizeLow;
info.atime = to_time_t(found.ftLastAccessTime);
info.mtime = to_time_t(found.ftLastWriteTime);
info.ctime = to_time_t(found.ftCreationTime);
return true;
2015-04-25 21:15:53 +02:00
#else
2015-04-25 22:24:45 +02:00
m_dd = (intptr_t)::opendir(m_path.get());
2015-04-25 21:15:53 +02:00
return get_next(name, info);
2015-04-25 22:55:15 +02:00
#endif
}
2015-04-25 21:15:53 +02:00
bool fs::dir::get_next(std::string& name, stat_t& info)
{
2015-04-25 21:15:53 +02:00
if (m_dd == null)
{
return false;
}
#ifdef _WIN32
WIN32_FIND_DATAW found;
if (!FindNextFileW((HANDLE)m_dd, &found))
{
return false;
}
name = ConvertWCharToUTF8(found.cFileName);
info.is_directory = (found.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
info.is_writable = (found.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
info.size = ((u64)found.nFileSizeHigh << 32) | (u64)found.nFileSizeLow;
info.atime = to_time_t(found.ftLastAccessTime);
info.mtime = to_time_t(found.ftLastWriteTime);
info.ctime = to_time_t(found.ftCreationTime);
#else
2015-04-25 22:10:39 +02:00
const auto found = ::readdir((DIR*)m_dd);
2015-04-25 21:15:53 +02:00
struct stat64 file_info;
2015-04-26 01:29:53 +02:00
if (!found || fstatat64(::dirfd((DIR*)m_dd), found->d_name, &file_info, 0) < 0)
2015-04-25 21:15:53 +02:00
{
return false;
}
2015-04-25 22:24:45 +02:00
name = found->d_name;
2015-04-25 21:15:53 +02:00
info.is_directory = S_ISDIR(file_info.st_mode);
info.is_writable = file_info.st_mode & 0200; // HACK: approximation
info.size = file_info.st_size;
info.atime = file_info.st_atime;
info.mtime = file_info.st_mtime;
info.ctime = file_info.st_ctime;
#endif
return true;
}