View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.sftp;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.Vector;
25  
26  import org.apache.commons.vfs2.FileNotFoundException;
27  import org.apache.commons.vfs2.FileObject;
28  import org.apache.commons.vfs2.FileSystemException;
29  import org.apache.commons.vfs2.FileType;
30  import org.apache.commons.vfs2.NameScope;
31  import org.apache.commons.vfs2.RandomAccessContent;
32  import org.apache.commons.vfs2.VFS;
33  import org.apache.commons.vfs2.provider.AbstractFileName;
34  import org.apache.commons.vfs2.provider.AbstractFileObject;
35  import org.apache.commons.vfs2.provider.UriParser;
36  import org.apache.commons.vfs2.util.FileObjectUtils;
37  import org.apache.commons.vfs2.util.MonitorInputStream;
38  import org.apache.commons.vfs2.util.MonitorOutputStream;
39  import org.apache.commons.vfs2.util.PosixPermissions;
40  import org.apache.commons.vfs2.util.RandomAccessMode;
41  
42  import com.jcraft.jsch.ChannelSftp;
43  import com.jcraft.jsch.ChannelSftp.LsEntry;
44  import com.jcraft.jsch.SftpATTRS;
45  import com.jcraft.jsch.SftpException;
46  
47  /**
48   * An SFTP file.
49   */
50  public class SftpFileObject extends AbstractFileObject<SftpFileSystem> {
51  
52      private static final long MOD_TIME_FACTOR = 1000L;
53  
54      private SftpATTRS attrs;
55      private final String relPath;
56  
57      private boolean inRefresh;
58  
59      protected SftpFileObject(final AbstractFileName name, final SftpFileSystem fileSystem) throws FileSystemException {
60          super(name, fileSystem);
61          relPath = UriParser.decode(fileSystem.getRootName().getRelativeName(name));
62      }
63  
64      /** @since 2.0 */
65      @Override
66      protected void doDetach() throws Exception {
67          attrs = null;
68      }
69  
70      /**
71       * Determines the type of this file, returns null if the file does not exist.
72       */
73      @Override
74      protected FileType doGetType() throws Exception {
75          if (attrs == null) {
76              statSelf();
77          }
78  
79          if (attrs == null) {
80              return FileType.IMAGINARY;
81          }
82  
83          if ((attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS) == 0) {
84              throw new FileSystemException("vfs.provider.sftp/unknown-permissions.error");
85          }
86          if (attrs.isDir()) {
87              return FileType.FOLDER;
88          }
89          return FileType.FILE;
90      }
91  
92      /**
93       * Called when the type or content of this file changes.
94       */
95      @Override
96      protected void onChange() throws Exception {
97          statSelf();
98      }
99  
100     /**
101      * Fetches file attributes from server.
102      *
103      * @throws IOException
104      */
105     private void statSelf() throws IOException {
106         ChannelSftp channel = getAbstractFileSystem().getChannel();
107         try {
108             setStat(channel.stat(relPath));
109         } catch (final SftpException e) {
110             try {
111                 // maybe the channel has some problems, so recreate the channel and retry
112                 if (e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE) {
113                     channel.disconnect();
114                     channel = getAbstractFileSystem().getChannel();
115                     setStat(channel.stat(relPath));
116                 } else {
117                     // Really does not exist
118                     attrs = null;
119                 }
120             } catch (final SftpException innerEx) {
121                 // TODO - not strictly true, but jsch 0.1.2 does not give us
122                 // enough info in the exception. Should be using:
123                 // if ( e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE )
124                 // However, sometimes the exception has the correct id, and
125                 // sometimes
126                 // it does not. Need to look into why.
127 
128                 // Does not exist
129                 attrs = null;
130             }
131         } finally {
132             getAbstractFileSystem().putChannel(channel);
133         }
134     }
135 
136     /**
137      * Sets attrs from listChildrenResolved
138      */
139     private void setStat(final SftpATTRS attrs) {
140         this.attrs = attrs;
141     }
142 
143     /**
144      * Creates this file as a folder.
145      */
146     @Override
147     protected void doCreateFolder() throws Exception {
148         final ChannelSftp channel = getAbstractFileSystem().getChannel();
149         try {
150             channel.mkdir(relPath);
151         } finally {
152             getAbstractFileSystem().putChannel(channel);
153         }
154     }
155 
156     @Override
157     protected long doGetLastModifiedTime() throws Exception {
158         if (attrs == null || (attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_ACMODTIME) == 0) {
159             throw new FileSystemException("vfs.provider.sftp/unknown-modtime.error");
160         }
161         return attrs.getMTime() * MOD_TIME_FACTOR;
162     }
163 
164     /**
165      * Sets the last modified time of this file. Is only called if {@link #doGetType} does not return
166      * {@link FileType#IMAGINARY}.
167      *
168      * @param modtime is modification time in milliseconds. SFTP protocol can send times with nanosecond precision but
169      *            at the moment jsch send them with second precision.
170      */
171     @Override
172     protected boolean doSetLastModifiedTime(final long modtime) throws Exception {
173         final int newMTime = (int) (modtime / MOD_TIME_FACTOR);
174         attrs.setACMODTIME(attrs.getATime(), newMTime);
175         flushStat();
176         return true;
177     }
178 
179     private void flushStat() throws IOException, SftpException {
180         final ChannelSftp channel = getAbstractFileSystem().getChannel();
181         try {
182             channel.setStat(relPath, attrs);
183         } finally {
184             getAbstractFileSystem().putChannel(channel);
185         }
186     }
187 
188     /**
189      * Deletes the file.
190      */
191     @Override
192     protected void doDelete() throws Exception {
193         final ChannelSftp channel = getAbstractFileSystem().getChannel();
194         try {
195             if (isFile()) {
196                 channel.rm(relPath);
197             } else {
198                 channel.rmdir(relPath);
199             }
200         } finally {
201             getAbstractFileSystem().putChannel(channel);
202         }
203     }
204 
205     /**
206      * Renames the file.
207      */
208     @Override
209     protected void doRename(final FileObject newFile) throws Exception {
210         final ChannelSftp channel = getAbstractFileSystem().getChannel();
211         try {
212             final SftpFileObjectrg/apache/commons/vfs2/provider/sftp/SftpFileObject.html#SftpFileObject">SftpFileObject newSftpFileObject = (SftpFileObject) FileObjectUtils.getAbstractFileObject(newFile);
213             channel.rename(relPath, newSftpFileObject.relPath);
214         } finally {
215             getAbstractFileSystem().putChannel(channel);
216         }
217     }
218 
219     /**
220      * Returns the POSIX type permissions of the file.
221      *
222      * @param checkIds {@code true} if user and group ID should be checked (needed for some access rights checks)
223      * @return A PosixPermission object
224      * @throws Exception If an error occurs
225      * @since 2.1
226      */
227     protected PosixPermissions getPermissions(final boolean checkIds) throws Exception {
228         statSelf();
229         boolean isInGroup = false;
230         if (checkIds) {
231             for (final int groupId : getAbstractFileSystem().getGroupsIds()) {
232                 if (groupId == attrs.getGId()) {
233                     isInGroup = true;
234                     break;
235                 }
236             }
237         }
238         final boolean isOwner = checkIds ? attrs.getUId() == getAbstractFileSystem().getUId() : false;
239         return new PosixPermissions(attrs.getPermissions(), isOwner, isInGroup);
240     }
241 
242     @Override
243     protected boolean doIsReadable() throws Exception {
244         return getPermissions(true).isReadable();
245     }
246 
247     @Override
248     protected boolean doSetReadable(final boolean readable, final boolean ownerOnly) throws Exception {
249         final PosixPermissions permissions = getPermissions(false);
250         final int newPermissions = permissions.makeReadable(readable, ownerOnly);
251         if (newPermissions == permissions.getPermissions()) {
252             return true;
253         }
254 
255         attrs.setPERMISSIONS(newPermissions);
256         flushStat();
257 
258         return true;
259     }
260 
261     @Override
262     protected boolean doIsWriteable() throws Exception {
263         return getPermissions(true).isWritable();
264     }
265 
266     @Override
267     protected boolean doSetWritable(final boolean writable, final boolean ownerOnly) throws Exception {
268         final PosixPermissions permissions = getPermissions(false);
269         final int newPermissions = permissions.makeWritable(writable, ownerOnly);
270         if (newPermissions == permissions.getPermissions()) {
271             return true;
272         }
273 
274         attrs.setPERMISSIONS(newPermissions);
275         flushStat();
276 
277         return true;
278     }
279 
280     @Override
281     protected boolean doIsExecutable() throws Exception {
282         return getPermissions(true).isExecutable();
283     }
284 
285     @Override
286     protected boolean doSetExecutable(final boolean executable, final boolean ownerOnly) throws Exception {
287         final PosixPermissions permissions = getPermissions(false);
288         final int newPermissions = permissions.makeExecutable(executable, ownerOnly);
289         if (newPermissions == permissions.getPermissions()) {
290             return true;
291         }
292 
293         attrs.setPERMISSIONS(newPermissions);
294         flushStat();
295 
296         return true;
297     }
298 
299     /**
300      * Lists the children of this file.
301      */
302     @Override
303     protected FileObject[] doListChildrenResolved() throws Exception {
304         // should not require a round-trip because type is already set.
305         if (this.isFile()) {
306             return null;
307         }
308         // List the contents of the folder
309         Vector<?> vector = null;
310         final ChannelSftp channel = getAbstractFileSystem().getChannel();
311 
312         try {
313             // try the direct way to list the directory on the server to avoid too many roundtrips
314             vector = channel.ls(relPath);
315         } catch (final SftpException e) {
316             String workingDirectory = null;
317             try {
318                 if (relPath != null) {
319                     workingDirectory = channel.pwd();
320                     channel.cd(relPath);
321                 }
322             } catch (final SftpException ex) {
323                 // VFS-210: seems not to be a directory
324                 return null;
325             }
326 
327             SftpException lsEx = null;
328             try {
329                 vector = channel.ls(".");
330             } catch (final SftpException ex) {
331                 lsEx = ex;
332             } finally {
333                 try {
334                     if (relPath != null) {
335                         channel.cd(workingDirectory);
336                     }
337                 } catch (final SftpException xe) {
338                     throw new FileSystemException("vfs.provider.sftp/change-work-directory-back.error",
339                             workingDirectory, lsEx);
340                 }
341             }
342 
343             if (lsEx != null) {
344                 throw lsEx;
345             }
346         } finally {
347             getAbstractFileSystem().putChannel(channel);
348         }
349         FileSystemException.requireNonNull(vector, "vfs.provider.sftp/list-children.error");
350 
351         // Extract the child names
352         final ArrayList<FileObject> children = new ArrayList<>();
353         for (@SuppressWarnings("unchecked") // OK because ChannelSftp.ls() is documented to return Vector<LsEntry>
354         final Iterator<LsEntry> iterator = (Iterator<LsEntry>) vector.iterator(); iterator.hasNext();) {
355             final LsEntry stat = iterator.next();
356 
357             String name = stat.getFilename();
358             if (VFS.isUriStyle() && stat.getAttrs().isDir() && name.charAt(name.length() - 1) != '/') {
359                 name = name + "/";
360             }
361 
362             if (name.equals(".") || name.equals("..") || name.equals("./") || name.equals("../")) {
363                 continue;
364             }
365 
366             final FileObject fo = getFileSystem().resolveFile(getFileSystem().getFileSystemManager()
367                     .resolveName(getName(), UriParser.encode(name), NameScope.CHILD));
368 
369             ((SftpFileObject) FileObjectUtils.getAbstractFileObject(fo)).setStat(stat.getAttrs());
370 
371             children.add(fo);
372         }
373 
374         return children.toArray(new FileObject[children.size()]);
375     }
376 
377     /**
378      * Lists the children of this file.
379      */
380     @Override
381     protected String[] doListChildren() throws Exception {
382         // use doListChildrenResolved for performance
383         return null;
384     }
385 
386     /**
387      * Returns the size of the file content (in bytes).
388      */
389     @Override
390     protected long doGetContentSize() throws Exception {
391         if (attrs == null || (attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_SIZE) == 0) {
392             throw new FileSystemException("vfs.provider.sftp/unknown-size.error");
393         }
394         return attrs.getSize();
395     }
396 
397     @Override
398     protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
399         return new SftpRandomAccessContent(this, mode);
400     }
401 
402     /**
403      * Creates an input stream to read the file content from. The input stream is starting at the given position in the
404      * file.
405      */
406     InputStream getInputStream(final long filePointer) throws IOException {
407         final ChannelSftp channel = getAbstractFileSystem().getChannel();
408         // Using InputStream directly from the channel
409         // is much faster than the memory method.
410         try {
411             return new SftpInputStream(channel, channel.get(getName().getPathDecoded(), null, filePointer));
412         } catch (final SftpException e) {
413             getAbstractFileSystem().putChannel(channel);
414             throw new FileSystemException(e);
415         }
416     }
417 
418     /**
419      * Creates an input stream to read the file content from.
420      */
421     @Override
422     protected InputStream doGetInputStream() throws Exception {
423         // VFS-113: avoid npe
424         synchronized (getAbstractFileSystem()) {
425             final ChannelSftp channel = getAbstractFileSystem().getChannel();
426             try {
427                 // return channel.get(getName().getPath());
428                 // hmmm - using the in memory method is soooo much faster ...
429 
430                 // TODO - Don't read the entire file into memory. Use the
431                 // stream-based methods on ChannelSftp once they work properly
432 
433                 /*
434                  * final ByteArrayOutputStream outstr = new ByteArrayOutputStream(); channel.get(relPath, outstr);
435                  * outstr.close(); return new ByteArrayInputStream(outstr.toByteArray());
436                  */
437 
438                 InputStream is;
439                 try {
440                     // VFS-210: sftp allows to gather an input stream even from a directory and will
441                     // fail on first read. So we need to check the type anyway
442                     if (!getType().hasContent()) {
443                         throw new FileSystemException("vfs.provider/read-not-file.error", getName());
444                     }
445 
446                     is = channel.get(relPath);
447                 } catch (final SftpException e) {
448                     if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
449                         throw new FileNotFoundException(getName());
450                     }
451 
452                     throw new FileSystemException(e);
453                 }
454 
455                 return new SftpInputStream(channel, is);
456 
457             } finally {
458                 // getAbstractFileSystem().putChannel(channel);
459             }
460         }
461     }
462 
463     /**
464      * Creates an output stream to write the file content to.
465      */
466     @Override
467     protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception {
468         // TODO - Don't write the entire file into memory. Use the stream-based
469         // methods on ChannelSftp once the work properly
470         /*
471          * final ChannelSftp channel = getAbstractFileSystem().getChannel(); return new SftpOutputStream(channel);
472          */
473 
474         final ChannelSftp channel = getAbstractFileSystem().getChannel();
475         return new SftpOutputStream(channel, channel.put(relPath, bAppend ? ChannelSftp.APPEND : ChannelSftp.OVERWRITE));
476     }
477 
478     /**
479      * An InputStream that monitors for end-of-file.
480      */
481     private class SftpInputStream extends MonitorInputStream {
482         private final ChannelSftp channel;
483 
484         public SftpInputStream(final ChannelSftp channel, final InputStream in) {
485             super(in);
486             this.channel = channel;
487         }
488 
489         /**
490          * Called after the stream has been closed.
491          */
492         @Override
493         protected void onClose() throws IOException {
494             getAbstractFileSystem().putChannel(channel);
495         }
496     }
497 
498     /**
499      * An OutputStream that wraps an sftp OutputStream, and closes the channel when the stream is closed.
500      */
501     private class SftpOutputStream extends MonitorOutputStream {
502         private final ChannelSftp channel;
503 
504         public SftpOutputStream(final ChannelSftp channel, final OutputStream out) {
505             super(out);
506             this.channel = channel;
507         }
508 
509         /**
510          * Called after this stream is closed.
511          */
512         @Override
513         protected void onClose() throws IOException {
514             getAbstractFileSystem().putChannel(channel);
515         }
516     }
517 
518 }