Skip to content

Commit

Permalink
use filebuf for cached file
Browse files Browse the repository at this point in the history
  • Loading branch information
wsobel committed Jan 27, 2024
1 parent a54ed34 commit 928faca
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
10 changes: 8 additions & 2 deletions src/mtconnect/sink/rest_sink/cached_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,14 @@ namespace mtconnect::sink::rest_sink {
if (cached)
{
allocate(m_size);
auto file = std::fopen(path.string().c_str(), "rb");
m_size = std::fread(m_buffer, 1, m_size, file);
std::filebuf file;
if (file.open(m_path, std::ios::binary | std::ios::in) != nullptr)
m_size = file.sgetn(m_buffer, m_size);
else
{
LOG(warning) << "Cannot open cached file: " << path;
m_cached = false;
}
}
m_lastWrite = std::filesystem::last_write_time(m_path);
}
Expand Down
2 changes: 1 addition & 1 deletion src/mtconnect/sink/rest_sink/rest_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ namespace mtconnect {
std::ofstream out(fc->m_path, std::ios::binary | std::ios_base::out);
if (!out.is_open())
throw std::runtime_error("Cannot open file for writing");

std::ostream_iterator<char, char> oi(out);

std::regex reg(
Expand Down
45 changes: 22 additions & 23 deletions test_package/config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ namespace {

fs::path copySampleFile(const std::string &src, fs::path target, chrono::seconds delta)
{
fs::path file { fs::path("samples") / src };
fs::path file {fs::path("samples") / src};
return copyFile(file, target, delta);
}

fs::path copyFile(const fs::path src, fs::path target, chrono::seconds delta)
{
fs::path file {fs::path(TEST_RESOURCE_DIR) / src};
Expand All @@ -109,7 +109,6 @@ namespace {
return target;
}


void replaceTextInFile(fs::path file, const std::string &from, const std::string &to)
{
ifstream is {file.string(), ios::binary | ios::ate};
Expand Down Expand Up @@ -2304,17 +2303,17 @@ Adapters {
ASSERT_TRUE(dev);
ASSERT_EQ("NEW-UUID", *(dev->getUuid()));
}

TEST_F(ConfigTest, should_update_stylesheet_versions)
{
fs::path root {createTempDirectory("14")};

fs::path styleDir { root / "styles" };
fs::path styleDir {root / "styles"};
fs::create_directory(styleDir);
fs::path styles { styleDir / "styles.xsl" };

fs::path styles {styleDir / "styles.xsl"};
copyFile("styles/styles.xsl", styles, 0min);

fs::path devices(root / "Devices.xml");
copySampleFile("empty.xml", devices, 0min);

Expand All @@ -2334,7 +2333,6 @@ Files {
}
DevicesStyle { Location = /styles/styles.xsl }
)DOC";

}

boost::program_options::variables_map options;
Expand All @@ -2345,10 +2343,10 @@ DevicesStyle { Location = /styles/styles.xsl }

ifstream file(styles);
ASSERT_TRUE(file.is_open());

stringstream sf;
sf << file.rdbuf();

ASSERT_EQ(R"DOC(<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
Expand All @@ -2367,21 +2365,22 @@ DevicesStyle { Location = /styles/styles.xsl }
<xsl:template match="/">
</xsl:template>
</xsl:stylesheet>
)DOC", sf.str());

)DOC",
sf.str());

m_config->stop();
}

TEST_F(ConfigTest, should_update_stylesheet_versions_with_path)
{
fs::path root {createTempDirectory("15")};

fs::path styleDir { root / "styles" };
fs::path styleDir {root / "styles"};
fs::create_directory(styleDir);
fs::path styles { styleDir / "styles.xsl" };

fs::path styles {styleDir / "styles.xsl"};
copyFile("styles/styles.xsl", styles, 0min);

fs::path devices(root / "Devices.xml");
copySampleFile("empty.xml", devices, 0min);

Expand All @@ -2398,7 +2397,6 @@ DevicesStyle {
Path = ./styles/styles.xsl
}
)DOC";

}

boost::program_options::variables_map options;
Expand All @@ -2409,10 +2407,10 @@ DevicesStyle {

ifstream file(styles);
ASSERT_TRUE(file.is_open());

stringstream sf;
sf << file.rdbuf();

ASSERT_EQ(R"DOC(<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
Expand All @@ -2431,8 +2429,9 @@ DevicesStyle {
<xsl:template match="/">
</xsl:template>
</xsl:stylesheet>
)DOC", sf.str());

)DOC",
sf.str());

m_config->stop();
}

Expand Down

0 comments on commit 928faca

Please sign in to comment.