capmaker.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include "jlib.hpp"
  15. #include "jfile.hpp"
  16. #include "mpcomm.hpp"
  17. #include "daclient.hpp"
  18. #include "dasds.hpp"
  19. void usage(const char *exe)
  20. {
  21. printf("USAGE: %s <import filename> [export_filename] OR\n", exe);
  22. printf("USAGE: %s -local <usage> <system> <export_filename>\n", exe);
  23. }
  24. static unsigned csvread(IIOStream &stream, char *dst, unsigned max, bool stopEOL=false)
  25. {
  26. char *p = dst;
  27. char c;
  28. for (;;)
  29. {
  30. if (!stream.read(1, &c))
  31. return 0;
  32. else
  33. {
  34. if (stopEOL && ('\n' == c || '\r' == c)) return 0;
  35. if (!isspace(c)) break;
  36. }
  37. }
  38. do
  39. {
  40. if (',' == c || '\n' == c || '\r' == c) { *p = '\0'; break; }
  41. else if (!isspace(c) && (p-dst)<max-1)
  42. *p++ = c;
  43. }
  44. while (stream.read(1, &c));
  45. return p-dst;
  46. }
  47. int main(int argc, char* argv[])
  48. {
  49. InitModuleObjects();
  50. EnableSEHtoExceptionMapping();
  51. if (argc<2)
  52. {
  53. usage(argv[0]);
  54. return 0;
  55. }
  56. CCapabilityExporter exporter(CLIENT_ENCRYPT_KEY);
  57. try
  58. {
  59. StringBuffer exportFname;
  60. if ('-' == *argv[1])
  61. {
  62. if (0!=stricmp(argv[1]+1, "local") || argc<5)
  63. {
  64. usage(argv[0]);
  65. return 0;
  66. }
  67. DaliClientRole roleType = CSystemCapability::decodeRole(argv[2]);
  68. if (DCR_Unknown == roleType)
  69. throw MakeStringException(0, "Unknown role: %s", argv[2]);
  70. const char *system = argv[3];
  71. CSystemCapability sc(roleType, system);
  72. StringBuffer s;
  73. dumpMAC(sc, s);
  74. PROGLOG("MAC detected: %s", s.str());
  75. dumpCPUSN(sc, s.clear());
  76. PROGLOG("CPU SN detected: %s", s.str());
  77. exportFname.append(argv[4]);
  78. OwnedIFile ifile = createIFile(exportFname.str());
  79. OwnedIFileIO ifileio = ifile->open(IFOcreate);
  80. OwnedIFileIOStream stream = createBufferedIOStream(ifileio);
  81. exporter.export(*stream);
  82. }
  83. else
  84. {
  85. const char *filename = argv[1];
  86. if (argc>2) exportFname.append(argv[2]);
  87. else
  88. {
  89. char *dot = strchr(filename, '.');
  90. if (!dot || (0 == stricmp("scd", dot+1)))
  91. exportFname.append(filename);
  92. else
  93. exportFname.append(dot-filename, filename);
  94. exportFname.append(".scd");
  95. }
  96. OwnedIFile ifile = createIFile(filename);
  97. OwnedIFileIO ifileio = ifile->open(IFOread);
  98. OwnedIFileIOStream stream = createBufferedIOStream(ifileio);
  99. char system[9], role[51], addr[18], cpusn[26];
  100. PROGLOG("Reading capabilities");
  101. while (csvread(*stream, system, sizeof(system)))
  102. {
  103. DaliClientRole roleType;
  104. if (!csvread(*stream, role, sizeof(role))) throw MakeStringException(0, "Invalid format (role)");
  105. else
  106. {
  107. roleType = CSystemCapability::decodeRole(role);
  108. if (DCR_Unknown == roleType)
  109. throw MakeStringException(0, "Unknown role: %s", role);
  110. }
  111. unsigned addrLength = csvread(*stream, addr, sizeof(addr));
  112. if (!addrLength)
  113. throw MakeStringException(0, "Invalid format address)");
  114. unsigned cpuSNLength = csvread(*stream, cpusn, sizeof(cpusn), true);
  115. exporter.add(roleType, system, addr, cpuSNLength?cpusn:NULL);
  116. }
  117. ifile.setown(createIFile(exportFname.str()));
  118. ifileio.setown(ifile->open(IFOcreate));
  119. stream.setown(createBufferedIOStream(ifileio));
  120. exporter.export(*stream);
  121. }
  122. PROGLOG("Capabilities exported to %s", exportFname.str());
  123. }
  124. catch (IException *e)
  125. {
  126. EXCLOG(e);
  127. e->Release();
  128. }
  129. closedownClientProcess();
  130. releaseAtoms();
  131. return 0;
  132. }