comparison src/org/tmatesoft/hg/util/FileWalker.java @ 413:7f27122011c3

Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 21 Mar 2012 20:40:28 +0100
parents ed6b74a58c66
children 48f993aa2f41
comparison
equal deleted inserted replaced
406:d56ea1a2537a 413:7f27122011c3
1 /* 1 /*
2 * Copyright (c) 2011 TMate Software Ltd 2 * Copyright (c) 2011-2012 TMate Software Ltd
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by 5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License. 6 * the Free Software Foundation; version 2 of the License.
7 * 7 *
18 18
19 import java.io.File; 19 import java.io.File;
20 import java.util.LinkedList; 20 import java.util.LinkedList;
21 import java.util.NoSuchElementException; 21 import java.util.NoSuchElementException;
22 22
23 import org.tmatesoft.hg.internal.Internals;
24
23 /** 25 /**
26 * Implementation of {@link FileIterator} using regular {@link java.io.File}
24 * 27 *
25 * @author Artem Tikhomirov 28 * @author Artem Tikhomirov
26 * @author TMate Software Ltd. 29 * @author TMate Software Ltd.
27 */ 30 */
28 public class FileWalker implements FileIterator { 31 public class FileWalker implements FileIterator {
30 private final File startDir; 33 private final File startDir;
31 private final Path.Source pathHelper; 34 private final Path.Source pathHelper;
32 private final LinkedList<File> dirQueue; 35 private final LinkedList<File> dirQueue;
33 private final LinkedList<File> fileQueue; 36 private final LinkedList<File> fileQueue;
34 private final Path.Matcher scope; 37 private final Path.Matcher scope;
38 private final boolean execCap, linkCap;
35 private RegularFileInfo nextFile; 39 private RegularFileInfo nextFile;
36 private Path nextPath; 40 private Path nextPath;
37 41
38 public FileWalker(File dir, Path.Source pathFactory) { 42 public FileWalker(File dir, Path.Source pathFactory) {
39 this(dir, pathFactory, null); 43 this(dir, pathFactory, null);
51 startDir = dir; 55 startDir = dir;
52 pathHelper = pathFactory; 56 pathHelper = pathFactory;
53 dirQueue = new LinkedList<File>(); 57 dirQueue = new LinkedList<File>();
54 fileQueue = new LinkedList<File>(); 58 fileQueue = new LinkedList<File>();
55 scope = scopeMatcher; 59 scope = scopeMatcher;
60 execCap = Internals.checkSupportsExecutables(startDir);
61 linkCap = Internals.checkSupportsSymlinks(startDir);
56 reset(); 62 reset();
57 } 63 }
58 64
59 public void reset() { 65 public void reset() {
60 fileQueue.clear(); 66 fileQueue.clear();
61 dirQueue.clear(); 67 dirQueue.clear();
62 dirQueue.add(startDir); 68 dirQueue.add(startDir);
63 nextFile = new RegularFileInfo(); 69 nextFile = new RegularFileInfo(supportsExecFlag(), supportsLinkFlag());
64 nextPath = null; 70 nextPath = null;
65 } 71 }
66 72
67 public boolean hasNext() { 73 public boolean hasNext() {
68 return fill(); 74 return fill();
88 public boolean inScope(Path file) { 94 public boolean inScope(Path file) {
89 /* by default, no limits, all files are of interest */ 95 /* by default, no limits, all files are of interest */
90 return scope == null ? true : scope.accept(file); 96 return scope == null ? true : scope.accept(file);
91 } 97 }
92 98
99 public boolean supportsExecFlag() {
100 return execCap;
101 }
102
103 public boolean supportsLinkFlag() {
104 return linkCap;
105 }
106
93 // returns non-null 107 // returns non-null
94 private File[] listFiles(File f) { 108 private File[] listFiles(File f) {
95 // in case we need to solve os-related file issues (mac with some encodings?) 109 // in case we need to solve os-related file issues (mac with some encodings?)
96 File[] rv = f.listFiles(); 110 File[] rv = f.listFiles();
97 // there are chances directory we query files for is missing (deleted), just treat it as empty 111 // there are chances directory we query files for is missing (deleted), just treat it as empty