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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. private final File directory;
  64. private final FS fs;
  65. /**
  66. * Create a new iterator to traverse the given directory and its children.
  67. *
  68. * @param root
  69. * the starting directory. This directory should correspond to
  70. * the root of the repository.
  71. * @param fs
  72. * the file system abstraction which will be necessary to
  73. * perform certain file system operations.
  74. */
  75. public FileTreeIterator(final File root, FS fs) {
  76. directory = root;
  77. this.fs = fs;
  78. init(entries());
  79. }
  80. /**
  81. * Create a new iterator to traverse a subdirectory.
  82. *
  83. * @param p
  84. * the parent iterator we were created from.
  85. * @param fs
  86. * the file system abstraction which will be necessary to
  87. * perform certain file system operations.
  88. * @param root
  89. * the subdirectory. This should be a directory contained within
  90. * the parent directory.
  91. */
  92. protected FileTreeIterator(final FileTreeIterator p, final File root, FS fs) {
  93. super(p);
  94. directory = root;
  95. this.fs = fs;
  96. init(entries());
  97. }
  98. @Override
  99. public AbstractTreeIterator createSubtreeIterator(final Repository repo)
  100. throws IncorrectObjectTypeException, IOException {
  101. return new FileTreeIterator(this, ((FileEntry) current()).file, fs);
  102. }
  103. private Entry[] entries() {
  104. final File[] all = directory.listFiles();
  105. if (all == null)
  106. return EOF;
  107. final Entry[] r = new Entry[all.length];
  108. for (int i = 0; i < r.length; i++)
  109. r[i] = new FileEntry(all[i], fs);
  110. return r;
  111. }
  112. /**
  113. * Wrapper for a standard Java IO file
  114. */
  115. static public class FileEntry extends Entry {
  116. final File file;
  117. private final FileMode mode;
  118. private long length = -1;
  119. private long lastModified;
  120. FileEntry(final File f, FS fs) {
  121. file = f;
  122. if (f.isDirectory()) {
  123. if (new File(f, Constants.DOT_GIT).isDirectory())
  124. mode = FileMode.GITLINK;
  125. else
  126. mode = FileMode.TREE;
  127. } else if (fs.canExecute(file))
  128. mode = FileMode.EXECUTABLE_FILE;
  129. else
  130. mode = FileMode.REGULAR_FILE;
  131. }
  132. @Override
  133. public FileMode getMode() {
  134. return mode;
  135. }
  136. @Override
  137. public String getName() {
  138. return file.getName();
  139. }
  140. @Override
  141. public long getLength() {
  142. if (length < 0)
  143. length = file.length();
  144. return length;
  145. }
  146. @Override
  147. public long getLastModified() {
  148. if (lastModified == 0)
  149. lastModified = file.lastModified();
  150. return lastModified;
  151. }
  152. @Override
  153. public InputStream openInputStream() throws IOException {
  154. return new FileInputStream(file);
  155. }
  156. /**
  157. * Get the underlying file of this entry.
  158. *
  159. * @return the underlying file of this entry
  160. */
  161. public File getFile() {
  162. return file;
  163. }
  164. }
  165. }