Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove fstream usage from hostmisc
  • Loading branch information
filipnavara committed Jan 25, 2025
commit 09c6433d90c23347ea5ee12f5f6d409a698c78c4
12 changes: 0 additions & 12 deletions src/native/corehost/hostmisc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include <cstring>
Expand Down Expand Up @@ -118,14 +117,6 @@ namespace pal
typedef wchar_t char_t;
typedef std::wstring string_t;
typedef std::wstringstream stringstream_t;
// TODO: Agree on the correct encoding of the files: The PoR for now is to
// temporarily wchar for Windows and char for Unix. Current implementation
// implicitly expects the contents on both Windows and Unix as char and
// converts them to wchar in code for Windows. This line should become:
// typedef std::basic_ifstream<char_t> ifstream_t.
typedef std::basic_ifstream<char> ifstream_t;
typedef std::istreambuf_iterator<ifstream_t::char_type> istreambuf_iterator_t;
typedef std::basic_istream<char> istream_t;
typedef HRESULT hresult_t;
typedef HMODULE dll_t;
typedef FARPROC proc_t;
Expand Down Expand Up @@ -207,9 +198,6 @@ namespace pal
typedef char char_t;
typedef std::string string_t;
typedef std::stringstream stringstream_t;
typedef std::basic_ifstream<char> ifstream_t;
typedef std::istreambuf_iterator<ifstream_t::char_type> istreambuf_iterator_t;
typedef std::basic_istream<char> istream_t;
typedef int hresult_t;
typedef void* dll_t;
typedef void* proc_t;
Expand Down
18 changes: 5 additions & 13 deletions src/native/corehost/hostmisc/pal.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ bool get_install_location_from_file(const pal::string_t& file_path, bool& file_f
{
file_found = true;
bool install_location_found = false;
FILE* install_location_file = pal::file_open(file_path, "r");
FILE* install_location_file = pal::file_open(file_path, _X("r"));
if (install_location_file != nullptr)
{
if (!get_line_from_file(install_location_file, install_location))
Expand Down Expand Up @@ -819,12 +819,10 @@ pal::string_t pal::get_current_os_rid_platform()
{
// Read the file to get ID and VERSION_ID data that will be used
// to construct the RID.
std::fstream fsVersionFile;

fsVersionFile.open(versionFile, std::fstream::in);
FILE* fsVersionFile = pal::file_open(versionFile, _X("r"));

// Proceed only if we were able to open the file
if (fsVersionFile.good())
if (fsVersionFile != nullptr)
{
pal::string_t line;
pal::string_t strID(_X("ID="));
Expand All @@ -834,11 +832,8 @@ pal::string_t pal::get_current_os_rid_platform()

bool fFoundID = false, fFoundVersion = false;

// Read the first line
std::getline(fsVersionFile, line);

// Loop until we are at the end of file
while (!fsVersionFile.eof())
while (get_line_from_file(fsVersionFile, line))
{
// Look for ID if we have not found it already
if (!fFoundID)
Expand Down Expand Up @@ -872,13 +867,10 @@ pal::string_t pal::get_current_os_rid_platform()
// We have everything we need to form the RID - break out of the loop.
break;
}

// Read the next line
std::getline(fsVersionFile, line);
}

// Close the file now that we are done with it.
fsVersionFile.close();
fclose(fsVersionFile);

if (fFoundID)
{
Expand Down
31 changes: 20 additions & 11 deletions src/native/corehost/json_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@
namespace {

// Try to match 0xEF 0xBB 0xBF byte sequence (no endianness here.)
std::streampos get_utf8_bom_length(pal::istream_t& stream)
int get_utf8_bom_length(FILE *stream)
{
if (stream.eof())
if (feof(stream))
{
return 0;
}

auto peeked = stream.peek();
auto peeked = fgetc(stream);
if (peeked == EOF || ((peeked & 0xFF) != 0xEF))
{
ungetc(peeked, stream);
return 0;
}

unsigned char bytes[3];
stream.read(reinterpret_cast<char*>(bytes), 3);
if ((stream.gcount() < 3) || (bytes[1] != 0xBB) || (bytes[2] != 0xBF))
size_t ret = fread(reinterpret_cast<char*>(bytes), 1, 3, stream);
if ((ret < 3) || (bytes[1] != 0xBB) || (bytes[2] != 0xBF))
{
return 0;
}
Expand Down Expand Up @@ -132,26 +133,34 @@ bool json_parser_t::parse_file(const pal::string_t& path)
}
}

pal::ifstream_t file{ path };
if (!file.good())
FILE *file = pal::file_open(path, _X("r"));
if (file == nullptr)
{
trace::error(_X("Cannot use file stream for [%s]: %s"), path.c_str(), pal::strerror(errno).c_str());
return false;
}

auto current_pos = ::get_utf8_bom_length(file);
file.seekg(0, file.end);
auto stream_size = file.tellg();
fseek(file, 0, SEEK_END);
auto stream_size = ftell(file);
if (stream_size == -1)
{
fclose(file);
trace::error(_X("Failed to get size of file [%s]"), path.c_str());
return false;
}

file.seekg(current_pos, file.beg);
fseek(file, current_pos, SEEK_SET);

realloc_buffer(static_cast<size_t>(stream_size - current_pos));
file.read(m_json.data(), stream_size - current_pos);
auto ret = fread(m_json.data(), 1, stream_size - current_pos, file);
fclose(file);

if (ret != (size_t)(stream_size - current_pos))
{
trace::error(_X("Failed to read contents of file [%s]"), path.c_str());
return false;
}

return parse_raw_data(m_json.data(), m_json.size(), path);
}
Expand Down
1 change: 1 addition & 0 deletions src/native/corehost/test/nativehost/host_context_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

#include <iostream>
#include <fstream>
#include <pal.h>
#include <error_codes.h>
#include <future>
Expand Down