Skip to content

Commit 794e329

Browse files
authored
fix(server): mkdirs before cloning problems (#588)
1 parent 4bc943f commit 794e329

File tree

7 files changed

+31
-52
lines changed

7 files changed

+31
-52
lines changed

judgels-backends/judgels-commons/judgels-fs/src/main/java/judgels/fs/FileSystem.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public interface FileSystem {
99
void createDirectory(Path dirPath);
1010
boolean directoryExists(Path dirPath);
11+
void copyDirectory(Path srcPath, Path destPath);
1112
void createFile(Path filePath);
1213
void removeFile(Path filePath);
1314
File getFile(Path filePath);
@@ -28,7 +29,4 @@ default void writeToFile(Path filePath, String content) {
2829
default String readFromFile(Path filePath) {
2930
return new String(readByteArrayFromFile(filePath));
3031
}
31-
32-
void copyDirectory(Path src, Path dest);
33-
void copy(Path src, Path dest);
3432
}

judgels-backends/judgels-commons/judgels-fs/src/main/java/judgels/fs/aws/AwsFileSystem.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public boolean directoryExists(Path dirPath) {
7373
throw new UnsupportedOperationException();
7474
}
7575

76+
@Override
77+
public void copyDirectory(Path srcPath, Path destPath) {
78+
throw new UnsupportedOperationException();
79+
}
80+
7681
@Override
7782
public void createFile(Path filePath) {
7883
throw new UnsupportedOperationException();
@@ -213,14 +218,4 @@ public byte[] readByteArrayFromFile(Path filePath) {
213218
throw new RuntimeException(e);
214219
}
215220
}
216-
217-
@Override
218-
public void copyDirectory(Path src, Path dest) {
219-
throw new UnsupportedOperationException();
220-
}
221-
222-
@Override
223-
public void copy(Path src, Path dest) {
224-
throw new UnsupportedOperationException();
225-
}
226221
}

judgels-backends/judgels-commons/judgels-fs/src/main/java/judgels/fs/duplex/DuplexFileSystem.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public boolean directoryExists(Path dirPath) {
2828
return local.directoryExists(dirPath);
2929
}
3030

31+
@Override
32+
public void copyDirectory(Path srcPath, Path destPath) {
33+
local.copyDirectory(srcPath, destPath);
34+
}
35+
3136
@Override
3237
public void createFile(Path filePath) {
3338
local.createFile(filePath);
@@ -102,14 +107,4 @@ public byte[] readByteArrayFromFile(Path filePath) {
102107
return aws.readByteArrayFromFile(filePath);
103108
}
104109
}
105-
106-
@Override
107-
public void copyDirectory(Path src, Path dest) {
108-
local.copyDirectory(src, dest);
109-
}
110-
111-
@Override
112-
public void copy(Path src, Path dest) {
113-
local.copy(src, dest);
114-
}
115110
}

judgels-backends/judgels-commons/judgels-fs/src/main/java/judgels/fs/local/LocalFileSystem.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public boolean directoryExists(Path dirPath) {
6565
return Files.isDirectory(baseDir.resolve(dirPath));
6666
}
6767

68+
@Override
69+
public void copyDirectory(Path srcPath, Path destPath) {
70+
try (Stream<Path> stream = Files.walk(baseDir.resolve(srcPath))) {
71+
stream.forEach(src -> copy(src, baseDir.resolve(destPath).resolve(baseDir.resolve(srcPath).relativize(src))));
72+
} catch (IOException e) {
73+
throw new RuntimeException(e);
74+
}
75+
}
76+
6877
@Override
6978
public void createFile(Path filePath) {
7079
writeByteArrayToFile(filePath, new byte[0]);
@@ -218,20 +227,10 @@ public byte[] readByteArrayFromFile(Path filePath) {
218227
}
219228
}
220229

221-
@Override
222-
public void copyDirectory(Path src, Path dest) {
223-
try (Stream<Path> stream = Files.walk(src)) {
224-
stream.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
225-
} catch (IOException e) {
226-
throw new RuntimeException(e);
227-
}
228-
}
229-
230-
@Override
231-
public void copy(Path src, Path dest) {
230+
private static void copy(Path src, Path dest) {
232231
try {
233232
Files.copy(src, dest, REPLACE_EXISTING, COPY_ATTRIBUTES);
234-
} catch (Exception e) {
233+
} catch (IOException e) {
235234
throw new RuntimeException(e.getMessage(), e);
236235
}
237236
}

judgels-backends/judgels-server-app/src/integTest/java/judgels/jophiel/user/avatar/UserAvatarIntegrationTestModule.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public boolean directoryExists(Path dirPath) {
3131
return false;
3232
}
3333

34+
@Override
35+
public void copyDirectory(Path srcPath, Path destPath) {}
36+
3437
@Override
3538
public void createFile(Path filePath) {}
3639

@@ -78,11 +81,5 @@ public void writeByteArrayToFile(Path filePath, byte[] content) {}
7881
public byte[] readByteArrayFromFile(Path filePath) {
7982
return new byte[0];
8083
}
81-
82-
@Override
83-
public void copyDirectory(Path src, Path dest) {}
84-
85-
@Override
86-
public void copy(Path src, Path dest) {}
8784
}
8885
}

judgels-backends/judgels-server-app/src/main/java/judgels/sandalphon/LocalGit.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ public void init(Path rootDirPath) {
4545

4646
@Override
4747
public void clone(Path originDirPath, Path rootDirPath) {
48-
String srcDirAbsolutePath = fs.getFile(originDirPath).getAbsolutePath();
49-
String destDirAbsolutePath = fs.getFile(rootDirPath).getAbsolutePath();
50-
51-
fs.copyDirectory(Path.of(srcDirAbsolutePath), Path.of(destDirAbsolutePath));
48+
fs.createDirectory(rootDirPath);
49+
fs.copyDirectory(originDirPath, rootDirPath);
5250

5351
File destDir = fs.getFile(rootDirPath);
54-
String destURI = "file://" + srcDirAbsolutePath;
52+
String destURI = "file://" + fs.getFile(originDirPath).getAbsolutePath();
5553

5654
try {
5755
org.eclipse.jgit.api.Git.open(destDir).remoteAdd().setName("origin").setUri(new URIish(destURI)).call();

judgels-backends/judgels-server-app/src/test/java/judgels/fs/InMemoryFileSystem.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public boolean directoryExists(Path dirPath) {
2828
throw new UnsupportedOperationException();
2929
}
3030

31+
@Override
32+
public void copyDirectory(Path srcPath, Path destPath) {}
33+
3134
@Override
3235
public void createFile(Path filePath) {
3336
throw new UnsupportedOperationException();
@@ -135,10 +138,4 @@ public void writeByteArrayToFile(Path filePath, byte[] content) {}
135138
public byte[] readByteArrayFromFile(Path filePath) {
136139
return fs.get(filePath);
137140
}
138-
139-
@Override
140-
public void copyDirectory(Path src, Path dest) {}
141-
142-
@Override
143-
public void copy(Path src, Path dest) {}
144141
}

0 commit comments

Comments
 (0)