This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retry read operations up to 10 times on EIO (#70)
Some faulty SDCard, or SDCard readers might throw EIO when reading data. Retrying a couple of times makes the issue go away. See: #73 Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
- Loading branch information
Juan Cruz Viotti
committed
Jan 5, 2017
1 parent
2d4b9b1
commit 81ab773
Showing
7 changed files
with
157 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2016 Resin.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the 'License'); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an 'AS IS' BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const stream = require('stream'); | ||
const filesystem = require('./filesystem'); | ||
const ImageStreamPosition = require('./image-stream-position'); | ||
|
||
module.exports = class ImageWriteStream extends stream.Readable { | ||
|
||
/** | ||
* @summary Create an instance of ImageReadStream | ||
* @name ImageReadStream | ||
* @class | ||
* @public | ||
* | ||
* @param {Number} fileDescriptor - file descriptor | ||
* @param {Object} options - options | ||
* @param {Number} options.chunkSize - chunk size | ||
* | ||
* @example | ||
* const fd = fs.openSync('/dev/rdisk2', 'rs+'); | ||
* | ||
* const stream = new ImageReadStream(fd, { | ||
* chunkSize: 65536 * 16 | ||
* }); | ||
* | ||
* stream.pipe(fs.createWriteStream('path/to/output/img')) | ||
* .pipe(new ImageWriteStream(fd)); | ||
*/ | ||
constructor(fileDescriptor, options) { | ||
const position = new ImageStreamPosition(); | ||
|
||
super({ | ||
highWaterMark: options.chunkSize, | ||
objectMode: false, | ||
read: (size) => { | ||
const streamPosition = position.getStreamPosition(); | ||
|
||
filesystem.readChunk(this.fileDescriptor, size, streamPosition, (error, buffer) => { | ||
if (error) { | ||
return this.emit('error', error); | ||
} | ||
|
||
if (buffer) { | ||
position.incrementStreamPosition(buffer.length); | ||
} | ||
|
||
this.push(buffer); | ||
}); | ||
} | ||
}); | ||
|
||
this.fileDescriptor = fileDescriptor; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters