diff --git a/app/src/main/java/me/zhanghai/android/files/provider/ftp/FtpFileSystemProvider.kt b/app/src/main/java/me/zhanghai/android/files/provider/ftp/FtpFileSystemProvider.kt index e9bd9112..570a856b 100644 --- a/app/src/main/java/me/zhanghai/android/files/provider/ftp/FtpFileSystemProvider.kt +++ b/app/src/main/java/me/zhanghai/android/files/provider/ftp/FtpFileSystemProvider.kt @@ -140,14 +140,14 @@ object FtpFileSystemProvider : FileSystemProvider(), PathObservableProvider, Sea } catch (e: IOException) { throw e.toFileSystemExceptionForFtp(file.toString()) } + if (openOptions.createNew && fileFile != null) { + throw FileAlreadyExistsException(file.toString()) + } if (openOptions.noFollowLinks && fileFile != null && fileFile.isSymbolicLink) { throw FileSystemException( file.toString(), null, "File is a symbolic link: $fileFile" ) } - if (openOptions.createNew && fileFile != null) { - throw FileAlreadyExistsException(file.toString()) - } if ((openOptions.create || openOptions.createNew) && fileFile == null) { try { Client.createFile(file) @@ -221,6 +221,32 @@ object FtpFileSystemProvider : FileSystemProvider(), PathObservableProvider, Sea if (openOptions.write && !openOptions.truncateExisting) { throw UnsupportedOperationException("Missing ${StandardOpenOption.TRUNCATE_EXISTING}") } + if (openOptions.write || openOptions.create || openOptions.createNew || + openOptions.noFollowLinks) { + val fileFile = try { + Client.listFileOrNull(file, true) + } catch (e: IOException) { + throw e.toFileSystemExceptionForFtp(file.toString()) + } + if (openOptions.createNew && fileFile != null) { + throw FileAlreadyExistsException(file.toString()) + } + if (openOptions.noFollowLinks && fileFile != null && fileFile.isSymbolicLink) { + throw FileSystemException( + file.toString(), null, "File is a symbolic link: $fileFile" + ) + } + if (fileFile == null) { + if (!(openOptions.create || openOptions.createNew)) { + throw NoSuchFileException(file.toString()) + } + try { + Client.createFile(file) + } catch (e: IOException) { + throw e.toFileSystemExceptionForFtp(file.toString()) + } + } + } if (attributes.isNotEmpty()) { throw UnsupportedOperationException(attributes.contentToString()) }