Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions app/src/main/java/me/zhanghai/android/files/filejob/FileJobs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ class DeleteFileJob(private val paths: List<Path>) : FileJob() {
Files.walkFileTree(path, object : SimpleFileVisitor<Path>() {
@Throws(IOException::class)
override fun visitFile(file: Path, attributes: BasicFileAttributes): FileVisitResult {
delete(file, transferInfo, actionAllInfo)
delete(file, attributes.isDirectory, transferInfo, actionAllInfo)
throwIfInterrupted()
return FileVisitResult.CONTINUE
}
Expand All @@ -984,7 +984,7 @@ class DeleteFileJob(private val paths: List<Path>) : FileJob() {
if (exception != null) {
throw exception
}
delete(directory, transferInfo, actionAllInfo)
delete(directory, true, transferInfo, actionAllInfo)
throwIfInterrupted()
return FileVisitResult.CONTINUE
}
Expand All @@ -993,12 +993,17 @@ class DeleteFileJob(private val paths: List<Path>) : FileJob() {
}

@Throws(IOException::class)
private fun FileJob.delete(path: Path, transferInfo: TransferInfo?, actionAllInfo: ActionAllInfo) {
private fun FileJob.delete(
path: Path,
isDirectory: Boolean?,
transferInfo: TransferInfo?,
actionAllInfo: ActionAllInfo
) {
var retry: Boolean
do {
retry = false
try {
path.delete()
path.delete(isDirectory)
if (transferInfo != null) {
transferInfo.incrementTransferredFileCount()
postDeleteNotification(transferInfo, path)
Expand Down Expand Up @@ -1152,7 +1157,7 @@ class MoveFileJob(private val sources: List<Path>, private val targetDirectory:
if (exception != null) {
throw exception
}
delete(directory, null, actionAllInfo)
delete(directory, true, null, actionAllInfo)
throwIfInterrupted()
return FileVisitResult.CONTINUE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class ProviderFtpFile(
false
} else {
try {
path.delete()
path.delete(null)
true
} catch (e: IOException) {
e.printStackTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ internal object ForeignCopyMove {
}
copy(source, target, *optionsForCopy)
try {
source.delete()
source.delete(null)
} catch (e: IOException) {
if (e !is NoSuchFileException) {
try {
target.delete()
target.delete(null)
} catch (e2: IOException) {
e.addSuppressed(e2)
} catch (e2: UnsupportedOperationException) {
Expand All @@ -156,7 +156,7 @@ internal object ForeignCopyMove {
throw e
} catch (e: UnsupportedOperationException) {
try {
target.delete()
target.delete(null)
} catch (e2: IOException) {
e.addSuppressed(e2)
} catch (e2: UnsupportedOperationException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package me.zhanghai.android.files.provider.common

import me.zhanghai.android.files.provider.webdav.WebDavFileSystemProvider
import me.zhanghai.android.files.provider.webdav.WebDavPath
import java8.nio.channels.SeekableByteChannel
import java8.nio.file.AccessMode
import java8.nio.file.CopyOption
Expand Down Expand Up @@ -81,8 +83,12 @@ fun Path.createSymbolicLink(target: ByteString, vararg attributes: FileAttribute
createSymbolicLink(ByteStringPath(target), *attributes)

@Throws(IOException::class)
fun Path.delete() {
Files.delete(this)
fun Path.delete(isDirectory: Boolean?) {
if (provider == WebDavFileSystemProvider && this is WebDavPath) {
WebDavFileSystemProvider.delete(this, isDirectory)
} else {
Files.delete(this)
}
}

@Throws(IOException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal object WebDavCopyMove {
throw FileAlreadyExistsException(source.toString(), target.toString(), null)
}
try {
Client.delete(target)
Client.delete(target, targetFile.isDirectory)
} catch (e: DavException) {
throw e.toFileSystemException(target.toString())
}
Expand Down Expand Up @@ -139,14 +139,14 @@ internal object WebDavCopyMove {
throw FileAlreadyExistsException(source.toString(), target.toString(), null)
}
try {
Client.delete(target)
Client.delete(target, targetResponse.isDirectory)
} catch (e: DavException) {
throw e.toFileSystemException(target.toString())
}
}
var renameSuccessful = false
try {
Client.move(source, target)
Client.move(source, target, sourceResponse.isDirectory)
renameSuccessful = true
} catch (e: DavException) {
if (copyOptions.atomicMove) {
Expand All @@ -170,11 +170,11 @@ internal object WebDavCopyMove {
}
copy(source, target, copyOptions)
try {
Client.delete(source)
Client.delete(source, sourceResponse.isDirectory)
} catch (e: DavException) {
if (e.toFileSystemException(source.toString()) !is NoSuchFileException) {
try {
Client.delete(target)
Client.delete(target, sourceResponse.isDirectory)
} catch (e2: DavException) {
e.addSuppressed(e2.toFileSystemException(target.toString()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import me.zhanghai.android.files.provider.common.toOpenOptions
import me.zhanghai.android.files.provider.webdav.client.Authority
import me.zhanghai.android.files.provider.webdav.client.Client
import me.zhanghai.android.files.provider.webdav.client.Protocol
import me.zhanghai.android.files.provider.webdav.client.isDirectory
import me.zhanghai.android.files.provider.webdav.client.isSymbolicLink
import java.io.IOException
import java.io.InputStream
Expand Down Expand Up @@ -300,8 +301,22 @@ object WebDavFileSystemProvider : FileSystemProvider(), PathObservableProvider,
@Throws(IOException::class)
override fun delete(path: Path) {
path as? WebDavPath ?: throw ProviderMismatchException(path.toString())
delete(path, null)
}

@Throws(IOException::class)
internal fun delete(path: WebDavPath, isDirectory: Boolean?) {
val resolvedIsDirectory = if (isDirectory != null) {
isDirectory
} else {
try {
Client.findProperties(path, true).isDirectory
} catch (e: DavException) {
throw e.toFileSystemException(path.toString())
}
}
try {
Client.delete(path)
Client.delete(path, resolvedIsDirectory)
} catch (e: DavException) {
throw e.toFileSystemException(path.toString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ internal class WebDavPath : ByteStringListPath<WebDavPath>, Client.Path {
.addPathSegments(toString().removePrefix("/"))
.build()

override val directoryUrl: HttpUrl
get() = if (toString() == "/") {
url
} else {
url.newBuilder()
.addPathSegment("")
.build()
}

private constructor(source: Parcel) : super(source) {
fileSystem = source.readParcelable()!!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ object Client {
}

@Throws(DavException::class)
fun delete(path: Path) {
fun delete(path: Path, isDirectory: Boolean = false) {
try {
DavResource(getClient(path.authority), path.url).delete {}
DavResource(getClient(path.authority), path.url(isDirectory)).delete {}
} catch (e: IOException) {
throw e.toDavException()
}
Expand All @@ -110,12 +110,13 @@ object Client {
}

@Throws(DavException::class)
fun move(source: Path, target: Path) {
fun move(source: Path, target: Path, isDirectory: Boolean = false) {
if (source.authority != target.authority) {
throw IOException("Paths aren't on the same authority")
}
try {
DavResource(getClient(source.authority), source.url).move(target.url, false) {}
DavResource(getClient(source.authority), source.url(isDirectory))
.move(target.url(isDirectory), false) {}
} catch (e: IOException) {
throw e.toDavException()
}
Expand Down Expand Up @@ -257,9 +258,17 @@ object Client {
interface Path {
val authority: Authority
val url: HttpUrl
val directoryUrl: HttpUrl
fun resolve(other: String): Path
}

private fun Path.url(isDirectory: Boolean): HttpUrl =
if (isDirectory) {
directoryUrl
} else {
url
}

private class OkHttpAuthenticatorInterceptor(
private val authenticator: Authenticator,
private val authority: Authority
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ class ImageViewerFragment : Fragment(), ConfirmDeleteDialogFragment.Listener {

override fun delete(path: Path) {
try {
path.delete()
// The image viewer only deletes files, se we pass isDirectory=false
path.delete(false)
} catch (e: IOException) {
e.printStackTrace()
showToast(e.toString())
Expand Down