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

FileTreeIterator.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (C) 2008, Google Inc.
  3. * Copyright (C) 2007-2010, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * Copyright (C) 2009, Tor Arne Vestbø <torarnv@gmail.com>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.treewalk;
  47. import java.io.File;
  48. import java.io.FileInputStream;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.FileMode;
  54. import org.eclipse.jgit.lib.ObjectReader;
  55. import org.eclipse.jgit.lib.Repository;
  56. import org.eclipse.jgit.util.FS;
  57. /**
  58. * Working directory iterator for standard Java IO.
  59. * <p>
  60. * This iterator uses the standard <code>java.io</code> package to read the
  61. * specified working directory as part of a {@link TreeWalk}.
  62. */
  63. public class FileTreeIterator extends WorkingTreeIterator {
  64. /**
  65. * the starting directory. This directory should correspond to the root of
  66. * the repository.
  67. */
  68. protected final File directory;
  69. /**
  70. * the file system abstraction which will be necessary to perform certain
  71. * file system operations.
  72. */
  73. protected final FS fs;
  74. /**
  75. * Create a new iterator to traverse the work tree and its children.
  76. *
  77. * @param repo
  78. * the repository whose working tree will be scanned.
  79. */
  80. public FileTreeIterator(Repository repo) {
  81. this(repo.getWorkTree(), repo.getFS(),
  82. repo.getConfig().get(WorkingTreeOptions.KEY));
  83. initRootIterator(repo);
  84. }
  85. /**
  86. * Create a new iterator to traverse the given directory and its children.
  87. *
  88. * @param root
  89. * the starting directory. This directory should correspond to
  90. * the root of the repository.
  91. * @param fs
  92. * the file system abstraction which will be necessary to perform
  93. * certain file system operations.
  94. * @param options
  95. * working tree options to be used
  96. */
  97. public FileTreeIterator(final File root, FS fs, WorkingTreeOptions options) {
  98. super(options);
  99. directory = root;
  100. this.fs = fs;
  101. init(entries());
  102. }
  103. /**
  104. * Create a new iterator to traverse a subdirectory.
  105. *
  106. * @param p
  107. * the parent iterator we were created from.
  108. * @param fs
  109. * the file system abstraction which will be necessary to perform
  110. * certain file system operations.
  111. * @param root
  112. * the subdirectory. This should be a directory contained within
  113. * the parent directory.
  114. */
  115. protected FileTreeIterator(final FileTreeIterator p, final File root, FS fs) {
  116. super(p);
  117. directory = root;
  118. this.fs = fs;
  119. init(entries());
  120. }
  121. @Override
  122. public AbstractTreeIterator createSubtreeIterator(final ObjectReader reader)
  123. throws IncorrectObjectTypeException, IOException {
  124. return new FileTreeIterator(this, ((FileEntry) current()).file, fs);
  125. }
  126. private Entry[] entries() {
  127. final File[] all = directory.listFiles();
  128. if (all == null)
  129. return EOF;
  130. final Entry[] r = new Entry[all.length];
  131. for (int i = 0; i < r.length; i++)
  132. r[i] = new FileEntry(all[i], fs);
  133. return r;
  134. }
  135. /**
  136. * Wrapper for a standard Java IO file
  137. */
  138. static public class FileEntry extends Entry {
  139. final File file;
  140. private final FileMode mode;
  141. private long length = -1;
  142. private long lastModified;
  143. FileEntry(final File f, FS fs) {
  144. file = f;
  145. if (f.isDirectory()) {
  146. if (new File(f, Constants.DOT_GIT).isDirectory())
  147. mode = FileMode.GITLINK;
  148. else
  149. mode = FileMode.TREE;
  150. } else if (fs.canExecute(file))
  151. mode = FileMode.EXECUTABLE_FILE;
  152. else
  153. mode = FileMode.REGULAR_FILE;
  154. }
  155. @Override
  156. public FileMode getMode() {
  157. return mode;
  158. }
  159. @Override
  160. public String getName() {
  161. return file.getName();
  162. }
  163. @Override
  164. public long getLength() {
  165. if (length < 0)
  166. length = file.length();
  167. return length;
  168. }
  169. @Override
  170. public long getLastModified() {
  171. if (lastModified == 0)
  172. lastModified = file.lastModified();
  173. return lastModified;
  174. }
  175. @Override
  176. public InputStream openInputStream() throws IOException {
  177. return new FileInputStream(file);
  178. }
  179. /**
  180. * Get the underlying file of this entry.
  181. *
  182. * @return the underlying file of this entry
  183. */
  184. public File getFile() {
  185. return file;
  186. }
  187. }
  188. /**
  189. * @return The root directory of this iterator
  190. */
  191. public File getDirectory() {
  192. return directory;
  193. }
  194. /**
  195. * @return The location of the working file. This is the same as {@code new
  196. * File(getDirectory(), getEntryPath())} but may be faster by
  197. * reusing an internal File instance.
  198. */
  199. public File getEntryFile() {
  200. return ((FileEntry) current()).getFile();
  201. }
  202. }