Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11089 from sepalani/pcap-share
NetworkCaptureLogger: Allow PCAP shared read access on Windows
  • Loading branch information
AdmiralCurtiss committed Jan 5, 2023
2 parents b6b46d8 + 481ddd1 commit 7b04a6b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
18 changes: 14 additions & 4 deletions Source/Core/Common/IOFile.cpp
Expand Up @@ -35,9 +35,10 @@ IOFile::IOFile(std::FILE* file) : m_file(file), m_good(true)
{
}

IOFile::IOFile(const std::string& filename, const char openmode[]) : m_file(nullptr), m_good(true)
IOFile::IOFile(const std::string& filename, const char openmode[], SharedAccess sh)
: m_file(nullptr), m_good(true)
{
Open(filename, openmode);
Open(filename, openmode, sh);
}

IOFile::~IOFile()
Expand All @@ -62,12 +63,21 @@ void IOFile::Swap(IOFile& other) noexcept
std::swap(m_good, other.m_good);
}

bool IOFile::Open(const std::string& filename, const char openmode[])
bool IOFile::Open(const std::string& filename, const char openmode[],
[[maybe_unused]] SharedAccess sh)
{
Close();

#ifdef _WIN32
m_good = _tfopen_s(&m_file, UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str()) == 0;
if (sh == SharedAccess::Default)
{
m_good = _tfopen_s(&m_file, UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str()) == 0;
}
else if (sh == SharedAccess::Read)
{
m_file = _tfsopen(UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str(), SH_DENYWR);
m_good = m_file != nullptr;
}
#else
#ifdef ANDROID
if (IsPathAndroidContent(filename))
Expand Down
12 changes: 10 additions & 2 deletions Source/Core/Common/IOFile.h
Expand Up @@ -20,6 +20,12 @@ enum class SeekOrigin
End,
};

enum class SharedAccess
{
Default,
Read,
};

// simple wrapper for cstdlib file functions to
// hopefully will make error checking easier
// and make forgetting an fclose() harder
Expand All @@ -28,7 +34,8 @@ class IOFile
public:
IOFile();
IOFile(std::FILE* file);
IOFile(const std::string& filename, const char openmode[]);
IOFile(const std::string& filename, const char openmode[],
SharedAccess sh = SharedAccess::Default);

~IOFile();

Expand All @@ -40,7 +47,8 @@ class IOFile

void Swap(IOFile& other) noexcept;

bool Open(const std::string& filename, const char openmode[]);
bool Open(const std::string& filename, const char openmode[],
SharedAccess sh = SharedAccess::Default);
bool Close();

template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/NetworkCaptureLogger.cpp
Expand Up @@ -83,8 +83,8 @@ PCAPSSLCaptureLogger::PCAPSSLCaptureLogger()
const std::string filepath =
fmt::format("{}{} {:%Y-%m-%d %Hh%Mm%Ss}.pcap", File::GetUserPath(D_DUMPSSL_IDX),
SConfig::GetInstance().GetGameID(), fmt::localtime(std::time(nullptr)));
m_file = std::make_unique<Common::PCAP>(new File::IOFile(filepath, "wb"),
Common::PCAP::LinkType::Ethernet);
m_file = std::make_unique<Common::PCAP>(
new File::IOFile(filepath, "wb", File::SharedAccess::Read), Common::PCAP::LinkType::Ethernet);
}

PCAPSSLCaptureLogger::~PCAPSSLCaptureLogger() = default;
Expand Down

0 comments on commit 7b04a6b

Please sign in to comment.