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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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,
  91. repo.getConfig().get(WorkingTreeOptions.KEY).isDirNoGitLinks() ?
  92. NoGitlinksStrategy.INSTANCE :
  93. DefaultFileModeStrategy.INSTANCE);
  94. }
  95. /**
  96. * Create a new iterator to traverse the work tree and its children.
  97. *
  98. * @param repo
  99. * the repository whose working tree will be scanned.
  100. * @param fileModeStrategy
  101. * the strategy to use to determine the FileMode for a FileEntry;
  102. * controls gitlinks etc.
  103. *
  104. * @since 4.3
  105. */
  106. public FileTreeIterator(Repository repo, FileModeStrategy fileModeStrategy) {
  107. this(repo.getWorkTree(), repo.getFS(),
  108. repo.getConfig().get(WorkingTreeOptions.KEY),
  109. fileModeStrategy);
  110. initRootIterator(repo);
  111. }
  112. /**
  113. * Create a new iterator to traverse the given directory and its children.
  114. *
  115. * @param root
  116. * the starting directory. This directory should correspond to
  117. * the root of the repository.
  118. * @param fs
  119. * the file system abstraction which will be necessary to perform
  120. * certain file system operations.
  121. * @param options
  122. * working tree options to be used
  123. */
  124. public FileTreeIterator(final File root, FS fs, WorkingTreeOptions options) {
  125. this(root, fs, options, DefaultFileModeStrategy.INSTANCE);
  126. }
  127. /**
  128. * Create a new iterator to traverse the given directory and its children.
  129. *
  130. * @param root
  131. * the starting directory. This directory should correspond to
  132. * the root of the repository.
  133. * @param fs
  134. * the file system abstraction which will be necessary to perform
  135. * certain file system operations.
  136. * @param options
  137. * working tree options to be used
  138. * @param fileModeStrategy
  139. * the strategy to use to determine the FileMode for a FileEntry;
  140. * controls gitlinks etc.
  141. *
  142. * @since 4.3
  143. */
  144. public FileTreeIterator(final File root, FS fs, WorkingTreeOptions options,
  145. FileModeStrategy fileModeStrategy) {
  146. super(options);
  147. directory = root;
  148. this.fs = fs;
  149. this.fileModeStrategy = fileModeStrategy;
  150. init(entries());
  151. }
  152. /**
  153. * Create a new iterator to traverse a subdirectory.
  154. *
  155. * @param p
  156. * the parent iterator we were created from.
  157. * @param root
  158. * the subdirectory. This should be a directory contained within
  159. * the parent directory.
  160. * @param fs
  161. * the file system abstraction which will be necessary to perform
  162. * certain file system operations.
  163. * @since 4.3
  164. * @deprecated use {@link #FileTreeIterator(FileTreeIterator, File, FS)}
  165. * instead.
  166. */
  167. @Deprecated
  168. protected FileTreeIterator(final WorkingTreeIterator p, final File root,
  169. FS fs) {
  170. this(p, root, fs, DefaultFileModeStrategy.INSTANCE);
  171. }
  172. /**
  173. * Create a new iterator to traverse a subdirectory.
  174. *
  175. * @param p
  176. * the parent iterator we were created from.
  177. * @param root
  178. * the subdirectory. This should be a directory contained within
  179. * the parent directory.
  180. * @param fs
  181. * the file system abstraction which will be necessary to perform
  182. * certain file system operations.
  183. *
  184. * @since 4.3
  185. */
  186. protected FileTreeIterator(final FileTreeIterator p, final File root,
  187. FS fs) {
  188. this(p, root, fs, p.fileModeStrategy);
  189. }
  190. /**
  191. * Create a new iterator to traverse a subdirectory, given the specified
  192. * FileModeStrategy.
  193. *
  194. * @param p
  195. * the parent iterator we were created from.
  196. * @param root
  197. * the subdirectory. This should be a directory contained within
  198. * the parent directory
  199. * @param fs
  200. * the file system abstraction which will be necessary to perform
  201. * certain file system operations.
  202. * @param fileModeStrategy
  203. * the strategy to use to determine the FileMode for a given
  204. * FileEntry.
  205. *
  206. * @since 4.3
  207. */
  208. protected FileTreeIterator(final WorkingTreeIterator p, final File root,
  209. FS fs, FileModeStrategy fileModeStrategy) {
  210. super(p);
  211. directory = root;
  212. this.fs = fs;
  213. this.fileModeStrategy = fileModeStrategy;
  214. init(entries());
  215. }
  216. @Override
  217. public AbstractTreeIterator createSubtreeIterator(final ObjectReader reader)
  218. throws IncorrectObjectTypeException, IOException {
  219. return new FileTreeIterator(this, ((FileEntry) current()).getFile(), fs, fileModeStrategy);
  220. }
  221. private Entry[] entries() {
  222. final File[] all = directory.listFiles();
  223. if (all == null)
  224. return EOF;
  225. final Entry[] r = new Entry[all.length];
  226. for (int i = 0; i < r.length; i++)
  227. r[i] = new FileEntry(all[i], fs, fileModeStrategy);
  228. return r;
  229. }
  230. /**
  231. * An interface representing the methods used to determine the FileMode for
  232. * a FileEntry.
  233. *
  234. * @since 4.3
  235. */
  236. public interface FileModeStrategy {
  237. /**
  238. * Compute the FileMode for a given File, based on its attributes.
  239. *
  240. * @param f
  241. * the file to return a FileMode for
  242. * @param attributes
  243. * the attributes of a file
  244. * @return a FileMode indicating whether the file is a regular file, a
  245. * directory, a gitlink, etc.
  246. */
  247. FileMode getMode(File f, FS.Attributes attributes);
  248. }
  249. /**
  250. * A default implementation of a FileModeStrategy; defaults to treating
  251. * nested .git directories as gitlinks, etc.
  252. *
  253. * @since 4.3
  254. */
  255. static public class DefaultFileModeStrategy implements FileModeStrategy {
  256. /**
  257. * a singleton instance of the default FileModeStrategy
  258. */
  259. public final static DefaultFileModeStrategy INSTANCE =
  260. new DefaultFileModeStrategy();
  261. @Override
  262. public FileMode getMode(File f, FS.Attributes attributes) {
  263. if (attributes.isSymbolicLink()) {
  264. return FileMode.SYMLINK;
  265. } else if (attributes.isDirectory()) {
  266. if (new File(f, Constants.DOT_GIT).exists()) {
  267. return FileMode.GITLINK;
  268. } else {
  269. return FileMode.TREE;
  270. }
  271. } else if (attributes.isExecutable()) {
  272. return FileMode.EXECUTABLE_FILE;
  273. } else {
  274. return FileMode.REGULAR_FILE;
  275. }
  276. }
  277. }
  278. /**
  279. * A FileModeStrategy that implements native git's DIR_NO_GITLINKS
  280. * behavior. This is the same as the default FileModeStrategy, except
  281. * all directories will be treated as directories regardless of whether
  282. * or not they contain a .git directory or file.
  283. *
  284. * @since 4.3
  285. */
  286. static public class NoGitlinksStrategy implements FileModeStrategy {
  287. /**
  288. * a singleton instance of the default FileModeStrategy
  289. */
  290. public final static NoGitlinksStrategy INSTANCE = new NoGitlinksStrategy();
  291. @Override
  292. public FileMode getMode(File f, FS.Attributes attributes) {
  293. if (attributes.isSymbolicLink()) {
  294. return FileMode.SYMLINK;
  295. } else if (attributes.isDirectory()) {
  296. return FileMode.TREE;
  297. } else if (attributes.isExecutable()) {
  298. return FileMode.EXECUTABLE_FILE;
  299. } else {
  300. return FileMode.REGULAR_FILE;
  301. }
  302. }
  303. }
  304. /**
  305. * Wrapper for a standard Java IO file
  306. */
  307. static public class FileEntry extends Entry {
  308. private final FileMode mode;
  309. private FS.Attributes attributes;
  310. private FS fs;
  311. /**
  312. * Create a new file entry.
  313. *
  314. * @param f
  315. * file
  316. * @param fs
  317. * file system
  318. */
  319. public FileEntry(File f, FS fs) {
  320. this(f, fs, DefaultFileModeStrategy.INSTANCE);
  321. }
  322. /**
  323. * Create a new file entry given the specified FileModeStrategy
  324. *
  325. * @param f
  326. * file
  327. * @param fs
  328. * file system
  329. * @param fileModeStrategy
  330. * the strategy to use when determining the FileMode of a
  331. * file; controls gitlinks etc.
  332. *
  333. * @since 4.3
  334. */
  335. public FileEntry(File f, FS fs, FileModeStrategy fileModeStrategy) {
  336. this.fs = fs;
  337. f = fs.normalize(f);
  338. attributes = fs.getAttributes(f);
  339. mode = fileModeStrategy.getMode(f, attributes);
  340. }
  341. @Override
  342. public FileMode getMode() {
  343. return mode;
  344. }
  345. @Override
  346. public String getName() {
  347. return attributes.getName();
  348. }
  349. @Override
  350. public long getLength() {
  351. return attributes.getLength();
  352. }
  353. @Override
  354. public long getLastModified() {
  355. return attributes.getLastModifiedTime();
  356. }
  357. @Override
  358. public InputStream openInputStream() throws IOException {
  359. if (fs.isSymLink(getFile()))
  360. return new ByteArrayInputStream(fs.readSymLink(getFile())
  361. .getBytes(
  362. Constants.CHARACTER_ENCODING));
  363. else
  364. return new FileInputStream(getFile());
  365. }
  366. /**
  367. * Get the underlying file of this entry.
  368. *
  369. * @return the underlying file of this entry
  370. */
  371. public File getFile() {
  372. return attributes.getFile();
  373. }
  374. }
  375. /**
  376. * @return The root directory of this iterator
  377. */
  378. public File getDirectory() {
  379. return directory;
  380. }
  381. /**
  382. * @return The location of the working file. This is the same as {@code new
  383. * File(getDirectory(), getEntryPath())} but may be faster by
  384. * reusing an internal File instance.
  385. */
  386. public File getEntryFile() {
  387. return ((FileEntry) current()).getFile();
  388. }
  389. @Override
  390. protected byte[] idSubmodule(final Entry e) {
  391. return idSubmodule(getDirectory(), e);
  392. }
  393. @Override
  394. protected String readSymlinkTarget(Entry entry) throws IOException {
  395. return fs.readSymLink(getEntryFile());
  396. }
  397. }