| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2013 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 WebSQL API | |
| 20 // | |
| 21 | |
| 22 var IO = exports.IO = | |
| 23 { | |
| 24 _db: null, | |
| 25 lineBreak: "\n", | |
| 26 | |
| 27 _transaction: function(callback) | |
| 28 { | |
| 29 var dbCreated = false; | |
| 30 if (!this._db) | |
| 31 this._db = openDatabase("adblockplus", "1.0", "", 102400, function() { dbC
reated = true; }); | |
| 32 | |
| 33 this._db.transaction(function(tx) | |
| 34 { | |
| 35 if (dbCreated) | |
| 36 tx.executeSql("CREATE TABLE files (path unique, last_modified, content)"
); | |
| 37 | |
| 38 callback(tx); | |
| 39 }); | |
| 40 }, | |
| 41 _getFilePath: function(file) | |
| 42 { | |
| 43 if (file instanceof FakeFile) | |
| 44 return file.path; | |
| 45 if ("spec" in file) | |
| 46 return file.spec; | |
| 47 | |
| 48 throw new Error("Unexpected file type"); | |
| 49 }, | |
| 50 resolveFilePath: function(path) | |
| 51 { | |
| 52 return new FakeFile(path); | |
| 53 }, | |
| 54 readFromFile: function(file, decode, listener, callback, timeLineID) | |
| 55 { | |
| 56 if ("spec" in file && /^defaults\b/.test(file.spec)) | |
| 57 { | |
| 58 // Code attempts to read the default patterns.ini, we don't have that. | |
| 59 // Make sure to execute first-run actions instead. | |
| 60 var Utils = require("utils").Utils; | |
| 61 Utils.runAsync(function() | |
| 62 { | |
| 63 if (localStorage.currentVersion) | |
| 64 seenDataCorruption = true; | |
| 65 delete localStorage.currentVersion; | |
| 66 callback(null); | |
| 67 }); | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 var path = this._getFilePath(file); | |
| 72 var runAsync = require("utils").Utils.runAsync; | |
| 73 | |
| 74 this._transaction(function(tx) | |
| 75 { | |
| 76 tx.executeSql( | |
| 77 "SELECT content FROM files WHERE path = ?", | |
| 78 [path], | |
| 79 function(tx, results) | |
| 80 { | |
| 81 if (results.rows.length == 0) | |
| 82 { | |
| 83 runAsync(callback, null, new Error("File doesn't exist")); | |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 var lines = results.rows.item(0).content.split(/[\r\n]+/); | |
| 88 runAsync(function() | |
| 89 { | |
| 90 for (var i = 0; i < lines.length; i++) | |
| 91 listener.process(lines[i]); | |
| 92 listener.process(null); | |
| 93 callback(null); | |
| 94 }); | |
| 95 } | |
| 96 ); | |
| 97 }); | |
| 98 }, | |
| 99 writeToFile: function(file, encode, data, callback, timeLineID) | |
| 100 { | |
| 101 var path = this._getFilePath(file); | |
| 102 var lnbr = this.lineBreak; | |
| 103 var runAsync = require("utils").Utils.runAsync; | |
| 104 | |
| 105 this._transaction(function(tx) | |
| 106 { | |
| 107 tx.executeSql( | |
| 108 "INSERT OR REPLACE INTO files VALUES (?, ?, ?)", | |
| 109 [path, Date.now(), data.join(lnbr) + lnbr], | |
| 110 function() { runAsync(callback, null, null); } | |
| 111 ); | |
| 112 }); | |
| 113 }, | |
| 114 copyFile: function(fromFile, toFile, callback) | |
| 115 { | |
| 116 var fromPath = this._getFilePath(fromFile); | |
| 117 var toPath = this._getFilePath(toFile); | |
| 118 var runAsync = require("utils").Utils.runAsync; | |
| 119 | |
| 120 this._transaction(function(tx) | |
| 121 { | |
| 122 tx.executeSql( | |
| 123 "INSERT OR REPLACE INTO files SELECT ?, ?, content FROM files WHERE path
= ?", | |
| 124 [toPath, Date.now(), fromPath], | |
| 125 function(tx, results) | |
| 126 { | |
| 127 if (results.rowsAffected == 0) | |
| 128 runAsync(callback, null, new Error("File doesn't exist")); | |
| 129 else | |
| 130 runAsync(callback, null, null); | |
| 131 } | |
| 132 ); | |
| 133 }); | |
| 134 }, | |
| 135 renameFile: function(fromFile, newName, callback) | |
| 136 { | |
| 137 var path = this._getFilePath(fromFile); | |
| 138 var runAsync = require("utils").Utils.runAsync; | |
| 139 | |
| 140 this._transaction(function(tx) | |
| 141 { | |
| 142 tx.executeSql( | |
| 143 "UPDATE files SET path = ? WHERE path = ?", | |
| 144 [newName, path], | |
| 145 function(tx, results) | |
| 146 { | |
| 147 if (results.rowsAffected == 0) | |
| 148 runAsync(callback, null, new Error("File doesn't exist")); | |
| 149 else | |
| 150 runAsync(callback, null, null); | |
| 151 } | |
| 152 ); | |
| 153 }); | |
| 154 }, | |
| 155 removeFile: function(file, callback) | |
| 156 { | |
| 157 var path = this._getFilePath(file); | |
| 158 var runAsync = require("utils").Utils.runAsync; | |
| 159 | |
| 160 this._transaction(function(tx) | |
| 161 { | |
| 162 tx.executeSql( | |
| 163 "DELETE FROM files WHERE path = ?", | |
| 164 [path], | |
| 165 function() { runAsync(callback, null, null); } | |
| 166 ); | |
| 167 }); | |
| 168 }, | |
| 169 statFile: function(file, callback) | |
| 170 { | |
| 171 var path = this._getFilePath(file); | |
| 172 var runAsync = require("utils").Utils.runAsync; | |
| 173 | |
| 174 this._transaction(function(tx) | |
| 175 { | |
| 176 tx.executeSql( | |
| 177 "SELECT last_modified FROM files WHERE path = ?", | |
| 178 [path], | |
| 179 function(tx, results) | |
| 180 { | |
| 181 if (results.rows.length == 0) | |
| 182 runAsync(callback, null, null, { | |
| 183 exists: false, | |
| 184 isDirectory: false, | |
| 185 isFile: false, | |
| 186 lastModified: 0 | |
| 187 }); | |
| 188 else | |
| 189 runAsync(callback, null, null, { | |
| 190 exists: true, | |
| 191 isDirectory: false, | |
| 192 isFile: true, | |
| 193 lastModified: results.rows.item(0).last_modified | |
| 194 }); | |
| 195 } | |
| 196 ); | |
| 197 }); | |
| 198 } | |
| 199 }; | |
| OLD | NEW |