]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common: add trailing newline before parsing cfg 33750/head
authorLucian Petrut <lpetrut@cloudbasesolutions.com>
Wed, 18 Nov 2020 13:17:18 +0000 (13:17 +0000)
committerLucian Petrut <lpetrut@cloudbasesolutions.com>
Wed, 18 Nov 2020 13:35:41 +0000 (13:35 +0000)
Ceph fails to parse config files that lack a trailing newline.
This mainly affects Windows, where text editors won't add one by
default.

This issue has been addressed by a previous commit [1] but that
code path is no longer reached [2][3], so we need to go down a level.

[1] 3b590314ac49b955b2123f03f4c4417e5534ec98
[2] http://paste.openstack.org/raw/800153/
[3] https://github.com/ceph/ceph/commit/e7dcc403b20922ffeeca114899f90a385bf25f0e

Signed-off-by: Lucian Petrut <lpetrut@cloudbasesolutions.com>
src/common/ConfUtils.cc

index 1ab84af80e9a0edf544bddeb1ca6c116a7bfd8de..6231c7a7a3da573fece0d18a67517d1484d6521b 100644 (file)
@@ -136,11 +136,6 @@ int ConfFile::parse_file(const std::string &fname,
   std::ifstream ifs{fname};
   std::string buffer{std::istreambuf_iterator<char>(ifs),
                                       std::istreambuf_iterator<char>()};
-  #ifdef _WIN32
-    // We'll need to ensure that there's a new line at the end of the file,
-    // otherwise the config parsing will fail.
-    buffer.append("\n");
-  #endif
   if (parse_buffer(buffer, warnings)) {
     return 0;
   } else {
@@ -255,17 +250,24 @@ struct IniGrammer : qi::grammar<Iterator, ConfFile(), Skipper>
 bool ConfFile::parse_buffer(std::string_view buf, std::ostream* err)
 {
   assert(err);
-  if (int err_pos = check_utf8(buf.data(), buf.size()); err_pos > 0) {
+#ifdef _WIN32
+  // We'll need to ensure that there's a new line at the end of the buffer,
+  // otherwise the config parsing will fail.
+  std::string _buf = std::string(buf) + "\n";
+#else
+  std::string_view _buf = buf;
+#endif
+  if (int err_pos = check_utf8(_buf.data(), _buf.size()); err_pos > 0) {
     *err << "parse error: invalid UTF-8 found at line "
-        << std::count(buf.begin(), std::next(buf.begin(), err_pos), '\n') + 1;
+        << std::count(_buf.begin(), std::next(_buf.begin(), err_pos), '\n') + 1;
     return false;
   }
-  using iter_t = boost::spirit::line_pos_iterator<decltype(buf.begin())>;
-  iter_t first{buf.begin()};
+  using iter_t = boost::spirit::line_pos_iterator<decltype(_buf.begin())>;
+  iter_t first{_buf.begin()};
   using skipper_t = qi::rule<iter_t>;
   IniGrammer<iter_t, skipper_t> grammar{first, *err};
   skipper_t skipper = grammar.continue_marker | grammar.comment;
-  return qi::phrase_parse(first, iter_t{buf.end()},
+  return qi::phrase_parse(first, iter_t{_buf.end()},
                          grammar, skipper, *this);
 }