Merge changes I39bfefee,I47795987,I70d120fb,I58cc5e01,I96bee7b9
[jgit.git] / org.eclipse.jgit / src / org / eclipse / jgit / treewalk / FileTreeIterator.java
blob2d032ab835c502ee1186bacfa192403b7876d86b
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.
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
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
17 * conditions are met:
19 * - Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
47 package org.eclipse.jgit.treewalk;
49 import java.io.File;
50 import java.io.FileInputStream;
51 import java.io.IOException;
52 import java.io.InputStream;
54 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
55 import org.eclipse.jgit.lib.Constants;
56 import org.eclipse.jgit.lib.FileMode;
57 import org.eclipse.jgit.lib.ObjectReader;
58 import org.eclipse.jgit.lib.Repository;
59 import org.eclipse.jgit.util.FS;
61 /**
62 * Working directory iterator for standard Java IO.
63 * <p>
64 * This iterator uses the standard <code>java.io</code> package to read the
65 * specified working directory as part of a {@link TreeWalk}.
67 public class FileTreeIterator extends WorkingTreeIterator {
68 /**
69 * the starting directory. This directory should correspond to
70 * the root of the repository.
72 protected final File directory;
74 /**
75 * the file system abstraction which will be necessary to
76 * perform certain file system operations.
78 protected final FS fs;
80 /**
81 * Create a new iterator to traverse the work tree and its children.
83 * @param repo
84 * the repository whose working tree will be scanned.
86 public FileTreeIterator(Repository repo) {
87 this(repo.getWorkTree(), repo.getFS());
88 initRootIterator(repo);
91 /**
92 * Create a new iterator to traverse the given directory and its children.
94 * @param root
95 * the starting directory. This directory should correspond to
96 * the root of the repository.
97 * @param fs
98 * the file system abstraction which will be necessary to
99 * perform certain file system operations.
101 public FileTreeIterator(final File root, FS fs) {
102 directory = root;
103 this.fs = fs;
104 init(entries());
108 * Create a new iterator to traverse a subdirectory.
110 * @param p
111 * the parent iterator we were created from.
112 * @param fs
113 * the file system abstraction which will be necessary to
114 * perform certain file system operations.
115 * @param root
116 * the subdirectory. This should be a directory contained within
117 * the parent directory.
119 protected FileTreeIterator(final FileTreeIterator p, final File root, FS fs) {
120 super(p);
121 directory = root;
122 this.fs = fs;
123 init(entries());
126 @Override
127 public AbstractTreeIterator createSubtreeIterator(final ObjectReader reader)
128 throws IncorrectObjectTypeException, IOException {
129 return new FileTreeIterator(this, ((FileEntry) current()).file, fs);
132 private Entry[] entries() {
133 final File[] all = directory.listFiles();
134 if (all == null)
135 return EOF;
136 final Entry[] r = new Entry[all.length];
137 for (int i = 0; i < r.length; i++)
138 r[i] = new FileEntry(all[i], fs);
139 return r;
143 * Wrapper for a standard Java IO file
145 static public class FileEntry extends Entry {
146 final File file;
148 private final FileMode mode;
150 private long length = -1;
152 private long lastModified;
154 FileEntry(final File f, FS fs) {
155 file = f;
157 if (f.isDirectory()) {
158 if (new File(f, Constants.DOT_GIT).isDirectory())
159 mode = FileMode.GITLINK;
160 else
161 mode = FileMode.TREE;
162 } else if (fs.canExecute(file))
163 mode = FileMode.EXECUTABLE_FILE;
164 else
165 mode = FileMode.REGULAR_FILE;
168 @Override
169 public FileMode getMode() {
170 return mode;
173 @Override
174 public String getName() {
175 return file.getName();
178 @Override
179 public long getLength() {
180 if (length < 0)
181 length = file.length();
182 return length;
185 @Override
186 public long getLastModified() {
187 if (lastModified == 0)
188 lastModified = file.lastModified();
189 return lastModified;
192 @Override
193 public InputStream openInputStream() throws IOException {
194 return new FileInputStream(file);
198 * Get the underlying file of this entry.
200 * @return the underlying file of this entry
202 public File getFile() {
203 return file;
208 * @return
209 * The root directory of this iterator
211 public File getDirectory() {
212 return directory;