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 callback(null); |
| 61 if (localStorage.currentVersion) |
| 62 seenDataCorruption = true; |
| 63 delete localStorage.currentVersion; |
| 64 return; |
| 65 } |
| 66 |
| 67 var path = this._getFilePath(file); |
| 68 var runAsync = require("utils").Utils.runAsync; |
| 69 |
| 70 this._transaction(function(tx) |
| 71 { |
| 72 tx.executeSql( |
| 73 "SELECT content FROM files WHERE path = ?", |
| 74 [path], |
| 75 function(tx, results) |
| 76 { |
| 77 if (results.rows.length == 0) |
| 78 { |
| 79 runAsync(callback, null, new Error("File doesn't exist")); |
| 80 return; |
| 81 } |
| 82 |
| 83 var lines = results.rows.item(0).content.split(/[\r\n]+/); |
| 84 runAsync(function() |
| 85 { |
| 86 for (var i = 0; i < lines.length; i++) |
| 87 listener.process(lines[i]); |
| 88 listener.process(null); |
| 89 callback(null); |
| 90 }); |
| 91 } |
| 92 ); |
| 93 }); |
| 94 }, |
| 95 writeToFile: function(file, encode, data, callback, timeLineID) |
| 96 { |
| 97 var path = this._getFilePath(file); |
| 98 var lnbr = this.lineBreak; |
| 99 var runAsync = require("utils").Utils.runAsync; |
| 100 |
| 101 this._transaction(function(tx) |
| 102 { |
| 103 tx.executeSql( |
| 104 "INSERT OR REPLACE INTO files VALUES (?, ?, ?)", |
| 105 [path, Date.now(), data.join(lnbr) + lnbr], |
| 106 function() { runAsync(callback, null, null); } |
| 107 ); |
| 108 }); |
| 109 }, |
| 110 copyFile: function(fromFile, toFile, callback) |
| 111 { |
| 112 var fromPath = this._getFilePath(fromFile); |
| 113 var toPath = this._getFilePath(toFile); |
| 114 var runAsync = require("utils").Utils.runAsync; |
| 115 |
| 116 this._transaction(function(tx) |
| 117 { |
| 118 tx.executeSql( |
| 119 "INSERT OR REPLACE INTO files SELECT ?, ?, content FROM files WHERE path
= ?", |
| 120 [toPath, Date.now(), fromPath], |
| 121 function(tx, results) |
| 122 { |
| 123 if (results.rowsAffected == 0) |
| 124 runAsync(callback, null, new Error("File doesn't exist")); |
| 125 else |
| 126 runAsync(callback, null, null); |
| 127 } |
| 128 ); |
| 129 }); |
| 130 }, |
| 131 renameFile: function(fromFile, newName, callback) |
| 132 { |
| 133 var path = this._getFilePath(fromFile); |
| 134 var runAsync = require("utils").Utils.runAsync; |
| 135 |
| 136 this._transaction(function(tx) |
| 137 { |
| 138 tx.executeSql( |
| 139 "UPDATE files SET path = ? WHERE path = ?", |
| 140 [newName, path], |
| 141 function(tx, results) |
| 142 { |
| 143 if (results.rowsAffected == 0) |
| 144 runAsync(callback, null, new Error("File doesn't exist")); |
| 145 else |
| 146 runAsync(callback, null, null); |
| 147 } |
| 148 ); |
| 149 }); |
| 150 }, |
| 151 removeFile: function(file, callback) |
| 152 { |
| 153 var path = this._getFilePath(file); |
| 154 var runAsync = require("utils").Utils.runAsync; |
| 155 |
| 156 this._transaction(function(tx) |
| 157 { |
| 158 tx.executeSql( |
| 159 "DELETE FROM files WHERE path = ?", |
| 160 [path], |
| 161 function() { runAsync(callback, null, null); } |
| 162 ); |
| 163 }); |
| 164 }, |
| 165 statFile: function(file, callback) |
| 166 { |
| 167 var path = this._getFilePath(file); |
| 168 var runAsync = require("utils").Utils.runAsync; |
| 169 |
| 170 this._transaction(function(tx) |
| 171 { |
| 172 tx.executeSql( |
| 173 "SELECT last_modified FROM files WHERE path = ?", |
| 174 [path], |
| 175 function(tx, results) |
| 176 { |
| 177 if (results.rows.length == 0) |
| 178 runAsync(callback, null, null, { |
| 179 exists: false, |
| 180 isDirectory: false, |
| 181 isFile: false, |
| 182 lastModified: 0 |
| 183 }); |
| 184 else |
| 185 runAsync(callback, null, null, { |
| 186 exists: true, |
| 187 isDirectory: false, |
| 188 isFile: true, |
| 189 lastModified: results.rows.item(0).last_modified |
| 190 }); |
| 191 } |
| 192 ); |
| 193 }); |
| 194 } |
| 195 }; |
OLD | NEW |