thanos panousis
thanos panousis

Reputation: 464

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 = null;
    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?

Upvotes: 2

Views: 3014

Answers (1)

adrian.tarau
adrian.tarau

Reputation: 3154

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.

Upvotes: 6

Related Questions