Skip to content

Commit e2470ec

Browse files
Pranav-0440netomi
andauthored
Crash at startup when local storage directory is not writable (eclipse-openvsx#1603)
* Fail fast at startup if local storage directory is not writable Signed-off-by: Pranav-0440 <pranavghorpade61@gmail.com> * Harden startup validation for local storage directory * Use Files.isWritable to validate local storage permissions Signed-off-by: Pranav-0440 <pranavghorpade61@gmail.com> * Remove duplicate Java settings in VS Code * Fix JSON formatting in settings.json * Update settings.json * Fix duplicate import in LocalStorageService Removed duplicate import of jakarta.annotation.PostConstruct. * simplify logic --------- Signed-off-by: Pranav-0440 <pranavghorpade61@gmail.com> Co-authored-by: Thomas Neidhart <thomas.neidhart@eclipse-foundation.org>
1 parent 17a63ed commit e2470ec

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

server/src/main/java/org/eclipse/openvsx/storage/LocalStorageService.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
* ****************************************************************************** */
1010
package org.eclipse.openvsx.storage;
1111

12+
import java.io.IOException;
13+
import java.net.URI;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.nio.file.StandardCopyOption;
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
20+
import jakarta.annotation.PostConstruct;
21+
1222
import org.apache.commons.lang3.StringUtils;
1323
import org.eclipse.openvsx.entities.FileResource;
1424
import org.eclipse.openvsx.entities.Namespace;
@@ -22,14 +32,6 @@
2232
import org.springframework.web.server.ServerErrorException;
2333
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
2434

25-
import java.io.IOException;
26-
import java.net.URI;
27-
import java.nio.file.Files;
28-
import java.nio.file.Path;
29-
import java.nio.file.StandardCopyOption;
30-
import java.util.ArrayList;
31-
import java.util.List;
32-
3335
@Component
3436
public class LocalStorageService implements IStorageService {
3537

@@ -40,6 +42,27 @@ public class LocalStorageService implements IStorageService {
4042
public boolean isEnabled() {
4143
return !StringUtils.isEmpty(storageDirectory);
4244
}
45+
46+
@PostConstruct
47+
public void validateStorageDirectory() throws IOException {
48+
if (!isEnabled()) {
49+
return;
50+
}
51+
52+
var storageDirectoryPath = Path.of(storageDirectory).toAbsolutePath();
53+
54+
if (!Files.exists(storageDirectoryPath)) {
55+
Files.createDirectories(storageDirectoryPath);
56+
}
57+
58+
if (!Files.isDirectory(storageDirectoryPath)) {
59+
throw new IllegalStateException("Local storage directory '" + storageDirectory + "' is not a directory");
60+
}
61+
62+
if (!Files.isWritable(storageDirectoryPath)) {
63+
throw new IllegalStateException("Local storage directory '" + storageDirectory + "' is not writable");
64+
}
65+
}
4366

4467
@Override
4568
public void uploadFile(TempFile tempFile) {

0 commit comments

Comments
 (0)