Skip to content

Commit 7151f83

Browse files
authored
ADFA-2540 | Fix crash when saving file in non-existent directory (#826)
* fix: ensure parent directory exists before saving file This prevents ENOENT crashes by attempting to create missing parent directories. * refactor: Avoid possible race condition
1 parent bf3fbce commit 7151f83

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

‎editor/src/main/java/com/itsaky/androidide/editor/utils/ContentReadWrite.kt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ object ContentReadWrite {
4141
fun Content.writeTo(file: File, progressConsumer: ((Int) -> Unit)? = null) {
4242
val consumer = discreteProgressConsumer(progressConsumer = progressConsumer)
4343

44+
checkForParentDir(file)
45+
4446
file.writer().buffered(DEFAULT_BUFFER_SIZE * 2).use { writer ->
4547
val lastLine = lineCount - 1
4648
val length = length
@@ -152,4 +154,12 @@ object ContentReadWrite {
152154

153155
return consumer
154156
}
157+
158+
private fun checkForParentDir(file: File) {
159+
val parent = file.parentFile ?: return
160+
161+
if (!parent.exists() && !parent.mkdirs() && !parent.exists()) {
162+
throw IOException("The parent directory could not be created for: ${file.absolutePath}")
163+
}
164+
}
155165
}

0 commit comments

Comments
 (0)