commit 36e5500c810d5fc5badbf22e8c79e8eeb0975d07 from: Aleksey Ryndin date: Tue Aug 15 16:49:52 2023 UTC Refectoring commit - 917c32a4f0ec162835da762d7dd0afdb11d69392 commit + 36e5500c810d5fc5badbf22e8c79e8eeb0975d07 blob - 7157976b86a0eabf00636cff867f8d832cb36960 blob + aa8fb179940b64dd6a25670b63d036b0023b888f --- vostokd/command_line_arguments.cc +++ vostokd/command_line_arguments.cc @@ -4,17 +4,42 @@ #include "error.h" #include +#include namespace vostok { +namespace +{ +/** Print usage and return false */ +bool usage(const char *program) +{ + const char *file_name = strrchr(program, '/'); + if (!file_name) + file_name = program; + else + ++file_name; + error::g_log << "Usage: " << file_name << " [OPTS]" << std::endl << std::endl; + error::g_log << "OPTS may be: " << std::endl; + error::g_log << "\t-a ADDR : listening network address (127.0.0.1 by default)" << std::endl; + error::g_log << "\t-p PORT : TCP/IP port number (1965 by default)" << std::endl; + error::g_log << "\t-c FILE : Server certificate file [REQUIRED]" << std::endl; + error::g_log << "\t-k FILE : Server key file [REQUIRED]" << std::endl; + error::g_log << "\t-d DIR : Path to data directory [REQUIRED]" << std::endl; + + return false; +} + +} // namespace + + bool command_line_arguments::parse_command_line(int argc, char *argv[]) { int ch; char *p = nullptr; - while ((ch = getopt(argc, argv, "a:p:c:k:")) != -1) { + while ((ch = getopt(argc, argv, "a:p:c:k:d:")) != -1) { switch (ch) { case 'a': m_addr = optarg; @@ -25,8 +50,7 @@ bool command_line_arguments::parse_command_line(int ar if (!m_port) { error::g_log << "Invalid PORT value: " << optarg << std::endl; - usage(argv[0]); - return false; + return usage(argv[0]); } break; case 'c': @@ -35,43 +59,42 @@ bool command_line_arguments::parse_command_line(int ar case 'k': m_key_file = optarg; break; + case 'd': + m_directory.reset(open(optarg, O_RDONLY | O_DIRECTORY)); + if (!m_directory) + { + error::occurred( + [] + { + error::g_log << "Open directory \"" << optarg << "\""; + }, + error::print{} + ); + return false; + } + break; default: - usage(argv[0]); - return false; + return usage(argv[0]); } } if (!m_cert_file) { error::g_log << "Invalid command line: -c option required" << std::endl; - usage(argv[0]); - return false; + return usage(argv[0]); } if (!m_key_file) { error::g_log << "Invalid command line: -k option required" << std::endl; - usage(argv[0]); - return false; + return usage(argv[0]); } + if (!m_directory) + { + error::g_log << "Invalid command line: -d option required" << std::endl; + return usage(argv[0]); + } return true; } -void command_line_arguments::usage(const char *program) -{ - const char *file_name = strrchr(program, '/'); - if (!file_name) - file_name = program; - else - ++file_name; - - error::g_log << "Usage: " << file_name << " [OPTS]" << std::endl << std::endl; - error::g_log << "OPTS may be: " << std::endl; - error::g_log << "\t-a ADDR : listening network address (127.0.0.1 by default)" << std::endl; - error::g_log << "\t-p PORT : TCP/IP port number (1965 by default)" << std::endl; - error::g_log << "\t-c FILE : Server certificate file [REQUIRED]" << std::endl; - error::g_log << "\t-k FILE : Server key file [REQUIRED]" << std::endl; -} - - } // namespace vostok blob - a05146429c24bd042d30740b1f5c8c48de36673a blob + 8d96bc9af9bfdbfece9be42bba38e35a07bd9ee1 --- vostokd/command_line_arguments.h +++ vostokd/command_line_arguments.h @@ -3,8 +3,9 @@ #pragma once -#include -#include +#include "not_null" +#include "zstring" +#include "unique_fd" namespace vostok @@ -17,10 +18,9 @@ struct command_line_arguments int m_port{1965}; czstring m_cert_file{nullptr}; czstring m_key_file{nullptr}; + unique_fd m_directory; bool parse_command_line(int argc, char *argv[]); - - void usage(const char *program); };