+ switch to external logger

+ clean legacy code
pull/1/head
io42630 3 years ago
parent 9113eeebb9
commit b28155a752

@ -30,6 +30,16 @@ DataRoot a data root
<br> <br>
### Flow
* `for` each `SyncBundle`
* `for` each `SyncDirectory`
* check what files were CRUD,
* propagate to other `SyncDirectory` of current `SyncBundle`.
<br>
### Demo<a name="demo"></a> ### Demo<a name="demo"></a>
[![IMAGE ALT TEXT](http://img.youtube.com/vi/znR3jyM_4Ss/0.jpg)](https://youtu.be/znR3jyM_4Ss "ensync WIP Demo") [![IMAGE ALT TEXT](http://img.youtube.com/vi/znR3jyM_4Ss/0.jpg)](https://youtu.be/znR3jyM_4Ss "ensync WIP Demo")

@ -1,150 +1,145 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <modelVersion>4.0.0</modelVersion>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <groupId>com.olexyn.ensync</groupId>
<modelVersion>4.0.0</modelVersion> <artifactId>ensync</artifactId>
<version>0.1</version>
<groupId>com.olexyn.ensync</groupId> <name>ensync</name>
<artifactId>ensync</artifactId> <properties>
<version>0.1</version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vaadin.version>7.3.1</vaadin.version>
<name>ensync</name> <vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version>
<url>http://www.example.com</url> <failOnMissingWebXml>false</failOnMissingWebXml>
<maven.compiler.source>18</maven.compiler.source>
<properties> <maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <kotlin.version>1.6.20-RC2</kotlin.version>
<vaadin.version>7.3.1</vaadin.version> </properties>
<vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version> <dependencies>
<failOnMissingWebXml>false</failOnMissingWebXml> <dependency>
<maven.compiler.source>1.11</maven.compiler.source> <groupId>junit</groupId>
<maven.compiler.target>1.11</maven.compiler.target> <artifactId>junit</artifactId>
<kotlin.version>1.6.20-RC2</kotlin.version> <version>4.11</version>
</properties> <scope>test</scope>
</dependency>
<dependencies> <dependency>
<dependency> <groupId>org.json</groupId>
<groupId>junit</groupId> <artifactId>json</artifactId>
<artifactId>junit</artifactId> <version>20190722</version>
<version>4.11</version> </dependency>
<scope>test</scope> <dependency>
</dependency> <groupId>org.jetbrains.kotlin</groupId>
<dependency> <artifactId>kotlin-stdlib-jdk8</artifactId>
<groupId>org.json</groupId> <version>${kotlin.version}</version>
<artifactId>json</artifactId> </dependency>
<version>20190722</version> <dependency>
</dependency> <groupId>org.jetbrains.kotlin</groupId>
<dependency> <artifactId>kotlin-test</artifactId>
<groupId>org.jetbrains.kotlin</groupId> <version>${kotlin.version}</version>
<artifactId>kotlin-stdlib-jdk8</artifactId> <scope>test</scope>
<version>${kotlin.version}</version> </dependency>
</dependency> <dependency>
<dependency> <groupId>org.jetbrains.kotlin</groupId>
<groupId>org.jetbrains.kotlin</groupId> <artifactId>kotlin-stdlib-jdk8</artifactId>
<artifactId>kotlin-test</artifactId> <version>${kotlin.version}</version>
<version>${kotlin.version}</version> </dependency>
<scope>test</scope> <dependency>
</dependency> <groupId>commons-io</groupId>
<dependency> <artifactId>commons-io</artifactId>
<groupId>org.jetbrains.kotlin</groupId> <version>2.11.0</version>
<artifactId>kotlin-stdlib-jdk8</artifactId> </dependency>
<version>${kotlin.version}</version> <dependency>
</dependency> <groupId>com.olexyn.min.log</groupId>
<dependency> <artifactId>min-log</artifactId>
<groupId>commons-io</groupId> <version>0.1.1</version>
<artifactId>commons-io</artifactId> <scope>compile</scope>
<version>2.11.0</version> </dependency>
</dependency> </dependencies>
</dependencies> <build>
<pluginManagement>
<build> <plugins>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugin>
<plugins> <artifactId>maven-clean-plugin</artifactId>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> <version>3.1.0</version>
<plugin> </plugin>
<artifactId>maven-clean-plugin</artifactId> <plugin>
<version>3.1.0</version> <artifactId>maven-resources-plugin</artifactId>
</plugin> <version>3.0.2</version>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> </plugin>
<plugin> <plugin>
<artifactId>maven-resources-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.0.2</version> <version>3.8.0</version>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>3.8.0</version> <version>2.22.1</version>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-jar-plugin</artifactId>
<version>2.22.1</version> <version>3.0.2</version>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-jar-plugin</artifactId> <artifactId>maven-install-plugin</artifactId>
<version>3.0.2</version> <version>2.5.2</version>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-install-plugin</artifactId> <artifactId>maven-deploy-plugin</artifactId>
<version>2.5.2</version> <version>2.8.2</version>
</plugin> </plugin>
<plugin> <plugin>
<artifactId>maven-deploy-plugin</artifactId> <artifactId>maven-site-plugin</artifactId>
<version>2.8.2</version> <version>3.7.1</version>
</plugin> </plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> <plugin>
<plugin> <artifactId>maven-project-info-reports-plugin</artifactId>
<artifactId>maven-site-plugin</artifactId> <version>3.0.0</version>
<version>3.7.1</version> </plugin>
</plugin> </plugins>
<plugin> </pluginManagement>
<artifactId>maven-project-info-reports-plugin</artifactId> <plugins>
<version>3.0.0</version> <plugin>
</plugin> <groupId>org.jetbrains.kotlin</groupId>
</plugins> <artifactId>kotlin-maven-plugin</artifactId>
</pluginManagement> <version>${kotlin.version}</version>
<plugins> <executions>
<plugin> <execution>
<groupId>org.jetbrains.kotlin</groupId> <id>compile</id>
<artifactId>kotlin-maven-plugin</artifactId> <phase>compile</phase>
<version>${kotlin.version}</version> <goals>
<executions> <goal>compile</goal>
<execution> </goals>
<id>compile</id> </execution>
<phase>compile</phase> <execution>
<goals> <id>test-compile</id>
<goal>compile</goal> <phase>test-compile</phase>
</goals> <goals>
</execution> <goal>test-compile</goal>
<execution> </goals>
<id>test-compile</id> </execution>
<phase>test-compile</phase> </executions>
<goals> <configuration>
<goal>test-compile</goal> <jvmTarget>18</jvmTarget>
</goals> </configuration>
</execution> </plugin>
</executions> <plugin>
<configuration> <groupId>org.apache.maven.plugins</groupId>
<jvmTarget>1.8</jvmTarget> <artifactId>maven-compiler-plugin</artifactId>
</configuration> <executions>
</plugin> <execution>
<plugin> <id>compile</id>
<groupId>org.apache.maven.plugins</groupId> <phase>compile</phase>
<artifactId>maven-compiler-plugin</artifactId> <goals>
<executions> <goal>compile</goal>
<execution> </goals>
<id>compile</id> </execution>
<phase>compile</phase> <execution>
<goals> <id>testCompile</id>
<goal>compile</goal> <phase>test-compile</phase>
</goals> <goals>
</execution> <goal>testCompile</goal>
<execution> </goals>
<id>testCompile</id> </execution>
<phase>test-compile</phase> </executions>
<goals> </plugin>
<goal>testCompile</goal> </plugins>
</goals> </build>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> </project>

@ -1,54 +0,0 @@
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 TwoBr execute(List<String> cmd) {
String[] cmdArr = new String[cmd.size()];
for (int i = 0; i < cmd.size(); i++) {
cmdArr[i] = cmd.get(i);
}
return execute(cmdArr);
}
public void executeBatch(List<String[]> batch) {
for (String[] strings : batch) {
execute(strings);
}
}
public class TwoBr {
public BufferedReader output;
public BufferedReader error;
}
}

@ -4,21 +4,19 @@ import com.olexyn.ensync.artifacts.DataRoot;
import com.olexyn.ensync.artifacts.Record; import com.olexyn.ensync.artifacts.Record;
import com.olexyn.ensync.artifacts.SyncDirectory; import com.olexyn.ensync.artifacts.SyncDirectory;
import com.olexyn.ensync.lock.LockKeeper; import com.olexyn.ensync.lock.LockKeeper;
import com.olexyn.min.log.LogU;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Flow implements Runnable { public class Flow implements Runnable {
private static final Logger LOGGER = LogUtil.get(Flow.class);
public static final long POLLING_PAUSE = 100; public static final long POLLING_PAUSE = 100;
private final AtomicBoolean running = new AtomicBoolean(false); private final AtomicBoolean running = new AtomicBoolean(false);
public void start() { public void start() {
LOGGER.info("START Flow."); LogU.infoStart("Flow");
Thread worker = new Thread(this, "FLOW_WORKER"); Thread worker = new Thread(this, "FLOW_WORKER");
worker.start(); worker.start();
} }
@ -48,10 +46,10 @@ public class Flow implements Runnable {
); );
} }
try { try {
LOGGER.info("Sleeping... for " + POLLING_PAUSE + "ms."); LogU.infoPlain("Sleeping... for %sms", POLLING_PAUSE);
Thread.sleep(POLLING_PAUSE); Thread.sleep(POLLING_PAUSE);
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
LOGGER.info("Thread interrupted."); LogU.warnPlain("Thread interrupted.");
} }
} }
} }
@ -60,19 +58,21 @@ public class Flow implements Runnable {
* *
*/ */
private void sync(SyncDirectory sDir) { private void sync(SyncDirectory sDir) {
LOGGER.info("DO SYNC " + sDir.directoryPath); LogU.infoPlain("DO SYNC %s", sDir.directoryPath);
var listFileSystem = sDir.readFileSystem(); var listFileSystem = sDir.readFileSystem();
LOGGER.info("# FS: " + listFileSystem.size()); LogU.infoPlain("# FS: %s", listFileSystem.size());
var record = new Record(sDir.directoryPath); var record = new Record(sDir.directoryPath);
record.getFiles().putAll(sDir.readRecord()); record.getFiles().putAll(sDir.readRecord());
LOGGER.info("# Record: " + record.getFiles().size()); LogU.infoPlain("# Record: %s", record.getFiles().size());
var listCreated = sDir.fillListOfLocallyCreatedFiles(listFileSystem, record); var listCreated = sDir.fillListOfLocallyCreatedFiles(listFileSystem, record);
LOGGER.info("# Created: " + listCreated.size());
var listDeleted = sDir.makeListOfLocallyDeletedFiles(listFileSystem, record); var listDeleted = sDir.makeListOfLocallyDeletedFiles(listFileSystem, record);
LOGGER.info("# Deleted: " + listDeleted.size());
var listModified = sDir.makeListOfLocallyModifiedFiles(listFileSystem, record); var listModified = sDir.makeListOfLocallyModifiedFiles(listFileSystem, record);
LOGGER.info("# Modified: " + listModified.size()); Tools tools = new Tools();
int newly = tools.setMinus(listCreated.keySet(), listDeleted.keySet()).size();
LogU.infoPlain("# Created: %s\n thereof newly created (not mv) %s", listCreated.size(), newly);
LogU.infoPlain("# Deleted: %s", listDeleted.size());
LogU.infoPlain("# Modified: %s", listModified.size());
sDir.doCreateOpsOnOtherSDs(listCreated); sDir.doCreateOpsOnOtherSDs(listCreated);
sDir.doDeleteOpsOnOtherSDs(listDeleted); sDir.doDeleteOpsOnOtherSDs(listDeleted);

@ -1,50 +0,0 @@
package com.olexyn.ensync;
import java.io.IOException;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class LogUtil {
private static final String format = "[%1$tF %1$tT] [%4$-7s] %5$-100s [%2$s]\n";
public static Logger get(Class<?> c) {
return get(c, Level.INFO);
}
public static Logger get(Class<?> c, Level level) {
System.setProperty("java.util.logging.SimpleFormatter.format", format);
Logger logger = Logger.getLogger(c.getName());
try {
String dir = System.getProperty("user.dir") + "/logs/main.log";
FileHandler fh = new FileHandler(dir, true);
fh.setFormatter(new SimpleFormatter() {
@Override
public synchronized String format(LogRecord logRecord) {
String msg = logRecord.getMessage();
return String.format(format,
new Date(logRecord.getMillis()),
logRecord.getSourceClassName() + " " + logRecord.getSourceMethodName(),
"",
logRecord.getLevel().getLocalizedName(),
msg
);
}
});
logger.addHandler(fh);
logger.setLevel(level);
} catch (NullPointerException | IOException e) {
e.printStackTrace();
}
return logger;
}
}

@ -4,6 +4,7 @@ import com.olexyn.ensync.artifacts.DataRoot;
import com.olexyn.ensync.artifacts.SyncBundle; import com.olexyn.ensync.artifacts.SyncBundle;
import com.olexyn.ensync.lock.LockUtil; import com.olexyn.ensync.lock.LockUtil;
import com.olexyn.ensync.util.IgnoreUtil; import com.olexyn.ensync.util.IgnoreUtil;
import com.olexyn.min.log.LogU;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -17,6 +18,7 @@ public class MainApp {
final private static Tools TOOLS = new Tools(); final private static Tools TOOLS = new Tools();
public static void main(String[] args) throws JSONException { public static void main(String[] args) throws JSONException {
LogU.remake(null, "com.olexyn.ensync.", "[%1$tF %1$tT] [%2$-7s] [%3$-10s] %4$-180s [%5$s]\n");
var configPath = Path.of(System.getProperty("user.dir") + "/src/main/resources/config.json"); var configPath = Path.of(System.getProperty("user.dir") + "/src/main/resources/config.json");
String configString = Tools.fileToString(LockUtil.lockFile(configPath).getFc()); String configString = Tools.fileToString(LockUtil.lockFile(configPath).getFc());

@ -1,6 +0,0 @@
package com.olexyn.ensync;
public enum OperationMode {
JAVA_FX,
JSON
}

@ -1,9 +1,9 @@
package com.olexyn.ensync.artifacts; package com.olexyn.ensync.artifacts;
import com.olexyn.ensync.LogUtil;
import com.olexyn.ensync.Tools; import com.olexyn.ensync.Tools;
import com.olexyn.ensync.lock.LockKeeper; import com.olexyn.ensync.lock.LockKeeper;
import com.olexyn.ensync.util.IgnoreUtil; import com.olexyn.ensync.util.IgnoreUtil;
import com.olexyn.min.log.LogU;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import java.io.File; import java.io.File;
@ -21,8 +21,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -34,7 +32,6 @@ import static com.olexyn.ensync.artifacts.Constants.RECORD_SEPARATOR;
*/ */
public class SyncDirectory { public class SyncDirectory {
private static final Logger LOGGER = LogUtil.get(SyncDirectory.class);
private final SyncBundle syncMap; private final SyncBundle syncMap;
public Path directoryPath; public Path directoryPath;
Tools tools = new Tools(); Tools tools = new Tools();
@ -136,7 +133,7 @@ public class SyncDirectory {
var i = new BigInteger(1, m.digest()); var i = new BigInteger(1, m.digest());
return String.format("%1$032X", i); return String.format("%1$032X", i);
} catch (Exception e) { } catch (Exception e) {
LOGGER.log(Level.INFO, "Failed to create Hash.", e); LogU.warnPlain("Failed to create Hash.\n%s", e.getMessage());
return null; return null;
} }
} }
@ -159,7 +156,7 @@ public class SyncDirectory {
outputList.add(line); outputList.add(line);
}); });
LOGGER.info("Writing " + outputList.size() + " files to Record: " + record.getPath()); LogU.infoPlain("Writing " + outputList.size() + " files to Record: " + record.getPath());
tools.writeStringListToFile(record.getPath().toString(), outputList); tools.writeStringListToFile(record.getPath().toString(), outputList);
} }
@ -173,7 +170,7 @@ public class SyncDirectory {
.filter(file -> IgnoreUtil.noIgnore(file.toPath())) .filter(file -> IgnoreUtil.noIgnore(file.toPath()))
.filter(file -> !file.getName().equals(Constants.STATE_FILE_NAME)); .filter(file -> !file.getName().equals(Constants.STATE_FILE_NAME));
} catch (IOException e) { } catch (IOException e) {
LOGGER.severe("Could walk the file tree : Record will be empty."); LogU.warnPlain("Could walk the file tree : Record will be empty.");
return Stream.empty(); return Stream.empty();
} }
} }
@ -187,11 +184,14 @@ public class SyncDirectory {
} }
public void doDeleteOpsOnOtherSDs(Map<String, RecordFile> listDeleted) { public void doDeleteOpsOnOtherSDs(Map<String, RecordFile> listDeleted) {
int deleteCount = 0;
for (var deletedFile : listDeleted.values()) { for (var deletedFile : listDeleted.values()) {
for (var otherFile : otherFiles(deletedFile)) { for (var otherFile : otherFiles(deletedFile)) {
deleteFileIfNewer(deletedFile, otherFile); var ok = deleteFileIfNewer(deletedFile, otherFile);
if (ok) { deleteCount++; }
} }
} }
LogU.infoPlain("DELETED " + deleteCount + "/" + listDeleted.size() + " Files.");
} }
public void doModifyOpsOnOtherSDs(Map<String, SyncFile> listModified) { public void doModifyOpsOnOtherSDs(Map<String, SyncFile> listModified) {
@ -214,26 +214,31 @@ public class SyncDirectory {
* Here the >= is crucial, since otherFile might have == modified, * Here the >= is crucial, since otherFile might have == modified,
* but in that case we still want to delete both files. * but in that case we still want to delete both files.
*/ */
private void deleteFileIfNewer(RecordFile thisFile, SyncFile otherFile) { private boolean deleteFileIfNewer(RecordFile thisFile, SyncFile otherFile) {
if (!otherFile.exists()) { if (!otherFile.exists()) {
LOGGER.info("Could not delete: " + otherFile.toPath() + " not found."); LogU.infoPlain("Not deleted (not found) " + otherFile.toPath() + " not found.");
return; } return false;
}
if (thisFile.lastModified() >= otherFile.lastModified()) { if (thisFile.lastModified() >= otherFile.lastModified()) {
try { try {
Files.delete(otherFile.toPath()); Files.delete(otherFile.toPath());
LOGGER.info("Deleted: " + otherFile.toPath()); LogU.infoPlain("Deleted " + otherFile.toPath());
return true;
} catch (IOException e) { } catch (IOException e) {
LOGGER.info("Could not delete: " + otherFile.toPath()); LogU.infoPlain("Not deleted (IOE) " + otherFile.toPath());
return false;
} }
} }
LogU.infoPlain("Not deleted (other file modified recently)");
return false;
} }
/** /**
* Overwrite other file if this file is newer. * Overwrite other file if this file is newer.
*/ */
private void writeFileIfNewer(SyncFile thisFile, SyncFile otherFile) { private void writeFileIfNewer(SyncFile thisFile, SyncFile otherFile) {
LOGGER.info("Try write from: " + thisFile.toPath()); LogU.infoPlain("Try write from: " + thisFile.toPath());
LOGGER.info(" to: " + otherFile.toPath()); LogU.infoPlain(" to: " + otherFile.toPath());
if (!thisFile.isFile()) { return; } if (!thisFile.isFile()) { return; }
if (otherFile.exists()) { if (otherFile.exists()) {
var thisHash = getHash(thisFile.toPath()); var thisHash = getHash(thisFile.toPath());
@ -243,7 +248,7 @@ public class SyncDirectory {
dropAge(thisFile, otherFile); dropAge(thisFile, otherFile);
return; return;
} else if (thisFile.lastModified() <= otherFile.lastModified()) { } else if (thisFile.lastModified() <= otherFile.lastModified()) {
LOGGER.info("Did not override due to target being newer."); LogU.infoPlain("Did not override due to target being newer.");
return; return;
} }
} }
@ -252,15 +257,15 @@ public class SyncDirectory {
private void dropAge(SyncFile thisFile, SyncFile otherFile) { private void dropAge(SyncFile thisFile, SyncFile otherFile) {
if (thisFile.lastModified() == otherFile.lastModified()) { if (thisFile.lastModified() == otherFile.lastModified()) {
LOGGER.info("Same age, ignore"); LogU.infoPlain("Same age, ignore");
return; return;
} }
if (thisFile.lastModified() < otherFile.lastModified()) { if (thisFile.lastModified() < otherFile.lastModified()) {
otherFile.setLastModified(thisFile.lastModified()); otherFile.setLastModified(thisFile.lastModified());
LOGGER.info("Dropped age of: " + otherFile.toPath() + " -> " + otherFile.lastModified()); LogU.infoPlain("Dropped age of: %s -> %s",otherFile.toPath(), otherFile.lastModified());
} else { } else {
thisFile.setLastModified(otherFile.lastModified()); thisFile.setLastModified(otherFile.lastModified());
LOGGER.info("Dropped age of: " + thisFile.toPath() + " -> " + thisFile.lastModified()); LogU.infoPlain("Dropped age of: %s -> %s",thisFile.toPath(), thisFile.lastModified());
} }
} }
@ -269,7 +274,7 @@ public class SyncDirectory {
try { try {
FileUtils.createParentDirectories(otherFile); FileUtils.createParentDirectories(otherFile);
} catch (IOException e) { } catch (IOException e) {
LOGGER.info("Could not create Parent"); LogU.warnPlain("Could not create Parent");
} }
} }
var thisFc = LockKeeper.getFc(thisFile.toPath()); var thisFc = LockKeeper.getFc(thisFile.toPath());
@ -280,14 +285,15 @@ public class SyncDirectory {
) { ) {
copyStream(is, os); copyStream(is, os);
} catch (Exception e) { } catch (Exception e) {
LOGGER.severe("Could not copy file from: " + thisFile.toPath()); LogU.warnPlain("Could not copy file from: %s", thisFile.toPath());
LOGGER.severe(" to: " + otherFile.toPath()); LogU.warnPlain(" to: %s", otherFile.toPath());
e.printStackTrace(); e.printStackTrace();
} }
LOGGER.info(thisFile.toPath() + " "+ thisFile.lastModified());
LOGGER.info(otherFile.toPath() + " " + otherFile.lastModified()); LogU.infoPlain(thisFile.toPath() + " "+ thisFile.lastModified());
LogU.infoPlain(otherFile.toPath() + " " + otherFile.lastModified());
otherFile.setLastModified(thisFile.lastModified()); otherFile.setLastModified(thisFile.lastModified());
LOGGER.info(otherFile.toPath() + " " + otherFile.lastModified()); LogU.infoPlain(otherFile.toPath() + " " + otherFile.lastModified());
} }
public static void copyStream(InputStream input, OutputStream output) public static void copyStream(InputStream input, OutputStream output)

@ -1,15 +1,13 @@
package com.olexyn.ensync.artifacts; package com.olexyn.ensync.artifacts;
import com.olexyn.ensync.LogUtil; import com.olexyn.min.log.LogU;
import java.io.File; import java.io.File;
import java.util.logging.Logger;
import static com.olexyn.ensync.artifacts.Constants.EMPTY; import static com.olexyn.ensync.artifacts.Constants.EMPTY;
public class SyncFile extends File { public class SyncFile extends File {
private static final Logger LOGGER = LogUtil.get(SyncFile.class);
private final String relativePath; private final String relativePath;
private final SyncDirectory sDir; private final SyncDirectory sDir;
@ -36,8 +34,8 @@ public class SyncFile extends File {
@Override @Override
public long lastModified(){ public long lastModified(){
if (exists()) { return super.lastModified(); } if (exists()) { return super.lastModified(); }
LOGGER.info("Did not find File for " + this); LogU.infoPlain("Did not find File for " + this);
LOGGER.info("Returning -1 (never existed)."); LogU.infoPlain("Returning -1 (never existed).");
return -1; return -1;
} }

@ -1,7 +1,7 @@
package com.olexyn.ensync.lock; package com.olexyn.ensync.lock;
import com.olexyn.ensync.LogUtil;
import com.olexyn.ensync.util.IgnoreUtil; import com.olexyn.ensync.util.IgnoreUtil;
import com.olexyn.min.log.LogU;
import java.io.IOException; import java.io.IOException;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
@ -10,15 +10,12 @@ import java.nio.file.Path;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Logger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class LockKeeper { public class LockKeeper {
private static final int TRY_COUNT = 4; private static final int TRY_COUNT = 4;
private static final Logger LOGGER = LogUtil.get(LockKeeper.class);
private final static Map<Path, FcState> LOCKS = new HashMap<>(); private final static Map<Path, FcState> LOCKS = new HashMap<>();
public static boolean lockDir(Path dirPath) { public static boolean lockDir(Path dirPath) {
@ -32,7 +29,7 @@ public class LockKeeper {
} catch (IOException e) { } catch (IOException e) {
return false; return false;
} }
LOGGER.info("LOCKED " + fcStates.size() + " files in " + dirPath); LogU.infoPlain("LOCKED " + fcStates.size() + " files in " + dirPath);
fcStates.forEach(fcState -> LOCKS.put(fcState.getPath(), fcState)); fcStates.forEach(fcState -> LOCKS.put(fcState.getPath(), fcState));
return fcStates.stream().noneMatch(FcState::isUnlocked); return fcStates.stream().noneMatch(FcState::isUnlocked);
} }
@ -40,7 +37,7 @@ public class LockKeeper {
public static void unlockAll() { public static void unlockAll() {
LOGGER.info("UNLOCKING ALL."); LogU.infoPlain("UNLOCKING ALL.");
LOCKS.values().forEach( LOCKS.values().forEach(
fcState -> LockUtil.unlockFile(fcState.getPath(), fcState.getFc(), 4) fcState -> LockUtil.unlockFile(fcState.getPath(), fcState.getFc(), 4)
); );

@ -1,34 +1,28 @@
package com.olexyn.ensync.lock; package com.olexyn.ensync.lock;
import com.olexyn.ensync.LogUtil; import com.olexyn.min.log.LogU;
import java.io.IOException; import java.io.IOException;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.channels.OverlappingFileLockException; import java.nio.channels.OverlappingFileLockException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.logging.Level;
import java.util.logging.Logger;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE_NEW; import static java.nio.file.StandardOpenOption.CREATE_NEW;
import static java.nio.file.StandardOpenOption.READ; import static java.nio.file.StandardOpenOption.READ;
import static java.nio.file.StandardOpenOption.WRITE; import static java.nio.file.StandardOpenOption.WRITE;
import static java.util.logging.Level.INFO;
public class LockUtil { public class LockUtil {
private static final int DEFAULT_LOCK_TRIES = 4; private static final int DEFAULT_LOCK_TRIES = 4;
private static final long SLEEP_DURATION = 1000; private static final long SLEEP_DURATION = 1000;
private static final Logger LOGGER = LogUtil.get(LockUtil.class);
public static FcState newFile(Path filePath) { public static FcState newFile(Path filePath) {
try { try {
var fc = FileChannel.open(filePath, CREATE_NEW, WRITE); var fc = FileChannel.open(filePath, CREATE_NEW, WRITE);
return new FcState(filePath, fc, false); return new FcState(filePath, fc, false);
} catch (IOException | OverlappingFileLockException e) { } catch (IOException | OverlappingFileLockException e) {
LOGGER.log(INFO, "Could not NEW " + filePath, e); LogU.warnPlain("Could not NEW %s\n%s", filePath, e.getMessage());
return new FcState(filePath, null, false); return new FcState(filePath, null, false);
} }
} }
@ -47,13 +41,13 @@ public class LockUtil {
} catch (IOException | OverlappingFileLockException e) { } catch (IOException | OverlappingFileLockException e) {
if (tryCount > 0) { if (tryCount > 0) {
tryCount--; tryCount--;
LOGGER.info("Could not lock " + filePath + " Will try " + tryCount + " times."); LogU.warnPlain("Could not lock %s. Will try %s times.", filePath, tryCount);
try { try {
Thread.sleep(SLEEP_DURATION); Thread.sleep(SLEEP_DURATION);
} catch (InterruptedException ignored) { } } catch (InterruptedException ignored) { }
return lockFile(filePath, tryCount); return lockFile(filePath, tryCount);
} }
LOGGER.log(INFO, "Could not lock " + filePath, e); LogU.warnPlain("Could not lock %s\n%s", filePath, e.getMessage());
return new FcState(filePath, null, false); return new FcState(filePath, null, false);
} }
} }
@ -70,10 +64,11 @@ public class LockUtil {
} catch (IOException | OverlappingFileLockException e) { } catch (IOException | OverlappingFileLockException e) {
if (tryCount > 0) { if (tryCount > 0) {
tryCount--; tryCount--;
LOGGER.info("Could not close " + fc + " Will try " + tryCount + " times."); LogU.warnPlain("Could not close %s. Will try %s times.", fc, tryCount);
return unlockFile(filePath, fc, tryCount); return unlockFile(filePath, fc, tryCount);
} }
LOGGER.info("Could not unlock " + fc); LogU.warnPlain("Could not unlock %s", fc);
return new FcState(filePath, null, true); return new FcState(filePath, null, true);
} }
} }

@ -4,4 +4,5 @@ collection/books
collection/movies collection/movies
collection/music collection/music
collection/software collection/software
collection/tv
collection/youtube collection/youtube

@ -1,11 +1,11 @@
package com.olexyn.ensync.files; package com.olexyn.ensync.files;
import com.olexyn.ensync.Flow; import com.olexyn.ensync.Flow;
import com.olexyn.ensync.LogUtil;
import com.olexyn.ensync.Tools; import com.olexyn.ensync.Tools;
import com.olexyn.ensync.artifacts.DataRoot; import com.olexyn.ensync.artifacts.DataRoot;
import com.olexyn.ensync.artifacts.SyncBundle; import com.olexyn.ensync.artifacts.SyncBundle;
import com.olexyn.ensync.lock.LockUtil; import com.olexyn.ensync.lock.LockUtil;
import com.olexyn.min.log.LogU;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
@ -20,7 +20,6 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Logger;
/** /**
@ -29,7 +28,6 @@ import java.util.logging.Logger;
*/ */
public class FifteenTests { public class FifteenTests {
private static final Logger LOGGER = LogUtil.get(FifteenTests.class);
final public static Flow FLOW = new Flow(); final public static Flow FLOW = new Flow();
@ -48,40 +46,40 @@ public class FifteenTests {
private List<String> createFile(File file) { private List<String> createFile(File file) {
if (file.exists()) { if (file.exists()) {
LOGGER.info("TEST can not create existing: " + file.toPath()); LogU.infoPlain("TEST can not create existing: " + file.toPath());
Assert.fail(); Assert.fail();
} }
List<String> stringList = new ArrayList<>(); List<String> stringList = new ArrayList<>();
stringList.add(LocalDateTime.now().format(dateTimeFormatter) + " CREATED"); stringList.add(LocalDateTime.now().format(dateTimeFormatter) + " CREATED");
tools.writeStringListToFile(file.getAbsolutePath(), stringList); tools.writeStringListToFile(file.getAbsolutePath(), stringList);
LOGGER.info("TEST CREATE: " + file.toPath()); LogU.infoPlain("TEST CREATE: " + file.toPath());
return stringList; return stringList;
} }
private List<String> modifyFile(File file) { private List<String> modifyFile(File file) {
LOGGER.info("TEST TRY MODIFY: " + file.toPath()); LogU.infoPlain("TEST TRY MODIFY: " + file.toPath());
var fcState = LockUtil.lockFile(file.toPath(), 10); var fcState = LockUtil.lockFile(file.toPath(), 10);
var stringList = new ArrayList<>(tools.fileToLines(fcState.getFc())); var stringList = new ArrayList<>(tools.fileToLines(fcState.getFc()));
stringList.add(LocalDateTime.now().format(dateTimeFormatter) + " MODIFIED"); stringList.add(LocalDateTime.now().format(dateTimeFormatter) + " MODIFIED");
tools.writeStringListToFile(file.getAbsolutePath(), stringList); tools.writeStringListToFile(file.getAbsolutePath(), stringList);
LOGGER.info("TEST MODIFY: " + file.toPath()); LogU.infoPlain("TEST MODIFY: " + file.toPath());
LockUtil.unlockFile(fcState, 10); LockUtil.unlockFile(fcState, 10);
LOGGER.info("TEST MODIFY UNLOCKED: " + file.toPath()); LogU.infoPlain("TEST MODIFY UNLOCKED: " + file.toPath());
return stringList; return stringList;
} }
private static void deleteFile(File file) { private static void deleteFile(File file) {
LOGGER.info("TEST TRY DELETE: " + file.toPath()); LogU.infoPlain("TEST TRY DELETE: " + file.toPath());
var fcState = LockUtil.lockFile(file.toPath(), 10); var fcState = LockUtil.lockFile(file.toPath(), 10);
try { try {
Files.delete(file.toPath()); Files.delete(file.toPath());
LOGGER.info("TEST DELETE: " + file.toPath()); LogU.infoPlain("TEST DELETE: " + file.toPath());
} catch (IOException e) { } catch (IOException e) {
LOGGER.severe("Could not delete file." + file.toPath()); LogU.warnPlain("Could not delete file." + file.toPath());
} }
LockUtil.unlockFile(fcState, 10); LockUtil.unlockFile(fcState, 10);
LOGGER.info("TEST DELETE UNLOCKED: " + file.toPath()); LogU.infoPlain("TEST DELETE UNLOCKED: " + file.toPath());
} }
private void cleanDirs(Path... dirs) { private void cleanDirs(Path... dirs) {
@ -90,7 +88,7 @@ public class FifteenTests {
FileUtils.deleteDirectory(dir.toFile()); FileUtils.deleteDirectory(dir.toFile());
Files.createDirectory(dir); Files.createDirectory(dir);
} catch (IOException e) { } catch (IOException e) {
LOGGER.severe("Could not clear dirs. " + dir + e.getMessage()); LogU.warnPlain("Could not clear dirs. " + dir + e.getMessage());
} }
} }
} }

@ -1,21 +1,15 @@
package com.olexyn.ensync.files; package com.olexyn.ensync.files;
import com.olexyn.ensync.LogUtil;
import com.olexyn.ensync.Tools; import com.olexyn.ensync.Tools;
import com.olexyn.ensync.artifacts.SyncDirectory;
import com.olexyn.ensync.lock.LockKeeper;
import com.olexyn.ensync.lock.LockUtil; import com.olexyn.ensync.lock.LockUtil;
import com.olexyn.min.log.LogU;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Logger;
public class TestFile extends File { public class TestFile extends File {
private static final Logger LOGGER = LogUtil.get(TestFile.class);
Tools tools = new Tools(); Tools tools = new Tools();
/** /**
@ -26,7 +20,7 @@ public class TestFile extends File {
} }
public List<String> readContent() { public List<String> readContent() {
LOGGER.info("TEST TRY READ: " + toPath()); LogU.infoPlain("TEST TRY READ: " + toPath());
var fcState = LockUtil.lockFile(toPath()); var fcState = LockUtil.lockFile(toPath());
return tools.fileToLines(fcState.getFc()); return tools.fileToLines(fcState.getFc());
} }

Loading…
Cancel
Save