Skip to content
This repository has been archived by the owner on Sep 22, 2021. It is now read-only.

Commit

Permalink
Fixed #30, #31, and #33.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkawamoto committed Dec 19, 2017
2 parents 9ff91cd + 9f162ed commit 802fa01
Show file tree
Hide file tree
Showing 25 changed files with 315 additions and 68 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ dependencies {
compile 'org.apache.logging.log4j:log4j-core:2.9.1'
compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.9.1'
compile 'com.intellij:annotations:12.0'
compile 'io.goobox:goobox-sync-common:0.0.2'
compile 'io.goobox:goobox-sync-common:0.0.1'
testCompile 'junit:junit:4.12'
testCompile 'org.jmockit:jmockit:1.36'
testCompile 'com.squareup.okhttp3:mockwebserver:3.9.1'
}

task packageTests(type: Jar) {
Expand All @@ -93,7 +94,7 @@ run {
}
}

def siaVersion = 'v1.3.1-rc2'
def siaVersion = 'v1.3.1-rc4'
def siaArchives = [
['key': 'win64', 'value': 'windows-amd64'],
['key': 'linux', 'value': 'linux-amd64'],
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/io/goobox/sync/sia/APIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import com.google.gson.JsonSyntaxException;
import io.goobox.sync.sia.client.ApiException;
import io.goobox.sync.sia.client.api.model.StandardError;
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

public class APIUtils {

Expand All @@ -43,4 +48,24 @@ public static String getErrorMessage(final ApiException e) {

}

@NotNull
public static String toSlash(@NotNull final Path path) {
return path.toString().replace("\\", "/");
}

@NotNull
public static Path fromSlash(@NotNull final String path) {

final String[] components = path.split("/");
if (components.length < 2) {
return Paths.get(path);
}
if (components[0].isEmpty()) {
components[0] = "/";
}

return Paths.get(components[0], Arrays.copyOfRange(components, 1, components.length));

}

}
7 changes: 7 additions & 0 deletions src/main/java/io/goobox/sync/sia/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
import io.goobox.sync.sia.command.Wallet;
import io.goobox.sync.sia.db.DB;
import io.goobox.sync.sia.db.SyncState;
import io.goobox.sync.sia.task.CheckDownloadStateTask;
import io.goobox.sync.sia.task.CheckStateTask;
import io.goobox.sync.sia.task.CheckUploadStateTask;
import io.goobox.sync.sia.task.DeleteCloudFileTask;
import io.goobox.sync.sia.task.DeleteLocalFileTask;
import io.goobox.sync.sia.task.DownloadCloudFileTask;
import io.goobox.sync.sia.task.UploadLocalFileTask;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/io/goobox/sync/sia/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class Config {
static final String ParityPieces = "parity-pieces";
static final Logger logger = LogManager.getLogger();

static final int DefaultDataPieces = 10;
static final int DefaultParityPieces = 20;
static final int MinimumParityPieces = 12;

/**
* User name.
*/
Expand Down Expand Up @@ -218,21 +222,21 @@ public static Config load(@NotNull final Path path) throws IOException {
}

try {
cfg.dataPieces = Integer.valueOf(props.getProperty(DataPieces, "1"));
cfg.dataPieces = Integer.valueOf(props.getProperty(DataPieces, String.valueOf(DefaultDataPieces)));
} catch (NumberFormatException e) {
logger.warn("Invalid data pieces {}", props.getProperty(DataPieces));
cfg.dataPieces = 1;
cfg.dataPieces = DefaultDataPieces;
}

try {
cfg.parityPieces = Integer.valueOf(props.getProperty(ParityPieces, "12"));
if (cfg.parityPieces < 12) {
logger.warn("Invalid parity pieces {}, minimum 12 pieces are required", cfg.parityPieces);
cfg.parityPieces = 12;
cfg.parityPieces = Integer.valueOf(props.getProperty(ParityPieces, String.valueOf(DefaultParityPieces)));
if (cfg.parityPieces < MinimumParityPieces) {
logger.warn("Invalid parity pieces {}, minimum {} pieces are required", cfg.parityPieces, MinimumParityPieces);
cfg.parityPieces = DefaultParityPieces;
}
} catch (NumberFormatException e) {
logger.warn("Invalid parity pieces {}", props.getProperty(ParityPieces));
cfg.parityPieces = 12;
cfg.parityPieces = DefaultParityPieces;
}

logger.info("Sync directory: {}", cfg.getSyncDir());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/goobox/sync/sia/RetryableTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class RetryableTask implements Runnable {
@NotNull
private final RecoveryTask recover;

RetryableTask(@NotNull final Callable<Void> task, @NotNull final RecoveryTask recover) {
public RetryableTask(@NotNull final Callable<Void> task, @NotNull final RecoveryTask recover) {
this.task = task;
this.recover = recover;
}
Expand All @@ -56,6 +56,7 @@ public void run() {

}

@SuppressWarnings("SimplifiableIfStatement")
@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -73,5 +74,4 @@ public int hashCode() {
result = 31 * result + recover.hashCode();
return result;
}

}
7 changes: 3 additions & 4 deletions src/main/java/io/goobox/sync/sia/model/AbstractSiaFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

package io.goobox.sync.sia.model;

import io.goobox.sync.sia.APIUtils;
import io.goobox.sync.sia.Context;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

public abstract class AbstractSiaFile implements SiaFile {
Expand All @@ -42,7 +42,7 @@ public abstract class AbstractSiaFile implements SiaFile {

AbstractSiaFile(@NotNull Context ctx, @NotNull final String cloudPath) {

this.cloudPath = Paths.get(cloudPath);
this.cloudPath = APIUtils.fromSlash(cloudPath);

Path withoutTimestamp = this.cloudPath;
Long created = null;
Expand All @@ -51,15 +51,14 @@ public abstract class AbstractSiaFile implements SiaFile {
created = Long.parseLong(this.cloudPath.getFileName().toString());
withoutTimestamp = this.cloudPath.getParent();
} catch (NumberFormatException e) {
logger.debug("siapath {} doesn't have its creation time", cloudPath);
logger.debug("cloud path {} doesn't have its creation time", cloudPath);
}
}
this.creationTime = created;

this.name = ctx.pathPrefix.relativize(withoutTimestamp);
this.localPath = ctx.getLocalPath(this.name.toString());


}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.goobox.sync.sia;
package io.goobox.sync.sia.task;

import io.goobox.sync.common.Utils;
import io.goobox.sync.sia.APIUtils;
import io.goobox.sync.sia.Context;
import io.goobox.sync.sia.client.ApiException;
import io.goobox.sync.sia.client.api.RenterApi;
import io.goobox.sync.sia.client.api.model.InlineResponse20010Downloads;
Expand Down Expand Up @@ -49,14 +51,14 @@
*
* @author junpei
*/
class CheckDownloadStateTask implements Callable<Void> {
public class CheckDownloadStateTask implements Callable<Void> {

private static final Logger logger = LogManager.getLogger();

@NotNull
private final Context ctx;

CheckDownloadStateTask(@NotNull final Context ctx) {
public CheckDownloadStateTask(@NotNull final Context ctx) {
this.ctx = ctx;
}

Expand All @@ -73,7 +75,10 @@ public Void call() throws ApiException {
final Optional<SyncFile> syncFileOpt = DB.get(file);

if (!file.getCloudPath().startsWith(this.ctx.pathPrefix) || !syncFileOpt.isPresent()) {
logger.trace("Found remote file {} but it's not managed by Goobox", file.getCloudPath());
logger.trace(
"Found remote file {} but it's not managed by Goobox (not starts with {})",
file.getCloudPath(),
this.ctx.pathPrefix);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.goobox.sync.sia;
package io.goobox.sync.sia.task;

import io.goobox.sync.sia.APIUtils;
import io.goobox.sync.sia.Context;
import io.goobox.sync.sia.RetryableTask;
import io.goobox.sync.sia.StartSiaDaemonTask;
import io.goobox.sync.sia.client.ApiException;
import io.goobox.sync.sia.client.api.RenterApi;
import io.goobox.sync.sia.client.api.model.InlineResponse20011Files;
Expand Down Expand Up @@ -45,7 +49,7 @@
*
* @author junpei
*/
class CheckStateTask implements Callable<Void> {
public class CheckStateTask implements Callable<Void> {

private static final Logger logger = LogManager.getLogger();

Expand All @@ -54,7 +58,7 @@ class CheckStateTask implements Callable<Void> {
@NotNull
private final Executor executor;

CheckStateTask(@NotNull final Context ctx, @NotNull final Executor executor) {
public CheckStateTask(@NotNull final Context ctx, @NotNull final Executor executor) {
this.ctx = ctx;
this.executor = executor;
}
Expand Down Expand Up @@ -170,11 +174,16 @@ public Void call() throws ApiException {
}
// This file is not stored in the cloud network and modified from the local directory.
// It should be uploaded.
final Path localPath = this.ctx.config.getSyncDir().resolve(syncFile.getName());
try {
logger.info("Local file {} is going to be uploaded", syncFile.getName());
this.enqueueForUpload(this.ctx.config.getSyncDir().resolve(syncFile.getName()));
this.enqueueForUpload(localPath);
} catch (IOException e) {
logger.error("Failed to upload {}: {}", syncFile.getName(), e.getMessage());
if (!localPath.toFile().exists()) {
logger.info("File {} was deleted", syncFile.getName());
DB.remove(syncFile.getName());
}
}
processedFiles.add(syncFile.getName());
});
Expand Down Expand Up @@ -232,14 +241,17 @@ private Collection<SiaFile> takeNewestFiles(final Collection<InlineResponse20011

if (!file.getAvailable()) {
// This file is still being uploaded.
logger.debug("Found remote file {} but it's not available", file.getSiapath());
logger.debug("Found remote file {} but it's not available (still being uploaded)", file.getSiapath());
return;
}

final SiaFile siaFile = new SiaFileFromFilesAPI(this.ctx, file);
if (!siaFile.getCloudPath().startsWith(this.ctx.pathPrefix)) {
// This file isn't managed by Goobox.
logger.debug("Found remote file {} but it's not managed by Goobox", siaFile.getCloudPath());
logger.debug(
"Found remote file {} but it's not managed by Goobox (not starts with {})",
siaFile.getCloudPath(),
this.ctx.pathPrefix);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package io.goobox.sync.sia;
package io.goobox.sync.sia.task;

import io.goobox.sync.sia.APIUtils;
import io.goobox.sync.sia.Context;
import io.goobox.sync.sia.client.ApiException;
import io.goobox.sync.sia.client.api.RenterApi;
import io.goobox.sync.sia.client.api.model.InlineResponse20011;
Expand All @@ -34,15 +36,15 @@
import java.util.Optional;
import java.util.concurrent.Callable;

class CheckUploadStateTask implements Callable<Void> {
public class CheckUploadStateTask implements Callable<Void> {

private static final Logger logger = LogManager.getLogger();
private static final BigDecimal Completed = new BigDecimal(100);

@NotNull
private final Context ctx;

CheckUploadStateTask(@NotNull final Context ctx) {
public CheckUploadStateTask(@NotNull final Context ctx) {
this.ctx = ctx;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package io.goobox.sync.sia;
package io.goobox.sync.sia.task;

import io.goobox.sync.sia.APIUtils;
import io.goobox.sync.sia.Context;
import io.goobox.sync.sia.client.ApiException;
import io.goobox.sync.sia.client.api.RenterApi;
import io.goobox.sync.sia.client.api.model.InlineResponse20011;
Expand All @@ -37,7 +39,7 @@
/**
* Deletes a given file from the cloud network and sync DB.
*/
class DeleteCloudFileTask implements Callable<Void> {
public class DeleteCloudFileTask implements Callable<Void> {

private static final Logger logger = LogManager.getLogger();

Expand All @@ -47,7 +49,7 @@ class DeleteCloudFileTask implements Callable<Void> {
@NotNull
private final String name;

DeleteCloudFileTask(@NotNull final Context ctx, @NotNull final String name) {
public DeleteCloudFileTask(@NotNull final Context ctx, @NotNull final String name) {
this.ctx = ctx;
this.name = name;
}
Expand Down Expand Up @@ -86,7 +88,7 @@ public Void call() throws ApiException {
if (siaFile.getName().equals(this.name)) {
logger.info("Delete file {}", siaFile.getCloudPath());
try {
api.renterDeleteSiapathPost(siaFile.getCloudPath().toString());
api.renterDeleteSiapathPost(APIUtils.toSlash(siaFile.getCloudPath()));
} catch (final ApiException e) {
if (e.getCause() instanceof ConnectException) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.goobox.sync.sia;
package io.goobox.sync.sia.task;

import io.goobox.sync.sia.Context;
import io.goobox.sync.sia.db.DB;
import io.goobox.sync.sia.db.SyncFile;
import io.goobox.sync.sia.db.SyncState;
Expand All @@ -34,7 +35,7 @@
*
* @author junpei
*/
class DeleteLocalFileTask implements Runnable {
public class DeleteLocalFileTask implements Runnable {

private static final Logger logger = LogManager.getLogger();

Expand All @@ -43,7 +44,7 @@ class DeleteLocalFileTask implements Runnable {
@NotNull
private final Path localPath;

DeleteLocalFileTask(@NotNull final Context ctx, @NotNull final Path localPath) {
public DeleteLocalFileTask(@NotNull final Context ctx, @NotNull final Path localPath) {
this.ctx = ctx;
this.localPath = localPath;
}
Expand Down
Loading

0 comments on commit 802fa01

Please sign in to comment.