Skip to content

Commit

Permalink
Remove FileOpenFlags
Browse files Browse the repository at this point in the history
Backport commit from reverted fs change vmangos#2746
  • Loading branch information
0blu committed Sep 7, 2024
1 parent 60ddc5c commit 2e30f60
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/realmd/ClientPatchCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void ClientPatchCache::LoadPatchesInfo()

for (const std::string& filePath : IO::Filesystem::GetAllFilesInFolder(fullFolderPath, IO::Filesystem::OutputFilePath::FullFilePath))
{
auto fileHandle = IO::Filesystem::TryOpenFileReadonly(filePath, IO::Filesystem::FileOpenFlags::HintSequentialRead);
auto fileHandle = IO::Filesystem::TryOpenFileReadonly(filePath);
if (fileHandle)
{
sLog.Out(LOG_BASIC, LOG_LVL_DEBUG, "[PatchCache] Calculate hash of %s", filePath.c_str());
Expand Down
9 changes: 1 addition & 8 deletions src/shared/IO/Filesystem/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
#include "FileHandle.h"

namespace IO { namespace Filesystem {
enum class FileOpenFlags
{
None = 0,
HintSequentialRead = (1 << 0), // Hint to the OS, that the file will be read sequentially. (The OS will read ahead and preload the file into memory)
};
DEFINE_ENUM_FLAG(FileOpenFlags);

enum class OutputFilePath
{
JustFileName,
Expand All @@ -25,7 +18,7 @@ namespace IO { namespace Filesystem {
/// You have to check the resulting pointer for nullptr!
/// If the file does not exists or you dont have permission to open it the ptr will be null
[[nodiscard("You need to use the file handle, otherwise the file will close immediately again")]]
std::unique_ptr<IO::Filesystem::FileHandleReadonly> TryOpenFileReadonly(std::string const& filePath, EnumFlag<FileOpenFlags> flags = IO::Filesystem::FileOpenFlags::None);
std::unique_ptr<IO::Filesystem::FileHandleReadonly> TryOpenFileReadonly(std::string const& filePath);

/// Will convert a partial path like "./data/myCoolFile.txt" to a complete absolute path like "/home/user/data/myCoolFile.txt"
[[nodiscard]]
Expand Down
32 changes: 1 addition & 31 deletions src/shared/IO/Filesystem/impl/unix/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// This function will open a file in read shared and binary mode
/// You have to check the resulting pointer for nullptr!
/// If the file does not exists or you dont have permission to open it the ptr will be null
std::unique_ptr<IO::Filesystem::FileHandleReadonly> IO::Filesystem::TryOpenFileReadonly(std::string const& filePath, EnumFlag<FileOpenFlags> flags)
std::unique_ptr<IO::Filesystem::FileHandleReadonly> IO::Filesystem::TryOpenFileReadonly(std::string const& filePath)
{
int nativeFlags = O_RDONLY;
IO::Native::FileHandle fileHandle = ::open(filePath.c_str(), nativeFlags);
Expand All @@ -20,36 +20,6 @@ std::unique_ptr<IO::Filesystem::FileHandleReadonly> IO::Filesystem::TryOpenFileR
return nullptr;
}

#if defined(__linux__)
// Tell Kernel: we might need the file in the near future, please preload it into mem
if (::posix_fadvise(fileHandle, 0, 0, POSIX_FADV_WILLNEED) != 0)
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "Failed to set WILLNEED hint for file");

if (flags.HasFlag(FileOpenFlags::HintSequentialRead))
{
// Tell Kernel: to preallocate and load memory pages while reading
if (::posix_fadvise(fileHandle, 0, 0, POSIX_FADV_SEQUENTIAL) != 0)
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "Failed to set SEQUENTIAL hint for file");
}
#elif defined(__APPLE__)
{ // Tell Kernel: we might need the file in the near future, please preload it into mem
struct radvisory radv;
radv.ra_offset = 0; // 0 = start of file
radv.ra_count = 0; // 0 = means read whole file if possible
if (::fcntl(fileHandle, F_RDADVISE, &radv) == -1)
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "Failed to set RDADVISE hint for file");
}

if (flags.HasFlag(FileOpenFlags::HintSequentialRead))
{
// Tell Kernel: to preallocate and load memory pages while reading
if (::fcntl(fileHandle, F_RDAHEAD, 1) == -1)
sLog.Out(LOG_BASIC, LOG_LVL_ERROR, "Failed to set SEQUENTIAL hint for file");
}
#else
#warning "IO::Filesystem::TryOpenFileReadonly(...) hints are not supported on your platform"
#endif

return std::unique_ptr<FileHandleReadonly>(new FileHandleReadonly(filePath, fileHandle));
}

Expand Down
10 changes: 2 additions & 8 deletions src/shared/IO/Filesystem/impl/windows/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,15 @@
/// This function will open a file in read shared and binary mode
/// You have to check the resulting pointer for nullptr!
/// If the file does not exists or you dont have permission to open it the ptr will be null
std::unique_ptr<IO::Filesystem::FileHandleReadonly> IO::Filesystem::TryOpenFileReadonly(std::string const& filePath, EnumFlag<FileOpenFlags> flags)
std::unique_ptr<IO::Filesystem::FileHandleReadonly> IO::Filesystem::TryOpenFileReadonly(std::string const& filePath)
{
DWORD nativeFlags = FILE_ATTRIBUTE_NORMAL;
if (flags.HasFlag(FileOpenFlags::HintSequentialRead))
{
nativeFlags |= FILE_FLAG_SEQUENTIAL_SCAN;
}

HANDLE nativeFileHandle = CreateFileA(
filePath.c_str(),
GENERIC_READ,
FILE_SHARE_READ, // Share mode: allow other processes to read
nullptr, // Security attributes
OPEN_EXISTING, // Open exising file. Fail if it does not exist
nativeFlags,
FILE_ATTRIBUTE_NORMAL, // Normal open, without any special flags
nullptr // Template file handle (would be used when creating a new file and copy the attributes)
);

Expand Down

0 comments on commit 2e30f60

Please sign in to comment.