You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CVSPass.java 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001-2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.io.BufferedReader;
  56. import java.io.File;
  57. import java.io.FileReader;
  58. import java.io.FileWriter;
  59. import java.io.IOException;
  60. import java.io.PrintWriter;
  61. import org.apache.tools.ant.BuildException;
  62. import org.apache.tools.ant.Project;
  63. import org.apache.tools.ant.Task;
  64. import org.apache.tools.ant.util.StringUtils;
  65. /**
  66. * Adds an new entry to a CVS password file.
  67. *
  68. * @author <a href="jeff@custommonkey.org">Jeff Martin</a>
  69. * @version $Revision$
  70. *
  71. * @since Ant 1.4
  72. *
  73. * @ant.task category="scm"
  74. */
  75. public class CVSPass extends Task {
  76. /** CVS Root */
  77. private String cvsRoot = null;
  78. /** Password file to add password to */
  79. private File passFile = null;
  80. /** Password to add to file */
  81. private String password = null;
  82. /** Array contain char conversion data */
  83. private final char[] shifts = {
  84. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  85. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  86. 114, 120, 53, 79, 96, 109, 72, 108, 70, 64, 76, 67, 116, 74, 68, 87,
  87. 111, 52, 75, 119, 49, 34, 82, 81, 95, 65, 112, 86, 118, 110, 122, 105,
  88. 41, 57, 83, 43, 46, 102, 40, 89, 38, 103, 45, 50, 42, 123, 91, 35,
  89. 125, 55, 54, 66, 124, 126, 59, 47, 92, 71, 115, 78, 88, 107, 106, 56,
  90. 36, 121, 117, 104, 101, 100, 69, 73, 99, 63, 94, 93, 39, 37, 61, 48,
  91. 58, 113, 32, 90, 44, 98, 60, 51, 33, 97, 62, 77, 84, 80, 85, 223,
  92. 225, 216, 187, 166, 229, 189, 222, 188, 141, 249, 148, 200, 184, 136, 248, 190,
  93. 199, 170, 181, 204, 138, 232, 218, 183, 255, 234, 220, 247, 213, 203, 226, 193,
  94. 174, 172, 228, 252, 217, 201, 131, 230, 197, 211, 145, 238, 161, 179, 160, 212,
  95. 207, 221, 254, 173, 202, 146, 224, 151, 140, 196, 205, 130, 135, 133, 143, 246,
  96. 192, 159, 244, 239, 185, 168, 215, 144, 139, 165, 180, 157, 147, 186, 214, 176,
  97. 227, 231, 219, 169, 175, 156, 206, 198, 129, 164, 150, 210, 154, 177, 134, 127,
  98. 182, 128, 158, 208, 162, 132, 167, 209, 149, 241, 153, 251, 237, 236, 171, 195,
  99. 243, 233, 253, 240, 194, 250, 191, 155, 142, 137, 245, 235, 163, 242, 178, 152
  100. };
  101. /**
  102. * Create a CVS task using the default cvspass file location.
  103. */
  104. public CVSPass(){
  105. passFile = new File(
  106. System.getProperty("cygwin.user.home",
  107. System.getProperty("user.home"))
  108. + File.separatorChar + ".cvspass");
  109. }
  110. /**
  111. * Does the work.
  112. *
  113. * @exception BuildException if someting goes wrong with the build
  114. */
  115. public final void execute() throws BuildException {
  116. if (cvsRoot == null) {
  117. throw new BuildException("cvsroot is required");
  118. }
  119. if (password == null) {
  120. throw new BuildException("password is required");
  121. }
  122. log("cvsRoot: " + cvsRoot, Project.MSG_DEBUG);
  123. log("password: " + password, Project.MSG_DEBUG);
  124. log("passFile: " + passFile, Project.MSG_DEBUG);
  125. BufferedReader reader = null;
  126. PrintWriter writer = null;
  127. try {
  128. StringBuffer buf = new StringBuffer();
  129. if (passFile.exists()) {
  130. reader = new BufferedReader(new FileReader(passFile));
  131. String line = null;
  132. while ((line = reader.readLine()) != null) {
  133. if (!line.startsWith(cvsRoot)) {
  134. buf.append(line).append(StringUtils.LINE_SEP);
  135. }
  136. }
  137. }
  138. String pwdfile = buf.toString() + cvsRoot + " A"
  139. + mangle(password);
  140. log("Writing -> " + pwdfile , Project.MSG_DEBUG);
  141. writer = new PrintWriter(new FileWriter(passFile));
  142. writer.println(pwdfile);
  143. } catch (IOException e) {
  144. throw new BuildException(e);
  145. } finally {
  146. if (reader != null) {
  147. try {
  148. reader.close();
  149. } catch (IOException e) {}
  150. }
  151. if (writer != null) {
  152. writer.close();
  153. }
  154. }
  155. }
  156. private final String mangle(String password){
  157. StringBuffer buf = new StringBuffer();
  158. for (int i = 0; i < password.length(); i++) {
  159. buf.append(shifts[password.charAt(i)]);
  160. }
  161. return buf.toString();
  162. }
  163. /**
  164. * The CVS repository to add an entry for.
  165. */
  166. public void setCvsroot(String cvsRoot) {
  167. this.cvsRoot = cvsRoot;
  168. }
  169. /**
  170. * Password file to add the entry to.
  171. */
  172. public void setPassfile(File passFile) {
  173. this.passFile = passFile;
  174. }
  175. /**
  176. * Password to be added to the password file.
  177. */
  178. public void setPassword(String password) {
  179. this.password = password;
  180. }
  181. }