Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException;
import com.owncloud.android.lib.common.network.CertificateCombinedException;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.files.CreateLocalFileException;
import com.owncloud.android.lib.resources.notifications.models.Notification;
import com.owncloud.android.lib.resources.notifications.models.PushResponse;

Expand Down Expand Up @@ -302,6 +303,12 @@ public RemoteOperationResult(Exception e) {
}
} else if (e instanceof FileNotFoundException) {
mCode = ResultCode.LOCAL_FILE_NOT_FOUND;
} else if (e instanceof CreateLocalFileException) {
if (((CreateLocalFileException) e).isCausedByInvalidPath()) {
mCode = ResultCode.INVALID_LOCAL_FILE_NAME;
} else {
mCode = ResultCode.CANNOT_CREATE_FILE;
}
} else {
mCode = ResultCode.UNKNOWN_ERROR;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Nextcloud Android Library is available under MIT license
*
* @author Álvaro Brey
* Copyright (C) 2023 Álvaro Brey
* Copyright (C) 2023 Nextcloud GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package com.owncloud.android.lib.resources.files

import java.io.IOException

class CreateLocalFileException(val path: String, cause: Throwable) : Exception(cause) {

override val message: String = "File could not be created"

/**
* Checks if the path associated to the exception contains invalid characters.
* There is no better way to check this, as `Paths` is not available in API < 26, and since this lib has a very low
* minSdk, that can't even be worked around with an `if` block.
*/
fun isCausedByInvalidPath(): Boolean {
return cause is IOException && (path.isEmpty() || INVALID_CHARS.any { path.contains(it) })
}

companion object {
private const val INVALID_CHARS = "\\:*?\"<>|"
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/* ownCloud Android Library is available under MIT license
* Copyright (C) 2015 ownCloud Inc.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
Expand Down Expand Up @@ -100,7 +100,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
}


private int downloadFile(OwnCloudClient client, File targetFile) throws IOException, OperationCancelledException {
private int downloadFile(OwnCloudClient client, File targetFile) throws IOException, OperationCancelledException, CreateLocalFileException {
int status;
boolean savedFile = false;
getMethod = new GetMethod(client.getFilesDavUri(remotePath));
Expand All @@ -110,15 +110,20 @@ private int downloadFile(OwnCloudClient client, File targetFile) throws IOExcept
try {
status = client.executeMethod(getMethod);
if (isSuccess(status)) {
targetFile.createNewFile();
try {
targetFile.createNewFile();
} catch (IOException | SecurityException ex) {
Log_OC.e(TAG, "Error creating file " + targetFile.getAbsolutePath(), ex);
throw new CreateLocalFileException(targetFile.getPath(), ex);
}
BufferedInputStream bis = new BufferedInputStream(getMethod.getResponseBodyAsStream());
fos = new FileOutputStream(targetFile);
long transferred = 0;

Header contentLength = getMethod.getResponseHeader("Content-Length");
long totalToTransfer = (contentLength != null &&
contentLength.getValue().length() > 0) ?
Long.parseLong(contentLength.getValue()) : 0;
contentLength.getValue().length() > 0) ?
Long.parseLong(contentLength.getValue()) : 0;

byte[] bytes = new byte[4096];
int readResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* Nextcloud Android Library is available under MIT license
*
* @author Álvaro Brey
* Copyright (C) 2023 Álvaro Brey
* Copyright (C) 2023 Nextcloud GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package com.owncloud.android.lib.resources.files

import org.junit.Assert.assertEquals
import org.junit.Test
import java.io.IOException

class CreateLocalFileExceptionTest {

private fun buildException(path: String, cause: Throwable = IOException()): CreateLocalFileException {
return CreateLocalFileException(path, cause)
}

@Test
fun `causedByInvalidPath should be false if cause is not IOException`() {
testInvalidPath("..::..://\\#<>", cause = Exception(), expected = false)
}

@Test
fun `causedByInvalidPath should be false if path is valid`() {
testInvalidPath("/path/too/foo.tar", expected = false)
testInvalidPath("/path/too/foo", expected = false)
testInvalidPath("/foo.tar", expected = false)
testInvalidPath("/foo", expected = false)
testInvalidPath("/", expected = false)
}

@Test
fun `causedByInvalidPath should be true if path contains invalid chars and cause is IOException`() {
testInvalidPath("")
testInvalidPath("/path/to:to/foo.tar")
testInvalidPath("/pa:th/to/foo.tar")

testInvalidPath("/path/to/foo:tar")
testInvalidPath("/path/to/foo\\tar")
testInvalidPath("/path/to/foo<tar")
testInvalidPath("/path/to/foo>tar")
testInvalidPath("/path/to/foo*tar")
testInvalidPath("/path/to/foo?tar")
testInvalidPath("/path/to/foo|tar")
}

private fun testInvalidPath(path: String, cause: Throwable = IOException(), expected: Boolean = true) {
val exception = buildException(path, cause)
assertEquals(
"Wrong value for isCausedByInvalidPath, path=\"$path\"",
expected,
exception.isCausedByInvalidPath()
)
}
}
2 changes: 1 addition & 1 deletion scripts/analysis/findbugs-results.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
141
140