Working code.

pull/1/head
Ivan Olexyn 5 years ago
parent 8bc4b0a108
commit 7322c50148

@ -1,8 +0,0 @@
package com.olexyn;
public class Main {
public static void main(String[] args) {
// write your code here
}
}

@ -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 <i>TwoBr</i> class, containing two BufferedReaders,
* <i>output</i> and <i>error</i>
* @see <i>output</i> BufferedReader, corresponds to STDOUT
* <i>error</i> 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<String[]> batch){
for (int i =0; i<batch.size();i++){
execute(batch.get(i));
}
}
public class TwoBr {
public BufferedReader output;
public BufferedReader error;
}
}

@ -0,0 +1,24 @@
package com.olexyn.ensync;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String conf = new Tools().getConf();
List<String[]> cmdBuffer = new Routines().parseConfToCmdBuffer(conf);
System.out.println("bar");
new Execute().executeBatch(cmdBuffer);
String br = null;
}
}

@ -0,0 +1,33 @@
### Table of Contents
1. [About](#about)
4. [Package Contents](#package-contents)
5. [Issues](#issues)
<br>
<br>
### About <a name="about"></a>
Sync files across directories:
* with rsync
* using config file: sync.conf
* at system start-up (TODO)
<br>
<br>
### Package Contents <a name="package-contents"></a>
| Class | Description |
|---------------|-------------|
| Execute | Issues shell commands.|
| Main | Main class. Run from here.|
| Routines | Contains higher level routines.|
| Tools | Simple tools used by other classes.|
<br>
<br>
### Issues <a name="issues"></a>
- What about parallel Threads?
- What about error handling? (i.e. if a wed-directory is not aviable)

@ -0,0 +1,51 @@
package com.olexyn.ensync;
import java.util.ArrayList;
import java.util.List;
public class Routines {
public List<String[]> parseConfToCmdBuffer(String conf) {
List<String[]> 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;
}
}

@ -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();
}
}

@ -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
Loading…
Cancel
Save