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