Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: libadblockplus-android/src/org/adblockplus/android/AndroidFileSystem.java

Issue 29347315: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Patch Set: made helper methods static, fixed 'remove' for fs callback Created Dec. 13, 2016, 9:32 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: libadblockplus-android/src/org/adblockplus/android/AndroidFileSystem.java
diff --git a/libadblockplus-android/src/org/adblockplus/android/AndroidFileSystem.java b/libadblockplus-android/src/org/adblockplus/android/AndroidFileSystem.java
new file mode 100644
index 0000000000000000000000000000000000000000..903f306a917ff28e279be1ea24d17a9e47321894
--- /dev/null
+++ b/libadblockplus-android/src/org/adblockplus/android/AndroidFileSystem.java
@@ -0,0 +1,142 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.adblockplus.android;
+
+import org.adblockplus.libadblockplus.AdblockPlusException;
+import org.adblockplus.libadblockplus.FileSystem;
+import org.adblockplus.libadblockplus.FileSystemUtils;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+
+public class AndroidFileSystem extends FileSystem
+{
+ private FileSystemUtils fileSystemUtils = new FileSystemUtils();
+ private File basePath;
+
+ public File getBasePath()
+ {
+ return basePath;
+ }
+
+ public AndroidFileSystem()
+ {
+ }
+
+ /*
+ * Sets the base path, all paths are considered relative to it.
+ * @param basePath base path
+ */
+ public AndroidFileSystem(File basePath)
+ {
+ this();
diegocarloslima 2017/04/27 12:22:40 This call isn't required
anton 2017/04/28 08:25:45 I think it's required as if we have some init logi
+ this.basePath = basePath;
+ }
+
+ private File buildFullPath(String path)
+ {
+ return basePath != null
+ ? new File(basePath, path)
+ : new File(path);
+ }
+
+ @Override
+ public String read(String path)
+ {
+ File file = buildFullPath(path);
+ if (!file.exists())
+ {
+ return null;
+ }
+
+ try
+ {
+ return fileSystemUtils.readFile(file);
+ }
+ catch (FileNotFoundException e)
+ {
+ throw new AdblockPlusException(e);
+ }
+ }
+
+ @Override
+ public void write(String path, String data)
+ {
+ File file = buildFullPath(path);
+ if (file.exists())
+ {
+ file.delete();
+ }
+
+ try
+ {
+ fileSystemUtils.writeFile(file, data);
+ }
+ catch (IOException e)
+ {
+ throw new AdblockPlusException(e);
+ }
+ }
+
+ @Override
+ public void move(String fromPath, String toPath)
+ {
+ File fullFromFile = buildFullPath(fromPath);
+ try
+ {
+ String data = fileSystemUtils.readFile(fullFromFile);
+ write(toPath, data);
+ remove(fromPath);
+ }
+ catch (FileNotFoundException e)
+ {
+ throw new AdblockPlusException(e);
+ }
+ }
+
+ @Override
+ public void remove(String path)
+ {
+ File file = buildFullPath(path);
+ if (file.exists())
+ {
+ file.delete();
+ }
+ }
+
+ @Override
+ public StatResult stat(String path)
+ {
+ File file = buildFullPath(path);
+ return new StatResult(
+ file.exists(),
+ file.isDirectory(),
+ file.isFile(),
+ file.lastModified());
+ }
+
+ @Override
+ public String resolve(String path)
+ {
+ String fullPath = buildFullPath(path).getAbsolutePath();
+ return basePath != null
+ ? fullPath.substring(basePath.getAbsolutePath().length())
+ : fullPath;
+ }
+}

Powered by Google App Engine
This is Rietveld