Skip to content

Commit 928faca

Browse files
committed
use filebuf for cached file
1 parent a54ed34 commit 928faca

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

src/mtconnect/sink/rest_sink/cached_file.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,14 @@ namespace mtconnect::sink::rest_sink {
9494
if (cached)
9595
{
9696
allocate(m_size);
97-
auto file = std::fopen(path.string().c_str(), "rb");
98-
m_size = std::fread(m_buffer, 1, m_size, file);
97+
std::filebuf file;
98+
if (file.open(m_path, std::ios::binary | std::ios::in) != nullptr)
99+
m_size = file.sgetn(m_buffer, m_size);
100+
else
101+
{
102+
LOG(warning) << "Cannot open cached file: " << path;
103+
m_cached = false;
104+
}
99105
}
100106
m_lastWrite = std::filesystem::last_write_time(m_path);
101107
}

src/mtconnect/sink/rest_sink/rest_service.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ namespace mtconnect {
309309
std::ofstream out(fc->m_path, std::ios::binary | std::ios_base::out);
310310
if (!out.is_open())
311311
throw std::runtime_error("Cannot open file for writing");
312-
312+
313313
std::ostream_iterator<char, char> oi(out);
314314

315315
std::regex reg(

test_package/config_test.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ namespace {
9393

9494
fs::path copySampleFile(const std::string &src, fs::path target, chrono::seconds delta)
9595
{
96-
fs::path file { fs::path("samples") / src };
96+
fs::path file {fs::path("samples") / src};
9797
return copyFile(file, target, delta);
9898
}
99-
99+
100100
fs::path copyFile(const fs::path src, fs::path target, chrono::seconds delta)
101101
{
102102
fs::path file {fs::path(TEST_RESOURCE_DIR) / src};
@@ -109,7 +109,6 @@ namespace {
109109
return target;
110110
}
111111

112-
113112
void replaceTextInFile(fs::path file, const std::string &from, const std::string &to)
114113
{
115114
ifstream is {file.string(), ios::binary | ios::ate};
@@ -2304,17 +2303,17 @@ Adapters {
23042303
ASSERT_TRUE(dev);
23052304
ASSERT_EQ("NEW-UUID", *(dev->getUuid()));
23062305
}
2307-
2306+
23082307
TEST_F(ConfigTest, should_update_stylesheet_versions)
23092308
{
23102309
fs::path root {createTempDirectory("14")};
23112310

2312-
fs::path styleDir { root / "styles" };
2311+
fs::path styleDir {root / "styles"};
23132312
fs::create_directory(styleDir);
2314-
2315-
fs::path styles { styleDir / "styles.xsl" };
2313+
2314+
fs::path styles {styleDir / "styles.xsl"};
23162315
copyFile("styles/styles.xsl", styles, 0min);
2317-
2316+
23182317
fs::path devices(root / "Devices.xml");
23192318
copySampleFile("empty.xml", devices, 0min);
23202319

@@ -2334,7 +2333,6 @@ Files {
23342333
}
23352334
DevicesStyle { Location = /styles/styles.xsl }
23362335
)DOC";
2337-
23382336
}
23392337

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

23462344
ifstream file(styles);
23472345
ASSERT_TRUE(file.is_open());
2348-
2346+
23492347
stringstream sf;
23502348
sf << file.rdbuf();
2351-
2349+
23522350
ASSERT_EQ(R"DOC(<?xml version="1.0" encoding="UTF-8"?>
23532351
<xsl:stylesheet version="1.0"
23542352
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
@@ -2367,21 +2365,22 @@ DevicesStyle { Location = /styles/styles.xsl }
23672365
<xsl:template match="/">
23682366
</xsl:template>
23692367
</xsl:stylesheet>
2370-
)DOC", sf.str());
2371-
2368+
)DOC",
2369+
sf.str());
2370+
23722371
m_config->stop();
23732372
}
2374-
2373+
23752374
TEST_F(ConfigTest, should_update_stylesheet_versions_with_path)
23762375
{
23772376
fs::path root {createTempDirectory("15")};
23782377

2379-
fs::path styleDir { root / "styles" };
2378+
fs::path styleDir {root / "styles"};
23802379
fs::create_directory(styleDir);
2381-
2382-
fs::path styles { styleDir / "styles.xsl" };
2380+
2381+
fs::path styles {styleDir / "styles.xsl"};
23832382
copyFile("styles/styles.xsl", styles, 0min);
2384-
2383+
23852384
fs::path devices(root / "Devices.xml");
23862385
copySampleFile("empty.xml", devices, 0min);
23872386

@@ -2398,7 +2397,6 @@ DevicesStyle {
23982397
Path = ./styles/styles.xsl
23992398
}
24002399
)DOC";
2401-
24022400
}
24032401

24042402
boost::program_options::variables_map options;
@@ -2409,10 +2407,10 @@ DevicesStyle {
24092407

24102408
ifstream file(styles);
24112409
ASSERT_TRUE(file.is_open());
2412-
2410+
24132411
stringstream sf;
24142412
sf << file.rdbuf();
2415-
2413+
24162414
ASSERT_EQ(R"DOC(<?xml version="1.0" encoding="UTF-8"?>
24172415
<xsl:stylesheet version="1.0"
24182416
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
@@ -2431,8 +2429,9 @@ DevicesStyle {
24312429
<xsl:template match="/">
24322430
</xsl:template>
24332431
</xsl:stylesheet>
2434-
)DOC", sf.str());
2435-
2432+
)DOC",
2433+
sf.str());
2434+
24362435
m_config->stop();
24372436
}
24382437

0 commit comments

Comments
 (0)