aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorRobin Rosenberg <robin.rosenberg@dewire.com>2014-01-23 06:07:12 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2014-02-12 01:35:25 +0100
commit14cd43e6df8045c8f2879ddc8db0f59f45f2ce9c (patch)
tree562ca7e1ef642b63a6b1837d097b6b81dfc14615 /org.eclipse.jgit
parent2852b6a07d78086e155c2f658ff9b5b235d1fb4e (diff)
downloadjgit-14cd43e6df8045c8f2879ddc8db0f59f45f2ce9c.tar.gz
jgit-14cd43e6df8045c8f2879ddc8db0f59f45f2ce9c.zip
Use fileAttributes to get more attributes in one go
On Windows the length reported by FileAttributes is the size of the target file (a bug, I guess) rather than the link, so we read the linke and look at the length of the link instead. Bug: 353771 Change-Id: I834b06d0447f84379612b8c9190fa77093617595 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/treewalk/FileTreeIterator.java65
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java146
2 files changed, 165 insertions, 46 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/FileTreeIterator.java b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/FileTreeIterator.java
index cb919ecbcd..9e89791b84 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/FileTreeIterator.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/treewalk/FileTreeIterator.java
@@ -132,7 +132,7 @@ public class FileTreeIterator extends WorkingTreeIterator {
@Override
public AbstractTreeIterator createSubtreeIterator(final ObjectReader reader)
throws IncorrectObjectTypeException, IOException {
- return new FileTreeIterator(this, ((FileEntry) current()).file, fs);
+ return new FileTreeIterator(this, ((FileEntry) current()).getFile(), fs);
}
private Entry[] entries() {
@@ -149,13 +149,9 @@ public class FileTreeIterator extends WorkingTreeIterator {
* Wrapper for a standard Java IO file
*/
static public class FileEntry extends Entry {
- final File file;
-
private final FileMode mode;
- private long length = -1;
-
- private long lastModified;
+ private FS.Attributes attributes;
private FS fs;
@@ -168,27 +164,17 @@ public class FileTreeIterator extends WorkingTreeIterator {
* file system
*/
public FileEntry(final File f, FS fs) {
- file = f;
this.fs = fs;
-
- @SuppressWarnings("hiding")
- FileMode mode = null;
- try {
- if (fs.isSymLink(f)) {
- mode = FileMode.SYMLINK;
- } else if (fs.isDirectory(f)) {
- if (fs.exists(new File(f, Constants.DOT_GIT)))
- mode = FileMode.GITLINK;
- else
- mode = FileMode.TREE;
- } else if (fs.canExecute(file))
- mode = FileMode.EXECUTABLE_FILE;
+ attributes = fs.getAttributes(f);
+ if (attributes.isDirectory()) {
+ if (new File(f, Constants.DOT_GIT).exists())
+ mode = FileMode.GITLINK;
else
- mode = FileMode.REGULAR_FILE;
- } catch (IOException e) {
- mode = FileMode.MISSING;
- }
- this.mode = mode;
+ mode = FileMode.TREE;
+ } else if (attributes.isExecutable())
+ mode = FileMode.EXECUTABLE_FILE;
+ else
+ mode = FileMode.REGULAR_FILE;
}
@Override
@@ -198,40 +184,27 @@ public class FileTreeIterator extends WorkingTreeIterator {
@Override
public String getName() {
- return file.getName();
+ return attributes.getName();
}
@Override
public long getLength() {
- if (length < 0) {
- try {
- length = fs.length(file);
- } catch (IOException e) {
- length = 0;
- }
- }
- return length;
+ return attributes.getLength();
}
@Override
public long getLastModified() {
- if (lastModified == 0) {
- try {
- lastModified = fs.lastModified(file);
- } catch (IOException e) {
- lastModified = 0;
- }
- }
- return lastModified;
+ return attributes.getLastModifiedTime();
}
@Override
public InputStream openInputStream() throws IOException {
- if (fs.isSymLink(file))
- return new ByteArrayInputStream(fs.readSymLink(file).getBytes(
+ if (fs.isSymLink(getFile()))
+ return new ByteArrayInputStream(fs.readSymLink(getFile())
+ .getBytes(
Constants.CHARACTER_ENCODING));
else
- return new FileInputStream(file);
+ return new FileInputStream(getFile());
}
/**
@@ -240,7 +213,7 @@ public class FileTreeIterator extends WorkingTreeIterator {
* @return the underlying file of this entry
*/
public File getFile() {
- return file;
+ return attributes.getFile();
}
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java
index a5642f149a..9e964fc363 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java
@@ -634,4 +634,150 @@ public abstract class FS {
this.value = value;
}
}
+
+ /**
+ * File attributes we typically care for.
+ *
+ * @since 3.3
+ */
+ public static class Attributes {
+
+ /**
+ * @return true if this are the attributes of a directory
+ */
+ public boolean isDirectory() {
+ return isDirectory;
+ }
+
+ /**
+ * @return true if this are the attributes of an executable file
+ */
+ public boolean isExecutable() {
+ return isExecutable;
+ }
+
+ /**
+ * @return true if this are the attributes of a symbolic link
+ */
+ public boolean isSymbolicLink() {
+ return isSymbolicLink;
+ }
+
+ /**
+ * @return true if this are the attributes of a regular file
+ */
+ public boolean isRegularFile() {
+ return isRegularFile;
+ }
+
+ /**
+ * @return the time when the file was created
+ */
+ public long getCreationTime() {
+ return creationTime;
+ }
+
+ /**
+ * @return the time (milliseconds since 1970-01-01) when this object was
+ * last modified
+ */
+ public long getLastModifiedTime() {
+ return lastModifiedTime;
+ }
+
+ private boolean isDirectory;
+
+ private boolean isSymbolicLink;
+
+ private boolean isRegularFile;
+
+ private long creationTime;
+
+ private long lastModifiedTime;
+
+ private boolean isExecutable;
+
+ private File file;
+
+ private boolean exists;
+
+ /**
+ * file length
+ */
+ protected long length = -1;
+
+ FS fs;
+
+ Attributes(FS fs, File file, boolean exists, boolean isDirectory,
+ boolean isExecutable, boolean isSymbolicLink,
+ boolean isRegularFile, long creationTime,
+ long lastModifiedTime, long length) {
+ this.fs = fs;
+ this.file = file;
+ this.exists = exists;
+ this.isDirectory = isDirectory;
+ this.isExecutable = isExecutable;
+ this.isSymbolicLink = isSymbolicLink;
+ this.isRegularFile = isRegularFile;
+ this.creationTime = creationTime;
+ this.lastModifiedTime = lastModifiedTime;
+ this.length = length;
+ }
+
+ /**
+ * Constructor when there are issues with reading
+ *
+ * @param fs
+ * @param path
+ */
+ public Attributes(File path, FS fs) {
+ this.file = path;
+ this.fs = fs;
+ }
+
+ /**
+ * @return length of this file object
+ */
+ public long getLength() {
+ if (length == -1)
+ return length = file.length();
+ return length;
+ }
+
+ /**
+ * @return the filename
+ */
+ public String getName() {
+ return file.getName();
+ }
+
+ /**
+ * @return the file the attributes apply to
+ */
+ public File getFile() {
+ return file;
+ }
+
+ boolean exists() {
+ return exists;
+ }
+ }
+
+ /**
+ * @param path
+ * @return the file attributes we care for
+ * @since 3.3
+ */
+ public Attributes getAttributes(File path) {
+ boolean isDirectory = isDirectory(path);
+ boolean isFile = !isDirectory && path.isFile();
+ assert path.exists() == isDirectory || isFile;
+ boolean exists = isDirectory || isFile;
+ boolean canExecute = exists && !isDirectory && canExecute(path);
+ boolean isSymlink = false;
+ long lastModified = exists ? path.lastModified() : 0L;
+ long createTime = 0L;
+ return new Attributes(this, path, exists, isDirectory, canExecute,
+ isSymlink, isFile, createTime, lastModified, -1);
+ }
}