14.restore.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * PROGRAM: Object oriented API samples.
  3. * MODULE: 14.restore.cpp
  4. * DESCRIPTION: A sample of making restore of database using service manager.
  5. *
  6. * Example for the following interfaces:
  7. * IService - interface to work with service manager.
  8. *
  9. * The contents of this file are subject to the Initial
  10. * Developer's Public License Version 1.0 (the "License");
  11. * you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. * http://www.ibphoenix.com/main.nfs?a=ibphoenix&page=ibp_idpl.
  14. *
  15. * Software distributed under the License is distributed AS IS,
  16. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing rights
  18. * and limitations under the License.
  19. *
  20. * The Original Code was created by Alexander Peshkoff
  21. * for the Firebird Open Source RDBMS project.
  22. *
  23. * Copyright (c) 2016 Alexander Peshkoff <[email protected]>
  24. * and all contributors signed below.
  25. *
  26. * All Rights Reserved.
  27. * Contributor(s): ______________________________________.
  28. *
  29. */
  30. #include "ifaceExamples.h"
  31. static IMaster* master = fb_get_master_interface();
  32. // print information, returned by isc_svc_query() call
  33. bool printLine(const unsigned char*& p)
  34. {
  35. const ISC_USHORT length = (ISC_USHORT) isc_vax_integer((char*) p, sizeof(ISC_USHORT));
  36. p += sizeof(ISC_USHORT);
  37. if (length > 0)
  38. printf("%*.*s\n", length, length, p);
  39. p += length;
  40. return length > 0;
  41. }
  42. bool printInfo(const unsigned char* p, size_t pSize)
  43. {
  44. bool ret = false;
  45. bool ignoreTruncation = false;
  46. const unsigned char* const end = p + pSize;
  47. while (p < end && *p != isc_info_end)
  48. {
  49. switch (*p++)
  50. {
  51. case isc_info_svc_line:
  52. ret = printLine(p);
  53. break;
  54. case isc_info_truncated:
  55. if (!ignoreTruncation)
  56. printf("\n<<< truncated >>>\n");
  57. fflush(stdout);
  58. ret = true;
  59. break;
  60. case isc_info_svc_timeout:
  61. case isc_info_data_not_ready:
  62. ret = true;
  63. break;
  64. default:
  65. printf("Unknown item 0x%x in received buffer\n", p[-1]);
  66. }
  67. }
  68. fflush(stdout);
  69. return ret;
  70. }
  71. int main()
  72. {
  73. int rc = 0;
  74. // Status wrapper
  75. ThrowStatusWrapper status(master->getStatus());
  76. // Declare pointers to required interfaces
  77. IProvider* prov = master->getDispatcher();
  78. IUtil* utl = master->getUtilInterface();
  79. IService* svc = NULL;
  80. IXpbBuilder* spb1 = NULL;
  81. IXpbBuilder* spb2 = NULL;
  82. try
  83. {
  84. printf("** Attaching to service manager...\n");
  85. // Prepare SPB to attach to service manager
  86. spb1 = utl->getXpbBuilder(&status, IXpbBuilder::SPB_ATTACH, NULL, 0);
  87. spb1->insertString(&status, isc_spb_user_name, "sysdba");
  88. spb1->insertString(&status, isc_spb_password, "masterkey");
  89. // Attach to service manager
  90. svc = prov->attachServiceManager(&status, "service_mgr", spb1->getBufferLength(&status),
  91. spb1->getBuffer(&status));
  92. // In the simplest case sendItems parameter may be NULL
  93. // Building receiveItems is mostly trivial
  94. //const unsigned char receiveItems[] = {isc_info_svc_server_version};
  95. // Output buffer
  96. unsigned char results[1024];
  97. printf("** Demo of making restore using service manager...\n");
  98. // Build service start SPB
  99. spb2 = utl->getXpbBuilder(&status, IXpbBuilder::SPB_START, NULL, 0);
  100. spb2->insertTag(&status, isc_action_svc_restore);
  101. spb2->insertString(&status, isc_spb_dbname, "employee-restored.fdb");
  102. spb2->insertString(&status, isc_spb_bkp_file, "employee.fbk");
  103. spb2->insertInt(&status, isc_spb_options, isc_spb_res_create);
  104. // Start service
  105. svc->start(&status, spb2->getBufferLength(&status), spb2->getBuffer(&status));
  106. // Prepare receiveItems block
  107. const unsigned char receiveItems2[] = {isc_info_svc_line};
  108. // Query service output
  109. do
  110. {
  111. svc->query(&status, 0, NULL, sizeof(receiveItems2), receiveItems2, sizeof(results), results);
  112. } while (printInfo(results, sizeof(results)));
  113. printf("** Detaching from service manager...\n");
  114. // Detach from service manager
  115. svc->detach(&status);
  116. svc = NULL;
  117. printf("** Done.\n");
  118. }
  119. catch (const FbException& error)
  120. {
  121. // handle error
  122. rc = 1;
  123. char buf[256];
  124. master->getUtilInterface()->formatStatus(buf, sizeof(buf), error.getStatus());
  125. fprintf(stderr, "%s\n", buf);
  126. }
  127. // release interfaces after error caught
  128. if (svc)
  129. svc->release();
  130. // generic cleanup
  131. prov->release();
  132. status.dispose();
  133. if (spb1)
  134. spb1->dispose();
  135. if (spb2)
  136. spb2->dispose();
  137. return rc;
  138. }