Mercurial > jhg
comparison src/org/tmatesoft/hg/util/RegularFileStats.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 | |
children | 9c9c442b5f2e |
comparison
equal
deleted
inserted
replaced
406:d56ea1a2537a | 413:7f27122011c3 |
---|---|
1 /* | |
2 * Copyright (c) 2012 TMate Software Ltd | |
3 * | |
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 | |
6 * the Free Software Foundation; version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * For information on how to redistribute this software under | |
14 * the terms of a license other than GNU General Public License | |
15 * contact TMate Software at support@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.util; | |
18 | |
19 import java.io.File; | |
20 import java.io.IOException; | |
21 import java.util.ArrayList; | |
22 import java.util.Arrays; | |
23 import java.util.Collections; | |
24 import java.util.HashMap; | |
25 import java.util.HashSet; | |
26 import java.util.List; | |
27 import java.util.Map; | |
28 import java.util.Set; | |
29 import java.util.TreeMap; | |
30 import java.util.regex.Matcher; | |
31 import java.util.regex.Pattern; | |
32 | |
33 import org.tmatesoft.hg.internal.Internals; | |
34 import org.tmatesoft.hg.internal.ProcessExecHelper; | |
35 | |
36 /** | |
37 * Utility to collect executable files and symbolic links in a directory. | |
38 * | |
39 * | |
40 * Not public as present approach (expect file but collect once per directory) may need to be made explicit | |
41 * | |
42 * TODO post-1.0 Add Linux-specific set of tests (similar to my test-flags repository, with symlink, executable and regular file, | |
43 * and few revisions where link and exec flags change. +testcase when link points to non-existing file (shall not report as missing, | |
44 * iow either FileInfo.exist() shall respect symlinks or WCSC account for ) | |
45 * | |
46 * TODO post-1.0 Add extraction of link modification time, see RegularFileInfo#lastModified() | |
47 * | |
48 * @author Artem Tikhomirov | |
49 * @author Tmate Software Ltd. | |
50 */ | |
51 /*package-local*/ class RegularFileStats { | |
52 private boolean isExec, isSymlink; | |
53 private String symlinkValue; | |
54 private final List<String> command; | |
55 private final ProcessExecHelper execHelper; | |
56 private final Matcher linkMatcher, execMatcher; | |
57 | |
58 | |
59 // directory name to (short link name -> link target) | |
60 private Map<String, Map<String, String>> dir2links = new TreeMap<String, Map<String, String>>(); | |
61 // directory name to set of executable file short names | |
62 private Map<String, Set<String>> dir2execs = new TreeMap<String, Set<String>>(); | |
63 | |
64 | |
65 RegularFileStats() { | |
66 if (Internals.runningOnWindows()) { | |
67 // 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 | |
69 // links, we don't really need this | |
70 command = Arrays.asList("cmd", "/c", "dir"); | |
71 // Windows patterns need to work against full directory listing (I didn't find a way | |
72 // to list single file with its attributes like SYMLINK) | |
73 Pattern pLink = Pattern.compile("^\\S+.*\\s+<SYMLINK>\\s+(\\S.*)\\s+\\[(.+)\\]$", Pattern.MULTILINE); | |
74 Pattern pExec = Pattern.compile("^\\S+.*\\s+\\d+\\s+(\\S.*\\.exe)$", Pattern.MULTILINE); | |
75 linkMatcher = pLink.matcher(""); | |
76 execMatcher = pExec.matcher(""); | |
77 } else { | |
78 command = Arrays.asList("/bin/ls", "-l", "-Q"); // -Q is essential to get quoted name - the only way to | |
79 // tell exact file name (which may start or end with spaces. | |
80 Pattern pLink = Pattern.compile("^lrwxrwxrwx\\s.*\\s\"(.*)\"\\s+->\\s+\"(.*)\"$", Pattern.MULTILINE); | |
81 // pLink: group(1) is full name if single file listing (ls -l /usr/bin/java) and short name if directory listing (ls -l /usr/bin) | |
82 // group(2) is link target | |
83 Pattern pExec = Pattern.compile("^-..[sx]..[sx]..[sx]\\s.*\\s\"(.+)\"$", Pattern.MULTILINE); | |
84 // pExec: group(1) is name of executable file | |
85 linkMatcher = pLink.matcher(""); | |
86 execMatcher = pExec.matcher(""); | |
87 } | |
88 execHelper = new ProcessExecHelper(); | |
89 } | |
90 | |
91 public void init(File f) { | |
92 // can't check isFile because Java would say false for a symlink with non-existing target | |
93 if (f.isDirectory()) { | |
94 // perhaps, shall just collect stats for all files and set false to exec/link flags? | |
95 throw new IllegalArgumentException(); // FIXME EXCEPTIONS | |
96 } | |
97 final String dirName = f.getParentFile().getAbsolutePath(); | |
98 final String fileName = f.getName(); | |
99 Map<String, String> links = dir2links.get(dirName); | |
100 Set<String> execs = dir2execs.get(dirName); | |
101 if (links == null || execs == null) { | |
102 try { | |
103 ArrayList<String> cmd = new ArrayList<String>(command); | |
104 cmd.add(dirName); | |
105 CharSequence result = execHelper.exec(cmd); | |
106 | |
107 if (execMatcher.reset(result).find()) { | |
108 execs = new HashSet<String>(); | |
109 do { | |
110 execs.add(execMatcher.group(1)); | |
111 } while (execMatcher.find()); | |
112 } else { | |
113 execs = Collections.emptySet(); // indicate we tried and found nothing | |
114 } | |
115 if (linkMatcher.reset(result).find()) { | |
116 links = new HashMap<String, String>(); | |
117 do { | |
118 links.put(linkMatcher.group(1), linkMatcher.group(2)); | |
119 } while (linkMatcher.find()); | |
120 } else { | |
121 links = Collections.emptyMap(); | |
122 } | |
123 dir2links.put(dirName, links); | |
124 dir2execs.put(dirName, execs); | |
125 } catch (InterruptedException ex) { | |
126 // try again? ensure not too long? stop right away? | |
127 // FIXME EXCEPTIONS | |
128 throw new RuntimeException(); | |
129 } catch (IOException ex) { | |
130 // FIXME EXCEPTIONS perhaps, fail silently indicating false for both x and l? | |
131 throw new RuntimeException(); | |
132 } | |
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 } | |
141 } | |
142 | |
143 public boolean isExecutable() { | |
144 return isExec; | |
145 } | |
146 | |
147 public boolean isSymlink() { | |
148 return isSymlink; | |
149 } | |
150 | |
151 public String getSymlinkTarget() { | |
152 if (isSymlink) { | |
153 return symlinkValue; | |
154 } | |
155 throw new UnsupportedOperationException(); | |
156 } | |
157 } |