Is there an easy way to check if there is a change in the SFTP server?

My goal is to poll the SFTP server for changes. My first thought is to check if the number of files in the directory has changed. Then maybe some additional checks for directory changes.

I am currently using the following:

try {
        FileSystemOptions opts = new FileSystemOptions();
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
        SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
        SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 60000);

        FileSystemManager manager = VFS.getManager();
        FileObject remoteFile = manager.resolveFile(SFTP_URL, opts);

        FileObject[] fileObjects = remoteFile.getChildren();
        System.out.println(DateTime.now() + " --> total number of files: " + Objects.length);
        for (FileObject fileObject : fileObjects) {
            if (fileObject.getName().getBaseName().startsWith("zzzz")) {
                System.out.println("found one: " + Object.getName().getBaseName());
            }
        }
} catch (Exception e) {
        e.printStackTrace();
}

      

This is using apache commons vfs2 2.2.0. It works "fine", but when there are too many files on the server it takes a few minutes to get the bill (currently it takes 2 minutes to get the bill for a server with ~ 10k files). Any way to get the invoice or other changes on the server faster?

+3


source to share


2 answers


Unfortunately, there is no easy way to get changes in the SFTP protocol. If a daemon can be running on the server, or if the source of new files can create / update an auxiliary file, it is possible to create such a file with the last modification time in its name or content.



+2


source


I know the SFTP protocol well, having developed commercial SFTP clients and SFTP server ( CompleteFTP ), and as far as I don’t know, there is no way in the protocol to get the number of files in a directory without listing it. Some servers, like ours, provide ways to add custom commands to the servers that you can call from the client, so a custom command could be added that returns the number of files in a directory. CompleteFTP also allows you to write custom filesystems so you can write them that only display files that have changed after a given timestamp when you do the listing, which may be different. Our server only runs on Windows, so this might be a show stopper for you.



+1


source







All Articles