1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-23 12:51:16 +00:00

Fixed possible crash if FolderObserver could not be created.

This commit is contained in:
Felix Ableitner 2015-12-16 01:10:16 +01:00
parent 0350778db9
commit 98f98fddb9
2 changed files with 31 additions and 9 deletions

View file

@ -44,12 +44,13 @@ public class FolderObserverTest extends AndroidTestCase
private RestApi.Folder createFolder(String id) {
RestApi.Folder r = new RestApi.Folder();
r.path = mTestFolder.getAbsolutePath();
r.path = mTestFolder.getPath();
r.id = id;
return r;
}
public void testRecursion() throws IOException, InterruptedException {
public void testRecursion() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testRecursion";
File subFolder = new File(mTestFolder, "subfolder");
subFolder.mkdir();
@ -63,7 +64,8 @@ public class FolderObserverTest extends AndroidTestCase
fo.stopWatching();
}
public void testRemoveFile() throws IOException, InterruptedException {
public void testRemoveFile() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testRemoveFile";
File test = new File(mTestFolder, "test");
test.createNewFile();
@ -77,7 +79,8 @@ public class FolderObserverTest extends AndroidTestCase
fo.stopWatching();
}
public void testMoveDirectoryOut() throws IOException, InterruptedException {
public void testMoveDirectoryOut() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testMoveDirectory";
File subFolder = new File(mTestFolder, "subfolder");
subFolder.mkdir();
@ -94,7 +97,8 @@ public class FolderObserverTest extends AndroidTestCase
fo.stopWatching();
}
public void testAddDirectory() throws IOException, InterruptedException {
public void testAddDirectory() throws IOException, InterruptedException,
FolderObserver.FolderNotExistingException {
mCurrentTest = "testAddDirectory";
File subFolder = new File(mTestFolder, "subfolder");
subFolder.mkdir();
@ -109,4 +113,16 @@ public class FolderObserverTest extends AndroidTestCase
fo.stopWatching();
}
public void testNotExisting() throws IOException, InterruptedException {
RestApi.Folder r = new RestApi.Folder();
r.path = new File(new MockContext(getContext()).getFilesDir(), "not-existing").getPath();
r.id = "testNotExisting";
try {
new FolderObserver(this, r);
fail("Expected FolderNotExistingException");
} catch (FolderObserver.FolderNotExistingException e) {
assertTrue(e.getMessage().contains(r.path));
}
}
}

View file

@ -28,11 +28,12 @@ public class FolderObserver extends FileObserver {
public void onFolderFileChange(String folderId, String relativePath);
}
public FolderObserver(OnFolderFileChangeListener listener, RestApi.Folder folder) {
public FolderObserver(OnFolderFileChangeListener listener, RestApi.Folder folder)
throws FolderNotExistingException {
this(listener, folder, "");
}
public class FolderNotExistingException extends RuntimeException {
public class FolderNotExistingException extends Exception {
private String mPath;
@ -53,7 +54,8 @@ public class FolderObserver extends FileObserver {
* @param folder The folder where this folder belongs to.
* @param path path to the monitored folder, relative to folder root.
*/
private FolderObserver(OnFolderFileChangeListener listener, RestApi.Folder folder, String path) {
private FolderObserver(OnFolderFileChangeListener listener, RestApi.Folder folder, String path)
throws FolderNotExistingException {
super(folder.path + "/" + path,
ATTRIB | CLOSE_WRITE | CREATE | DELETE | DELETE_SELF | MOVED_FROM |
MOVED_TO | MOVE_SELF);
@ -115,7 +117,11 @@ public class FolderObserver extends FileObserver {
// fall through
case CREATE:
if (fullPath.isDirectory()) {
mChilds.add(new FolderObserver(mListener, mFolder, path));
try {
mChilds.add(new FolderObserver(mListener, mFolder, path));
} catch (FolderNotExistingException e) {
Log.w(TAG, "Failed to add listener for nonexisting folder", e);
}
}
// fall through
default: