archive.ck 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*++
  2. Copyright (c) 2017 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. archive.ck
  9. Abstract:
  10. This module implements the archive command, a low level command used to
  11. create and extract archives.
  12. Author:
  13. Evan Green 20-Jun-2017
  14. Environment:
  15. Chalk
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. from getopt import gnuGetopt;
  21. from os import chdir;
  22. from santa.config import config;
  23. from santa.lib.archive import Archive;
  24. //
  25. // --------------------------------------------------------------------- Macros
  26. //
  27. //
  28. // ---------------------------------------------------------------- Definitions
  29. //
  30. //
  31. // ------------------------------------------------------ Data Type Definitions
  32. //
  33. //
  34. // ----------------------------------------------- Internal Function Prototypes
  35. //
  36. //
  37. // -------------------------------------------------------------------- Globals
  38. //
  39. var description = "Low-level command to create and extract archives";
  40. var shortOptions = "C:cf:txh";
  41. var longOptions = [
  42. "create",
  43. "directory=",
  44. "file=",
  45. "list",
  46. "extract",
  47. "help"
  48. ];
  49. var usage =
  50. "usage: santa archive [-t|-x|-c] -f <archive> [options] [files...]\n"
  51. "This low-level command directly creates, lists, or extracts the contents\n"
  52. "of an archive. Specify one of -c, -x, or -t, for create, extract, or \n"
  53. "list respectively. Options are:\n"
  54. " -C, --directory=dir -- Change to the given directory before \n"
  55. " processing archive members.\n"
  56. " -f, --file=archive -- Specifies the archive name. Required.\n"
  57. " -h, --help -- Print this help text.\n";
  58. //
  59. // ------------------------------------------------------------------ Functions
  60. //
  61. function
  62. command (
  63. args
  64. )
  65. /*++
  66. Routine Description:
  67. This routine implements the archive command.
  68. Arguments:
  69. args - Supplies the arguments to the function.
  70. Return Value:
  71. Returns an exit code.
  72. --*/
  73. {
  74. var archive;
  75. var argc;
  76. var command = [];
  77. var directory;
  78. var mode = "r";
  79. var name;
  80. var options = gnuGetopt(args[1...-1], shortOptions, longOptions);
  81. var path;
  82. var value;
  83. args = options[1];
  84. argc = args.length();
  85. options = options[0];
  86. for (option in options) {
  87. name = option[0];
  88. value = option[1];
  89. if ((name == "-c") || (name == "--create")) {
  90. command.append("create");
  91. } else if ((name == "-C") || (name == "--directory")) {
  92. directory = value;
  93. } else if ((name == "-f") || (name == "--file")) {
  94. path = value;
  95. } else if ((name == "-t") || (name == "--list")) {
  96. command.append("list");
  97. } else if ((name == "-x") || (name == "--extract")) {
  98. command.append("extract");
  99. } else if ((name == "-h") || (name == "--help")) {
  100. Core.print(usage);
  101. return 1;
  102. } else {
  103. Core.raise(ValueError("Invalid option '%s'" % name));
  104. }
  105. }
  106. if (path == null) {
  107. Core.raise(ValueError("Expected an archive name via -f <archive>"));
  108. }
  109. if (command.length() != 1) {
  110. Core.raise(ValueError("Expected exactly one of -c, -t, or -x."));
  111. }
  112. command = command[0];
  113. if (command == "create") {
  114. mode = "w";
  115. }
  116. archive = Archive.open(path, mode);
  117. if (directory) {
  118. chdir(directory);
  119. }
  120. if (command == "create") {
  121. for (arg in args) {
  122. archive.add(arg, arg);
  123. }
  124. } else if (command == "extract") {
  125. if (args.length()) {
  126. for (arg in args) {
  127. archive.extract(arg, arg);
  128. }
  129. } else {
  130. archive.cpio.extractAll(null, null, -1, -1, true);
  131. }
  132. } else if (command == "list") {
  133. archive.cpio.list(null, true);
  134. }
  135. archive.close();
  136. return 0;
  137. }
  138. //
  139. // --------------------------------------------------------- Internal Functions
  140. //