blob: 099bc0d3d22d224e9a7ae2473e31a457cb2f48c5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package com.wireguard.android.configStore;
import android.content.Context;
import android.util.Log;
import com.wireguard.android.Application.ApplicationContext;
import com.wireguard.android.util.AsyncWorker;
import com.wireguard.config.Config;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Set;
import java9.util.concurrent.CompletionStage;
import java9.util.stream.Collectors;
import java9.util.stream.Stream;
/**
* Created by samuel on 12/28/17.
*/
public final class FileConfigStore implements ConfigStore {
private static final String TAG = FileConfigStore.class.getSimpleName();
private final AsyncWorker asyncWorker;
private final Context context;
public FileConfigStore(final AsyncWorker asyncWorker,
@ApplicationContext final Context context) {
this.asyncWorker = asyncWorker;
this.context = context;
}
@Override
public CompletionStage<Config> create(final String name, final Config config) {
return asyncWorker.supplyAsync(() -> {
final File file = fileFor(name);
if (!file.createNewFile()) {
final String message = "Configuration file " + file.getName() + " already exists";
throw new IllegalStateException(message);
}
try (FileOutputStream stream = new FileOutputStream(file, false)) {
stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
return config;
}
});
}
@Override
public CompletionStage<Void> delete(final String name) {
return asyncWorker.runAsync(() -> {
final File file = fileFor(name);
if (!file.delete())
throw new IOException("Cannot delete configuration file " + file.getName());
});
}
@Override
public CompletionStage<Set<String>> enumerate() {
return asyncWorker.supplyAsync(() -> Stream.of(context.fileList())
.filter(name -> name.endsWith(".conf"))
.map(name -> name.substring(0, name.length() - ".conf".length()))
.collect(Collectors.toUnmodifiableSet()));
}
private File fileFor(final String name) {
return new File(context.getFilesDir(), name + ".conf");
}
@Override
public CompletionStage<Config> load(final String name) {
return asyncWorker.supplyAsync(() -> {
try (FileInputStream stream = new FileInputStream(fileFor(name))) {
return Config.from(stream);
}
});
}
@Override
public CompletionStage<Config> save(final String name, final Config config) {
Log.d(TAG, "Requested save config for tunnel " + name);
return asyncWorker.supplyAsync(() -> {
final File file = fileFor(name);
if (!file.isFile()) {
final String message = "Configuration file " + file.getName() + " not found";
throw new IllegalStateException(message);
}
try (FileOutputStream stream = new FileOutputStream(file, false)) {
Log.d(TAG, "Writing out config for tunnel " + name);
stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
return config;
}
});
}
}
|