How to set autoreconnect option with mysql connector c++ - c++

Greetings, how can i set autoReconnect option with mysql connector c++ ?
( not with mysql c api http://dev.mysql.com/doc/refman/5.0/en/mysql-options.html )

I am not a user of this library, so my knowledge of it is only that last 10 mins worth, so please do verify.
As a general rule, the best resource of such information about usage of various specific details of a library is to take a look at its unit tests. The best thing about OSS.
So if you look at MySQL Connector/C++ unit tests that can be found on their source tree, you will see the below extract.
sql::ConnectOptionsMap connection_properties;
...
connection_properties["OPT_RECONNECT"]=true;
try
{
con.reset(driver->connect(connection_properties));
}
catch (sql::SQLException &e)
{
std::cerr << e.what();
}
For more information, please do the below, so that you can take a look yourselves.
~/tmp$ bzr branch lp:~mysql/mysql-connector-cpp/trunk mysql-connector-cpp
~/tmp$ vi mysql-connector-cpp/test/unit/classes/connection.cpp +170
~/tmp$ vi mysql-connector-cpp/test/unit/classes/connection.h
Having said all that, reconnect option in mysql has to be used very carefully, as you will have to reset any session variables, etc. You will have to treat a reconnected connection as a brand new connection. This has to be verified with the documentation of the particular version of MySQL you are working with.

A more complete example
header
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
std::string host_name = "localhost";
std::string user_name = "user1234";
std::string password = "pw1234";
std::string database_name = "TestingDB";
bool reconnect_state = true;
sql::ConnectOptionsMap connection_properties;
sql::Driver *driver;
boost::shared_ptr <sql::Connection> con;
boost::shared_ptr <sql::Statement> stmt;
boost::shared_ptr <sql::ResultSet> res;
boost::shared_ptr <sql::PreparedStatement> pstmt;
connect
driver = get_driver_instance (); // protected
con.reset(driver->connect (host_name, user_name, password)); // connect to mysql
con->setClientOption("OPT_RECONNECT", &reconnect_state);
con->setSchema(database_name);
thread
std::vector <std::string> database::string_from_sql (std::string query, std::string column_name)
{
std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | started" << std::endl;
std::vector <std::string> svec;
try
{
driver->threadInit(); // prevents multiple open connections
if (con.get() == NULL)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | connection is not open" << std::endl;
throw -2;
}
stmt.reset (con->createStatement ());
res.reset (stmt->executeQuery (query));
while (res->next())
{
svec.push_back(res->getString (column_name));
}
driver->threadEnd();
}
catch (sql::SQLException &e)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | e.what(): " << e.what() << " (MySQL error code: " << e.getErrorCode() << ", SQLState: " << e.getSQLState() << " )" << std::endl;
throw -1;
}
if (svec.empty())
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | return vector size is 0 (Empty set)" << std::endl;
throw -3;
}
std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | ended" << std::endl;
return svec;
}

You need to pass the boolean value by reference. My code does:
bool myTrue = true;
con->setClientOption("OPT_RECONNECT", &myTrue);
And that worked for me.

Related

Open 2 distinct sockets to 1 common remote endpoint using Boost::asio

I am trying to test sending data from 2 distinct network adapters on the same machine to a common remote endpoint, but I keep getting "bind: invalid argument" AFTER the first bind comes through. What am I missing? I have searched, tried to modify the code, but I was not able to find any lead and I keep getting the same error. The same happens when I swap out the IPs.
#include <iostream>
#include <boost/asio.hpp>
#include <sstream>
#include <thread>
#include <chrono>
#include <boost/random.hpp>
const unsigned int MS_INTERVAL = 100;
enum CMD_ARG
{
PROG_NAME = 0,
LOCAL_IP_1,
LOCAL_IP_2,
REMOTE_IP,
REMOTE_PORT
};
using namespace boost::asio;
using std::string;
using std::cout;
using std::endl;
int main(int argc, char *argv[]) {
if(argc == 5)
{
//Test data initialisation
unsigned int counter = 0;
boost::random::mt19937 randSeed; // seed, produces randomness out of thin air
boost::random::uniform_int_distribution<> randGen(-1000,1000); // Random number generator between -100 and 100
//Initialise ASIO service
io_service io_service;
//socket creation and binding (one per network adapter)
std::cout << "Opening and binding local sockets to " << argv[LOCAL_IP_1] << " and " << argv[LOCAL_IP_2] << std::endl;
ip::tcp::socket socket1(io_service);
ip::tcp::socket socket2(io_service);
socket1.open(ip::tcp::v4());
socket2.open(ip::tcp::v4());
socket1.bind(ip::tcp::endpoint(ip::address::from_string(argv[LOCAL_IP_1]), 0));
std::cout << "1/2 done" << std::endl;
socket2.bind(ip::tcp::endpoint(ip::address::from_string(argv[LOCAL_IP_2]), 0));
//Connection to remote end point starting with defining the remote endpoint
std::istringstream iss(argv[REMOTE_PORT]);
unsigned int port = 0;
iss >> port;
ip::tcp::endpoint remoteEndpoint = ip::tcp::endpoint( ip::address::from_string(argv[REMOTE_IP]), port);
std::cout << "Connecting to " << argv[REMOTE_IP] << " on port " << port << std::endl;
socket1.connect(remoteEndpoint);
std::cout << "1/2 done" << std::endl;
socket2.connect(remoteEndpoint);
std::cout << "Ready" << std::endl;
while(1)
{
//Build message
std::ostringstream oss;
oss << counter << "," << randGen(randSeed) << "," << randGen(randSeed) << "," << randGen(randSeed) << std::endl;
//Send message on both interfaces
boost::system::error_code error1, error2;
write(socket1, boost::asio::buffer(oss.str()), error1);
write(socket2, boost::asio::buffer(oss.str()), error2);
//Check errors
if( !error1 && !error2) {
cout << "Sending: " << oss.str() << endl;
counter++;
}
else {
cout << "Error: " << (error1?error1.message():error2.message()) << endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(MS_INTERVAL));
}
}
else
{
std::cout << "Usage: <program> <local IP 1> <local IP 2> <remote server IP> <server's opened port>" << argc << std::endl;
}
return 0;
}
socket1.bind(ip::tcp::endpoint(ip::address::from_string(argv[LOCAL_IP_1]), 0));
...
socket1.bind(ip::tcp::endpoint(ip::address::from_string(argv[LOCAL_IP_2]), 0));
You are trying to bind the same socket1 twice. Likely you mean socket2 in the second statement.

C++ Paho MQTT connection as a subscriber issue

I'm pretty new to C++ and was playing around with the Paho MQTT C++ client.
I've rearranged the sample async subscriber, but ran into an error of 'connection lost, cause: connect onSuccess called'. Spent hours trying to find the solution to no prevail.
My Main file:
int main()
{
subscriber();
return 0;
}
My header file: 'subscriber.hpp' ..has all the dependacy and mqtt/async_client.h
int subscriber();
My source file: subscriber.cpp
#include "subscriber.hpp"
const std::string SERVER_ADDRESS("tcp://localhost:1883");
const std::string CLIENT_ID("eclipse_subscriber");
const std::string TOPIC("topic2");
const int QOS = 1;
const int N_RETRY_ATTEMPTS = 5;
/////////////////////////////////////////////////////////////////////////////
// Callbacks for the success or failures of requested actions.
// This could be used to initiate further action, but here we just log the
// results to the console.
class action_listener : public virtual mqtt::iaction_listener
{
std::string name_;
void on_failure(const mqtt::token& tok) override {
std::cout << name_ << " failure";
if (tok.get_message_id() != 0)
std::cout << " for token: [" << tok.get_message_id() << "]" << std::endl;
std::cout << std::endl;
}
void on_success(const mqtt::token& tok) override {
std::cout << name_ << " success";
if (tok.get_message_id() != 0)
std::cout << " for token: [" << tok.get_message_id() << "]" << std::endl;
auto top = tok.get_topics();
if (top && !top->empty())
std::cout << "\ttoken topic: '" << (*top)[0] << "', ..." << std::endl;
std::cout << std::endl;
}
public:
action_listener(const std::string& name) : name_(name) {}
};
/////////////////////////////////////////////////////////////////////////////
/**
* Local callback & listener class for use with the client connection.
* This is primarily intended to receive messages, but it will also monitor
* the connection to the broker. If the connection is lost, it will attempt
* to restore the connection and re-subscribe to the topic.
*/
class callback : public virtual mqtt::callback,
public virtual mqtt::iaction_listener
{
// Counter for the number of connection retries
int nretry_;
// The MQTT client
mqtt::async_client& cli_;
// Options to use if we need to reconnect
mqtt::connect_options& connOpts_;
// An action listener to display the result of actions.
action_listener subListener_;
// This deomonstrates manually reconnecting to the broker by calling
// connect() again. This is a possibility for an application that keeps
// a copy of it's original connect_options, or if the app wants to
// reconnect with different options.
// Another way this can be done manually, if using the same options, is
// to just call the async_client::reconnect() method.
void reconnect() {
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
try {
cli_.connect(connOpts_, nullptr, *this);
}
catch (const mqtt::exception& exc) {
std::cerr << "Error: " << exc.what() << std::endl;
exit(1);
}
}
// Re-connection failure
void on_failure(const mqtt::token& tok) override {
std::cout << "Connection attempt failed" << std::endl;
if (++nretry_ > N_RETRY_ATTEMPTS)
exit(1);
reconnect();
}
// (Re)connection success
// Either this or connected() can be used for callbacks.
void on_success(const mqtt::token& tok) override {}
// (Re)connection success
void connected(const std::string& cause) override {
std::cout << "\nConnection success" << std::endl;
std::cout << "\nSubscribing to topic '" << TOPIC << "'\n"
<< "\tfor client " << CLIENT_ID
<< " using QoS" << QOS << "\n"
<< "\nPress Q<Enter> to quit\n" << std::endl;
cli_.subscribe(TOPIC, QOS, nullptr, subListener_);
}
// Callback for when the connection is lost.
// This will initiate the attempt to manually reconnect.
void connection_lost(const std::string& cause) override {
std::cout << "\nConnection lost" << std::endl;
if (!cause.empty())
std::cout << "\tcause: " << cause << std::endl;
std::cout << "Reconnecting..." << std::endl;
nretry_ = 0;
reconnect();
}
// Callback for when a message arrives.
void message_arrived(mqtt::const_message_ptr msg) override {
std::cout << "Message arrived" << std::endl;
std::cout << "\ttopic: '" << msg->get_topic() << "'" << std::endl;
std::cout << "\tpayload: '" << msg->to_string() << "'\n" << std::endl;
}
void delivery_complete(mqtt::delivery_token_ptr token) override {}
public:
callback(mqtt::async_client& cli, mqtt::connect_options& connOpts)
: nretry_(0), cli_(cli), connOpts_(connOpts), subListener_("Subscription") {}
};
/////////////////////////////////////////////////////////////////////////////
int subscriber()
{
mqtt::connect_options connOpts;
connOpts.set_keep_alive_interval(20);
connOpts.set_clean_session(true);
mqtt::async_client client(SERVER_ADDRESS, CLIENT_ID);
callback cb(client, connOpts);
client.set_callback(cb);
// Start the connection.
// When completed, the callback will subscribe to topic.
try {
std::cout << "Connecting to the MQTT server..." << std::flush;
client.connect(connOpts, nullptr, cb);
}
catch (const mqtt::exception&) {
std::cerr << "\nERROR: Unable to connect to MQTT server: '"
<< SERVER_ADDRESS << "'" << std::endl;
return 1;
}
// Just block till user tells us to quit.
while (std::tolower(std::cin.get()) != 'q')
;
// Disconnect
try {
std::cout << "\nDisconnecting from the MQTT server..." << std::flush;
client.disconnect()->wait();
std::cout << "OK" << std::endl;
}
catch (const mqtt::exception& exc) {
std::cerr << exc.what() << std::endl;
return 1;
}
return 0;
}

OpenCL Child Kernel Error

Aloha,
I'm struggling with OpenCL child kernel feature.
Kernel SRC (Minimal example):
kernel void launcher()
{
ndrange_t ndrange = ndrange_1D(1);
enqueue_kernel(get_default_queue(), CLK_ENQUEUE_FLAGS_WAIT_KERNEL, ndrange,
^{
size_t id = get_global_id(0);
}
);
}
stdafx.h:
#pragma once
#define __CL_ENABLE_EXCEPTIONS
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_TARGET_OPENCL_VERSION 200
#include "targetver.h"
#include <CL/cl2.hpp>
#include <iostream>
#include <string>
Full SRC (Minimal):
#include "stdafx.h"
std::string kernel2_source(
"kernel void launcher() ""\n"
"{ ""\n"
" ndrange_t ndrange = ndrange_1D(1);""\n"
" enqueue_kernel(get_default_queue(), CLK_ENQUEUE_FLAGS_WAIT_KERNEL, ndrange,""\n"
" ^{""\n"
" size_t id = get_global_id(0);""\n"
" }""\n"
" );""\n"
"}""\n");
//Number of Input Elements
constexpr int numTriangles = 10;
cl_int errorcode = CL_BUILD_ERROR; //Has to be set to build error, because errorcode isn't set when exception occurs
//Move variable definitions out of main for test purposes;
//Numerous definitions
cl::Program program;
std::vector<cl::Device> devices;
std::vector<cl::Platform> platforms;
cl::CommandQueue queue;
cl::Program::Sources source{ kernel2_source };
int main() {
try {
// Query for platforms
cl::Platform::get(&platforms);
std::cout << "Num Platforms: " << platforms.size() << std::endl;
// Get a list of devices on this platform
platforms[0].getDevices(CL_DEVICE_TYPE_ALL, &devices);
std::cout << "Using platform: " << platforms[0].getInfo<CL_PLATFORM_NAME>() << std::endl;
std::cout << "Num Devices: " << devices.size() << std::endl;
// Create a context for the devices
std::cout << "Using device: " << devices[0].getInfo<CL_DEVICE_NAME>() << std::endl;
//Create a context for the first device
//cl::Context context({ devices[0]});
cl::Context context({ devices[0] });
// Create a command−queue for the first device
queue = cl::CommandQueue(context, devices[0]);
cl::DeviceCommandQueue deviceQueue;
deviceQueue = cl::DeviceCommandQueue(context, devices[0]);
// Create the program from the source code
program = cl::Program(context, source);
std::cout << "Building Program" << std::endl;
// Build the program for the devices
errorcode = program.build("-cl-std=CL2.0 -g");
std::cout << "Success!" << std::endl;
cl::Kernel kernel = cl::Kernel(program, "launcher");
cl::NDRange global = numTriangles;
cl::NDRange local = 1;
queue.enqueueNDRangeKernel(kernel, cl::NullRange, global, local);
std::cout << "finished" << std::endl;
std::cin.get();
}
catch (cl::Error error)
{
std::cout << "Error!" << std::endl;
std::cout << error.what() << "(" << error.err() << ")" << std::endl;
std::cout << "Errorcode: " << errorcode << std::endl;
if (errorcode != CL_SUCCESS) { //...
std::cout << "Build Status: " << program.getBuildInfo<CL_PROGRAM_BUILD_STATUS>(devices[0]) << std::endl;
//std::cout << "Build Status: " << program.getBuildInfo<CL_PROGRAM_BUILD_STATUS>(devices[1]) << std::endl;
std::cout << "Build Options:" << program.getBuildInfo<CL_PROGRAM_BUILD_OPTIONS>(devices[0]) << std::endl;
//std::cout << "Build Options:" << program.getBuildInfo<CL_PROGRAM_BUILD_OPTIONS>(devices[1]) << std::endl;
std::cout << "Build Log:" << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]) << std::endl;
//std::cout << "Build Log:" << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[1]) << std::endl;
}
}
std::cin.get();
return 0;
}
Output:
Num Platforms: 1
Using platform: AMD Accelerated Parallel Processing
Num Devices: 2
Using device: Hawaii
Building Program
=> Exception.
There appears an uncaught exception which is strange, because all build error should be caught.
The ndrange_1D(1) is just for testing purposes (and to produce an acceptable amount of dummy output).
The device (AMD R9 390X) is OpenCL 2.0 capable.
Any ideas how to fix this?
EDIT:
Even not using exceptions and using errorcodes throws this an exception!

C++ ping-like function without superuser access

I am trying to write a simple C++ ping function to see if a network address is responding. I don't need ICMP specifically, I just need to see if the server is there and responding to anything. I have been doing some research and every solution I come up with requires creating a raw socket or something which requires the program to have sudo access. I won't be able to guarantee that the system I am running this on will be able to modify the network stack, so this is not valid.
Here are some related questions I have already looked at.
Opening RAW sockets in linux without being superuser
ICMP sockets (linux)
How to Ping Using Sockets Library - C
Why does ping work without administrator privileges?
C++ Boost.asio Ping
It appears that ping requires superuser access for a good reason. I don't want to purposefully create a security loophole, I just want to see if a server is responding. Is there a good c++ function or resource that can do this? I will make sure to post any sample solution I come up with. I need a Linux (BSD) socket solution. Since almost every unix-like system runs SSH, I can even just test against port 22. I am only going to target Linux systems as a constraint.
Thanks
Here is an example with popen. I would love a better solution, so if there is a socket or other method which does not resort to a shell call, I would greatly appreciate it. This should run with just g++ ping_demo.cpp -o ping_demo. Let me know if it causes compile errors.
// C++ Libraries
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
/**
* #brief Convert String to Number
*/
template <typename TP>
TP str2num( std::string const& value ){
std::stringstream sin;
sin << value;
TP output;
sin >> output;
return output;
}
/**
* #brief Convert number to string
*/
template <typename TP>
std::string num2str( TP const& value ){
std::stringstream sin;
sin << value;
return sin.str();
}
/**
* #brief Execute Generic Shell Command
*
* #param[in] command Command to execute.
* #param[out] output Shell output.
* #param[in] mode read/write access
*
* #return 0 for success, 1 otherwise.
*
*/
int Execute_Command( const std::string& command,
std::string& output,
const std::string& mode = "r")
{
// Create the stringstream
std::stringstream sout;
// Run Popen
FILE *in;
char buff[512];
// Test output
if(!(in = popen(command.c_str(), mode.c_str()))){
return 1;
}
// Parse output
while(fgets(buff, sizeof(buff), in)!=NULL){
sout << buff;
}
// Close
int exit_code = pclose(in);
// set output
output = sout.str();
// Return exit code
return exit_code;
}
/**
* #brief Ping
*
* #param[in] address Address to ping.
* #param[in] max_attempts Number of attempts to try and ping.
* #param[out] details Details of failure if one occurs.
*
* #return True if responsive, false otherwise.
*
* #note { I am redirecting stderr to stdout. I would recommend
* capturing this information separately.}
*/
bool Ping( const std::string& address,
const int& max_attempts,
std::string& details )
{
// Format a command string
std::string command = "ping -c " + num2str(max_attempts) + " " + address + " 2>&1";
std::string output;
// Execute the ping command
int code = Execute_Command( command, details );
return (code == 0);
}
/**
* #brief Main Function
*/
int main( int argc, char* argv[] )
{
// Parse input
if( argc < 2 ){
std::cerr << "usage: " << argv[0] << " <address> <max-attempts = 3>" << std::endl;
return 1;
}
// Get the address
std::string host = argv[1];
// Get the max attempts
int max_attempts = 1;
if( argc > 2 ){
max_attempts = str2num<int>(argv[2]);
}
if( max_attempts < 1 ){
std::cerr << "max-attempts must be > 0" << std::endl;
return 1;
}
// Execute the command
std::string details;
bool result = Ping( host, max_attempts, details );
// Print the result
std::cout << host << " ";
if( result == true ){
std::cout << " is responding." << std::endl;
}
else{
std::cout << " is not responding. Cause: " << details << std::endl;
}
return 0;
}
Sample outputs
$> g++ ping_demo.cpp -o ping_demo
$> # Valid Example
$> ./ping_demo localhost
localhost is responding.
$> # Invalid Example
$> ./ping_demo localhostt
localhostt is not responding. Cause: ping: unknown host localhostt
$> # Valid Example
$> ./ping_demo 8.8.8.8
8.8.8.8 is responding.
I created an easy to use ping class with added ethernet port detection capability. I based the code on msmith81886's answer but wanted to condense for those using it in industry.
ping.hpp
#ifndef __PING_HPP_
#define __PING_HPP_
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <cerrno>
#include <cstring>
class system_ping
{
public:
int test_connection (std::string ip_address, int max_attempts, bool check_eth_port = false, int eth_port_number = 0);
private:
int ping_ip_address(std::string ip_address, int max_attempts, std::string& details);
};
#endif
ping.cpp
#include "../include/ping.hpp"
int system_ping::ping_ip_address(std::string ip_address, int max_attempts, std::string& details)
{
std::stringstream ss;
std::string command;
FILE *in;
char buff[512];
int exit_code;
try
{
command = "ping -c " + std::to_string(max_attempts) + " " + ip_address + " 2>&1";
if(!(in = popen(command.c_str(), "r"))) // open process as read only
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | popen error = " << std::strerror(errno) << std::endl;
return -1;
}
while(fgets(buff, sizeof(buff), in) != NULL) // put response into stream
{
ss << buff;
}
exit_code = pclose(in); // blocks until process is done; returns exit status of command
details = ss.str();
}
catch (const std::exception &e)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | e.what() = " << e.what() << std::endl;
return -2;
}
return (exit_code == 0);
}
int system_ping::test_connection (std::string ip_address, int max_attempts, bool check_eth_port, int eth_port_number)
{
std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | started" << std::endl;
int eth_conn_status_int;
std::string details;
try
{
if (check_eth_port)
{
std::ifstream eth_conn_status ("/sys/class/net/eth" + std::to_string(eth_port_number) + "/carrier");
eth_conn_status >> eth_conn_status_int; // 0: not connected; 1: connected
if (eth_conn_status_int != 1)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | eth" << std::to_string(eth_port_number) << " unplugged";
return -1;
}
}
if (ping_ip_address(ip_address, max_attempts, details) != 1)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | cannot ping " << ip_address << " | " << details << std::endl;
return -2;
}
}
catch (const std::exception &e)
{
std::cerr << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | e.what() = " << e.what() << std::endl;
return -3;
}
std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | ping " << ip_address << " OK" << std::endl;
std::cout << __FILE__ << "(" << __FUNCTION__ << ":" << __LINE__ << ") | ended" << std::endl;
return 0;
}

Unit Testing Boost filesystem create_directories

I want to unit test the boost filesystem function create_directories() for it's failure case, i.e., when create_directory fails. Can someone please provide any suggestions on how to do this? Another requirement is that the code needs to be cross-platform.
You could try to create a directory in a path to a file:
#include <fstream>
#include <iostream>
#include "boost/filesystem/path.hpp"
#include "boost/filesystem/operations.hpp"
namespace bfs = boost::filesystem;
int main() {
// Create test dir
boost::system::error_code ec;
bfs::path test_root(bfs::unique_path(
bfs::temp_directory_path(ec) / "%%%%-%%%%-%%%%"));
if (!bfs::create_directory(test_root, ec) || ec) {
std::cout << "Failed creating " << test_root << ": " << ec.message() << '\n';
return -1;
}
// Create file in test dir
bfs::path test_file(test_root / "file");
std::ofstream file_out(test_file.c_str());
file_out.close();
if (!bfs::exists(test_file, ec)) {
std::cout << "Failed creating " << test_file << ": " << ec.message() << '\n';
return -2;
}
// Try to create directory in test_file - should fail
bfs::path invalid_dir(test_file / "dir");
if (bfs::create_directory(invalid_dir, ec)) {
std::cout << "Succeeded creating invalid dir " << invalid_dir << '\n';
return -3;
}
// Try to create nested directory in test_file - should fail
bfs::path nested_invalid_dir(invalid_dir / "nested_dir");
if (bfs::create_directories(nested_invalid_dir, ec)) {
std::cout << "Succeeded creating nested invalid dir " << invalid_dir << '\n';
return -4;
}
// Clean up
bfs::remove_all(test_root);
return 0;
}