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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.Repository;
  55. import org.eclipse.jgit.util.FS;
  56. /**
  57. * Working directory iterator for standard Java IO.
  58. * <p>
  59. * This iterator uses the standard <code>java.io</code> package to read the
  60. * specified working directory as part of a {@link TreeWalk}.
  61. */
  62. public class FileTreeIterator extends WorkingTreeIterator {
  63. /**
  64. * the starting directory. This directory should correspond to
  65. * the root of the repository.
  66. */
  67. protected final File directory;
  68. /**
  69. * the file system abstraction which will be necessary to
  70. * perform certain file system operations.
  71. */
  72. protected final FS fs;
  73. /**
  74. * Create a new iterator to traverse the work tree and its children.
  75. *
  76. * @param repo
  77. * the repository whose working tree will be scanned.
  78. */
  79. public FileTreeIterator(Repository repo) {
  80. this(repo.getWorkDir(), repo.getFS());
  81. initRootIterator(repo);
  82. }
  83. /**
  84. * Create a new iterator to traverse the given directory and its children.
  85. *
  86. * @param root
  87. * the starting directory. This directory should correspond to
  88. * the root of the repository.
  89. * @param fs
  90. * the file system abstraction which will be necessary to
  91. * perform certain file system operations.
  92. */
  93. public FileTreeIterator(final File root, FS fs) {
  94. directory = root;
  95. this.fs = fs;
  96. init(entries());
  97. }
  98. /**
  99. * Create a new iterator to traverse a subdirectory.
  100. *
  101. * @param p
  102. * the parent iterator we were created from.
  103. * @param fs
  104. * the file system abstraction which will be necessary to
  105. * perform certain file system operations.
  106. * @param root
  107. * the subdirectory. This should be a directory contained within
  108. * the parent directory.
  109. */
  110. protected FileTreeIterator(final FileTreeIterator p, final File root, FS fs) {
  111. super(p);
  112. directory = root;
  113. this.fs = fs;
  114. init(entries());
  115. }
  116. @Override
  117. public AbstractTreeIterator createSubtreeIterator(final Repository repo)
  118. throws IncorrectObjectTypeException, IOException {
  119. return new FileTreeIterator(this, ((FileEntry) current()).file, fs);
  120. }
  121. private Entry[] entries() {
  122. final File[] all = directory.listFiles();
  123. if (all == null)
  124. return EOF;
  125. final Entry[] r = new Entry[all.length];
  126. for (int i = 0; i < r.length; i++)
  127. r[i] = new FileEntry(all[i], fs);
  128. return r;
  129. }
  130. /**
  131. * Wrapper for a standard Java IO file
  132. */
  133. static public class FileEntry extends Entry {
  134. final File file;
  135. private final FileMode mode;
  136. private long length = -1;
  137. private long lastModified;
  138. FileEntry(final File f, FS fs) {
  139. file = f;
  140. if (f.isDirectory()) {
  141. if (new File(f, Constants.DOT_GIT).isDirectory())
  142. mode = FileMode.GITLINK;
  143. else
  144. mode = FileMode.TREE;
  145. } else if (fs.canExecute(file))
  146. mode = FileMode.EXECUTABLE_FILE;
  147. else
  148. mode = FileMode.REGULAR_FILE;
  149. }
  150. @Override
  151. public FileMode getMode() {
  152. return mode;
  153. }
  154. @Override
  155. public String getName() {
  156. return file.getName();
  157. }
  158. @Override
  159. public long getLength() {
  160. if (length < 0)
  161. length = file.length();
  162. return length;
  163. }
  164. @Override
  165. public long getLastModified() {
  166. if (lastModified == 0)
  167. lastModified = file.lastModified();
  168. return lastModified;
  169. }
  170. @Override
  171. public InputStream openInputStream() throws IOException {
  172. return new FileInputStream(file);
  173. }
  174. /**
  175. * Get the underlying file of this entry.
  176. *
  177. * @return the underlying file of this entry
  178. */
  179. public File getFile() {
  180. return file;
  181. }
  182. }
  183. /**
  184. * @return
  185. * The root directory of this iterator
  186. */
  187. public File getDirectory() {
  188. return directory;
  189. }
  190. }