1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
|
// distribution boxbackup-0.11rc2 (svn version: 2072)
//
// Copyright (c) 2003 - 2008
// Ben Summers and contributors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All use of this software and associated advertising materials must
// display the following acknowledgment:
// This product includes software developed by Ben Summers.
// 4. The names of the Authors may not be used to endorse or promote
// products derived from this software without specific prior written
// permission.
//
// [Where legally impermissible the Authors do not disclaim liability for
// direct physical injury or death caused solely by defects in the software
// unless it is modified by a third party.]
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
//
//
// --------------------------------------------------------------------------
//
// File
// Name: BackupClientContext.cpp
// Purpose: Keep track of context
// Created: 2003/10/08
//
// --------------------------------------------------------------------------
#include "Box.h"
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include "BoxPortsAndFiles.h"
#include "BoxTime.h"
#include "BackupClientContext.h"
#include "SocketStreamTLS.h"
#include "Socket.h"
#include "BackupStoreConstants.h"
#include "BackupStoreException.h"
#include "BackupDaemon.h"
#include "autogen_BackupProtocolClient.h"
#include "BackupStoreFile.h"
#include "Logging.h"
#include "MemLeakFindOn.h"
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::BackupClientContext(BackupDaemon &, TLSContext &, const std::string &, int32_t, bool, bool, std::string)
// Purpose: Constructor
// Created: 2003/10/08
//
// --------------------------------------------------------------------------
BackupClientContext::BackupClientContext
(
BackupDaemon &rDaemon,
TLSContext &rTLSContext,
const std::string &rHostname,
int32_t AccountNumber,
bool ExtendedLogging,
bool ExtendedLogToFile,
std::string ExtendedLogFile
)
: mrDaemon(rDaemon),
mrTLSContext(rTLSContext),
mHostname(rHostname),
mAccountNumber(AccountNumber),
mpSocket(0),
mpConnection(0),
mExtendedLogging(ExtendedLogging),
mExtendedLogToFile(ExtendedLogToFile),
mExtendedLogFile(ExtendedLogFile),
mpExtendedLogFileHandle(NULL),
mClientStoreMarker(ClientStoreMarker_NotKnown),
mpDeleteList(0),
mpCurrentIDMap(0),
mpNewIDMap(0),
mStorageLimitExceeded(false),
mpExcludeFiles(0),
mpExcludeDirs(0),
mKeepAliveTimer(0),
mbIsManaged(false)
{
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::~BackupClientContext()
// Purpose: Destructor
// Created: 2003/10/08
//
// --------------------------------------------------------------------------
BackupClientContext::~BackupClientContext()
{
CloseAnyOpenConnection();
// Delete delete list
if(mpDeleteList != 0)
{
delete mpDeleteList;
mpDeleteList = 0;
}
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::GetConnection()
// Purpose: Returns the connection, making the connection and logging into
// the backup store if necessary.
// Created: 2003/10/08
//
// --------------------------------------------------------------------------
BackupProtocolClient &BackupClientContext::GetConnection()
{
// Already got it? Just return it.
if(mpConnection != 0)
{
return *mpConnection;
}
// Get a socket connection
if(mpSocket == 0)
{
mpSocket = new SocketStreamTLS;
ASSERT(mpSocket != 0); // will have exceptioned if this was a problem
}
try
{
// Defensive.
if(mpConnection != 0)
{
delete mpConnection;
mpConnection = 0;
}
// Log intention
BOX_INFO("Opening connection to server '" <<
mHostname << "'...");
// Connect!
mpSocket->Open(mrTLSContext, Socket::TypeINET, mHostname.c_str(), BOX_PORT_BBSTORED);
// And create a procotol object
mpConnection = new BackupProtocolClient(*mpSocket);
// Set logging option
mpConnection->SetLogToSysLog(mExtendedLogging);
if (mExtendedLogToFile)
{
ASSERT(mpExtendedLogFileHandle == NULL);
mpExtendedLogFileHandle = fopen(
mExtendedLogFile.c_str(), "a+");
if (!mpExtendedLogFileHandle)
{
BOX_ERROR("Failed to open extended log "
"file: " << strerror(errno));
}
else
{
mpConnection->SetLogToFile(mpExtendedLogFileHandle);
}
}
// Handshake
mpConnection->Handshake();
// Check the version of the server
{
std::auto_ptr<BackupProtocolClientVersion> serverVersion(mpConnection->QueryVersion(BACKUP_STORE_SERVER_VERSION));
if(serverVersion->GetVersion() != BACKUP_STORE_SERVER_VERSION)
{
THROW_EXCEPTION(BackupStoreException, WrongServerVersion)
}
}
// Login -- if this fails, the Protocol will exception
std::auto_ptr<BackupProtocolClientLoginConfirmed> loginConf(mpConnection->QueryLogin(mAccountNumber, 0 /* read/write */));
// Check that the client store marker is the one we expect
if(mClientStoreMarker != ClientStoreMarker_NotKnown)
{
if(loginConf->GetClientStoreMarker() != mClientStoreMarker)
{
// Not good... finish the connection, abort, etc, ignoring errors
try
{
mpConnection->QueryFinished();
mpSocket->Shutdown();
mpSocket->Close();
}
catch(...)
{
// IGNORE
}
// Then throw an exception about this
THROW_EXCEPTION(BackupStoreException, ClientMarkerNotAsExpected)
}
}
// Log success
BOX_INFO("Connection made, login successful");
// Check to see if there is any space available on the server
if(loginConf->GetBlocksUsed() >= loginConf->GetBlocksHardLimit())
{
// no -- flag so only things like deletions happen
mStorageLimitExceeded = true;
// Log
BOX_WARNING("Exceeded storage hard-limit on server, "
"not uploading changes to files");
}
}
catch(...)
{
// Clean up.
delete mpConnection;
mpConnection = 0;
delete mpSocket;
mpSocket = 0;
throw;
}
return *mpConnection;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::CloseAnyOpenConnection()
// Purpose: Closes a connection, if it's open
// Created: 2003/10/08
//
// --------------------------------------------------------------------------
void BackupClientContext::CloseAnyOpenConnection()
{
if(mpConnection)
{
try
{
// Need to set a client store marker?
if(mClientStoreMarker == ClientStoreMarker_NotKnown)
{
// Yes, choose one, the current time will do
box_time_t marker = GetCurrentBoxTime();
// Set it on the store
mpConnection->QuerySetClientStoreMarker(marker);
// Record it so that it can be picked up later.
mClientStoreMarker = marker;
}
// Quit nicely
mpConnection->QueryFinished();
}
catch(...)
{
// Ignore errors here
}
// Delete it anyway.
delete mpConnection;
mpConnection = 0;
}
if(mpSocket)
{
try
{
// Be nice about closing the socket
mpSocket->Shutdown();
mpSocket->Close();
}
catch(...)
{
// Ignore errors
}
// Delete object
delete mpSocket;
mpSocket = 0;
}
// Delete any pending list
if(mpDeleteList != 0)
{
delete mpDeleteList;
mpDeleteList = 0;
}
if (mpExtendedLogFileHandle != NULL)
{
fclose(mpExtendedLogFileHandle);
mpExtendedLogFileHandle = NULL;
}
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::GetTimeout()
// Purpose: Gets the current timeout time.
// Created: 2003/10/08
//
// --------------------------------------------------------------------------
int BackupClientContext::GetTimeout() const
{
if(mpConnection)
{
return mpConnection->GetTimeout();
}
return (15*60*1000);
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::GetDeleteList()
// Purpose: Returns the delete list, creating one if necessary
// Created: 10/11/03
//
// --------------------------------------------------------------------------
BackupClientDeleteList &BackupClientContext::GetDeleteList()
{
// Already created?
if(mpDeleteList == 0)
{
mpDeleteList = new BackupClientDeleteList;
}
// Return reference to object
return *mpDeleteList;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::PerformDeletions()
// Purpose: Perform any pending file deletions.
// Created: 10/11/03
//
// --------------------------------------------------------------------------
void BackupClientContext::PerformDeletions()
{
// Got a list?
if(mpDeleteList == 0)
{
// Nothing to do
return;
}
// Delegate to the delete list object
mpDeleteList->PerformDeletions(*this);
// Delete the object
delete mpDeleteList;
mpDeleteList = 0;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::GetCurrentIDMap() const
// Purpose: Return a (const) reference to the current ID map
// Created: 11/11/03
//
// --------------------------------------------------------------------------
const BackupClientInodeToIDMap &BackupClientContext::GetCurrentIDMap() const
{
ASSERT(mpCurrentIDMap != 0);
if(mpCurrentIDMap == 0)
{
THROW_EXCEPTION(CommonException, Internal)
}
return *mpCurrentIDMap;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::GetNewIDMap() const
// Purpose: Return a reference to the new ID map
// Created: 11/11/03
//
// --------------------------------------------------------------------------
BackupClientInodeToIDMap &BackupClientContext::GetNewIDMap() const
{
ASSERT(mpNewIDMap != 0);
if(mpNewIDMap == 0)
{
THROW_EXCEPTION(CommonException, Internal)
}
return *mpNewIDMap;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::FindFilename(int64_t, int64_t, std::string &, bool &) const
// Purpose: Attempts to find the pathname of an object with a given ID on the server.
// Returns true if it can be found, in which case rPathOut is the local filename,
// and rIsDirectoryOut == true if the local object is a directory.
// Created: 12/11/03
//
// --------------------------------------------------------------------------
bool BackupClientContext::FindFilename(int64_t ObjectID, int64_t ContainingDirectory, std::string &rPathOut, bool &rIsDirectoryOut,
bool &rIsCurrentVersionOut, box_time_t *pModTimeOnServer, box_time_t *pAttributesHashOnServer, BackupStoreFilenameClear *pLeafname)
{
// Make a connection to the server
BackupProtocolClient &connection(GetConnection());
// Request filenames from the server, in a "safe" manner to ignore errors properly
{
BackupProtocolClientGetObjectName send(ObjectID, ContainingDirectory);
connection.Send(send);
}
std::auto_ptr<BackupProtocolObjectCl> preply(connection.Receive());
// Is it of the right type?
if(preply->GetType() != BackupProtocolClientObjectName::TypeID)
{
// Was an error or something
return false;
}
// Cast to expected type.
BackupProtocolClientObjectName *names = (BackupProtocolClientObjectName *)(preply.get());
// Anything found?
int32_t numElements = names->GetNumNameElements();
if(numElements <= 0)
{
// No.
return false;
}
// Get the stream containing all the names
std::auto_ptr<IOStream> nameStream(connection.ReceiveStream());
// Path
std::string path;
// Remember this is in reverse order!
for(int l = 0; l < numElements; ++l)
{
BackupStoreFilenameClear elementName;
elementName.ReadFromStream(*nameStream, GetTimeout());
// Store leafname for caller?
if(l == 0 && pLeafname)
{
*pLeafname = elementName;
}
// Is it part of the filename in the location?
if(l < (numElements - 1))
{
// Part of filename within
path = (path.empty())?(elementName.GetClearFilename()):(elementName.GetClearFilename() + DIRECTORY_SEPARATOR_ASCHAR + path);
}
else
{
// Location name -- look up in daemon's records
std::string locPath;
if(!mrDaemon.FindLocationPathName(elementName.GetClearFilename(), locPath))
{
// Didn't find the location... so can't give the local filename
return false;
}
// Add in location path
path = (path.empty())?(locPath):(locPath + DIRECTORY_SEPARATOR_ASCHAR + path);
}
}
// Is it a directory?
rIsDirectoryOut = ((names->GetFlags() & BackupProtocolClientListDirectory::Flags_Dir) == BackupProtocolClientListDirectory::Flags_Dir);
// Is it the current version?
rIsCurrentVersionOut = ((names->GetFlags() & (BackupProtocolClientListDirectory::Flags_OldVersion | BackupProtocolClientListDirectory::Flags_Deleted)) == 0);
// And other information which may be required
if(pModTimeOnServer) *pModTimeOnServer = names->GetModificationTime();
if(pAttributesHashOnServer) *pAttributesHashOnServer = names->GetAttributesHash();
// Tell caller about the pathname
rPathOut = path;
// Found
return true;
}
void BackupClientContext::SetMaximumDiffingTime(int iSeconds)
{
mMaximumDiffingTime = iSeconds < 0 ? 0 : iSeconds;
BOX_TRACE("Set maximum diffing time to " << mMaximumDiffingTime <<
" seconds");
}
void BackupClientContext::SetKeepAliveTime(int iSeconds)
{
mKeepAliveTime = iSeconds < 0 ? 0 : iSeconds;
BOX_TRACE("Set keep-alive time to " << mKeepAliveTime << " seconds");
mKeepAliveTimer = Timer(mKeepAliveTime);
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::ManageDiffProcess()
// Purpose: Initiates a file diff control timer
// Created: 04/19/2005
//
// --------------------------------------------------------------------------
void BackupClientContext::ManageDiffProcess()
{
ASSERT(!mbIsManaged);
mbIsManaged = true;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::UnManageDiffProcess()
// Purpose: suspends file diff control timer
// Created: 04/19/2005
//
// --------------------------------------------------------------------------
void BackupClientContext::UnManageDiffProcess()
{
// ASSERT(mbIsManaged);
mbIsManaged = false;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::DoKeepAlive()
// Purpose: Check whether it's time to send a KeepAlive
// message over the SSL link, and if so, send it.
// Created: 04/19/2005
//
// --------------------------------------------------------------------------
void BackupClientContext::DoKeepAlive()
{
if (!mpConnection)
{
return;
}
if (mKeepAliveTime == 0)
{
return;
}
if (!mKeepAliveTimer.HasExpired())
{
return;
}
BOX_TRACE("KeepAliveTime reached, sending keep-alive message");
mpConnection->QueryGetIsAlive();
mKeepAliveTimer = Timer(mKeepAliveTime);
}
int BackupClientContext::GetMaximumDiffingTime()
{
return mMaximumDiffingTime;
}
|