Sunday, September 26, 2010

Two way sync

Two way sync
This time I have attempted to solve another important task - Backup.

Everyone understands the importance of taking backup of personal files and it is difficult to find a program which does this job smartly. I googled and explored a few which don't suite my requirements, so I made a small attempt here to write a program myself than using third party tools.

As part of this, first I started with two way sync. This code generates the list of missed files in both directories by walking through and sync in both the ways.

The following code uses the power of new NIO2 from JDK7. To run this code you need JDK7


import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.*;
import java.util.concurrent.locks.*;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.Map.Entry;
import static java.nio.file.FileVisitResult.*;
import static java.nio.file.StandardCopyOption.*;

public class AstuteSync {
    private final static Lock lock = new ReentrantLock();
    private static Map<String,String> diffList = new ConcurrentSkipListMap<String,String>();
    private final static String SOURCE = "/extra/movies";
    private final static String TARGET = "/extra/movies_back";
    static class SyncFileVisitor extends SimpleFileVisitor<Path> {
        private String head;
        public SyncFileVisitor(String head) {
            this.head = head.endsWith(System.getProperty("file.separator"))
                    ? head.substring(0, head.length() - 1) : head;
        }
        @Override
        public FileVisitResult preVisitDirectory(Path dir) {
            toList(dir);
            return CONTINUE;
        }
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            toList(file);
            return CONTINUE;
        }
        private void toList(Path file) {
            try {
                lock.lock();
                if(diffList.remove(file.toString().replace(head, "")) == null)
                      diffList.put(file.toString().replace(head, ""),file.toString());
                lock.unlock();
            } catch (Exception exc) { exc.printStackTrace(); }
        }
    }
    public static void printDiffList() {
        if (diffList.isEmpty()) {
            System.out.println("Both folders are in Sync.");
        } else {
            Iterator<String> ite = diffList.values().iterator();
            System.out.println("Folder Diff:");
            while (ite.hasNext()) System.out.println(ite.next());
        }
    }
    public static void sync() {
        Set<Entry<String,String>>entries = diffList.entrySet();
        for (Map.Entry<String,String>entry : entries) {
            try {
                Paths.get(entry.getValue()).copyTo(Paths.get(
                        (entry.getValue().startsWith(TARGET) ? SOURCE : TARGET) +
                        System.getProperty("file.separator") + entry.getKey()), COPY_ATTRIBUTES);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    public static void main(String... arg) throws Exception {
        final Path source = Paths.get(SOURCE);
        final Path target = Paths.get(TARGET);
        if(!source.exists() || !target.exists()) throw new Exception("Path doesn't exists");
        new Thread(new Runnable(){ public void run() {
        Files.walkFileTree(source, new SyncFileVisitor(SOURCE));}}).start();
        new Thread(new Runnable() {public void run() {
        Files.walkFileTree(target, new SyncFileVisitor(TARGET));}}).start();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override   public void run() {
                printDiffList();
                if (!diffList.isEmpty())  sync();
            }
        });
    }
}
$ java AstuteSync
/extra/movies/k.txt
/extra/movies/p.txt
/extra/movies_back/test.t

$java AstuteSync
Both folders are in Sync.