Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

FileTreeIterator.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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.ByteArrayInputStream;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.FileMode;
  55. import org.eclipse.jgit.lib.ObjectReader;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.util.FS;
  58. /**
  59. * Working directory iterator for standard Java IO.
  60. * <p>
  61. * This iterator uses the standard <code>java.io</code> package to read the
  62. * specified working directory as part of a {@link TreeWalk}.
  63. */
  64. public class FileTreeIterator extends WorkingTreeIterator {
  65. /**
  66. * the starting directory of this Iterator. All entries are located directly
  67. * in this directory.
  68. */
  69. protected final File directory;
  70. /**
  71. * the file system abstraction which will be necessary to perform certain
  72. * file system operations.
  73. */
  74. protected final FS fs;
  75. /**
  76. * the strategy used to compute the FileMode for a FileEntry. Can be used to
  77. * control things such as whether to recurse into a directory or create a
  78. * gitlink.
  79. *
  80. * @since 4.3
  81. */
  82. protected final FileModeStrategy fileModeStrategy;
  83. /**
  84. * Create a new iterator to traverse the work tree and its children.
  85. *
  86. * @param repo
  87. * the repository whose working tree will be scanned.
  88. */
  89. public FileTreeIterator(Repository repo) {
  90. this(repo, DefaultFileModeStrategy.INSTANCE);
  91. }
  92. /**
  93. * Create a new iterator to traverse the work tree and its children.
  94. *
  95. * @param repo
  96. * the repository whose working tree will be scanned.
  97. * @param fileModeStrategy
  98. * the strategy to use to determine the FileMode for a FileEntry;
  99. * controls gitlinks etc.
  100. *
  101. * @since 4.3
  102. */
  103. public FileTreeIterator(Repository repo, FileModeStrategy fileModeStrategy) {
  104. this(repo.getWorkTree(), repo.getFS(),
  105. repo.getConfig().get(WorkingTreeOptions.KEY),
  106. fileModeStrategy);
  107. initRootIterator(repo);
  108. }
  109. /**
  110. * Create a new iterator to traverse the given directory and its children.
  111. *
  112. * @param root
  113. * the starting directory. This directory should correspond to
  114. * the root of the repository.
  115. * @param fs
  116. * the file system abstraction which will be necessary to perform
  117. * certain file system operations.
  118. * @param options
  119. * working tree options to be used
  120. */
  121. public FileTreeIterator(final File root, FS fs, WorkingTreeOptions options) {
  122. this(root, fs, options, DefaultFileModeStrategy.INSTANCE);
  123. }
  124. /**
  125. * Create a new iterator to traverse the given directory and its children.
  126. *
  127. * @param root
  128. * the starting directory. This directory should correspond to
  129. * the root of the repository.
  130. * @param fs
  131. * the file system abstraction which will be necessary to perform
  132. * certain file system operations.
  133. * @param options
  134. * working tree options to be used
  135. * @param fileModeStrategy
  136. * the strategy to use to determine the FileMode for a FileEntry;
  137. * controls gitlinks etc.
  138. *
  139. * @since 4.3
  140. */
  141. public FileTreeIterator(final File root, FS fs, WorkingTreeOptions options,
  142. FileModeStrategy fileModeStrategy) {
  143. super(options);
  144. directory = root;
  145. this.fs = fs;
  146. this.fileModeStrategy = fileModeStrategy;
  147. init(entries());
  148. }
  149. /**
  150. * Create a new iterator to traverse a subdirectory.
  151. *
  152. * @param p
  153. * the parent iterator we were created from.
  154. * @param root
  155. * the subdirectory. This should be a directory contained within
  156. * the parent directory.
  157. * @param fs
  158. * the file system abstraction which will be necessary to perform
  159. * certain file system operations.
  160. * @since 4.3
  161. * @deprecated use {@link #FileTreeIterator(FileTreeIterator, File, FS)}
  162. * instead.
  163. */
  164. @Deprecated
  165. protected FileTreeIterator(final WorkingTreeIterator p, final File root,
  166. FS fs) {
  167. this(p, root, fs, DefaultFileModeStrategy.INSTANCE);
  168. }
  169. /**
  170. * Create a new iterator to traverse a subdirectory.
  171. *
  172. * @param p
  173. * the parent iterator we were created from.
  174. * @param root
  175. * the subdirectory. This should be a directory contained within
  176. * the parent directory.
  177. * @param fs
  178. * the file system abstraction which will be necessary to perform
  179. * certain file system operations.
  180. *
  181. * @since 4.3
  182. */
  183. protected FileTreeIterator(final FileTreeIterator p, final File root,
  184. FS fs) {
  185. this(p, root, fs, p.fileModeStrategy);
  186. }
  187. /**
  188. * Create a new iterator to traverse a subdirectory, given the specified
  189. * FileModeStrategy.
  190. *
  191. * @param p
  192. * the parent iterator we were created from.
  193. * @param root
  194. * the subdirectory. This should be a directory contained within
  195. * the parent directory
  196. * @param fs
  197. * the file system abstraction which will be necessary to perform
  198. * certain file system operations.
  199. * @param fileModeStrategy
  200. * the strategy to use to determine the FileMode for a given
  201. * FileEntry.
  202. *
  203. * @since 4.3
  204. */
  205. protected FileTreeIterator(final WorkingTreeIterator p, final File root,
  206. FS fs, FileModeStrategy fileModeStrategy) {
  207. super(p);
  208. directory = root;
  209. this.fs = fs;
  210. this.fileModeStrategy = fileModeStrategy;
  211. init(entries());
  212. }
  213. @Override
  214. public AbstractTreeIterator createSubtreeIterator(final ObjectReader reader)
  215. throws IncorrectObjectTypeException, IOException {
  216. return new FileTreeIterator(this, ((FileEntry) current()).getFile(), fs, fileModeStrategy);
  217. }
  218. private Entry[] entries() {
  219. final File[] all = directory.listFiles();
  220. if (all == null)
  221. return EOF;
  222. final Entry[] r = new Entry[all.length];
  223. for (int i = 0; i < r.length; i++)
  224. r[i] = new FileEntry(all[i], fs, fileModeStrategy);
  225. return r;
  226. }
  227. /**
  228. * An interface representing the methods used to determine the FileMode for
  229. * a FileEntry.
  230. *
  231. * @since 4.3
  232. */
  233. public interface FileModeStrategy {
  234. /**
  235. * Compute the FileMode for a given File, based on its attributes.
  236. *
  237. * @param f
  238. * the file to return a FileMode for
  239. * @param attributes
  240. * the attributes of a file
  241. * @return a FileMode indicating whether the file is a regular file, a
  242. * directory, a gitlink, etc.
  243. */
  244. FileMode getMode(File f, FS.Attributes attributes);
  245. }
  246. /**
  247. * A default implementation of a FileModeStrategy; defaults to treating
  248. * nested .git directories as gitlinks, etc.
  249. *
  250. * @since 4.3
  251. */
  252. static public class DefaultFileModeStrategy implements FileModeStrategy {
  253. /**
  254. * a singleton instance of the default FileModeStrategy
  255. */
  256. public final static DefaultFileModeStrategy INSTANCE =
  257. new DefaultFileModeStrategy();
  258. @Override
  259. public FileMode getMode(File f, FS.Attributes attributes) {
  260. if (attributes.isSymbolicLink()) {
  261. return FileMode.SYMLINK;
  262. } else if (attributes.isDirectory()) {
  263. if (new File(f, Constants.DOT_GIT).exists()) {
  264. return FileMode.GITLINK;
  265. } else {
  266. return FileMode.TREE;
  267. }
  268. } else if (attributes.isExecutable()) {
  269. return FileMode.EXECUTABLE_FILE;
  270. } else {
  271. return FileMode.REGULAR_FILE;
  272. }
  273. }
  274. }
  275. /**
  276. * Wrapper for a standard Java IO file
  277. */
  278. static public class FileEntry extends Entry {
  279. private final FileMode mode;
  280. private FS.Attributes attributes;
  281. private FS fs;
  282. /**
  283. * Create a new file entry.
  284. *
  285. * @param f
  286. * file
  287. * @param fs
  288. * file system
  289. */
  290. public FileEntry(File f, FS fs) {
  291. this(f, fs, DefaultFileModeStrategy.INSTANCE);
  292. }
  293. /**
  294. * Create a new file entry given the specified FileModeStrategy
  295. *
  296. * @param f
  297. * file
  298. * @param fs
  299. * file system
  300. * @param fileModeStrategy
  301. * the strategy to use when determining the FileMode of a
  302. * file; controls gitlinks etc.
  303. *
  304. * @since 4.3
  305. */
  306. public FileEntry(File f, FS fs, FileModeStrategy fileModeStrategy) {
  307. this.fs = fs;
  308. f = fs.normalize(f);
  309. attributes = fs.getAttributes(f);
  310. mode = fileModeStrategy.getMode(f, attributes);
  311. }
  312. @Override
  313. public FileMode getMode() {
  314. return mode;
  315. }
  316. @Override
  317. public String getName() {
  318. return attributes.getName();
  319. }
  320. @Override
  321. public long getLength() {
  322. return attributes.getLength();
  323. }
  324. @Override
  325. public long getLastModified() {
  326. return attributes.getLastModifiedTime();
  327. }
  328. @Override
  329. public InputStream openInputStream() throws IOException {
  330. if (fs.isSymLink(getFile()))
  331. return new ByteArrayInputStream(fs.readSymLink(getFile())
  332. .getBytes(
  333. Constants.CHARACTER_ENCODING));
  334. else
  335. return new FileInputStream(getFile());
  336. }
  337. /**
  338. * Get the underlying file of this entry.
  339. *
  340. * @return the underlying file of this entry
  341. */
  342. public File getFile() {
  343. return attributes.getFile();
  344. }
  345. }
  346. /**
  347. * @return The root directory of this iterator
  348. */
  349. public File getDirectory() {
  350. return directory;
  351. }
  352. /**
  353. * @return The location of the working file. This is the same as {@code new
  354. * File(getDirectory(), getEntryPath())} but may be faster by
  355. * reusing an internal File instance.
  356. */
  357. public File getEntryFile() {
  358. return ((FileEntry) current()).getFile();
  359. }
  360. @Override
  361. protected byte[] idSubmodule(final Entry e) {
  362. return idSubmodule(getDirectory(), e);
  363. }
  364. }