| OLD | NEW | 
|---|
|  | (Empty) | 
| 1 /* |  | 
| 2  * This file is part of Adblock Plus <https://adblockplus.org/>, |  | 
| 3  * Copyright (C) 2006-2015 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 FileSystem API |  | 
| 20 // |  | 
| 21 |  | 
| 22 var IO = exports.IO = |  | 
| 23 { |  | 
| 24   _getFileEntry: function(file, create, successCallback, errorCallback) |  | 
| 25   { |  | 
| 26     if (file instanceof FakeFile) |  | 
| 27       file = file.path; |  | 
| 28     else if ("spec" in file) |  | 
| 29       file = file.spec; |  | 
| 30 |  | 
| 31     // Remove directory path - we operate on a single directory in Chrome |  | 
| 32     file = file.replace(/^.*[\/\\]/, ""); |  | 
| 33 |  | 
| 34     // We request a gigabyte of space, just in case |  | 
| 35     (window.requestFileSystem || window.webkitRequestFileSystem)(window.PERSISTE
     NT, 1024*1024*1024, function(fs) |  | 
| 36     { |  | 
| 37       fs.root.getFile(file, {create: create}, function(fileEntry) |  | 
| 38       { |  | 
| 39         successCallback(fs, fileEntry); |  | 
| 40       }, errorCallback); |  | 
| 41     }, errorCallback); |  | 
| 42   }, |  | 
| 43 |  | 
| 44   lineBreak: "\n", |  | 
| 45 |  | 
| 46   resolveFilePath: function(path) |  | 
| 47   { |  | 
| 48     return new FakeFile(path); |  | 
| 49   }, |  | 
| 50 |  | 
| 51   readFromFile: function(file, listener, callback, timeLineID) |  | 
| 52   { |  | 
| 53     this._getFileEntry(file, false, function(fs, fileEntry) |  | 
| 54     { |  | 
| 55       fileEntry.file(function(file) |  | 
| 56       { |  | 
| 57         if (file.size == 0) |  | 
| 58         { |  | 
| 59           callback("File is empty"); |  | 
| 60           return; |  | 
| 61         } |  | 
| 62 |  | 
| 63         var reader = new FileReader(); |  | 
| 64         reader.onloadend = function() |  | 
| 65         { |  | 
| 66           if (reader.error) |  | 
| 67             callback(reader.error); |  | 
| 68           else |  | 
| 69           { |  | 
| 70             var lines = reader.result.split(/[\r\n]+/); |  | 
| 71             for (var i = 0; i < lines.length; i++) |  | 
| 72               listener.process(lines[i]); |  | 
| 73             listener.process(null); |  | 
| 74             callback(null); |  | 
| 75           } |  | 
| 76         }; |  | 
| 77         reader.readAsText(file); |  | 
| 78       }, callback); |  | 
| 79     }, callback); |  | 
| 80   }, |  | 
| 81 |  | 
| 82   writeToFile: function(file, data, callback, timeLineID) |  | 
| 83   { |  | 
| 84     this._getFileEntry(file, true, function(fs, fileEntry) |  | 
| 85     { |  | 
| 86       fileEntry.createWriter(function(writer) |  | 
| 87       { |  | 
| 88         var executeWriteOperation = function(op, nextOperation) |  | 
| 89         { |  | 
| 90           writer.onwriteend = function() |  | 
| 91           { |  | 
| 92             if (writer.error) |  | 
| 93               callback(writer.error); |  | 
| 94             else |  | 
| 95               nextOperation(); |  | 
| 96           }.bind(this); |  | 
| 97 |  | 
| 98           op(); |  | 
| 99         }.bind(this); |  | 
| 100 |  | 
| 101         var blob; |  | 
| 102 |  | 
| 103         try |  | 
| 104         { |  | 
| 105           blob = new Blob([data.join(this.lineBreak) + this.lineBreak], {type: "
     text/plain"}); |  | 
| 106         } |  | 
| 107         catch (e) |  | 
| 108         { |  | 
| 109           if (!(e instanceof TypeError)) |  | 
| 110             throw e; |  | 
| 111 |  | 
| 112           // Blob wasn't a constructor before Chrome 20 |  | 
| 113           var builder = new (window.BlobBuilder || window.WebKitBlobBuilder); |  | 
| 114           builder.append(data.join(this.lineBreak) + this.lineBreak); |  | 
| 115           blob = builder.getBlob("text/plain"); |  | 
| 116         } |  | 
| 117         executeWriteOperation(writer.write.bind(writer, blob), function() |  | 
| 118         { |  | 
| 119           executeWriteOperation(writer.truncate.bind(writer, writer.position), c
     allback.bind(null, null)); |  | 
| 120         }); |  | 
| 121       }.bind(this), callback); |  | 
| 122     }.bind(this), callback); |  | 
| 123   }, |  | 
| 124 |  | 
| 125   copyFile: function(fromFile, toFile, callback) |  | 
| 126   { |  | 
| 127     // Simply combine read and write operations |  | 
| 128     var data = []; |  | 
| 129     this.readFromFile(fromFile, { |  | 
| 130       process: function(line) |  | 
| 131       { |  | 
| 132         if (line !== null) |  | 
| 133           data.push(line); |  | 
| 134       } |  | 
| 135     }, function(e) |  | 
| 136     { |  | 
| 137       if (e) |  | 
| 138         callback(e); |  | 
| 139       else |  | 
| 140         this.writeToFile(toFile, data, callback); |  | 
| 141     }.bind(this)); |  | 
| 142   }, |  | 
| 143 |  | 
| 144   renameFile: function(fromFile, newName, callback) |  | 
| 145   { |  | 
| 146     this._getFileEntry(fromFile, false, function(fs, fileEntry) |  | 
| 147     { |  | 
| 148       fileEntry.moveTo(fs.root, newName, function() |  | 
| 149       { |  | 
| 150         callback(null); |  | 
| 151       }, callback); |  | 
| 152     }, callback); |  | 
| 153   }, |  | 
| 154 |  | 
| 155   removeFile: function(file, callback) |  | 
| 156   { |  | 
| 157     this._getFileEntry(file, false, function(fs, fileEntry) |  | 
| 158     { |  | 
| 159       fileEntry.remove(function() |  | 
| 160       { |  | 
| 161         callback(null); |  | 
| 162       }, callback); |  | 
| 163     }, callback); |  | 
| 164   }, |  | 
| 165 |  | 
| 166   statFile: function(file, callback) |  | 
| 167   { |  | 
| 168     // This needs to use Utils.runAsync(), otherwise FilterStorage might |  | 
| 169     // initialize too early - see #337. |  | 
| 170     require("utils").Utils.runAsync(function() { |  | 
| 171       this._getFileEntry(file, false, function(fs, fileEntry) |  | 
| 172       { |  | 
| 173         fileEntry.getMetadata(function(metadata) |  | 
| 174         { |  | 
| 175           callback(null, { |  | 
| 176             exists: true, |  | 
| 177             isDirectory: fileEntry.isDirectory, |  | 
| 178             isFile: fileEntry.isFile, |  | 
| 179             lastModified: metadata.modificationTime.getTime() |  | 
| 180           }); |  | 
| 181         }, callback); |  | 
| 182       }, callback); |  | 
| 183     }.bind(this)); |  | 
| 184   } |  | 
| 185 }; |  | 
| OLD | NEW | 
|---|