All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.duracloud.sync.walker.RestartDirWalker Maven / Gradle / Ivy

There is a newer version: 8.1.0
Show newest version
/*
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 *     http://duracloud.org/license/
 */
package org.duracloud.sync.walker;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Walks a set of directory trees just like a DirWalker, but only adds files
 * to the changed list if their modified date is more recent than the time of
 * the last backup. This provides a listing of files which have been added or
 * updated since the last backup.
 *
 * All files in directories which have changed are added to the changed list as
 * well in order to handle the possibility of directory names having been
 * changed.
 *
 * @author: Bill Branan
 * Date: Mar 24, 2010
 */
public class RestartDirWalker extends DirWalker {

    private long lastBackup;
    private List changedDirs;
    private File excludeFile;

    protected RestartDirWalker(List topDirs,
                               File excludeFile,
                               long lastBackup) {
        super(topDirs, excludeFile);
        this.excludeFile = excludeFile;
        this.lastBackup = lastBackup;
        changedDirs = new ArrayList();
    }

    @Override
    protected void walkDirs() {
        super.walkDirs();

        // Walk and add all files in directories which have changed
        if(changedDirs.size() > 0) {
            DirWalker dirWalker = new DirWalker(changedDirs, excludeFile);
            dirWalker.walkDirs();
        }
    }

    @Override
    protected void handleFile(File file, int depth, Collection results) {
        if(file.lastModified() > lastBackup) {
            super.handleFile(file, depth, results);
        }
    }

    @Override
    protected boolean handleDirectory(File directory,
                                      int depth,
                                      Collection results) {
        if(directory.lastModified() > lastBackup) {
            changedDirs.add(directory);
        }
        return true;
    }

    public static DirWalker start(List topDirs,
                                  File excludeFile,
                                  long lastBackup) {
        RestartDirWalker dirWalker =
            new RestartDirWalker(topDirs, excludeFile, lastBackup);
        (new Thread(dirWalker)).start();
        return dirWalker;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy