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