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

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