Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of fc/io/fstream.hpp #2047

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
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
8 changes: 4 additions & 4 deletions libraries/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
#include <graphene/net/core_messages.hpp>
#include <graphene/net/exceptions.hpp>

#include <graphene/utilities/file_util.hpp>
#include <graphene/utilities/key_conversion.hpp>
#include <graphene/chain/worker_evaluator.hpp>

#include <fc/asio.hpp>
#include <fc/io/fstream.hpp>
#include <fc/rpc/api_connection.hpp>
#include <fc/rpc/websocket_api.hpp>
#include <fc/network/resolve.hpp>
Expand Down Expand Up @@ -392,8 +392,8 @@ void application_impl::startup()
ilog("Initializing database...");
if( _options->count("genesis-json") )
{
std::string genesis_str;
fc::read_file_contents( _options->at("genesis-json").as<boost::filesystem::path>(), genesis_str );
const std::string genesis_file = _options->at("genesis-json").as<std::string>();
std::string genesis_str = graphene::utilities::read_file_contents( genesis_file );
graphene::chain::genesis_state_type genesis = fc::json::from_string( genesis_str ).as<graphene::chain::genesis_state_type>( 20 );
bool modified_genesis = false;
if( _options->count("genesis-timestamp") )
Expand Down Expand Up @@ -1039,7 +1039,7 @@ void application::set_program_options(boost::program_options::options_descriptio
"Endpoint for TLS websocket RPC to listen on")
("server-pem,p", bpo::value<string>()->implicit_value("server.pem"), "The TLS certificate file for this server")
("server-pem-password,P", bpo::value<string>()->implicit_value(""), "Password for this certificate")
("genesis-json", bpo::value<boost::filesystem::path>(), "File to read Genesis State from")
("genesis-json", bpo::value<std::string>(), "File to read Genesis State from")
("dbg-init-key", bpo::value<string>(), "Block signing key to use for init witnesses, overrides genesis file")
("api-access", bpo::value<boost::filesystem::path>(), "JSON file specifying API permissions")
("io-threads", bpo::value<uint16_t>()->implicit_value(0), "Number of IO threads, default to 0 for auto-configuration")
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ add_library( graphene_chain
)

add_dependencies( graphene_chain build_hardfork_hpp )
target_link_libraries( graphene_chain fc graphene_db graphene_protocol )
target_link_libraries( graphene_chain fc graphene_db graphene_protocol graphene_utilities )
target_include_directories( graphene_chain
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_BINARY_DIR}/include" )

Expand Down
12 changes: 6 additions & 6 deletions libraries/chain/db_management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
#include <graphene/chain/operation_history_object.hpp>

#include <graphene/protocol/fee_schedule.hpp>

#include <fc/io/fstream.hpp>
#include <graphene/utilities/file_util.hpp>

#include <fstream>
#include <functional>
Expand Down Expand Up @@ -182,15 +181,16 @@ void database::open(
wipe_object_db = true;
else
{
std::string version_string;
fc::read_file_contents( data_dir / "db_version", version_string );
std::string version_string = graphene::utilities::read_file_contents( (data_dir / "db_version").string() );
wipe_object_db = ( version_string != db_version );
}
if( wipe_object_db ) {
ilog("Wiping object_database due to missing or wrong version");
object_database::wipe( data_dir );
std::ofstream version_file( (data_dir / "db_version").generic_string().c_str(),
std::ios::out | std::ios::binary | std::ios::trunc );
std::ofstream version_file( (data_dir / "db_version").generic_string(),
std::ios::binary | std::ios::trunc );
FC_ASSERT( !version_file.fail() && !version_file.bad(), "Failed to open file '${f}'",
("f",(data_dir / "db_version").string()) );
version_file.write( db_version.c_str(), db_version.size() );
version_file.close();
}
Expand Down
8 changes: 5 additions & 3 deletions libraries/plugins/snapshot/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

#include <graphene/chain/database.hpp>

#include <fc/io/fstream.hpp>
#include <fstream>
#include <ios>

using namespace graphene::snapshot_plugin;
using std::string;
Expand Down Expand Up @@ -87,10 +88,11 @@ void snapshot_plugin::plugin_shutdown() {}
static void create_snapshot( const graphene::chain::database& db, const fc::path& dest )
{
ilog("snapshot plugin: creating snapshot");
fc::ofstream out;
std::ofstream out;
try
{
out.open( dest );
out.open( dest.string(), std::ios_base::binary | std::ios_base::trunc );
FC_ASSERT( !out.fail() && !out.bad(), "Failed to open file '${f}'", ("f",dest.string()) );
}
catch ( fc::exception& e )
{
Expand Down
8 changes: 4 additions & 4 deletions libraries/plugins/witness/witness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
#include <graphene/utilities/key_conversion.hpp>

#include <fc/thread/thread.hpp>
#include <fc/io/fstream.hpp>

#include <boost/filesystem/path.hpp>

#include <fstream>
#include <iostream>

using namespace graphene::witness_plugin;
Expand Down Expand Up @@ -138,9 +138,9 @@ void witness_plugin::plugin_initialize(const boost::program_options::variables_m
{
if (fc::exists(key_id_to_wif_pair_file))
{
std::string file_content;
fc::read_file_contents(key_id_to_wif_pair_file, file_content);
std::istringstream file_content_as_stream(file_content);
std::ifstream file_content_as_stream( key_id_to_wif_pair_file.string() );
FC_ASSERT( !file_content_as_stream.fail() && !file_content_as_stream.bad(),
"Failed to open file '${f}'", ("f",key_id_to_wif_pair_file.string()) );

std::string line; // key_id_to_wif_pair_string
while (std::getline(file_content_as_stream, line))
Expand Down
1 change: 1 addition & 0 deletions libraries/utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ endif(NOT GRAPHENE_GIT_REVISION_DESCRIPTION)
file(GLOB HEADERS "include/graphene/utilities/*.hpp")

set(sources
file_util.cpp
key_conversion.cpp
string_escape.cpp
tempdir.cpp
Expand Down
49 changes: 49 additions & 0 deletions libraries/utilities/file_util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <graphene/utilities/file_util.hpp>

#include <fc/exception/exception.hpp>

#include <ios>
#include <fstream>

namespace graphene { namespace utilities {

std::string read_file_contents( const std::string& path )
{
std::ifstream input( path );
FC_ASSERT( !input.fail() && !input.bad(), "Failed to open file '${f}'", ("f",path) );
input.seekg( 0, std::ios_base::end );
const auto size = input.tellg();
input.seekg( 0 );
std::vector<char> result;
result.resize( size );
input.read( result.data(), size );
FC_ASSERT( size == input.gcount(), "Incomplete file read from '${f}', expected ${s} but got ${c}?!",
("f",path)("s",size)("c",input.gcount()) );
return std::string( result.begin(), result.end() );
abitmore marked this conversation as resolved.
Show resolved Hide resolved
}

} } // graphene::utilities
38 changes: 38 additions & 0 deletions libraries/utilities/include/graphene/utilities/file_util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

#include <string>

namespace graphene { namespace utilities {

/**
* @brief Reads the file at the given path and returns the full contents in a string. Note that the result
* will contain non-printable characters if the file does.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I saw "non-printable characters", '\0' didn't come to my mind. I don't know whether it's only me though. Perhaps better if we explicitly mention '\0'? Callers need to be careful when using this function, since functions like c_str() are heavily used in the code base, a strcpy() or strlen() call on the result of c_str() may return unexpected results.

* @param path the pathname of the file to read
* @return the file contents as a string
*/
std::string read_file_contents( const std::string& path );

} } // graphene::utilities
1 change: 0 additions & 1 deletion libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
#include <boost/multi_index/hashed_index.hpp>

#include <fc/git_revision.hpp>
#include <fc/io/fstream.hpp>
#include <fc/io/json.hpp>
#include <fc/io/stdio.hpp>
#include <fc/network/http/websocket.hpp>
Expand Down
14 changes: 9 additions & 5 deletions libraries/wallet/wallet_api_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@
#include <fc/popcount.hpp>
#include <fc/git_revision.hpp>
#include <fc/thread/scoped_lock.hpp>
#include <fc/io/fstream.hpp>

#include <graphene/wallet/wallet.hpp>
#include "wallet_api_impl.hpp"

#include <graphene/utilities/file_util.hpp>
#include <graphene/utilities/git_revision.hpp>

#ifndef WIN32
# include <sys/types.h>
# include <sys/stat.h>
#endif

#include <fstream>
#include <ios>

// explicit instantiation for later use
namespace fc {
template class api<graphene::wallet::wallet_api, identity_member_with_optionals>;
Expand Down Expand Up @@ -440,16 +444,16 @@ namespace graphene { namespace wallet { namespace detail {
// http://en.wikipedia.org/wiki/Most_vexing_parse
//
std::string tmp_wallet_filename = wallet_filename + ".tmp";
fc::ofstream outfile{ fc::path( tmp_wallet_filename ) };
std::ofstream outfile( tmp_wallet_filename, std::ios_base::trunc );
FC_ASSERT( !outfile.fail() && !outfile.bad(), "Failed to open file '${f}'",
("f",tmp_wallet_filename) );
outfile.write( data.c_str(), data.length() );
outfile.flush();
outfile.close();

wlog( "saved successfully wallet to tmp file ${fn}", ("fn", tmp_wallet_filename) );

std::string wallet_file_content;
fc::read_file_contents(tmp_wallet_filename, wallet_file_content);

std::string wallet_file_content = graphene::utilities::read_file_contents( tmp_wallet_filename );
if (wallet_file_content == data) {
wlog( "validated successfully tmp wallet file ${fn}", ("fn", tmp_wallet_filename) );

Expand Down
11 changes: 5 additions & 6 deletions programs/genesis_util/genesis_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
#include <iostream>
#include <iterator>

#include <fc/io/fstream.hpp>
#include <fc/io/json.hpp>
#include <fc/io/stdio.hpp>

#include <graphene/app/api.hpp>
#include <graphene/protocol/address.hpp>
#include <graphene/egenesis/egenesis.hpp>
#include <graphene/utilities/file_util.hpp>
#include <graphene/utilities/key_conversion.hpp>

#include <boost/filesystem.hpp>
Expand Down Expand Up @@ -61,7 +61,7 @@ int main( int argc, char** argv )
bpo::options_description cli_options("BitShares empty blocks");
cli_options.add_options()
("help,h", "Print this help message and exit.")
("genesis-json,g", bpo::value<boost::filesystem::path>(), "File to read genesis state from")
("genesis-json,g", bpo::value<std::string>(), "File to read genesis state from")
("out,o", bpo::value<boost::filesystem::path>(), "File to output new genesis to")
("dev-account-prefix", bpo::value<std::string>()->default_value("devacct"), "Prefix for dev accounts")
("dev-key-prefix", bpo::value<std::string>()->default_value("devkey-"), "Prefix for dev key")
Expand Down Expand Up @@ -102,10 +102,9 @@ int main( int argc, char** argv )
genesis_state_type genesis;
if( options.count("genesis-json") )
{
fc::path genesis_json_filename = options["genesis-json"].as<boost::filesystem::path>();
std::cerr << "update_genesis: Reading genesis from file " << genesis_json_filename.preferred_string() << "\n";
std::string genesis_json;
read_file_contents( genesis_json_filename, genesis_json );
std::string genesis_json_filename = options["genesis-json"].as<std::string>();
std::cerr << "update_genesis: Reading genesis from file " << genesis_json_filename << "\n";
std::string genesis_json = graphene::utilities::read_file_contents( genesis_json_filename );
genesis = fc::json::from_string( genesis_json ).as< genesis_state_type >(20);
}
else
Expand Down
4 changes: 2 additions & 2 deletions tests/common/genesis_file_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace graphene { namespace app { namespace detail {
/// @param directory the directory to place the file "genesis.json"
/// @returns the full path to the file
////////
boost::filesystem::path create_genesis_file(fc::temp_directory& directory) {
std::string create_genesis_file(fc::temp_directory& directory) {
boost::filesystem::path genesis_path = boost::filesystem::path{directory.path().generic_string()} / "genesis.json";
fc::path genesis_out = genesis_path;
graphene::chain::genesis_state_type genesis_state = graphene::app::detail::create_example_genesis();
Expand Down Expand Up @@ -42,5 +42,5 @@ boost::filesystem::path create_genesis_file(fc::temp_directory& directory) {
*/

fc::json::save_to_file(genesis_state, genesis_out);
return genesis_path;
return genesis_path.string();
}
11 changes: 5 additions & 6 deletions tests/generate_empty_blocks/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
#include <iostream>
#include <iterator>

#include <fc/io/fstream.hpp>
#include <fc/io/json.hpp>
#include <fc/io/stdio.hpp>

#include <graphene/app/api.hpp>
#include <graphene/egenesis/egenesis.hpp>
#include <graphene/utilities/file_util.hpp>
#include <graphene/utilities/key_conversion.hpp>

#include <boost/filesystem.hpp>
Expand Down Expand Up @@ -61,7 +61,7 @@ int main( int argc, char** argv )
cli_options.add_options()
("help,h", "Print this help message and exit.")
("data-dir", bpo::value<boost::filesystem::path>()->default_value("empty_blocks_data_dir"), "Directory containing generator database")
("genesis-json,g", bpo::value<boost::filesystem::path>(), "File to read genesis state from")
("genesis-json,g", bpo::value<std::string>(), "File to read genesis state from")
("genesis-time,t", bpo::value<uint32_t>()->default_value(0), "Timestamp for genesis state (0=use value from file/example)")
("num-blocks,n", bpo::value<uint32_t>()->default_value(1000000), "Number of blocks to generate")
("miss-rate,r", bpo::value<uint32_t>()->default_value(3), "Percentage of blocks to miss")
Expand Down Expand Up @@ -96,10 +96,9 @@ int main( int argc, char** argv )
genesis_state_type genesis;
if( options.count("genesis-json") )
{
fc::path genesis_json_filename = options["genesis-json"].as<boost::filesystem::path>();
std::cerr << "embed_genesis: Reading genesis from file " << genesis_json_filename.preferred_string() << "\n";
std::string genesis_json;
read_file_contents( genesis_json_filename, genesis_json );
std::string genesis_json_filename = options["genesis-json"].as<std::string>();
std::cerr << "generate_empty_blocks: Reading genesis from file " << genesis_json_filename << "\n";
std::string genesis_json = graphene::utilities::read_file_contents( genesis_json_filename );
genesis = fc::json::from_string( genesis_json ).as< genesis_state_type >(20);
}
else
Expand Down