| 1 |
/* |
| 2 |
* This File is part of Davix, The IO library for HTTP based protocols |
| 3 |
* Copyright (C) CERN 2013 |
| 4 |
* Author: Adrien Devresse <[email protected]> |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <utils/davix_types.hpp> |
| 23 |
#include <davix.hpp> |
| 24 |
#include <davix_internal.hpp> |
| 25 |
#include <utils/stringutils.hpp> |
| 26 |
#include "davix_tool_util.hpp" |
| 27 |
#include <utils/davix_logger_internal.hpp> |
| 28 |
#include <utils/davix_gcloud_utils.hpp> |
| 29 |
|
| 30 |
#include <ctype.h> |
| 31 |
#include "utils/simple_get_pass.h" |
| 32 |
|
| 33 |
#include <sys/ioctl.h> |
| 34 |
#include <unistd.h> |
| 35 |
|
| 36 |
namespace Davix{ |
| 37 |
|
| 38 |
namespace Tool{ |
| 39 |
|
| 40 |
using namespace StrUtil; |
| 41 |
using namespace std::placeholders; |
| 42 |
|
| 43 |
dav_ssize_t writeToFd(int fd, const void* buffer, dav_size_t s_buff){ |
| 44 |
while(1){ |
| 45 |
ssize_t ret; |
| 46 |
errno =0; |
| 47 |
if((ret= write(fd, buffer, static_cast<size_t>(s_buff))) <0 && errno == EINTR){ |
| 48 |
continue; |
| 49 |
} |
| 50 |
return static_cast<dav_ssize_t>(ret); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
dav_ssize_t writeToFd(int fd, const std::string & str){ |
| 55 |
return writeToFd(fd, str.c_str(), str.size()); |
| 56 |
} |
| 57 |
|
| 58 |
std::string string_from_ptime(const time_t & t){ |
| 59 |
char b[255]; |
| 60 |
b[sizeof(b)-1]= '\0'; |
| 61 |
|
| 62 |
struct tm local_time; |
| 63 |
(void) localtime_r(&t, &local_time); |
| 64 |
strftime(b, 254, "%F %T", &local_time); |
| 65 |
return std::string(b); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
dav_ssize_t ask_user_login(std::string & login){ |
| 71 |
char l[1024] ={0}; |
| 72 |
(std::cerr << "Login: ").flush(); |
| 73 |
std::cin.getline(l, 1023); |
| 74 |
login.assign(l); |
| 75 |
std::fill(l, l+1024,'\0'); |
| 76 |
return login.size(); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
dav_ssize_t ask_user_passwd(std::string & passwd, const std::string &prompt = "Password: "){ |
| 81 |
char p[1024] ={0}; |
| 82 |
std::cerr << prompt; |
| 83 |
std::cerr.flush(); |
| 84 |
if(simple_get_pass(p, 1023) > 0){ |
| 85 |
passwd.assign(p); |
| 86 |
std::fill(p, p+1024,'\0'); |
| 87 |
return passwd.size(); |
| 88 |
} |
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
void writeConsoleLine(int fd, char symbol, const std::string & msg){ |
| 93 |
std::ostringstream ss; |
| 94 |
ss << symbol << " " << msg << "\n"; |
| 95 |
writeToFd(fd, ss.str()); |
| 96 |
} |
| 97 |
|
| 98 |
int configureAuth(OptParams & opts){ |
| 99 |
// setup client side credential |
| 100 |
opts.params.setClientCertCallbackX509(&authCallbackCert, &opts); |
| 101 |
// setup client login / password |
| 102 |
opts.params.setClientLoginPasswordCallback(&authCallbackLoginPassword, &opts); |
| 103 |
|
| 104 |
//setup aws creds |
| 105 |
if(opts.aws_auth.first.empty() == false){ |
| 106 |
opts.params.setAwsAuthorizationKeys(opts.aws_auth.first, opts.aws_auth.second); |
| 107 |
opts.params.setAwsRegion(opts.aws_region); |
| 108 |
opts.params.setAwsToken(opts.aws_token); |
| 109 |
opts.params.setAwsAlternate(opts.aws_alternate); |
| 110 |
opts.params.setProtocol(RequestProtocol::AwsS3); |
| 111 |
} |
| 112 |
|
| 113 |
// setup azure creds |
| 114 |
if(opts.azure_key.empty() == false) { |
| 115 |
opts.params.setAzureKey(opts.azure_key); |
| 116 |
opts.params.setProtocol(RequestProtocol::Azure); |
| 117 |
} |
| 118 |
|
| 119 |
// setup gcloud creds |
| 120 |
if(opts.gcloud_creds_path.empty() == false) { |
| 121 |
gcloud::CredentialProvider provider; |
| 122 |
opts.params.setGcloudCredentials(provider.fromFile(opts.gcloud_creds_path)); |
| 123 |
opts.params.setProtocol(RequestProtocol::Gcloud); |
| 124 |
} |
| 125 |
|
| 126 |
// setup swift creds |
| 127 |
if(opts.os_token.empty() == false && (opts.os_project_id.empty() == false || opts.swift_account.empty() == false)) { |
| 128 |
opts.params.setOSToken(opts.os_token); |
| 129 |
opts.params.setOSProjectID(opts.os_project_id); |
| 130 |
opts.params.setSwiftAccount(opts.swift_account); |
| 131 |
opts.params.setProtocol(RequestProtocol::Swift); |
| 132 |
} |
| 133 |
|
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static bool startswith(const std::string &str, const std::string &prefix) { |
| 138 |
if(prefix.size() > str.size()) return false; |
| 139 |
|
| 140 |
for(size_t i = 0; i < prefix.size(); i++) { |
| 141 |
if(str[i] != prefix[i]) return false; |
| 142 |
} |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
const std::string scope_params = "Davix::Tools::Params"; |
| 147 |
bool checkProtocolSanity(OptParams &opts, const std::string &url, DavixError **err) { |
| 148 |
if(!opts.aws_auth.first.empty()) { |
| 149 |
if(!startswith(url, "s3://") && !startswith(url, "s3s://") && !startswith(url, "http://") && !startswith(url, "https://")) { |
| 150 |
DavixError::setupError(err, scope_params, StatusCode::InvalidArgument, fmt::format(" S3 credentials cannot be used with the protocol given in this URL : {}", url)); |
| 151 |
return false; |
| 152 |
} |
| 153 |
} |
| 154 |
if(!opts.os_token.empty()) { |
| 155 |
if(!startswith(url, "swift://") && !startswith(url, "swifts://") && !startswith(url, "http://") && !startswith(url, "https://")) { |
| 156 |
DavixError::setupError(err, scope_params, StatusCode::InvalidArgument, fmt::format(" Swift credentials cannot be used with the protocol given in this URL : {}", url)); |
| 157 |
return false; |
| 158 |
} |
| 159 |
} |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
static void printHookHeaders(char symbol, const std::string & first_msg, const std::string & start_line){ |
| 165 |
std::string req_header(start_line); |
| 166 |
StrUtil::remove(req_header, '\r'); |
| 167 |
rtrim(req_header, StrUtil::isCrLf()); |
| 168 |
std::vector<std::string> res; |
| 169 |
split(req_header, '\n', res); |
| 170 |
|
| 171 |
writeConsoleLine(STDOUT_FILENO, '*', first_msg); |
| 172 |
for(stringVec::iterator it =res.begin(); it != res.end(); ++it){ |
| 173 |
writeConsoleLine(STDOUT_FILENO, symbol, *it); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
static void printHookHeadersVec(char symbol, const std::string & first_msg, const std::string & init_line, |
| 178 |
const HeaderVec & headers){ |
| 179 |
std::string parsed_first_line(init_line); |
| 180 |
StrUtil::remove(parsed_first_line, '\r'); |
| 181 |
rtrim(parsed_first_line, StrUtil::isCrLf()); |
| 182 |
|
| 183 |
writeConsoleLine(STDOUT_FILENO, '*', first_msg); |
| 184 |
writeConsoleLine(STDOUT_FILENO, symbol, parsed_first_line); |
| 185 |
for(HeaderVec::const_iterator it =headers.begin(); it != headers.end(); ++it){ |
| 186 |
std::string header_line(it->first); |
| 187 |
header_line.append(": ").append(it->second); |
| 188 |
writeConsoleLine(STDOUT_FILENO, symbol, header_line); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void hook_davix_tool_pre_send(HttpRequest& req, const std::string & start_line){ |
| 193 |
(void) req; |
| 194 |
printHookHeaders('>', "Send request", start_line); |
| 195 |
} |
| 196 |
|
| 197 |
void hook_davix_tool_pre_rec(HttpRequest& req, const std::string & init_line, const HeaderVec & headers, int status_code){ |
| 198 |
(void) req; |
| 199 |
(void) status_code; |
| 200 |
printHookHeadersVec('<', "Receive answer", init_line, headers); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
void configureContext(Context &context, const OptParams & opts){ |
| 205 |
if(opts.pres_flag & DISPLAY_HEADERS){ |
| 206 |
RequestPreSendHook send_hook(hook_davix_tool_pre_send); |
| 207 |
RequestPreReceHook rece_hook(hook_davix_tool_pre_rec); |
| 208 |
context.setHook(send_hook); |
| 209 |
context.setHook(rece_hook); |
| 210 |
} |
| 211 |
for(std::vector<std::string>::const_iterator it = opts.modules_list.begin(); it != opts.modules_list.end(); it++){ |
| 212 |
context.loadModule(*it); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
int getOutFd(const Tool::OptParams & opts, const std::string & scope, DavixError** err){ |
| 217 |
int fd = -1; |
| 218 |
|
| 219 |
if(opts.output_file_path.empty() == false){ |
| 220 |
if((fd = open(opts.output_file_path.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0777)) <0 ){ |
| 221 |
davix_errno_to_davix_error(errno, scope, std::string("for destination file ").append(opts.output_file_path), err); |
| 222 |
return -1; |
| 223 |
} |
| 224 |
}else{ |
| 225 |
fd = dup(STDOUT_FILENO); |
| 226 |
} |
| 227 |
return fd; |
| 228 |
} |
| 229 |
|
| 230 |
int getInFd(const Tool::OptParams & opts, const std::string & scope, DavixError** err){ |
| 231 |
int fd = -1; |
| 232 |
if(opts.input_file_path.empty() == false){ |
| 233 |
if((fd = open(opts.input_file_path.c_str(), O_RDONLY)) <0 ){ |
| 234 |
davix_errno_to_davix_error(errno, scope, std::string("for source file ").append(opts.input_file_path), err); |
| 235 |
return -1; |
| 236 |
} |
| 237 |
}else{ |
| 238 |
fd = dup(STDOUT_FILENO); |
| 239 |
} |
| 240 |
return fd; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
void errorPrint(DavixError ** err){ |
| 245 |
if(err && *err){ |
| 246 |
std::cerr << "("<< (*err)->getErrScope() <<") Error: "<< (*err)->getErrMsg() << std::endl; |
| 247 |
DavixError::clearError(err); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
std::string mode_to_stringmode(mode_t mode){ |
| 252 |
mode_t tmp_mode = mode; |
| 253 |
//static const char * strv= "xwrxwrxwr"; |
| 254 |
char res[11]; |
| 255 |
std::fill(res, res+10, '-'); |
| 256 |
res[10]='\0'; |
| 257 |
for(int i=0; i <9; i++){ |
| 258 |
res[9-i] = ( mode = (tmp_mode >> 1)) & 0x01; |
| 259 |
} |
| 260 |
res[0]= S_ISDIR(mode); |
| 261 |
return std::string(res); |
| 262 |
} |
| 263 |
|
| 264 |
int authCallbackLoginPassword(void* userdata, const SessionInfo & info, std::string & login, std::string & password, |
| 265 |
int count, DavixError** err){ |
| 266 |
(void) info; |
| 267 |
OptParams* opts = static_cast<OptParams*>(userdata); |
| 268 |
|
| 269 |
// authentication based on command-line parameters? |
| 270 |
// never prompt for a password, even if provided credentials are wrong |
| 271 |
if(opts->userlogpasswd_through_cmdline) { |
| 272 |
login = opts->userlogpasswd.first; |
| 273 |
password = opts->userlogpasswd.second; |
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
// keyboard-based authentication? |
| 278 |
// prompt again only if last attempt failed |
| 279 |
if(count == 0 && !opts->userlogpasswd.first.empty()) { |
| 280 |
login = opts->userlogpasswd.first; |
| 281 |
password = opts->userlogpasswd.second; |
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
// need to provide credentials through keyboard |
| 286 |
if(count == 0) { |
| 287 |
std::cerr << "Basic authentication - server is asking for username and password:\n"; |
| 288 |
} |
| 289 |
else { |
| 290 |
std::cerr << "Authentication failure, try again:\n"; |
| 291 |
} |
| 292 |
|
| 293 |
if(ask_user_login(login) > 0) { |
| 294 |
if(ask_user_passwd(password) > 0) { |
| 295 |
std::cerr << std::endl; |
| 296 |
opts->userlogpasswd.first = login; |
| 297 |
opts->userlogpasswd.second = password; |
| 298 |
return 0; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
DavixError::setupError(err, "Davix::Tool::Auth", |
| 303 |
StatusCode::LoginPasswordError, |
| 304 |
"No valid login/password provided"); |
| 305 |
return -1; |
| 306 |
} |
| 307 |
|
| 308 |
int authCallbackCert(void* userdata, const SessionInfo & info, X509Credential* cert, DavixError** err){ |
| 309 |
(void) info; |
| 310 |
OptParams* opts = static_cast<OptParams*>(userdata); |
| 311 |
std::string key_path(opts->priv_key), cred_path(opts->cred_path); |
| 312 |
|
| 313 |
if(key_path.empty()) { |
| 314 |
key_path = cred_path; |
| 315 |
} |
| 316 |
|
| 317 |
// empty credentials? |
| 318 |
if(opts->cred_path.empty()) { |
| 319 |
if(err && *err == NULL){ |
| 320 |
DavixError::setupError(err, "Davix::Tool::Auth", |
| 321 |
StatusCode::LoginPasswordError, |
| 322 |
"No valid client credential provided"); |
| 323 |
} |
| 324 |
return -1; |
| 325 |
} |
| 326 |
|
| 327 |
// try with existing, cached password first |
| 328 |
if(cert->loadFromFilePEM(key_path, cred_path, opts->cred_pass, err) <0) { |
| 329 |
if( (*err)->getStatus() != StatusCode::CredDecryptionError || !opts->cred_pass.empty()) { |
| 330 |
// credential specific error |
| 331 |
std::cerr << "("<< (*err)->getErrScope() <<") Error: "<< (*err)->getErrMsg() << std::endl; |
| 332 |
DavixError::clearError(err); |
| 333 |
return -1; |
| 334 |
} |
| 335 |
} |
| 336 |
else { |
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
// password is empty, prompt user |
| 341 |
DavixError::clearError(err); |
| 342 |
if(ask_user_passwd(opts->cred_pass, "Certificate password: ") < 0 |
| 343 |
|| cert->loadFromFilePEM(key_path, cred_path, opts->cred_pass, err) < 0) { |
| 344 |
|
| 345 |
if(err && *err) { |
| 346 |
std::cerr << "("<< (*err)->getErrScope() <<") Error: "<< (*err)->getErrMsg() << std::endl; |
| 347 |
} |
| 348 |
return -1; |
| 349 |
} |
| 350 |
std::cerr << std::endl; |
| 351 |
return 0; |
| 352 |
} |
| 353 |
|
| 354 |
std::string string_from_mode(mode_t mode){ |
| 355 |
const char* rmask ="xwr"; |
| 356 |
std::string str(10,'-'); |
| 357 |
|
| 358 |
str[0] = (S_ISDIR(mode))?'d':'-'; |
| 359 |
for(size_t i=0; i < 9; ++i){ |
| 360 |
str[9-i] = (( mode & (0x01 << i))?(rmask[i%3]):'-'); |
| 361 |
} |
| 362 |
return str; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
std::string string_from_size_t(size_t number, size_t size_string){ |
| 367 |
unsigned int digit= static_cast<unsigned int>(log10(static_cast<double>(number))); |
| 368 |
std::ostringstream ss; |
| 369 |
ss << number; |
| 370 |
ss << std::string(((digit < size_string)?(size_string - digit):0), ' '); |
| 371 |
return ss.str(); |
| 372 |
} |
| 373 |
|
| 374 |
bool isShell(int fd){ |
| 375 |
if(isatty(fd) ==1) |
| 376 |
return true; |
| 377 |
errno =0; |
| 378 |
return false; |
| 379 |
} |
| 380 |
|
| 381 |
void flushFinalLineShell(int fd){ |
| 382 |
if(isShell(fd)){ |
| 383 |
writeToFd(fd, "\n", 1); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
std::string SanitiseTildedPath(const char * path){ |
| 388 |
if(path[0] == '~') |
| 389 |
{ |
| 390 |
std::string newpath(path); |
| 391 |
newpath.erase(0, 1); |
| 392 |
const char * val = std::getenv("HOME"); |
| 393 |
|
| 394 |
if(val != NULL){ |
| 395 |
newpath.insert(0, val); |
| 396 |
} |
| 397 |
else{ |
| 398 |
newpath.insert(0, "/"); |
| 399 |
} |
| 400 |
return newpath; |
| 401 |
} |
| 402 |
return std::string(path); |
| 403 |
} |
| 404 |
|
| 405 |
bool is_number(const std::string& s) |
| 406 |
{ |
| 407 |
std::string::const_iterator it = s.begin(); |
| 408 |
while (it != s.end() && std::isdigit(*it)) ++it; |
| 409 |
return !s.empty() && it == s.end(); |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
void TransferMonitor(const Uri & url, Transfer::Type op_type, dav_ssize_t bytes_transfered, dav_size_t total_size, Transfer::Type tool_type){ |
| 414 |
static bool print_header= false; |
| 415 |
static Chrono::TimePoint start_time, last_time; |
| 416 |
Chrono::TimePoint current_time; |
| 417 |
Chrono::Clock clk(Chrono::Clock::Monolitic, Chrono::Clock::Second); |
| 418 |
|
| 419 |
int out_fd = STDERR_FILENO; // output fd -> stderr |
| 420 |
|
| 421 |
if((op_type != tool_type)){ |
| 422 |
return; |
| 423 |
} |
| 424 |
|
| 425 |
int progress = 0; |
| 426 |
dav_size_t baudrate =0; |
| 427 |
current_time = clk.now(); |
| 428 |
|
| 429 |
if(print_header == false){ |
| 430 |
std::string type = ((op_type == Transfer::Read) ? "Read" : "Write"); |
| 431 |
last_time = start_time = clk.now(); |
| 432 |
|
| 433 |
std::cout << "Performing " << type << " operation on: " << url << std::endl; |
| 434 |
print_header = true; |
| 435 |
}else{ |
| 436 |
// tty display is slow |
| 437 |
// display progression only every second |
| 438 |
if(current_time < (last_time + Chrono::Duration(1)) && (static_cast<dav_size_t>(bytes_transfered) != total_size)) |
| 439 |
return; |
| 440 |
} |
| 441 |
last_time = clk.now(); |
| 442 |
Chrono::Duration diff_time = last_time - start_time; |
| 443 |
if( diff_time.toTimeValue() > 0){ |
| 444 |
baudrate = bytes_transfered/(diff_time.toTimeValue()); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
if(total_size > 0){ |
| 449 |
progress = static_cast<int>((static_cast<double>(bytes_transfered) / static_cast<double>(total_size)) * 100); |
| 450 |
} |
| 451 |
|
| 452 |
printProgressBar(out_fd, progress, bytes_transfered, total_size, baudrate); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
std::string normalize_unit(dav_size_t bytes){ |
| 457 |
const char * tab_unit[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei" }; |
| 458 |
size_t size_tab_unit= sizeof(tab_unit)/sizeof(const char*); |
| 459 |
|
| 460 |
const dav_size_t factor = 10*1024; |
| 461 |
size_t i; |
| 462 |
|
| 463 |
for(i =0; i < size_tab_unit; ++i){ |
| 464 |
if(bytes < factor){ |
| 465 |
break; |
| 466 |
} |
| 467 |
bytes /= 1024; |
| 468 |
} |
| 469 |
return fmt::format("{}{}", bytes, tab_unit[i]); |
| 470 |
} |
| 471 |
|
| 472 |
void printProgressBar(int out_fd, int percent, dav_ssize_t bytes_transfered, dav_size_t total_size, dav_size_t baudrate){ |
| 473 |
std::string bar; |
| 474 |
std::string unit; |
| 475 |
static bool printOnce = true; |
| 476 |
dav_size_t size = total_size; |
| 477 |
|
| 478 |
struct winsize win; |
| 479 |
ioctl(out_fd, TIOCGWINSZ, &win); |
| 480 |
|
| 481 |
|
| 482 |
// w = width of bar, r = 100/w |
| 483 |
double w = static_cast<double>(win.ws_row)/1.5; |
| 484 |
double r = 100/w; |
| 485 |
for(double i = 0; i < w; ++i){ |
| 486 |
if(i < (static_cast<double>(percent)/r) ){ |
| 487 |
bar.replace(i,1,"="); |
| 488 |
}else{ |
| 489 |
bar.replace(i,1," "); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
if((percent == 100) && !printOnce) |
| 494 |
return; |
| 495 |
|
| 496 |
std::cout << "\r" "[" << bar << "] "; |
| 497 |
std::cout.width(3); |
| 498 |
std::cout << percent << "% " << |
| 499 |
normalize_unit(bytes_transfered) << 'B' << "/" << normalize_unit(size) << 'B'; |
| 500 |
std::cout.width(10); |
| 501 |
std::cout << normalize_unit(baudrate) << "B/s" << |
| 502 |
" " << std::flush; |
| 503 |
|
| 504 |
if(static_cast<dav_size_t>(bytes_transfered) == size){ |
| 505 |
std::cout << std::endl; |
| 506 |
printOnce = false; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
int configureMonitorCB(OptParams & opts, Transfer::Type type){ |
| 511 |
using namespace std; |
| 512 |
|
| 513 |
// active monitoring callback only if output != stdout and stderr is a terminal |
| 514 |
if(opts.output_file_path.empty() == false |
| 515 |
&& isatty(STDERR_FILENO) ==1 ){ |
| 516 |
TransferMonitorCB transMonCB; |
| 517 |
transMonCB = bind(&Tool::TransferMonitor, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, type); |
| 518 |
opts.params.setTransfertMonitorCb(transMonCB); |
| 519 |
} |
| 520 |
|
| 521 |
return 0; |
| 522 |
} |
| 523 |
|
| 524 |
void tokeniseUrl(std::string url, std::deque<std::string>& dirVec){ |
| 525 |
if(url.empty()) |
| 526 |
return; |
| 527 |
|
| 528 |
std::string::iterator iter; |
| 529 |
|
| 530 |
for(iter = url.begin(); iter != url.end(); ){ |
| 531 |
std::string::iterator tmpIter = std::find(iter, url.end(), '/'); |
| 532 |
std::string tmp = std::string(iter, tmpIter); |
| 533 |
dirVec.push_back(tmp); |
| 534 |
|
| 535 |
iter = tmpIter; |
| 536 |
if(tmpIter != url.end()) |
| 537 |
++iter; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
int mkdirP(std::string url, bool trim){ |
| 542 |
|
| 543 |
int ret = -1; |
| 544 |
|
| 545 |
std::deque<std::string> dirVec; |
| 546 |
std::string::iterator iter; |
| 547 |
|
| 548 |
for(iter = url.begin(); iter != url.end(); ){ |
| 549 |
std::string::iterator tmpIter = std::find(iter, url.end(), '/'); |
| 550 |
std::string tmp = std::string(url.begin(), tmpIter); |
| 551 |
dirVec.push_back(tmp); |
| 552 |
|
| 553 |
iter = tmpIter; |
| 554 |
if(tmpIter != url.end()) |
| 555 |
++iter; |
| 556 |
} |
| 557 |
|
| 558 |
ret = mkdirP(dirVec, trim); |
| 559 |
|
| 560 |
return ret; |
| 561 |
} |
| 562 |
|
| 563 |
int mkdirP(std::deque<std::string>& dirVec, bool trim){ |
| 564 |
std::deque<std::string> dirList; |
| 565 |
|
| 566 |
// ignore . entries, don't need to create |
| 567 |
if(dirVec[0] == "." || dirVec[0] == "./") |
| 568 |
dirVec.pop_front(); |
| 569 |
|
| 570 |
// discard first and last entry, for cases where first already exists and last is a file to be create/open |
| 571 |
if(trim){ |
| 572 |
dirVec.pop_front(); |
| 573 |
dirVec.pop_back(); |
| 574 |
} |
| 575 |
|
| 576 |
/* |
| 577 |
//TODO: optimise logic to skip tokens that have been created previously |
| 578 |
for(unsigned int i=0; i<dirVec.size(); ++i){ |
| 579 |
if(mkdir(dirVec[i].c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0){ |
| 580 |
if(errno == EEXIST) |
| 581 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_CORE, "Failed to create local directory, {} already exist. Continuing", dirVec[i]); |
| 582 |
else{ |
| 583 |
std::cout << std::endl << "Failed to create local directory for " << dirVec[i] << std::endl; |
| 584 |
return -1; |
| 585 |
} |
| 586 |
} |
| 587 |
return 0; |
| 588 |
} |
| 589 |
return 0; |
| 590 |
*/ |
| 591 |
if(dirVec.size() == 0) |
| 592 |
return 0; |
| 593 |
|
| 594 |
int ret = -1; |
| 595 |
|
| 596 |
// loop backwards and attempt to create dir with largest depth |
| 597 |
// break when an existing dir has been found, or one has been created successfully |
| 598 |
// failed attemps are pushed to queue to retry once we have the location of an existing dir |
| 599 |
for(int i=dirVec.size()-1; i>=0; --i){ |
| 600 |
ret = mkdir(dirVec[i].c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); |
| 601 |
|
| 602 |
if((ret == 0) || (errno == EEXIST)){ |
| 603 |
break; |
| 604 |
} |
| 605 |
else{ |
| 606 |
std::string tmp = dirVec[i]; |
| 607 |
dirList.push_front(tmp); |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
// at this point we only have to create the dir(s) they do not already exist |
| 612 |
for(unsigned int ii=0; ii<dirList.size(); ++ii){ |
| 613 |
if(mkdir(dirList[ii].c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0){ |
| 614 |
if(errno == EEXIST) |
| 615 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_CORE, "Failed to create local directory, {} already exist. Continuing", dirList[ii]); |
| 616 |
else{ |
| 617 |
std::cout << std::endl << "Failed to create local directory for " << dirList[ii] << std::endl; |
| 618 |
return -1; |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
void batchTransferMonitor(std::string dirPath, std::string msg, int entryCount, int totalEntry){ |
| 627 |
if(isatty(STDERR_FILENO) !=1) |
| 628 |
return; |
| 629 |
|
| 630 |
static bool print_header = false; |
| 631 |
static Chrono::TimePoint start_time, last_time; |
| 632 |
Chrono::TimePoint current_time; |
| 633 |
Chrono::Clock clk(Chrono::Clock::Monolitic, Chrono::Clock::Second); |
| 634 |
|
| 635 |
current_time = clk.now(); |
| 636 |
|
| 637 |
|
| 638 |
if(print_header == false){ |
| 639 |
last_time = start_time = clk.now(); |
| 640 |
print_header = true; |
| 641 |
}else{ |
| 642 |
if(current_time < (last_time + Chrono::Duration(1))) |
| 643 |
return; |
| 644 |
} |
| 645 |
|
| 646 |
std::cout << "\r" << msg << " " << dirPath << |
| 647 |
" Files processed: "; |
| 648 |
std::cout.width(10); |
| 649 |
std::cout << entryCount; |
| 650 |
|
| 651 |
if(totalEntry != 0) |
| 652 |
std::cout << "/" << totalEntry; |
| 653 |
|
| 654 |
std::cout << " " << std::flush; |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
} //Tool |
| 659 |
} //Davix |