开发者

Apache vfs: fetch latest changed file of a directory (sftp)

I am trying to pull the latest file out of a directory, which is located on a sftp server. The way I do it right now is more or less:

public FileObject getLatestFile(String directory) throws FileSystemException {
    FileObject fo = fsManager.resolveFile(this.host+directory, fsOptions);
    FileObject latestFile = n开发者_如何学Pythonull;
    long max  = 0;
    fo.getContent().
    for (FileObject fob : fo.getChildren()){
        if (fob.getContent().getLastModifiedTime() > max) {
            max = fob.getContent().getLastModifiedTime();
            latestFile = fob;
        }
    }
    return latestFile;
}

The problem with this approach is that I am basically downloading every file in the given directory, everytime the method is called.

Is there any better way to do this?


You are not downloading the content.

If you look in the source code:

/**
 * Returns the file's content.
 */
public FileContent getContent() throws FileSystemException
{
    synchronized (fs)
    {
        attach();
        if (content == null)
        {
            content = new DefaultFileContent(this, getFileContentInfoFactory());
        }
        return content;
    }
}

calling getContent just returns an object implementation and getting attributes like size, modified dates basically it is extracted when exploring the remote folder(every protocol is different, but for example when you list an FTP folder you get all the files attributes).

For SFTP this what you actually call :

protected long doGetLastModifiedTime() throws Exception
{
    if (attrs == null
            || (attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_ACMODTIME) == 0)
    {
        throw new FileSystemException(
                "vfs.provider.sftp/unknown-modtime.error");
    }
    return attrs.getMTime() * 1000L;
}

I agree, naming is confusing and it implies that content is retrieved when getContent is invoked but actually is not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜