diff --git a/src/com/olexyn/Main.java b/src/com/olexyn/Main.java deleted file mode 100644 index c86cc13..0000000 --- a/src/com/olexyn/Main.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.olexyn; - -public class Main { - - public static void main(String[] args) { - // write your code here - } -} diff --git a/src/com/olexyn/ensync/Execute.java b/src/com/olexyn/ensync/Execute.java new file mode 100644 index 0000000..2937bf3 --- /dev/null +++ b/src/com/olexyn/ensync/Execute.java @@ -0,0 +1,45 @@ +package com.olexyn.ensync; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.List; + +public class Execute { + + + /** + * @param cmd an array representing a shell command + * @return TwoBr class, containing two BufferedReaders, + * output and error + * @see output BufferedReader, corresponds to STDOUT + * error BufferedReader, corresponds to STDERR + */ + public TwoBr execute(String cmd[]) { + TwoBr twobr = new TwoBr(); + try { + Process process = Runtime.getRuntime().exec(cmd); + process.waitFor(); + twobr.output = new BufferedReader(new InputStreamReader(process.getInputStream())); + twobr.error = new BufferedReader(new InputStreamReader(process.getErrorStream())); + } catch (Exception e) { + e.printStackTrace(); + } + return twobr; + } + + public void executeBatch(List batch){ + + for (int i =0; i cmdBuffer = new Routines().parseConfToCmdBuffer(conf); + + System.out.println("bar"); + new Execute().executeBatch(cmdBuffer); + + + String br = null; + + } +} diff --git a/src/com/olexyn/ensync/README.md b/src/com/olexyn/ensync/README.md new file mode 100644 index 0000000..cc1d099 --- /dev/null +++ b/src/com/olexyn/ensync/README.md @@ -0,0 +1,33 @@ +### Table of Contents +1. [About](#about) +4. [Package Contents](#package-contents) +5. [Issues](#issues) + +
+
+ +### About +Sync files across directories: +* with rsync +* using config file: sync.conf +* at system start-up (TODO) + +
+
+ +### Package Contents + +| Class | Description | +|---------------|-------------| +| Execute | Issues shell commands.| +| Main | Main class. Run from here.| +| Routines | Contains higher level routines.| +| Tools | Simple tools used by other classes.| + +
+
+ +### Issues + +- What about parallel Threads? +- What about error handling? (i.e. if a wed-directory is not aviable) \ No newline at end of file diff --git a/src/com/olexyn/ensync/Routines.java b/src/com/olexyn/ensync/Routines.java new file mode 100644 index 0000000..d495af1 --- /dev/null +++ b/src/com/olexyn/ensync/Routines.java @@ -0,0 +1,51 @@ +package com.olexyn.ensync; + +import java.util.ArrayList; +import java.util.List; + +public class Routines { + + + public List parseConfToCmdBuffer(String conf) { + + + List cmdBuffer = new ArrayList<>(); + + String[] confLines = conf.split("\n"); + for (int i = 0; i < confLines.length; i++) { + String line = confLines[i]; + if (!line.startsWith("#")) { + + if (line.contains("----")) { + // dirA <- urvtW ---- urvtW -> dirB + String dirA = line.split(" ---- ")[0].split(" <- ")[0]; + String optA = line.split(" ---- ")[0].split(" <- ")[1]; + String optB = line.split(" ---- ")[1].split(" -> ")[0]; + String dirB = line.split(" ---- ")[1].split(" -> ")[1]; + + cmdBuffer.add(new String[]{"rsync", + "-" + optA, + dirB, + dirA}); + cmdBuffer.add(new String[]{"rsync", + "-" + optB, + dirA, + dirB}); + } else if (line.contains("->")) { + // dirA -- urvtW -> dirB + String dirA = line.split(" -- ")[0]; + String optA = line.split(" -- ")[1].split(" -> ")[0]; + String dirB = line.split(" -> ")[1]; + + cmdBuffer.add(new String[]{"rsync", + "-" + optA, + dirA, + dirB}); + } + + + } + } + return cmdBuffer; + } +} \ No newline at end of file diff --git a/src/com/olexyn/ensync/Tools.java b/src/com/olexyn/ensync/Tools.java new file mode 100644 index 0000000..c77b774 --- /dev/null +++ b/src/com/olexyn/ensync/Tools.java @@ -0,0 +1,37 @@ +package com.olexyn.ensync; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Tools { + + private final Execute x; + + public Tools() { + x = new Execute(); + } + + + /** + */ + public void rsync(String param , String source , String destination) { + // + BufferedReader foo = x.execute(new String[]{"rsync", param, source, destination}).output; + } + + public String getConf(){ + BufferedReader output = x.execute(new String[]{"cat", System.getProperty("user.dir")+"/src/com/olexyn/ensync/sync.conf"}).output; + return brToString(output); + } + + public String brToString(BufferedReader br) { + StringBuilder sb = new StringBuilder(); + Object[] br_array = br.lines().toArray(); + for (int i = 0; i < br_array.length; i++) { + sb.append(br_array[i].toString() + "\n"); + } + return sb.toString(); + } +} diff --git a/src/com/olexyn/ensync/sync.conf b/src/com/olexyn/ensync/sync.conf new file mode 100644 index 0000000..b2aca4c --- /dev/null +++ b/src/com/olexyn/ensync/sync.conf @@ -0,0 +1,13 @@ +# -u update +# -r recursive +# -v verbose +# -t keep modification times +# -W whole file (faster if network bandwith is faster then disk bandwith) +# +# examples: +# ./dirA -- urvtW -> ./dirB +# ./dirA <- urvtW ---- urvtW -> ./dirB +/home/user/ws/idea/env-sync/src/com/olexyn/ensync/tmp/in/ <- urvtW ---- urvtW -> /home/user/ws/idea/env-sync/src/com/olexyn/ensync/tmp/out/ +# backup +# /home/user/ws/ -- urvtW -> /media/time/backup/auto/ws +/home/user/wiki/ -- urvtW -> /media/time/backup/auto/wiki \ No newline at end of file