1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package openr66.context.filesystem;
22
23 import goldengate.common.command.exception.CommandAbstractException;
24 import goldengate.common.command.exception.Reply550Exception;
25 import goldengate.common.command.exception.Reply553Exception;
26 import goldengate.common.file.filesystembased.FilesystemBasedDirImpl;
27 import goldengate.common.file.filesystembased.FilesystemBasedOptsMLSxImpl;
28 import goldengate.common.file.filesystembased.specific.FilesystemBasedCommonsIo;
29 import goldengate.common.file.filesystembased.specific.FilesystemBasedDirJdkAbstract;
30
31 import java.io.File;
32 import java.io.FileFilter;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import openr66.context.R66Session;
38 import openr66.context.authentication.R66Auth;
39 import openr66.protocol.configuration.Configuration;
40
41
42
43
44
45
46
47 public class R66Dir extends FilesystemBasedDirImpl {
48
49
50
51
52 public R66Dir(R66Session session) {
53 super(session, new FilesystemBasedOptsMLSxImpl());
54 }
55
56
57
58
59
60
61
62 @Override
63 public R66File newFile(String path, boolean append)
64 throws CommandAbstractException {
65 return new R66File((R66Session) getSession(), this, path, append);
66 }
67
68
69
70
71
72
73
74
75
76 public synchronized R66File setUniqueFile(long prefix, String filename)
77 throws CommandAbstractException {
78 checkIdentify();
79 File file = null;
80 String prename = prefix + "_";
81 if (prename.length() < 3) {
82 prename = "xx_"+prename;
83 }
84 String basename = R66File.getBasename(filename);
85 try {
86 file = File.createTempFile(prename, "_" + basename +
87 Configuration.EXT_R66, getFileFromPath(currentDir));
88 } catch (IOException e) {
89 throw new Reply550Exception("Cannot create unique file from " +
90 basename);
91 }
92 String currentFile = getRelativePath(file);
93 return newFile(normalizePath(currentFile), false);
94 }
95
96
97
98
99
100
101 public static String getFinalUniqueFilename(R66File file) {
102 String finalpath = file.getBasename();
103 int pos = finalpath.lastIndexOf(Configuration.EXT_R66);
104 if (pos > 0) {
105 finalpath = finalpath.substring(0, pos);
106 }
107 return finalpath;
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122 protected List<String> wildcardFilesNoCheck(String pathWithWildcard)
123 throws CommandAbstractException {
124 List<String> resultPaths = new ArrayList<String>();
125
126 if (!(pathWithWildcard.contains("*") || pathWithWildcard.contains("?") || pathWithWildcard
127 .contains("~"))) {
128
129 resultPaths.add(pathWithWildcard);
130 return resultPaths;
131 }
132
133 if (!FilesystemBasedDirJdkAbstract.ueApacheCommonsIo) {
134 throw new Reply553Exception("Wildcards in pathname is not allowed");
135 }
136 File wildcardFile = new File(pathWithWildcard);
137 File rootFile;
138 initWindowsSupport();
139 if (ISUNIX) {
140 rootFile = new File("/");
141 } else {
142 rootFile = getCorrespondingRoot(wildcardFile);
143 }
144
145 List<String> subdirs = new ArrayList<String>();
146 while (wildcardFile != null) {
147 File parent = wildcardFile.getParentFile();
148 if (parent == null) {
149 subdirs.add(0, wildcardFile.getPath());
150 break;
151 }
152 subdirs.add(0, wildcardFile.getName());
153 if (parent.equals(rootFile)) {
154
155 subdirs.add(0, parent.getPath());
156 break;
157 }
158 wildcardFile = parent;
159 }
160 List<File> basedPaths = new ArrayList<File>();
161
162 basedPaths.add(new File(subdirs.get(0)));
163 int i = 1;
164
165 while (i < subdirs.size()) {
166
167 FileFilter fileFilter = FilesystemBasedCommonsIo
168 .getWildcardFileFilter(subdirs.get(i));
169 List<File> newBasedPaths = new ArrayList<File>();
170
171 for (File dir: basedPaths) {
172 if (dir.isDirectory()) {
173 for (File match: dir.listFiles(fileFilter)) {
174 newBasedPaths.add(match);
175 }
176 }
177 }
178
179 basedPaths = newBasedPaths;
180 i ++;
181 }
182
183 for (File file: basedPaths) {
184 resultPaths.add(file.getAbsolutePath());
185 }
186 return resultPaths;
187 }
188
189
190
191
192
193
194
195
196 public R66File setFileNoCheck(String path)
197 throws CommandAbstractException {
198 checkIdentify();
199 String newpath = consolidatePath(path);
200 List<String> paths = wildcardFilesNoCheck(newpath);
201 if (paths.size() != 1) {
202 throw new Reply550Exception("FileInterface not found: " +
203 paths.size() + " founds");
204 }
205 String extDir = paths.get(0);
206 return new R66File((R66Session) getSession(), this, extDir);
207 }
208
209
210
211
212
213
214 public String getFullPath() {
215 if (session.getAuth() == null) {
216 return currentDir;
217 }
218 return ((R66Auth) session.getAuth()).getAbsolutePath(currentDir);
219 }
220
221 @Override
222 public String toString() {
223 return "Dir: " + currentDir;
224 }
225 }