diff options
author | 2017-09-17 22:41:29 +0300 | |
---|---|---|
committer | 2017-09-20 20:44:41 +0300 | |
commit | 8ea9f9f61aef64d70159e4f5788fd40c12163380 (patch) | |
tree | 463dc3bf66c29f6d86fc20a1a1c6f1a88b6151bd /src/controller.cpp | |
parent | e171c84de8a67a978a29889067f597a0f0432a1d (diff) | |
download | newsboat-8ea9f9f61aef64d70159e4f5788fd40c12163380.tar.gz newsboat-8ea9f9f61aef64d70159e4f5788fd40c12163380.tar.zst newsboat-8ea9f9f61aef64d70159e4f5788fd40c12163380.zip |
Migrate Newsbeuter configuration on the first run
Closes #1.
Diffstat (limited to 'src/controller.cpp')
-rw-r--r-- | src/controller.cpp | 145 |
1 files changed, 139 insertions, 6 deletions
diff --git a/src/controller.cpp b/src/controller.cpp index 8965ea63..aff4b452 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -149,9 +149,6 @@ bool controller::setup_dirs_xdg(const char *env_home, bool silent) { config_dir = xdg_config_dir; - // create data directory if it doesn't exist - utils::mkdir_parents(xdg_data_dir, 0700); - /* in config */ url_file = config_dir + std::string(NEWSBEUTER_PATH_SEP) + url_file; config_file = config_dir + std::string(NEWSBEUTER_PATH_SEP) + config_file; @@ -166,7 +163,7 @@ bool controller::setup_dirs_xdg(const char *env_home, bool silent) { return true; } -void controller::setup_dirs(bool silent) { +bool controller::setup_dirs(bool silent) { const char * env_home; if (!(env_home = ::getenv("HOME"))) { struct passwd * spw = ::getpwuid(::getuid()); @@ -184,9 +181,9 @@ void controller::setup_dirs(bool silent) { config_dir.append(NEWSBOAT_CONFIG_SUBDIR); if (setup_dirs_xdg(env_home, silent)) - return; + return false; - mkdir(config_dir.c_str(),0700); // create configuration directory if it doesn't exist + bool first_run = ! (0 == access(config_dir.c_str(), R_OK | X_OK)); url_file = config_dir + std::string(NEWSBEUTER_PATH_SEP) + url_file; cache_file = config_dir + std::string(NEWSBEUTER_PATH_SEP) + cache_file; @@ -196,6 +193,130 @@ void controller::setup_dirs(bool silent) { searchfile = strprintf::fmt("%s%shistory.search", config_dir, NEWSBEUTER_PATH_SEP); cmdlinefile = strprintf::fmt("%s%shistory.cmdline", config_dir, NEWSBEUTER_PATH_SEP); + + return first_run; +} + +void copy_file( + const std::string& input_filepath, + const std::string& output_filepath) +{ + std::ifstream src(input_filepath, std::ios_base::binary); + std::ofstream dst(output_filepath, std::ios_base::binary); + dst << src.rdbuf(); +} + +bool controller::migrate_data_from_newsbeuter_xdg(const char* env_home, bool silent) { + const char* env_xdg_config = ::getenv("XDG_CONFIG_HOME"); + std::string xdg_config_dir; + if (env_xdg_config) { + xdg_config_dir = env_xdg_config; + } else { + xdg_config_dir = env_home; + xdg_config_dir.append(NEWSBEUTER_PATH_SEP); + xdg_config_dir.append(".config"); + } + + const char* env_xdg_data = ::getenv("XDG_DATA_HOME"); + std::string xdg_data_dir; + if (env_xdg_data) { + xdg_data_dir = env_xdg_data; + } else { + xdg_data_dir = env_home; + xdg_data_dir.append(NEWSBEUTER_PATH_SEP); + xdg_data_dir.append(".local"); + xdg_data_dir.append(NEWSBEUTER_PATH_SEP); + xdg_data_dir.append("share"); + } + + const auto newsbeuter_config_dir + = xdg_config_dir + NEWSBEUTER_PATH_SEP + NEWSBEUTER_SUBDIR_XDG + NEWSBEUTER_PATH_SEP; + const auto newsbeuter_data_dir + = xdg_data_dir + NEWSBEUTER_PATH_SEP + NEWSBEUTER_SUBDIR_XDG + NEWSBEUTER_PATH_SEP; + + const auto newsboat_config_dir + = xdg_config_dir + NEWSBEUTER_PATH_SEP + NEWSBOAT_SUBDIR_XDG + NEWSBEUTER_PATH_SEP; + const auto newsboat_data_dir + = xdg_data_dir + NEWSBEUTER_PATH_SEP + NEWSBOAT_SUBDIR_XDG + NEWSBEUTER_PATH_SEP; + + bool newsbeuter_config_dir_exists = 0 == access(newsbeuter_config_dir.c_str(), R_OK | X_OK); + + if (! newsbeuter_config_dir_exists) { + return false; + } + + utils::mkdir_parents(newsboat_config_dir, 0700); + utils::mkdir_parents(newsboat_data_dir, 0700); + + if (! silent) { + std::cerr << "Migrating configs and data from Newsbeuter..." << std::endl; + } + + /* in config */ + copy_file(newsbeuter_config_dir + "urls", newsboat_config_dir + "urls"); + copy_file(newsbeuter_config_dir + "config", newsboat_config_dir + "config"); + + /* in data */ + copy_file(newsbeuter_data_dir + "cache.db", newsboat_data_dir + "cache.db"); + copy_file(newsbeuter_data_dir + "queue", newsboat_data_dir + "queue"); + copy_file(newsbeuter_data_dir + "history.search", newsboat_data_dir + "history.search"); + copy_file(newsbeuter_data_dir + "history.cmdline", newsboat_data_dir + "history.cmdline"); + + return true; +} + +void controller::migrate_data_from_newsbeuter_simple(const char* env_home, bool silent) { + std::string newsbeuter_dir = env_home; + newsbeuter_dir += NEWSBEUTER_PATH_SEP; + newsbeuter_dir += NEWSBEUTER_CONFIG_SUBDIR; + newsbeuter_dir += NEWSBEUTER_PATH_SEP; + + if (0 != access(newsbeuter_dir.c_str(), R_OK | X_OK)) { + return; + } + + if (! silent) { + std::cerr << "Migrating configs and data from Newsbeuter..." << std::endl; + } + + std::string newsboat_dir = env_home; + newsboat_dir += NEWSBEUTER_PATH_SEP; + newsboat_dir += NEWSBOAT_CONFIG_SUBDIR; + newsboat_dir += NEWSBEUTER_PATH_SEP; + + ::mkdir(newsboat_dir.c_str(),0700); // create configuration directory if it doesn't exist + + copy_file(newsbeuter_dir + "urls", newsboat_dir + "urls"); + copy_file(newsbeuter_dir + "cache.db", newsboat_dir + "cache.db"); + copy_file(newsbeuter_dir + "config", newsboat_dir + "config"); + copy_file(newsbeuter_dir + "queue", newsboat_dir + "queue"); + copy_file(newsbeuter_dir + "history.search", newsboat_dir + "history.search"); + copy_file(newsbeuter_dir + "history.cmdline", newsboat_dir + "history.cmdline"); +} + +void controller::migrate_data_from_newsbeuter(bool silent) { + const char * env_home; + if (!(env_home = ::getenv("HOME"))) { + struct passwd * spw = ::getpwuid(::getuid()); + if (spw) { + env_home = spw->pw_dir; + } else { + std::cerr << _("Fatal error: couldn't determine home directory!") << std::endl; + std::cerr << strprintf::fmt(_("Please set the HOME environment variable or add a valid user for UID %u!"), ::getuid()) << std::endl; + ::exit(EXIT_FAILURE); + } + } + + if (migrate_data_from_newsbeuter_xdg(env_home, silent)) { + // Re-running to pick up XDG dirs + url_file = "urls"; + cache_file = "cache.db"; + config_file = "config"; + queue_file = "queue"; + setup_dirs(silent); + } else { + migrate_data_from_newsbeuter_simple(env_home, silent); + } } controller::~controller() { @@ -263,6 +384,8 @@ void controller::run(int argc, char * argv[]) { setup_dirs(silent); + bool using_nonstandard_configs = false; + /* Now that silencing's set up, let's rewind to the beginning of argv and * process the options */ optind = 1; @@ -292,14 +415,17 @@ void controller::run(int argc, char * argv[]) { break; case 'u': url_file = optarg; + using_nonstandard_configs = true; break; case 'c': cache_file = optarg; lock_file = std::string(cache_file) + LOCK_SUFFIX; cachefile_given_on_cmdline = true; + using_nonstandard_configs = true; break; case 'C': config_file = optarg; + using_nonstandard_configs = true; break; case 'X': do_vacuum = true; @@ -361,6 +487,13 @@ void controller::run(int argc, char * argv[]) { LOG(level::INFO, "nl_langinfo(CODESET): %s", nl_langinfo(CODESET)); + if ((! using_nonstandard_configs) && (0 != access(url_file.c_str(), R_OK))) { + migrate_data_from_newsbeuter(silent); + } else { + utils::mkdir_parents(config_dir.c_str(), 0700); + utils::mkdir_parents(data_dir.c_str(), 0700); + } + if (!do_export) { if (!silent) |