OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 | 51 |
52 exports.IO = | 52 exports.IO = |
53 { | 53 { |
54 resolveFilePath: function(path) | 54 resolveFilePath: function(path) |
55 { | 55 { |
56 return new FakeFile(path); | 56 return new FakeFile(path); |
57 }, | 57 }, |
58 | 58 |
59 readFromFile: function(file, listener, callback) | 59 readFromFile: function(file, listener, callback) |
60 { | 60 { |
61 runWhenMigrated(function() | 61 function onLoaded(entry) |
62 { | 62 { |
63 function onLoaded(entry) | 63 for (let line of entry.content) |
64 { | 64 listener.process(line); |
65 for (let line of entry.content) | |
66 listener.process(line); | |
67 | 65 |
68 listener.process(null); | 66 listener.process(null); |
69 callback(null); | 67 callback(null); |
70 } | 68 } |
71 | 69 |
72 loadFile(file, onLoaded, callback); | 70 loadFile(file, onLoaded, callback); |
73 }); | |
74 }, | 71 }, |
75 | 72 |
76 writeToFile: function(file, data, callback) | 73 writeToFile: function(file, data, callback) |
77 { | 74 { |
78 runWhenMigrated(function() | 75 saveFile(file, data, callback); |
79 { | |
80 saveFile(file, data, callback); | |
81 }); | |
82 }, | 76 }, |
83 | 77 |
84 copyFile: function(fromFile, toFile, callback) | 78 copyFile: function(fromFile, toFile, callback) |
85 { | 79 { |
86 runWhenMigrated(function() | 80 function onLoaded(entry) |
87 { | 81 { |
88 function onLoaded(entry) | 82 saveFile(toFile, entry.content, callback); |
89 { | 83 } |
90 saveFile(toFile, entry.content, callback); | |
91 } | |
92 | 84 |
93 loadFile(fromFile, onLoaded, callback); | 85 loadFile(fromFile, onLoaded, callback); |
94 }); | |
95 }, | 86 }, |
96 | 87 |
97 renameFile: function(fromFile, newName, callback) | 88 renameFile: function(fromFile, newName, callback) |
98 { | 89 { |
99 runWhenMigrated(function() | 90 function onLoaded() |
100 { | 91 { |
101 function onLoaded() | 92 ext.storage.remove(fileToKey(fromFile), function() |
102 { | 93 { |
103 ext.storage.remove(fileToKey(fromFile), function() | 94 ext.storage.set(keyPrefix + newName, entry, callback); |
104 { | 95 }); |
105 ext.storage.set(keyPrefix + newName, entry, callback); | 96 } |
106 }); | |
107 } | |
108 | 97 |
109 loadFile(fromFile, onLoaded, callback); | 98 loadFile(fromFile, onLoaded, callback); |
110 }); | |
111 }, | 99 }, |
112 | 100 |
113 removeFile: function(file, callback) | 101 removeFile: function(file, callback) |
114 { | 102 { |
115 runWhenMigrated(function() | 103 ext.storage.remove(fileToKey(file), callback); |
116 { | |
117 ext.storage.remove(fileToKey(file), callback); | |
118 }); | |
119 }, | 104 }, |
120 | 105 |
121 statFile: function(file, callback) | 106 statFile: function(file, callback) |
122 { | 107 { |
123 runWhenMigrated(function() | 108 function onLoaded(entry) |
124 { | 109 { |
125 function onLoaded(entry) | 110 callback(null, { |
126 { | 111 exists: true, |
127 callback(null, { | 112 lastModified: entry.lastModified |
128 exists: true, | 113 }); |
129 lastModified: entry.lastModified | 114 } |
130 }); | |
131 } | |
132 | 115 |
133 loadFile(file, onLoaded, callback); | 116 loadFile(file, onLoaded, callback); |
134 }); | |
135 } | 117 } |
136 }; | 118 }; |
137 | |
138 // Migrate files for users updating from old versions. | |
139 // Defer IO operations until migration is complete. | |
140 // TODO: Remove the migration code after a few releases. | |
141 let migrated = false; | |
142 let deferred = []; | |
143 | |
144 function runWhenMigrated(callback) | |
145 { | |
146 if (migrated) | |
147 callback(); | |
148 else | |
149 deferred.push(callback); | |
150 } | |
151 | |
152 ext.storage.migrateFiles(function() | |
153 { | |
154 migrated = true; | |
155 | |
156 while (deferred.length > 0) | |
157 deferred.shift()(); | |
158 }); | |
OLD | NEW |