comparison src/org/tmatesoft/hg/util/RegularFileStats.java @ 425:48f993aa2f41

FIXMEs: exceptions, javadoc
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 28 Mar 2012 18:39:29 +0200
parents 9c9c442b5f2e
children cd658b24a620
comparison
equal deleted inserted replaced
424:6437d261048a 425:48f993aa2f41
28 import java.util.Set; 28 import java.util.Set;
29 import java.util.TreeMap; 29 import java.util.TreeMap;
30 import java.util.regex.Matcher; 30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern; 31 import java.util.regex.Pattern;
32 32
33 import org.tmatesoft.hg.core.SessionContext;
33 import org.tmatesoft.hg.internal.Internals; 34 import org.tmatesoft.hg.internal.Internals;
34 import org.tmatesoft.hg.internal.ProcessExecHelper; 35 import org.tmatesoft.hg.internal.ProcessExecHelper;
35 36
36 /** 37 /**
37 * Utility to collect executable files and symbolic links in a directory. 38 * Utility to collect executable files and symbolic links in a directory.
52 private boolean isExec, isSymlink; 53 private boolean isExec, isSymlink;
53 private String symlinkValue; 54 private String symlinkValue;
54 private final List<String> command; 55 private final List<String> command;
55 private final ProcessExecHelper execHelper; 56 private final ProcessExecHelper execHelper;
56 private final Matcher linkMatcher, execMatcher; 57 private final Matcher linkMatcher, execMatcher;
58 private final SessionContext sessionContext;
57 59
58 60
59 // directory name to (short link name -> link target) 61 // directory name to (short link name -> link target)
60 private Map<String, Map<String, String>> dir2links = new TreeMap<String, Map<String, String>>(); 62 private Map<String, Map<String, String>> dir2links = new TreeMap<String, Map<String, String>>();
61 // directory name to set of executable file short names 63 // directory name to set of executable file short names
62 private Map<String, Set<String>> dir2execs = new TreeMap<String, Set<String>>(); 64 private Map<String, Set<String>> dir2execs = new TreeMap<String, Set<String>>();
63 65
64 66
65 RegularFileStats() { 67 RegularFileStats(SessionContext ctx) {
68 sessionContext = ctx;
66 if (Internals.runningOnWindows()) { 69 if (Internals.runningOnWindows()) {
67 // XXX this implementation is not yet tested against any Windows repository, 70 // XXX this implementation is not yet tested against any Windows repository,
68 // only against sample dir listings. As long as Mercurial doesn't handle Windows 71 // only against sample dir listings. As long as Mercurial doesn't handle Windows
69 // links, we don't really need this 72 // links, we don't really need this
70 command = Arrays.asList("cmd", "/c", "dir"); 73 command = Arrays.asList("cmd", "/c", "dir");
86 execMatcher = pExec.matcher(""); 89 execMatcher = pExec.matcher("");
87 } 90 }
88 execHelper = new ProcessExecHelper(); 91 execHelper = new ProcessExecHelper();
89 } 92 }
90 93
94 /**
95 * Fails silently indicating false for both x and l in case interaction with file system failed
96 * @param f file to check, doesn't need to exist
97 */
91 public void init(File f) { 98 public void init(File f) {
99 isExec = isSymlink = false;
100 symlinkValue = null;
101 //
92 // can't check isFile because Java would say false for a symlink with non-existing target 102 // can't check isFile because Java would say false for a symlink with non-existing target
93 if (f.isDirectory()) { 103 if (f.isDirectory()) {
94 // perhaps, shall just collect stats for all files and set false to exec/link flags? 104 // perhaps, shall just collect stats for all files and set false to exec/link flags?
95 throw new IllegalArgumentException(); // FIXME EXCEPTIONS 105 throw new IllegalArgumentException(); // FIXME EXCEPTIONS
96 } 106 }
120 } else { 130 } else {
121 links = Collections.emptyMap(); 131 links = Collections.emptyMap();
122 } 132 }
123 dir2links.put(dirName, links); 133 dir2links.put(dirName, links);
124 dir2execs.put(dirName, execs); 134 dir2execs.put(dirName, execs);
135 isExec = execs.contains(fileName);
136 isSymlink = links.containsKey(fileName);
137 if (isSymlink) {
138 symlinkValue = links.get(fileName);
139 } else {
140 symlinkValue = null;
141 }
125 } catch (InterruptedException ex) { 142 } catch (InterruptedException ex) {
143 sessionContext.getLog().warn(getClass(), ex, String.format("Failed to detect flags for %s", f));
126 // try again? ensure not too long? stop right away? 144 // try again? ensure not too long? stop right away?
127 // FIXME EXCEPTIONS 145 // IGNORE, keep isExec and isSymlink false
128 throw new RuntimeException();
129 } catch (IOException ex) { 146 } catch (IOException ex) {
130 // FIXME EXCEPTIONS perhaps, fail silently indicating false for both x and l? 147 sessionContext.getLog().warn(getClass(), ex, String.format("Failed to detect flags for %s", f));
131 throw new RuntimeException(); 148 // IGNORE, keep isExec and isSymlink false
132 } 149 }
133 }
134 isExec = execs.contains(fileName);
135 isSymlink = links.containsKey(fileName);
136 if (isSymlink) {
137 symlinkValue = links.get(fileName);
138 } else {
139 symlinkValue = null;
140 } 150 }
141 } 151 }
142 152
143 public boolean isExecutable() { 153 public boolean isExecutable() {
144 return isExec; 154 return isExec;