OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 // |
| 19 // No direct file system access, using LocalStorage API |
| 20 // |
| 21 |
| 22 var IO = exports.IO = |
| 23 { |
| 24 _getFilePath: function(file) |
| 25 { |
| 26 if (file instanceof FakeFile) |
| 27 return file.path; |
| 28 else if ("spec" in file) |
| 29 return file.spec; |
| 30 |
| 31 throw new Error("Unexpected file type"); |
| 32 }, |
| 33 |
| 34 _setFileContents: function(path, contents, lastModified) |
| 35 { |
| 36 ext.storage[path] = contents; |
| 37 ext.storage[path + "/lastModified"] = lastModified || 0; |
| 38 }, |
| 39 |
| 40 lineBreak: "\n", |
| 41 |
| 42 resolveFilePath: function(path) |
| 43 { |
| 44 return new FakeFile(path); |
| 45 }, |
| 46 |
| 47 readFromFile: function(file, listener, callback, timeLineID) |
| 48 { |
| 49 var Utils = require("utils").Utils; |
| 50 Utils.runAsync(function() |
| 51 { |
| 52 var path = this._getFilePath(file); |
| 53 if (!(path in ext.storage)) |
| 54 { |
| 55 callback(new Error("File doesn't exist")) |
| 56 return; |
| 57 } |
| 58 |
| 59 var lines = ext.storage[path].split(/[\r\n]+/); |
| 60 for (var i = 0; i < lines.length; i++) |
| 61 listener.process(lines[i]); |
| 62 listener.process(null); |
| 63 callback(null); |
| 64 }.bind(this)); |
| 65 }, |
| 66 |
| 67 writeToFile: function(file, data, callback, timeLineID) |
| 68 { |
| 69 var path = this._getFilePath(file); |
| 70 this._setFileContents(path, data.join(this.lineBreak) + this.lineBreak, Date
.now()); |
| 71 |
| 72 var Utils = require("utils").Utils; |
| 73 Utils.runAsync(callback, null, null); |
| 74 }, |
| 75 |
| 76 copyFile: function(fromFile, toFile, callback) |
| 77 { |
| 78 // Simply combine read and write operations |
| 79 var data = []; |
| 80 this.readFromFile(fromFile, { |
| 81 process: function(line) |
| 82 { |
| 83 if (line !== null) |
| 84 data.push(line); |
| 85 } |
| 86 }, function(e) |
| 87 { |
| 88 if (e) |
| 89 callback(e); |
| 90 else |
| 91 this.writeToFile(toFile, data, callback); |
| 92 }.bind(this)); |
| 93 }, |
| 94 |
| 95 renameFile: function(fromFile, newName, callback) |
| 96 { |
| 97 var path = this._getFilePath(fromFile); |
| 98 if (!(path in ext.storage)) |
| 99 { |
| 100 callback(new Error("File doesn't exist")) |
| 101 return; |
| 102 } |
| 103 |
| 104 this._setFileContents(newName, ext.storage[path], ext.storage[path + "/lastM
odified"]); |
| 105 this.removeFile(fromFile, callback); |
| 106 }, |
| 107 |
| 108 removeFile: function(file, callback) |
| 109 { |
| 110 var path = this._getFilePath(file); |
| 111 delete ext.storage[path]; |
| 112 delete ext.storage[path + "/lastModified"]; |
| 113 callback(null); |
| 114 }, |
| 115 |
| 116 statFile: function(file, callback) |
| 117 { |
| 118 var path = this._getFilePath(file); |
| 119 |
| 120 // This needs to use Utils.runAsync(), otherwise FilterStorage might |
| 121 // initialize too early - see #337. |
| 122 require("utils").Utils.runAsync(callback.bind(null, null, { |
| 123 exists: path in ext.storage, |
| 124 isDirectory: false, |
| 125 isFile: true, |
| 126 lastModified: parseInt(ext.storage[path + "/lastModified"], 10) || 0 |
| 127 })); |
| 128 } |
| 129 }; |
OLD | NEW |