@@ -54,7 +54,29 @@ class keychain_base
5454 std::string uid_hash;
5555};
5656
57- fc::variant open_keyfile (const char * filename);
57+ template <typename char_t >
58+ fc::variant open_keyfile (const char_t * filename)
59+ {
60+ std::ifstream fin = std::ifstream (filename);
61+ if (!fin.is_open ())
62+ throw std::runtime_error (" Error: cannot open keyfile" );
63+ std::array<char , 1024 > read_buf;
64+ memset (read_buf.data (), 0x00 , read_buf.size ());
65+ auto pbuf = read_buf.data ();
66+ auto it = read_buf.begin ();
67+ size_t read_count = 0 ;
68+ while (!fin.eof ()&&fin.good ())
69+ {
70+ fin.getline (pbuf, std::distance (it, read_buf.end ()));
71+ pbuf += fin.gcount () - 1 ;
72+ it += fin.gcount () - 1 ;
73+ read_count += fin.gcount () - 1 ;
74+ }
75+ if (!fin.good ()&&read_count==0 )
76+ throw std::runtime_error (" Error: cannot read keyfile" );
77+ return fc::json::from_string (std::string (read_buf.begin (), read_buf.end ()), fc::json::strict_parser);
78+ }
79+
5880void create_keyfile (const char * filename, const fc::variant& keyfile_var);
5981secp256_private_key get_priv_key_from_str (const std::string& str);
6082fc::sha256 get_hash (const keychain_app::unit_list_t &list);
@@ -111,11 +133,13 @@ struct find_keyfile_by_username
111133 , m_keyfile(keyfile)
112134 {
113135 }
136+
114137 bool operator ()(bfs::directory_entry &unit)
115138 {
116139 if (!bfs::is_regular_file (unit.status ()))
117140 return false ;
118141 const auto &file_path = unit.path ().filename ();
142+
119143 auto j_keyfile = open_keyfile (file_path.c_str ());
120144 auto keyfile = j_keyfile.as <keyfile_format::keyfile_t >();
121145 if (m_keyfile)
0 commit comments