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.

Truncate.java 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.RandomAccessFile;
  22. import java.util.Iterator;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.Project;
  25. import org.apache.tools.ant.Task;
  26. import org.apache.tools.ant.types.Path;
  27. import org.apache.tools.ant.types.ResourceCollection;
  28. import org.apache.tools.ant.types.resources.FileResource;
  29. import org.apache.tools.ant.util.FileUtils;
  30. import org.apache.tools.ant.util.StringUtils;
  31. /**
  32. * Set the length of one or more files, as the intermittently available
  33. * <code>truncate</code> Unix utility/function.
  34. * @since Ant 1.7.1
  35. */
  36. public class Truncate extends Task {
  37. private static final Long ZERO = new Long(0L);
  38. private static final String NO_CHILD = "No files specified.";
  39. private static final String INVALID_LENGTH = "Cannot truncate to length ";
  40. private static final String READ_WRITE = "rw";
  41. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  42. private static final byte[] FILL_BUFFER = new byte[1024];
  43. private Path path;
  44. private boolean create = true;
  45. private boolean mkdirs = false;
  46. private Long length;
  47. private Long adjust;
  48. /**
  49. * Set a single target File.
  50. * @param f the single File
  51. */
  52. public void setFile(File f) {
  53. add(new FileResource(f));
  54. }
  55. /**
  56. * Add a nested (filesystem-only) ResourceCollection.
  57. * @param rc the ResourceCollection to add.
  58. */
  59. public void add(ResourceCollection rc) {
  60. getPath().add(rc);
  61. }
  62. /**
  63. * Set the amount by which files' lengths should be adjusted.
  64. * It is permissible to append K / M / G / T / P.
  65. * @param adjust (positive or negative) adjustment amount.
  66. */
  67. public void setAdjust(Long adjust) {
  68. this.adjust = adjust;
  69. }
  70. /**
  71. * Set the length to which files should be set.
  72. * It is permissible to append K / M / G / T / P.
  73. * @param adjust (positive) adjustment amount.
  74. */
  75. public void setLength(Long length) {
  76. this.length = length;
  77. if (length != null && length.longValue() < 0) {
  78. throw new BuildException(INVALID_LENGTH + length);
  79. }
  80. }
  81. /**
  82. * Set whether to create nonexistent files.
  83. * @param create boolean, default <code>true</code>.
  84. */
  85. public void setCreate(boolean create) {
  86. this.create = create;
  87. }
  88. /**
  89. * Set whether, when creating nonexistent files, nonexistent directories
  90. * should also be created.
  91. * @param mkdirs boolean, default <code>false</code>.
  92. */
  93. public void setMkdirs(boolean mkdirs) {
  94. this.mkdirs = mkdirs;
  95. }
  96. /** {@inheritDoc}. */
  97. public void execute() {
  98. if (length != null && adjust != null) {
  99. throw new BuildException(
  100. "length and adjust are mutually exclusive options");
  101. }
  102. if (length == null && adjust == null) {
  103. length = ZERO;
  104. }
  105. if (path == null) {
  106. throw new BuildException(NO_CHILD);
  107. }
  108. for (Iterator it = path.iterator(); it.hasNext();) {
  109. File f = ((FileResource) it.next()).getFile();
  110. if (shouldProcess(f)) {
  111. process(f);
  112. }
  113. }
  114. }
  115. private boolean shouldProcess(File f) {
  116. if (f.isFile()) {
  117. return true;
  118. }
  119. if (!create) {
  120. return false;
  121. }
  122. Exception exception = null;
  123. try {
  124. if (FILE_UTILS.createNewFile(f, mkdirs)) {
  125. return true;
  126. }
  127. } catch (IOException e) {
  128. exception = e;
  129. }
  130. String msg = "Unable to create " + f;
  131. if (exception == null) {
  132. log(msg, Project.MSG_WARN);
  133. return false;
  134. }
  135. throw new BuildException(msg, exception);
  136. }
  137. private void process(File f) {
  138. long len = f.length();
  139. long newLength = length == null
  140. ? len + adjust.longValue() : length.longValue();
  141. if (len == newLength) {
  142. //nothing to do!
  143. return;
  144. }
  145. RandomAccessFile raf = null;
  146. try {
  147. raf = new RandomAccessFile(f, READ_WRITE);
  148. } catch (Exception e) {
  149. throw new BuildException("Could not open " + f + " for writing", e);
  150. }
  151. try {
  152. if (newLength > len) {
  153. long pos = len;
  154. raf.seek(pos);
  155. while (pos < newLength) {
  156. long writeCount = Math.min(FILL_BUFFER.length,
  157. newLength - pos);
  158. raf.write(FILL_BUFFER, 0, (int) writeCount);
  159. pos += writeCount;
  160. }
  161. } else {
  162. raf.setLength(newLength);
  163. }
  164. } catch (IOException e) {
  165. throw new BuildException("Exception working with " + raf, e);
  166. } finally {
  167. try {
  168. raf.close();
  169. } catch (IOException e) {
  170. log("Caught " + e + " closing " + raf, Project.MSG_WARN);
  171. }
  172. }
  173. }
  174. private synchronized Path getPath() {
  175. if (path == null) {
  176. path = new Path(getProject());
  177. }
  178. return path;
  179. }
  180. }