Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/io.js

Issue 6083164824928256: Issue 1510 - Stop using OS.File.readTo() (Closed)
Patch Set: Created Oct. 30, 2014, 10:03 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 10 matching lines...) Expand all
21 21
22 let {Services} = Cu.import("resource://gre/modules/Services.jsm", null); 22 let {Services} = Cu.import("resource://gre/modules/Services.jsm", null);
23 let {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", null); 23 let {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", null);
24 let {OS} = Cu.import("resource://gre/modules/osfile.jsm", null); 24 let {OS} = Cu.import("resource://gre/modules/osfile.jsm", null);
25 let {Task} = Cu.import("resource://gre/modules/Task.jsm", null); 25 let {Task} = Cu.import("resource://gre/modules/Task.jsm", null);
26 26
27 let {Prefs} = require("prefs"); 27 let {Prefs} = require("prefs");
28 let {Utils} = require("utils"); 28 let {Utils} = require("utils");
29 29
30 let firstRead = true; 30 let firstRead = true;
31 const BUFFER_SIZE = 0x8000; // 32kB 31 const BUFFER_SIZE = 0x80000; // 512kB
32 32
33 let IO = exports.IO = 33 let IO = exports.IO =
34 { 34 {
35 /** 35 /**
36 * Retrieves the platform-dependent line break string. 36 * Retrieves the platform-dependent line break string.
37 */ 37 */
38 get lineBreak() 38 get lineBreak()
39 { 39 {
40 let lineBreak = (Services.appinfo.OS == "WINNT" ? "\r\n" : "\n"); 40 let lineBreak = (Services.appinfo.OS == "WINNT" ? "\r\n" : "\n");
41 Object.defineProperty(this, "lineBreak", {value: lineBreak}); 41 Object.defineProperty(this, "lineBreak", {value: lineBreak});
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 { 143 {
144 // Still processing data, delay processing this event. 144 // Still processing data, delay processing this event.
145 error = e; 145 error = e;
146 return; 146 return;
147 } 147 }
148 148
149 callback(e); 149 callback(e);
150 }; 150 };
151 151
152 let decoder = new TextDecoder(); 152 let decoder = new TextDecoder();
153 let array = new Uint8Array(BUFFER_SIZE);
154 Task.spawn(function() 153 Task.spawn(function()
155 { 154 {
156 if (firstRead && Services.vc.compare(Utils.platformVersion, "23.0a1") <= 0) 155 if (firstRead && Services.vc.compare(Utils.platformVersion, "23.0a1") <= 0)
157 { 156 {
158 // See https://issues.adblockplus.org/ticket/530 - the first file 157 // See https://issues.adblockplus.org/ticket/530 - the first file
159 // opened cannot be closed due to Gecko bug 858723. Make sure that 158 // opened cannot be closed due to Gecko bug 858723. Make sure that
160 // our patterns.ini file doesn't stay locked by opening a dummy file 159 // our patterns.ini file doesn't stay locked by opening a dummy file
161 // first. 160 // first.
162 try 161 try
163 { 162 {
164 let dummyPath = IO.resolveFilePath(Prefs.data_directory + "/dummy"). path; 163 let dummyPath = IO.resolveFilePath(Prefs.data_directory + "/dummy"). path;
165 let dummy = yield OS.File.open(dummyPath, {write: true, truncate: tr ue}); 164 let dummy = yield OS.File.open(dummyPath, {write: true, truncate: tr ue});
166 yield dummy.close(); 165 yield dummy.close();
167 } 166 }
168 catch (e) 167 catch (e)
169 { 168 {
170 // Dummy might be locked already, we don't care 169 // Dummy might be locked already, we don't care
171 } 170 }
172 } 171 }
173 firstRead = false; 172 firstRead = false;
174 173
175 let f = yield OS.File.open(file.path, {read: true}); 174 let f = yield OS.File.open(file.path, {read: true});
176 let numBytes; 175 while (true)
177 do
178 { 176 {
179 numBytes = yield f.readTo(array); 177 let array = yield f.read(BUFFER_SIZE);
180 if (numBytes) 178 if (!array.length)
181 { 179 break;
182 let data = decoder.decode(numBytes == BUFFER_SIZE ?
183 array :
184 array.subarray(0, numBytes), {stream: true });
185 onProgress(data);
186 }
187 } while (numBytes);
188 180
181 let data = decoder.decode(array, {stream: true});
182 onProgress(data);
183 }
189 yield f.close(); 184 yield f.close();
190 }.bind(this)).then(onSuccess, onError); 185 }.bind(this)).then(onSuccess, onError);
191 } 186 }
192 catch (e) 187 catch (e)
193 { 188 {
194 callback(e); 189 callback(e);
195 } 190 }
196 }, 191 },
197 192
198 /** 193 /**
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 else 323 else
329 callback(e); 324 callback(e);
330 }); 325 });
331 } 326 }
332 catch(e) 327 catch(e)
333 { 328 {
334 callback(e); 329 callback(e);
335 } 330 }
336 } 331 }
337 } 332 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld