2018-10-29 23:47:12 +00:00
|
|
|
/*!
|
|
|
|
* \file gnss_sdr_create_directory.cc
|
|
|
|
* \brief Create a directory
|
|
|
|
* \author Carles Fernandez-Prades, 2018. cfernandez(at)cttc.es
|
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*
|
2019-07-26 10:38:20 +00:00
|
|
|
* Copyright (C) 2010-2019 (see AUTHORS file for a list of contributors)
|
2018-10-29 23:47:12 +00:00
|
|
|
*
|
|
|
|
* GNSS-SDR is a software defined Global Navigation
|
|
|
|
* Satellite Systems receiver
|
|
|
|
*
|
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
2020-02-08 00:20:02 +00:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2018-10-29 23:47:12 +00:00
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gnss_sdr_create_directory.h"
|
2019-06-12 18:39:29 +00:00
|
|
|
#include <exception> // for exception
|
|
|
|
#include <fstream> // for ofstream
|
|
|
|
|
2019-06-10 19:41:13 +00:00
|
|
|
#if HAS_STD_FILESYSTEM
|
|
|
|
#include <system_error>
|
2019-06-12 18:39:29 +00:00
|
|
|
namespace errorlib = std;
|
|
|
|
#if HAS_STD_FILESYSTEM_EXPERIMENTAL
|
|
|
|
#include <experimental/filesystem>
|
|
|
|
namespace fs = std::experimental::filesystem;
|
|
|
|
#else
|
|
|
|
#include <filesystem>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
#endif
|
2019-06-10 19:41:13 +00:00
|
|
|
#else
|
2018-10-29 23:47:12 +00:00
|
|
|
#include <boost/filesystem/operations.hpp> // for create_directories, exists
|
|
|
|
#include <boost/filesystem/path.hpp> // for path, operator<<
|
|
|
|
#include <boost/filesystem/path_traits.hpp> // for filesystem
|
2019-03-06 23:15:28 +00:00
|
|
|
#include <boost/system/error_code.hpp> // for error_code
|
2019-06-10 19:41:13 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
namespace errorlib = boost::system;
|
|
|
|
#endif
|
2018-10-29 23:47:12 +00:00
|
|
|
|
|
|
|
bool gnss_sdr_create_directory(const std::string& foldername)
|
|
|
|
{
|
|
|
|
std::string new_folder;
|
2019-06-10 19:41:13 +00:00
|
|
|
for (auto& folder : fs::path(foldername))
|
2018-10-29 23:47:12 +00:00
|
|
|
{
|
|
|
|
new_folder += folder.string();
|
2019-06-10 19:41:13 +00:00
|
|
|
errorlib::error_code ec;
|
|
|
|
if (!fs::exists(new_folder))
|
2018-10-29 23:47:12 +00:00
|
|
|
{
|
2019-06-10 19:41:13 +00:00
|
|
|
if (!fs::create_directory(new_folder, ec))
|
2018-10-29 23:47:12 +00:00
|
|
|
{
|
2019-06-10 19:41:13 +00:00
|
|
|
return false;
|
2018-10-29 23:47:12 +00:00
|
|
|
}
|
2019-06-10 19:41:13 +00:00
|
|
|
|
|
|
|
if (static_cast<bool>(ec))
|
2018-10-29 23:47:12 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 19:41:13 +00:00
|
|
|
new_folder += fs::path::preferred_separator;
|
2018-10-29 23:47:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we have writing permissions
|
|
|
|
std::string test_file = foldername + "/test_file.txt";
|
|
|
|
std::ofstream os_test_file;
|
|
|
|
os_test_file.open(test_file.c_str(), std::ios::out | std::ios::binary);
|
|
|
|
|
|
|
|
if (os_test_file.is_open())
|
|
|
|
{
|
2019-06-10 19:41:13 +00:00
|
|
|
errorlib::error_code ec;
|
2018-10-29 23:47:12 +00:00
|
|
|
os_test_file.close();
|
2019-06-10 19:41:13 +00:00
|
|
|
|
2019-07-13 22:14:13 +00:00
|
|
|
if (!fs::remove(test_file, ec))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-10 19:41:13 +00:00
|
|
|
if (static_cast<bool>(ec))
|
2018-10-29 23:47:12 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
|
|
|
return false;
|
2018-10-29 23:47:12 +00:00
|
|
|
}
|