/
ScpFileOpener.java
368 lines (333 loc) · 16.4 KB
/
ScpFileOpener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sshd.scp.common;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.AccessDeniedException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.LinkOption;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.sshd.common.SshException;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.common.util.SelectorUtils;
import org.apache.sshd.common.util.io.DirectoryScanner;
import org.apache.sshd.common.util.io.IoUtils;
import org.apache.sshd.scp.common.helpers.ScpTimestampCommandDetails;
/**
* Plug-in mechanism for users to intervene in the SCP process - e.g., apply some kind of traffic shaping mechanism,
* display upload/download progress, etc...
*
* @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
*/
public interface ScpFileOpener {
/**
* Invoked when receiving a new file to via a directory command
*
* @param session The client/server {@link Session} through which the transfer is being executed
* @param localPath The target local path
* @param name The target file name
* @param preserve Whether requested to preserve the permissions and timestamp
* @param permissions The requested file permissions
* @param time The requested {@link ScpTimestampCommandDetails} - may be {@code null} if nothing to update
* @return The actual target file path for the incoming file/directory
* @throws IOException If failed to resolve the file path
* @see #updateFileProperties(Path, Set, ScpTimestampCommandDetails) updateFileProperties
*/
default Path resolveIncomingFilePath(
Session session, Path localPath, String name, boolean preserve, Set<PosixFilePermission> permissions,
ScpTimestampCommandDetails time)
throws IOException {
LinkOption[] options = IoUtils.getLinkOptions(true);
Boolean status = IoUtils.checkFileExists(localPath, options);
if (status == null) {
throw new AccessDeniedException("Receive directory existence status cannot be determined: " + localPath);
}
Path file = null;
if (status && Files.isDirectory(localPath, options)) {
String localName = name.replace('/', File.separatorChar);
file = localPath.resolve(localName);
} else if (!status) {
Path parent = localPath.getParent();
status = IoUtils.checkFileExists(parent, options);
if (status == null) {
throw new AccessDeniedException(
"Receive directory parent (" + parent + ") existence status cannot be determined for " + localPath);
}
if (status && Files.isDirectory(parent, options)) {
file = localPath;
}
}
if (file == null) {
throw new IOException("Cannot write to " + localPath);
}
status = IoUtils.checkFileExists(file, options);
if (status == null) {
throw new AccessDeniedException("Receive directory file existence status cannot be determined: " + file);
}
if (!(status && Files.isDirectory(file, options))) {
Files.createDirectory(file);
}
if (preserve) {
updateFileProperties(file, permissions, time);
}
return file;
}
/**
* Invoked when required to send a pattern of files
*
* @param session The client/server {@link Session} through which the transfer is being executed
* @param basedir The base directory - may be {@code null}/empty to indicate CWD
* @param pattern The required pattern - ignored if {@code null}/empty - returns empty result
* @return The matching <U>relative paths</U> of the children to send
* @throws IOException If failed to scan the directory
*/
default Iterable<Path> getMatchingFilesToSend(Session session, Path basedir, String pattern) throws IOException {
if (GenericUtils.isEmpty(pattern)) {
return Collections.emptyList();
}
if (basedir == null) {
Path cwdPath = OsUtils.getCurrentWorkingDirectory();
if (cwdPath == null) {
throw new FileNotFoundException("No CWD value available");
}
basedir = cwdPath.toAbsolutePath();
}
// We may reach this location with a rooted path which uses '/' as the separator
FileSystem fs = basedir.getFileSystem();
String fsSep = fs.getSeparator();
DirectoryScanner ds = new DirectoryScanner(basedir);
ds.setSeparator(fsSep);
ds.setIncludes(Collections.singletonList(pattern));
return ds.scan();
}
/**
* Invoked on a local path in order to decide whether it should be sent as a file or as a directory
*
* @param session The client/server {@link Session} through which the transfer is being executed
* @param path The local {@link Path}
* @param options The {@link LinkOption}-s
* @return Whether to send the file as a regular one - <B>Note:</B> if {@code false} then the
* {@link #sendAsDirectory(Session, Path, LinkOption...)} is consulted.
* @throws IOException If failed to decide
*/
default boolean sendAsRegularFile(Session session, Path path, LinkOption... options)
throws IOException {
return Files.isRegularFile(path, options);
}
/**
* Invoked on a local path in order to decide whether it should be sent as a file or as a directory
*
* @param session The client/server {@link Session} through which the transfer is being executed
* @param path The local {@link Path}
* @param options The {@link LinkOption}-s
* @return Whether to send the file as a directory - <B>Note:</B> if {@code true} then
* {@link #getLocalFolderChildren(Session, Path)} is consulted
* @throws IOException If failed to decide
*/
default boolean sendAsDirectory(Session session, Path path, LinkOption... options)
throws IOException {
return Files.isDirectory(path, options);
}
/**
* Invoked when required to send all children of a local directory
*
* @param session The client/server {@link Session} through which the transfer is being executed
* @param path The local folder {@link Path}
* @return The {@link DirectoryStream} of children to send - <B>Note:</B> for each child the decision
* whether to send it as a file or a directory will be reached by consulting the respective
* {@link #sendAsRegularFile(Session, Path, LinkOption...) sendAsRegularFile} and
* {@link #sendAsDirectory(Session, Path, LinkOption...) sendAsDirectory} methods
* @throws IOException If failed to provide the children stream
* @see #sendAsDirectory(Session, Path, LinkOption...) sendAsDirectory
*/
default DirectoryStream<Path> getLocalFolderChildren(Session session, Path path) throws IOException {
return Files.newDirectoryStream(path);
}
default BasicFileAttributes getLocalBasicFileAttributes(
Session session, Path path, LinkOption... options)
throws IOException {
BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class, options);
return view.readAttributes();
}
default Set<PosixFilePermission> getLocalFilePermissions(
Session session, Path path, LinkOption... options)
throws IOException {
return IoUtils.getPermissions(path, options);
}
/**
* @param session The client/server {@link Session} through which the transfer is being executed
* @param fileSystem The <U>local</U> {@link FileSystem} on which local file should reside
* @param commandPath The command path using the <U>local</U> file separator
* @return The resolved absolute and normalized local {@link Path}
* @throws IOException If failed to resolve the path
* @throws InvalidPathException If invalid local path value
*/
default Path resolveLocalPath(Session session, FileSystem fileSystem, String commandPath)
throws IOException, InvalidPathException {
String path = SelectorUtils.translateToLocalFileSystemPath(commandPath, File.separatorChar, fileSystem);
Path lcl = fileSystem.getPath(path);
Path abs = lcl.isAbsolute() ? lcl : lcl.toAbsolutePath();
return abs.normalize();
}
/**
* Invoked when a request to receive something is processed
*
* @param session The client/server {@link Session} through which the transfer is being executed
* @param path The local target {@link Path} of the request
* @param recursive Whether the request is recursive
* @param shouldBeDir Whether target path is expected to be a directory
* @param preserve Whether target path is expected to preserve attributes (permissions, times)
* @return The effective target path - default=same as input
* @throws IOException If failed to resolve target location
*/
default Path resolveIncomingReceiveLocation(
Session session, Path path, boolean recursive, boolean shouldBeDir, boolean preserve)
throws IOException {
if (!shouldBeDir) {
return path;
}
LinkOption[] options = IoUtils.getLinkOptions(true);
Boolean status = IoUtils.checkFileExists(path, options);
if (status == null) {
throw new SshException("Target directory " + path + " is most like inaccessible");
}
if (!status) {
throw new SshException("Target directory " + path + " does not exist");
}
if (!Files.isDirectory(path, options)) {
throw new SshException("Target directory " + path + " is not a directory");
}
return path;
}
/**
* Called when there is a candidate file/folder for sending
*
* @param session The client/server {@link Session} through which the transfer is being executed
* @param localPath The original file/folder {@link Path} for sending
* @param options The {@link LinkOption}-s to use for validation
* @return The effective outgoing file path (default=same as input)
* @throws IOException If failed to resolve
*/
default Path resolveOutgoingFilePath(
Session session, Path localPath, LinkOption... options)
throws IOException {
Boolean status = IoUtils.checkFileExists(localPath, options);
if (status == null) {
throw new AccessDeniedException("Send file existence status cannot be determined: " + localPath);
}
if (!status) {
throw new IOException(localPath + ": no such file or directory");
}
return localPath;
}
/**
* Create an input stream to read from a file
*
* @param session The {@link Session} requesting the access
* @param file The requested local file {@link Path}
* @param size The expected transfer bytes count
* @param permissions The requested file permissions
* @param options The {@link OpenOption}s - may be {@code null}/empty
* @return The open {@link InputStream} never {@code null}
* @throws IOException If failed to open the file
*/
InputStream openRead(
Session session, Path file, long size, Set<PosixFilePermission> permissions, OpenOption... options)
throws IOException;
/**
* Called when the stream obtained from {@link #openRead(Session, Path, long, Set, OpenOption...) openRead} is no
* longer required since data has been successfully copied.
*
* @param session The {@link Session} requesting the access
* @param file The requested local file {@link Path}
* @param size The expected transfer bytes count
* @param permissions The requested file permissions
* @param stream The {@link InputStream} to close
* @throws IOException If failed to close the stream - <B>Note:</B> stream will be closed regardless of whether this
* method throws an exception or not.
*/
default void closeRead(
Session session, Path file, long size, Set<PosixFilePermission> permissions, InputStream stream)
throws IOException {
if (stream != null) {
stream.close();
}
}
ScpSourceStreamResolver createScpSourceStreamResolver(Session session, Path path) throws IOException;
/**
* Create an output stream to write to a file
*
* @param session The {@link Session} requesting the access
* @param file The requested local file {@link Path}
* @param size The expected transfer byte count
* @param permissions The requested file permissions
* @param options The {@link OpenOption}s - may be {@code null}/empty
* @return The open {@link OutputStream} never {@code null}
* @throws IOException If failed to open the file
*/
OutputStream openWrite(
Session session, Path file, long size, Set<PosixFilePermission> permissions, OpenOption... options)
throws IOException;
/**
* Called when output stream obtained from {@link #openWrite(Session, Path, long, Set, OpenOption...) openWrite} is
* no longer needed since data copying has been successfully completed.
*
* @param session The {@link Session} requesting the access
* @param file The requested local file {@link Path}
* @param size The expected transfer byte count
* @param permissions The requested file permissions
* @param os The opened {@link OutputStream}
* @throws IOException If failed to close the stream - <B>Note:</B> stream will be closed regardless of whether this
* method throws an exception or not.
*/
default void closeWrite(
Session session, Path file, long size, Set<PosixFilePermission> permissions, OutputStream os)
throws IOException {
if (os != null) {
os.close();
}
}
ScpTargetStreamResolver createScpTargetStreamResolver(Session session, Path path) throws IOException;
static void updateFileProperties(Path file, Set<PosixFilePermission> perms, ScpTimestampCommandDetails time)
throws IOException {
IoUtils.setPermissions(file, perms);
if (time != null) {
BasicFileAttributeView view = Files.getFileAttributeView(file, BasicFileAttributeView.class);
FileTime lastModified = FileTime.from(time.getLastModifiedTime(), TimeUnit.MILLISECONDS);
FileTime lastAccess = FileTime.from(time.getLastAccessTime(), TimeUnit.MILLISECONDS);
view.setTimes(lastModified, lastAccess, null);
}
}
}