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 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
|
// --------------------------------------------------------------------------
//
// File
// Name: BackupCommands.cpp
// Purpose: Implement commands for the Backup store protocol
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
#include "Box.h"
#include <set>
#include <sstream>
#include "autogen_BackupProtocol.h"
#include "autogen_RaidFileException.h"
#include "BackupConstants.h"
#include "BackupStoreContext.h"
#include "BackupStoreConstants.h"
#include "BackupStoreDirectory.h"
#include "BackupStoreException.h"
#include "BackupStoreFile.h"
#include "BackupStoreInfo.h"
#include "BufferedStream.h"
#include "CollectInBufferStream.h"
#include "FileStream.h"
#include "InvisibleTempFileStream.h"
#include "RaidFileController.h"
#include "StreamableMemBlock.h"
#include "MemLeakFindOn.h"
#define PROTOCOL_ERROR(code) \
std::auto_ptr<BackupProtocolMessage>(new BackupProtocolError( \
BackupProtocolError::ErrorType, \
BackupProtocolError::code));
#define CHECK_PHASE(phase) \
if(rContext.GetPhase() != BackupStoreContext::phase) \
{ \
BOX_ERROR("Received command " << ToString() << " " \
"in wrong protocol phase " << rContext.GetPhaseName() << ", " \
"expected in " #phase); \
return PROTOCOL_ERROR(Err_NotInRightProtocolPhase); \
}
#define CHECK_WRITEABLE_SESSION \
if(rContext.SessionIsReadOnly()) \
{ \
BOX_ERROR("Received command " << ToString() << " " \
"in a read-only session"); \
return PROTOCOL_ERROR(Err_SessionReadOnly); \
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolMessage::HandleException(BoxException& e)
// Purpose: Return an error message appropriate to the passed-in
// exception, or rethrow it.
// Created: 2014/09/14
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolReplyable::HandleException(BoxException& e) const
{
if(e.GetType() == RaidFileException::ExceptionType &&
e.GetSubType() == RaidFileException::RaidFileDoesntExist)
{
return PROTOCOL_ERROR(Err_DoesNotExist);
}
else if (e.GetType() == BackupStoreException::ExceptionType)
{
if(e.GetSubType() == BackupStoreException::AddedFileDoesNotVerify)
{
return PROTOCOL_ERROR(Err_FileDoesNotVerify);
}
else if(e.GetSubType() == BackupStoreException::AddedFileExceedsStorageLimit)
{
return PROTOCOL_ERROR(Err_StorageLimitExceeded);
}
else if(e.GetSubType() == BackupStoreException::MultiplyReferencedObject)
{
return PROTOCOL_ERROR(Err_MultiplyReferencedObject);
}
else if(e.GetSubType() == BackupStoreException::CouldNotFindEntryInDirectory)
{
return PROTOCOL_ERROR(Err_DoesNotExistInDirectory);
}
else if(e.GetSubType() == BackupStoreException::NameAlreadyExistsInDirectory)
{
return PROTOCOL_ERROR(Err_TargetNameExists);
}
else if(e.GetSubType() == BackupStoreException::ObjectDoesNotExist)
{
return PROTOCOL_ERROR(Err_DoesNotExist);
}
else if(e.GetSubType() == BackupStoreException::PatchChainInfoBadInDirectory)
{
return PROTOCOL_ERROR(Err_PatchConsistencyError);
}
}
throw;
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolVersion::DoCommand(Protocol &,
// BackupStoreContext &)
// Purpose: Return the current version, or an error if the
// requested version isn't allowed
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolVersion::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Version)
// Correct version?
if(mVersion != BACKUP_STORE_SERVER_VERSION)
{
return PROTOCOL_ERROR(Err_WrongVersion);
}
// Mark the next phase
rContext.SetPhase(BackupStoreContext::Phase_Login);
// Return our version
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolVersion(BACKUP_STORE_SERVER_VERSION));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolLogin::DoCommand(Protocol &, BackupStoreContext &)
// Purpose: Return the current version, or an error if the requested version isn't allowed
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolLogin::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Login)
// Check given client ID against the ID in the certificate certificate
// and that the client actually has an account on this machine
if(mClientID != rContext.GetClientID())
{
BOX_WARNING("Failed login from client ID " <<
BOX_FORMAT_ACCOUNT(mClientID) << ": "
"wrong certificate for this account");
return PROTOCOL_ERROR(Err_BadLogin);
}
if(!rContext.GetClientHasAccount())
{
BOX_WARNING("Failed login from client ID " <<
BOX_FORMAT_ACCOUNT(mClientID) << ": "
"no such account on this server");
return PROTOCOL_ERROR(Err_BadLogin);
}
// If we need to write, check that nothing else has got a write lock
if((mFlags & Flags_ReadOnly) != Flags_ReadOnly)
{
// See if the context will get the lock
if(!rContext.AttemptToGetWriteLock())
{
BOX_WARNING("Failed to get write lock for Client ID " <<
BOX_FORMAT_ACCOUNT(mClientID));
return PROTOCOL_ERROR(Err_CannotLockStoreForWriting);
}
// Debug: check we got the lock
ASSERT(!rContext.SessionIsReadOnly());
}
// Load the store info
rContext.LoadStoreInfo();
if(!rContext.GetBackupStoreInfo().IsAccountEnabled())
{
BOX_WARNING("Refused login from disabled client ID " <<
BOX_FORMAT_ACCOUNT(mClientID));
return PROTOCOL_ERROR(Err_DisabledAccount);
}
// Get the last client store marker
int64_t clientStoreMarker = rContext.GetClientStoreMarker();
// Mark the next phase
rContext.SetPhase(BackupStoreContext::Phase_Commands);
// Log login
BOX_NOTICE("Login from Client ID " <<
BOX_FORMAT_ACCOUNT(mClientID) << " "
"(name=" << rContext.GetAccountName() << "): " <<
(((mFlags & Flags_ReadOnly) != Flags_ReadOnly)
?"Read/Write":"Read-only") << " from " <<
rContext.GetConnectionDetails());
// Get the usage info for reporting to the client
int64_t blocksUsed = 0, blocksSoftLimit = 0, blocksHardLimit = 0;
rContext.GetStoreDiscUsageInfo(blocksUsed, blocksSoftLimit, blocksHardLimit);
// Return success
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolLoginConfirmed(clientStoreMarker, blocksUsed, blocksSoftLimit, blocksHardLimit));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolFinished::DoCommand(Protocol &, BackupStoreContext &)
// Purpose: Marks end of conversation (Protocol framework handles this)
// Created: 2003/08/20
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolFinished::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
// can be called in any phase
BOX_NOTICE("Session finished for Client ID " <<
BOX_FORMAT_ACCOUNT(rContext.GetClientID()) << " "
"(name=" << rContext.GetAccountName() << ")");
// Let the context know about it
rContext.ReceivedFinishCommand();
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolFinished);
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolListDirectory::DoCommand(Protocol &, BackupStoreContext &)
// Purpose: Command to list a directory
// Created: 2003/09/02
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolListDirectory::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
// Store the listing to a stream
std::auto_ptr<CollectInBufferStream> stream(new CollectInBufferStream);
// Ask the context for a directory
const BackupStoreDirectory &rdir(
rContext.GetDirectory(mObjectID));
rdir.WriteToStream(*stream, mFlagsMustBeSet,
mFlagsNotToBeSet, mSendAttributes,
false /* never send dependency info to the client */);
stream->SetForReading();
// Get the protocol to send the stream
rProtocol.SendStreamAfterCommand(static_cast< std::auto_ptr<IOStream> > (stream));
return std::auto_ptr<BackupProtocolMessage>(
new BackupProtocolSuccess(mObjectID));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolStoreFile::DoCommand(Protocol &, BackupStoreContext &)
// Purpose: Command to store a file on the server
// Created: 2003/09/02
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolStoreFile::DoCommand(
BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext,
IOStream& rDataStream) const
{
CHECK_PHASE(Phase_Commands)
CHECK_WRITEABLE_SESSION
std::auto_ptr<BackupProtocolMessage> hookResult =
rContext.StartCommandHook(*this);
if(hookResult.get())
{
return hookResult;
}
// Check that the diff from file actually exists, if it's specified
if(mDiffFromFileID != 0)
{
if(!rContext.ObjectExists(mDiffFromFileID,
BackupStoreContext::ObjectExists_File))
{
return PROTOCOL_ERROR(Err_DiffFromFileDoesNotExist);
}
}
// Ask the context to store it
int64_t id = rContext.AddFile(rDataStream, mDirectoryObjectID,
mModificationTime, mAttributesHash, mDiffFromFileID,
mFilename,
true /* mark files with same name as old versions */);
// Tell the caller what the file ID was
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(id));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolGetObject::DoCommand(Protocol &, BackupStoreContext &)
// Purpose: Command to get an arbitary object from the server
// Created: 2003/09/03
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolGetObject::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
// Check the object exists
if(!rContext.ObjectExists(mObjectID))
{
return PROTOCOL_ERROR(Err_DoesNotExist);
}
// Open the object
std::auto_ptr<IOStream> object(rContext.OpenObject(mObjectID));
// Stream it to the peer
rProtocol.SendStreamAfterCommand(object);
// Tell the caller what the file was
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(mObjectID));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolGetFile::DoCommand(Protocol &, BackupStoreContext &)
// Purpose: Command to get an file object from the server -- may have to do a bit of
// work to get the object.
// Created: 2003/09/03
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolGetFile::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
// Check the objects exist
if(!rContext.ObjectExists(mObjectID)
|| !rContext.ObjectExists(mInDirectory))
{
return PROTOCOL_ERROR(Err_DoesNotExist);
}
// Get the directory it's in
const BackupStoreDirectory &rdir(rContext.GetDirectory(mInDirectory));
// Find the object within the directory
BackupStoreDirectory::Entry *pfileEntry = rdir.FindEntryByID(mObjectID);
if(pfileEntry == 0)
{
return PROTOCOL_ERROR(Err_DoesNotExistInDirectory);
}
// The result
std::auto_ptr<IOStream> stream;
// Does this depend on anything?
if(pfileEntry->GetDependsNewer() != 0)
{
// File exists, but is a patch from a new version. Generate the older version.
std::vector<int64_t> patchChain;
int64_t id = mObjectID;
BackupStoreDirectory::Entry *en = 0;
do
{
patchChain.push_back(id);
en = rdir.FindEntryByID(id);
if(en == 0)
{
BOX_ERROR("Object " <<
BOX_FORMAT_OBJECTID(mObjectID) <<
" in dir " <<
BOX_FORMAT_OBJECTID(mInDirectory) <<
" for account " <<
BOX_FORMAT_ACCOUNT(rContext.GetClientID()) <<
" references object " <<
BOX_FORMAT_OBJECTID(id) <<
" which does not exist in dir");
return PROTOCOL_ERROR(Err_PatchConsistencyError);
}
id = en->GetDependsNewer();
}
while(en != 0 && id != 0);
// OK! The last entry in the chain is the full file, the others are patches back from it.
// Open the last one, which is the current from file
std::auto_ptr<IOStream> from(rContext.OpenObject(patchChain[patchChain.size() - 1]));
// Then, for each patch in the chain, do a combine
for(int p = ((int)patchChain.size()) - 2; p >= 0; --p)
{
// ID of patch
int64_t patchID = patchChain[p];
// Open it a couple of times
std::auto_ptr<IOStream> diff(rContext.OpenObject(patchID));
std::auto_ptr<IOStream> diff2(rContext.OpenObject(patchID));
// Choose a temporary filename for the result of the combination
std::ostringstream fs;
fs << rContext.GetAccountRoot() << ".recombinetemp." << p;
std::string tempFn =
RaidFileController::DiscSetPathToFileSystemPath(
rContext.GetStoreDiscSet(), fs.str(),
p + 16);
// Open the temporary file
std::auto_ptr<IOStream> combined(
new InvisibleTempFileStream(
tempFn, O_RDWR | O_CREAT | O_EXCL |
O_BINARY | O_TRUNC));
// Do the combining
BackupStoreFile::CombineFile(*diff, *diff2, *from, *combined);
// Move to the beginning of the combined file
combined->Seek(0, IOStream::SeekType_Absolute);
// Then shuffle round for the next go
if (from.get()) from->Close();
from = combined;
}
// Now, from contains a nice file to send to the client. Reorder it
{
// Write nastily to allow this to work with gcc 2.x
std::auto_ptr<IOStream> t(BackupStoreFile::ReorderFileToStreamOrder(from.get(), true /* take ownership */));
stream = t;
}
// Release from file to avoid double deletion
from.release();
}
else
{
// Simple case: file already exists on disc ready to go
// Open the object
std::auto_ptr<IOStream> object(rContext.OpenObject(mObjectID));
BufferedStream buf(*object);
// Verify it
if(!BackupStoreFile::VerifyEncodedFileFormat(buf))
{
return PROTOCOL_ERROR(Err_FileDoesNotVerify);
}
// Reset stream -- seek to beginning
object->Seek(0, IOStream::SeekType_Absolute);
// Reorder the stream/file into stream order
{
// Write nastily to allow this to work with gcc 2.x
std::auto_ptr<IOStream> t(BackupStoreFile::ReorderFileToStreamOrder(object.get(), true /* take ownership */));
stream = t;
}
// Object will be deleted when the stream is deleted,
// so can release the object auto_ptr here to avoid
// premature deletion
object.release();
}
// Stream the reordered stream to the peer
rProtocol.SendStreamAfterCommand(stream);
// Tell the caller what the file was
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(mObjectID));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolCreateDirectory::DoCommand(Protocol &, BackupStoreContext &)
// Purpose: Create directory command
// Created: 2003/09/04
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolCreateDirectory::DoCommand(
BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext,
IOStream& rDataStream) const
{
return BackupProtocolCreateDirectory2(mContainingDirectoryID,
mAttributesModTime, 0 /* ModificationTime */,
mDirectoryName).DoCommand(rProtocol, rContext, rDataStream);
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolCreateDirectory2::DoCommand(Protocol &, BackupStoreContext &)
// Purpose: Create directory command, with a specific
// modification time.
// Created: 2014/02/11
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolCreateDirectory2::DoCommand(
BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext,
IOStream& rDataStream) const
{
CHECK_PHASE(Phase_Commands)
CHECK_WRITEABLE_SESSION
// Collect the attributes -- do this now so no matter what the outcome,
// the data has been absorbed.
StreamableMemBlock attr;
attr.Set(rDataStream, rProtocol.GetTimeout());
// Check to see if the hard limit has been exceeded
if(rContext.HardLimitExceeded())
{
// Won't allow creation if the limit has been exceeded
return PROTOCOL_ERROR(Err_StorageLimitExceeded);
}
bool alreadyExists = false;
int64_t id = rContext.AddDirectory(mContainingDirectoryID,
mDirectoryName, attr, mAttributesModTime, mModificationTime,
alreadyExists);
if(alreadyExists)
{
return PROTOCOL_ERROR(Err_DirectoryAlreadyExists);
}
// Tell the caller what the file was
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(id));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolChangeDirAttributes::DoCommand(Protocol &, BackupStoreContext &)
// Purpose: Change attributes on directory
// Created: 2003/09/06
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolChangeDirAttributes::DoCommand(
BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext,
IOStream& rDataStream) const
{
CHECK_PHASE(Phase_Commands)
CHECK_WRITEABLE_SESSION
// Collect the attributes -- do this now so no matter what the outcome,
// the data has been absorbed.
StreamableMemBlock attr;
attr.Set(rDataStream, rProtocol.GetTimeout());
// Get the context to do it's magic
rContext.ChangeDirAttributes(mObjectID, attr, mAttributesModTime);
// Tell the caller what the file was
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(mObjectID));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolSetReplacementFileAttributes::DoCommand(Protocol &, BackupStoreContext &)
// Purpose: Change attributes on directory
// Created: 2003/09/06
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage>
BackupProtocolSetReplacementFileAttributes::DoCommand(
BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext,
IOStream& rDataStream) const
{
CHECK_PHASE(Phase_Commands)
CHECK_WRITEABLE_SESSION
// Collect the attributes -- do this now so no matter what the outcome,
// the data has been absorbed.
StreamableMemBlock attr;
attr.Set(rDataStream, rProtocol.GetTimeout());
// Get the context to do it's magic
int64_t objectID = 0;
if(!rContext.ChangeFileAttributes(mFilename, mInDirectory, attr, mAttributesHash, objectID))
{
// Didn't exist
return PROTOCOL_ERROR(Err_DoesNotExist);
}
// Tell the caller what the file was
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(objectID));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolDeleteFile::DoCommand(BackupProtocolReplyable &, BackupStoreContext &)
// Purpose: Delete a file
// Created: 2003/10/21
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolDeleteFile::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
CHECK_WRITEABLE_SESSION
// Context handles this
int64_t objectID = 0;
rContext.DeleteFile(mFilename, mInDirectory, objectID);
// return the object ID or zero for not found
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(objectID));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolUndeleteFile::DoCommand(
// BackupProtocolBase &, BackupStoreContext &)
// Purpose: Undelete a file
// Created: 2008-09-12
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolUndeleteFile::DoCommand(
BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
CHECK_WRITEABLE_SESSION
// Context handles this
bool result = rContext.UndeleteFile(mObjectID, mInDirectory);
// return the object ID or zero for not found
return std::auto_ptr<BackupProtocolMessage>(
new BackupProtocolSuccess(result ? mObjectID : 0));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolDeleteDirectory::DoCommand(BackupProtocolReplyable &, BackupStoreContext &)
// Purpose: Delete a directory
// Created: 2003/10/21
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolDeleteDirectory::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
CHECK_WRITEABLE_SESSION
// Check it's not asking for the root directory to be deleted
if(mObjectID == BACKUPSTORE_ROOT_DIRECTORY_ID)
{
return PROTOCOL_ERROR(Err_CannotDeleteRoot);
}
// Context handles this
rContext.DeleteDirectory(mObjectID);
// return the object ID
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(mObjectID));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolUndeleteDirectory::DoCommand(BackupProtocolReplyable &, BackupStoreContext &)
// Purpose: Undelete a directory
// Created: 23/11/03
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolUndeleteDirectory::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
CHECK_WRITEABLE_SESSION
// Check it's not asking for the root directory to be deleted
if(mObjectID == BACKUPSTORE_ROOT_DIRECTORY_ID)
{
return PROTOCOL_ERROR(Err_CannotDeleteRoot);
}
// Context handles this
rContext.DeleteDirectory(mObjectID, true /* undelete */);
// return the object ID
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(mObjectID));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolSetClientStoreMarker::DoCommand(BackupProtocolReplyable &, BackupStoreContext &)
// Purpose: Command to set the client's store marker
// Created: 2003/10/29
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolSetClientStoreMarker::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
CHECK_WRITEABLE_SESSION
// Set the marker
rContext.SetClientStoreMarker(mClientStoreMarker);
// return store marker set
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(mClientStoreMarker));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolMoveObject::DoCommand(BackupProtocolReplyable &, BackupStoreContext &)
// Purpose: Command to move an object from one directory to another
// Created: 2003/11/12
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolMoveObject::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
CHECK_WRITEABLE_SESSION
// Let context do this, but modify error reporting on exceptions...
rContext.MoveObject(mObjectID, mMoveFromDirectory, mMoveToDirectory,
mNewFilename, (mFlags & Flags_MoveAllWithSameName) == Flags_MoveAllWithSameName,
(mFlags & Flags_AllowMoveOverDeletedObject) == Flags_AllowMoveOverDeletedObject);
// Return the object ID
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(mObjectID));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolGetObjectName::DoCommand(BackupProtocolReplyable &, BackupStoreContext &)
// Purpose: Command to find the name of an object
// Created: 12/11/03
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolGetObjectName::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
// Create a stream for the list of filenames
std::auto_ptr<CollectInBufferStream> stream(new CollectInBufferStream);
// Object and directory IDs
int64_t objectID = mObjectID;
int64_t dirID = mContainingDirectoryID;
// Data to return in the reply
int32_t numNameElements = 0;
int16_t objectFlags = 0;
int64_t modTime = 0;
uint64_t attrModHash = 0;
bool haveModTimes = false;
do
{
// Check the directory really exists
if(!rContext.ObjectExists(dirID, BackupStoreContext::ObjectExists_Directory))
{
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolObjectName(BackupProtocolObjectName::NumNameElements_ObjectDoesntExist, 0, 0, 0));
}
// Load up the directory
const BackupStoreDirectory *pDir;
try
{
pDir = &rContext.GetDirectory(dirID);
}
catch(BackupStoreException &e)
{
if(e.GetSubType() == BackupStoreException::ObjectDoesNotExist)
{
// If this can't be found, then there is a problem...
// tell the caller it can't be found.
return std::auto_ptr<BackupProtocolMessage>(
new BackupProtocolObjectName(
BackupProtocolObjectName::NumNameElements_ObjectDoesntExist,
0, 0, 0));
}
throw;
}
const BackupStoreDirectory& rdir(*pDir);
// Find the element in this directory and store it's name
if(objectID != ObjectID_DirectoryOnly)
{
const BackupStoreDirectory::Entry *en = rdir.FindEntryByID(objectID);
// If this can't be found, then there is a problem... tell the caller it can't be found
if(en == 0)
{
// Abort!
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolObjectName(BackupProtocolObjectName::NumNameElements_ObjectDoesntExist, 0, 0, 0));
}
// Store flags?
if(objectFlags == 0)
{
objectFlags = en->GetFlags();
}
// Store modification times?
if(!haveModTimes)
{
modTime = en->GetModificationTime();
attrModHash = en->GetAttributesHash();
haveModTimes = true;
}
// Store the name in the stream
en->GetName().WriteToStream(*stream);
// Count of name elements
++numNameElements;
}
// Setup for next time round
objectID = dirID;
dirID = rdir.GetContainerID();
} while(objectID != 0 && objectID != BACKUPSTORE_ROOT_DIRECTORY_ID);
// Stream to send?
if(numNameElements > 0)
{
// Get the stream ready to go
stream->SetForReading();
// Tell the protocol to send the stream
rProtocol.SendStreamAfterCommand(static_cast< std::auto_ptr<IOStream> >(stream));
}
// Make reply
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolObjectName(numNameElements, modTime, attrModHash, objectFlags));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolGetBlockIndexByID::DoCommand(BackupProtocolReplyable &, BackupStoreContext &)
// Purpose: Get the block index from a file, by ID
// Created: 19/1/04
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolGetBlockIndexByID::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
// Open the file
std::auto_ptr<IOStream> stream(rContext.OpenObject(mObjectID));
// Move the file pointer to the block index
BackupStoreFile::MoveStreamPositionToBlockIndex(*stream);
// Return the stream to the client
rProtocol.SendStreamAfterCommand(stream);
// Return the object ID
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(mObjectID));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolGetBlockIndexByName::DoCommand(BackupProtocolReplyable &, BackupStoreContext &)
// Purpose: Get the block index from a file, by name within a directory
// Created: 19/1/04
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolGetBlockIndexByName::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
// Get the directory
const BackupStoreDirectory &dir(rContext.GetDirectory(mInDirectory));
// Find the latest object ID within it which has the same name
int64_t objectID = 0;
BackupStoreDirectory::Iterator i(dir);
BackupStoreDirectory::Entry *en = 0;
while((en = i.Next(BackupStoreDirectory::Entry::Flags_File)) != 0)
{
if(en->GetName() == mFilename)
{
// Store the ID, if it's a newer ID than the last one
if(en->GetObjectID() > objectID)
{
objectID = en->GetObjectID();
}
}
}
// Found anything?
if(objectID == 0)
{
// No... return a zero object ID
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(0));
}
// Open the file
std::auto_ptr<IOStream> stream(rContext.OpenObject(objectID));
// Move the file pointer to the block index
BackupStoreFile::MoveStreamPositionToBlockIndex(*stream);
// Return the stream to the client
rProtocol.SendStreamAfterCommand(stream);
// Return the object ID
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolSuccess(objectID));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolGetAccountUsage::DoCommand(BackupProtocolReplyable &, BackupStoreContext &)
// Purpose: Return the amount of disc space used
// Created: 19/4/04
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolGetAccountUsage::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
// Get store info from context
const BackupStoreInfo &rinfo(rContext.GetBackupStoreInfo());
// Find block size
RaidFileController &rcontroller(RaidFileController::GetController());
RaidFileDiscSet &rdiscSet(rcontroller.GetDiscSet(rinfo.GetDiscSetNumber()));
// Return info
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolAccountUsage(
rinfo.GetBlocksUsed(),
rinfo.GetBlocksInOldFiles(),
rinfo.GetBlocksInDeletedFiles(),
rinfo.GetBlocksInDirectories(),
rinfo.GetBlocksSoftLimit(),
rinfo.GetBlocksHardLimit(),
rdiscSet.GetBlockSize()
));
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolGetIsAlive::DoCommand(BackupProtocolReplyable &, BackupStoreContext &)
// Purpose: Return the amount of disc space used
// Created: 19/4/04
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolGetIsAlive::DoCommand(BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
//
// NOOP
//
return std::auto_ptr<BackupProtocolMessage>(new BackupProtocolIsAlive());
}
// --------------------------------------------------------------------------
//
// Function
// Name: BackupProtocolGetAccountUsage2::DoCommand(BackupProtocolReplyable &, BackupStoreContext &)
// Purpose: Return the amount of disc space used
// Created: 26/12/13
//
// --------------------------------------------------------------------------
std::auto_ptr<BackupProtocolMessage> BackupProtocolGetAccountUsage2::DoCommand(
BackupProtocolReplyable &rProtocol, BackupStoreContext &rContext) const
{
CHECK_PHASE(Phase_Commands)
// Get store info from context
const BackupStoreInfo &info(rContext.GetBackupStoreInfo());
// Find block size
RaidFileController &rcontroller(RaidFileController::GetController());
RaidFileDiscSet &rdiscSet(rcontroller.GetDiscSet(info.GetDiscSetNumber()));
// Return info
BackupProtocolAccountUsage2* usage = new BackupProtocolAccountUsage2();
std::auto_ptr<BackupProtocolMessage> reply(usage);
#define COPY(name) usage->Set ## name (info.Get ## name ())
COPY(AccountName);
usage->SetAccountEnabled(info.IsAccountEnabled());
COPY(ClientStoreMarker);
usage->SetBlockSize(rdiscSet.GetBlockSize());
COPY(LastObjectIDUsed);
COPY(BlocksUsed);
COPY(BlocksInCurrentFiles);
COPY(BlocksInOldFiles);
COPY(BlocksInDeletedFiles);
COPY(BlocksInDirectories);
COPY(BlocksSoftLimit);
COPY(BlocksHardLimit);
COPY(NumCurrentFiles);
COPY(NumOldFiles);
COPY(NumDeletedFiles);
COPY(NumDirectories);
#undef COPY
return reply;
}
|