| Index: lib/filesystem/io.js |
| =================================================================== |
| --- a/lib/filesystem/io.js |
| +++ b/lib/filesystem/io.js |
| @@ -1,6 +1,6 @@ |
| /* |
| * This file is part of Adblock Plus <http://adblockplus.org/>, |
| - * Copyright (C) 2006-2013 Eyeo GmbH |
| + * Copyright (C) 2006-2014 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 |
| @@ -48,7 +48,7 @@ |
| return new FakeFile(path); |
| }, |
| - readFromFile: function(file, decode, listener, callback, timeLineID) |
| + readFromFile: function(file, listener, callback, timeLineID) |
| { |
| // Hack to allow importing old data |
| if (typeof file == "string") |
| @@ -65,25 +65,16 @@ |
| return; |
| } |
| - if ("spec" in file && /^defaults\b/.test(file.spec)) |
| - { |
| - // Code attempts to read the default patterns.ini, we don't have that. |
| - // Make sure to execute first-run actions instead. |
| - var Utils = require("utils").Utils; |
| - Utils.runAsync(function() |
| - { |
| - if (localStorage.currentVersion) |
| - seenDataCorruption = true; |
| - delete localStorage.currentVersion; |
| - callback(null); |
| - }); |
| - return; |
| - } |
| - |
| this._getFileEntry(file, false, function(fs, fileEntry) |
| { |
| fileEntry.file(function(file) |
| { |
| + if (file.size == 0) |
| + { |
| + callback("File is empty"); |
| + return; |
| + } |
| + |
| var reader = new FileReader(); |
| reader.onloadend = function() |
| { |
| @@ -103,7 +94,7 @@ |
| }, callback); |
| }, |
| - writeToFile: function(file, encode, data, callback, timeLineID) |
| + writeToFile: function(file, data, callback, timeLineID) |
| { |
| this._getFileEntry(file, true, function(fs, fileEntry) |
| { |
| @@ -122,25 +113,26 @@ |
| op(); |
| }.bind(this); |
| - executeWriteOperation(writer.truncate.bind(writer, 0), function() |
| + var blob; |
| + |
| + try |
| { |
| - var blob; |
| - try |
| - { |
| - blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {type: "text/plain"}); |
| - } |
| - catch (e) |
| - { |
| - if (!(e instanceof TypeError)) |
| - throw e; |
| + blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {type: "text/plain"}); |
| + } |
| + catch (e) |
| + { |
| + if (!(e instanceof TypeError)) |
| + throw e; |
| - // Blob wasn't a constructor before Chrome 20 |
| - var builder = new (window.BlobBuilder || window.WebKitBlobBuilder); |
| - builder.append(data.join(this.lineBreak) + this.lineBreak); |
| - blob = builder.getBlob("text/plain"); |
| - } |
| - executeWriteOperation(writer.write.bind(writer, blob), callback.bind(null, null)); |
| - }.bind(this)); |
| + // Blob wasn't a constructor before Chrome 20 |
| + var builder = new (window.BlobBuilder || window.WebKitBlobBuilder); |
| + builder.append(data.join(this.lineBreak) + this.lineBreak); |
| + blob = builder.getBlob("text/plain"); |
| + } |
| + executeWriteOperation(writer.write.bind(writer, blob), function() |
| + { |
| + executeWriteOperation(writer.truncate.bind(writer, writer.position), callback.bind(null, null)); |
| + }); |
| }.bind(this), callback); |
| }.bind(this), callback); |
| }, |
| @@ -149,7 +141,7 @@ |
| { |
| // Simply combine read and write operations |
| var data = []; |
| - this.readFromFile(fromFile, false, { |
| + this.readFromFile(fromFile, { |
| process: function(line) |
| { |
| if (line !== null) |
| @@ -160,7 +152,7 @@ |
| if (e) |
| callback(e); |
| else |
| - this.writeToFile(toFile, false, data, callback); |
| + this.writeToFile(toFile, data, callback); |
| }.bind(this)); |
| }, |
| @@ -201,17 +193,21 @@ |
| return; |
| } |
| - this._getFileEntry(file, false, function(fs, fileEntry) |
| - { |
| - fileEntry.getMetadata(function(metadata) |
| + // This needs to use Utils.runAsync(), otherwise FilterStorage might |
| + // initialize too early - see #337. |
| + require("utils").Utils.runAsync(function() { |
| + this._getFileEntry(file, false, function(fs, fileEntry) |
| { |
| - callback(null, { |
| - exists: true, |
| - isDirectory: fileEntry.isDirectory, |
| - isFile: fileEntry.isFile, |
| - lastModified: metadata.modificationTime.getTime() |
| - }); |
| + fileEntry.getMetadata(function(metadata) |
| + { |
| + callback(null, { |
| + exists: true, |
| + isDirectory: fileEntry.isDirectory, |
| + isFile: fileEntry.isFile, |
| + lastModified: metadata.modificationTime.getTime() |
| + }); |
| + }, callback); |
| }, callback); |
| - }, callback); |
| + }.bind(this)); |
| } |
| }; |