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
|
// --------------------------------------------------------------------------
//
// 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_BackupProtocol.h"
#include "BackupStoreFile.h"
#include "Logging.h"
#include "TcpNice.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
(
LocationResolver &rResolver,
TLSContext &rTLSContext,
const std::string &rHostname,
int Port,
uint32_t AccountNumber,
bool ExtendedLogging,
bool ExtendedLogToFile,
std::string ExtendedLogFile,
ProgressNotifier& rProgressNotifier,
bool TcpNiceMode
)
: mExperimentalSnapshotMode(false),
mrResolver(rResolver),
mrTLSContext(rTLSContext),
mHostname(rHostname),
mPort(Port),
mAccountNumber(AccountNumber),
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, "KeepAliveTime"),
mbIsManaged(false),
mrProgressNotifier(rProgressNotifier),
mTcpNiceMode(TcpNiceMode),
mpNice(NULL)
{
}
// --------------------------------------------------------------------------
//
// 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
//
// --------------------------------------------------------------------------
BackupProtocolCallable &BackupClientContext::GetConnection()
{
// Already got it? Just return it.
if(mapConnection.get())
{
return *mapConnection;
}
// Defensive. Must close connection before releasing any old socket.
mapConnection.reset();
std::auto_ptr<SocketStream> apSocket(new SocketStreamTLS);
try
{
// Defensive.
mapConnection.reset();
// Log intention
BOX_INFO("Opening connection to server '" << mHostname <<
"'...");
// Connect!
((SocketStreamTLS *)(apSocket.get()))->Open(mrTLSContext,
Socket::TypeINET, mHostname, mPort);
if(mTcpNiceMode)
{
// Pass control of apSocket to NiceSocketStream,
// which will take care of destroying it for us.
// But we need to hang onto a pointer to the nice
// socket, so we can enable and disable nice mode.
// This is scary, it could be deallocated under us.
mpNice = new NiceSocketStream(apSocket);
apSocket.reset(mpNice);
}
// We need to call some methods that aren't defined in
// BackupProtocolCallable, so we need to hang onto a more
// strongly typed pointer (to avoid far too many casts).
BackupProtocolClient *pClient = new BackupProtocolClient(apSocket);
mapConnection.reset(pClient);
// Set logging option
pClient->SetLogToSysLog(mExtendedLogging);
if (mExtendedLogToFile)
{
ASSERT(mpExtendedLogFileHandle == NULL);
mpExtendedLogFileHandle = fopen(
mExtendedLogFile.c_str(), "a+");
if (!mpExtendedLogFileHandle)
{
BOX_LOG_SYS_ERROR("Failed to open extended "
"log file: " << mExtendedLogFile);
}
else
{
pClient->SetLogToFile(mpExtendedLogFileHandle);
}
}
// Handshake
pClient->Handshake();
// Check the version of the server
{
std::auto_ptr<BackupProtocolVersion> serverVersion(
mapConnection->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<BackupProtocolLoginConfirmed> loginConf(
mapConnection->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
{
mapConnection->QueryFinished();
}
catch(...)
{
// IGNORE
}
// Then throw an exception about this
THROW_EXCEPTION_MESSAGE(BackupStoreException,
ClientMarkerNotAsExpected,
"Expected " << mClientStoreMarker <<
" but found " << loginConf->GetClientStoreMarker() <<
": is someone else writing to the "
"same account?");
}
}
else // mClientStoreMarker == ClientStoreMarker_NotKnown
{
// Yes, choose one, the current time will do
box_time_t marker = GetCurrentBoxTime();
// Set it on the store
mapConnection->QuerySetClientStoreMarker(marker);
// Record it so that it can be picked up later.
mClientStoreMarker = marker;
}
// 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.
mapConnection.reset();
throw;
}
return *mapConnection;
}
BackupProtocolCallable* BackupClientContext::GetOpenConnection() const
{
return mapConnection.get();
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupClientContext::CloseAnyOpenConnection()
// Purpose: Closes a connection, if it's open
// Created: 2003/10/08
//
// --------------------------------------------------------------------------
void BackupClientContext::CloseAnyOpenConnection()
{
BackupProtocolCallable* pConnection(GetOpenConnection());
if(pConnection)
{
try
{
// Quit nicely
pConnection->QueryFinished();
}
catch(...)
{
// Ignore errors here
}
// Delete it anyway.
mapConnection.reset();
}
// 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
{
BackupProtocolCallable* pConnection(GetOpenConnection());
if(pConnection)
{
return pConnection->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
BackupProtocolCallable &connection(GetConnection());
// Request filenames from the server, in a "safe" manner to ignore errors properly
{
BackupProtocolGetObjectName send(ObjectID, ContainingDirectory);
connection.Send(send);
}
std::auto_ptr<BackupProtocolMessage> preply(connection.Receive());
// Is it of the right type?
if(preply->GetType() != BackupProtocolObjectName::TypeID)
{
// Was an error or something
return false;
}
// Cast to expected type.
BackupProtocolObjectName *names = (BackupProtocolObjectName *)(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(!mrResolver.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() & BackupProtocolListDirectory::Flags_Dir) == BackupProtocolListDirectory::Flags_Dir);
// Is it the current version?
rIsCurrentVersionOut = ((names->GetFlags() & (BackupProtocolListDirectory::Flags_OldVersion | BackupProtocolListDirectory::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.Reset(mKeepAliveTime * MILLI_SEC_IN_SEC);
}
// --------------------------------------------------------------------------
//
// 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()
{
BackupProtocolCallable* pConnection(GetOpenConnection());
if (!pConnection)
{
return;
}
if (mKeepAliveTime == 0)
{
return;
}
if (!mKeepAliveTimer.HasExpired())
{
return;
}
BOX_TRACE("KeepAliveTime reached, sending keep-alive message");
pConnection->QueryGetIsAlive();
mKeepAliveTimer.Reset(mKeepAliveTime * MILLI_SEC_IN_SEC);
}
int BackupClientContext::GetMaximumDiffingTime()
{
return mMaximumDiffingTime;
}
|