lineReader does not read last line of file if not terminated with \n #49

Closed
opened 2013-01-25 13:27:15 -08:00 by dylanb · 1 comment
dylanb commented 2013-01-25 13:27:15 -08:00 (Migrated from github.com)

Or if you have a file with a single line that is not terminated with a \n, then lineReader will behave as if the file is empty.

The following code generates hash values for all files in a file directory. If there are two single line files, their hash will be the same value and this value will also be the same as a file that is totally empty.

(function () {
    'use strict';
    var fs = require('fs'),
        wrench = require('wrench'),
        util = require('util'),
        crypto = require('crypto'),
        allFiles,
        fd,
        ws;

allFiles = wrench.readdirSyncRecursive('.');
allFiles.sort();
allFiles = allFiles.filter(function (element) {
return (element.indexOf('.git') === -1 && element.indexOf('.log') === -1);
});
if (allFiles.indexOf('review') === -1) {
// create the review directory
wrench.mkdirSyncRecursive('review', '0700');
} else {
// don't want to create hashes for ourselves or git files
allFiles.splice(allFiles.indexOf('review'), 1);
allFiles.splice(allFiles.indexOf('review/hashes.txt'), 1);
}
ws = fs.createWriteStream('review/hashes.txt', { flags: 'w', encoding: null, mode: '0600' });
ws.on('open', function (fd) {
var i,
ilen,
name,
stats,
f,
content,
hash,
buffer;
for (i = 0, ilen = allFiles.length; i < ilen; i += 1) {
name = allFiles[i];
stats = fs.statSync(name);
console.log('processing file [' + name + ']');
if (stats.isFile()) {
f = new wrench.LineReader(name);
content = '';
while (f.hasNextLine()) {
content += f.getNextLine();
}
fs.closeSync( f.fd);
hash = crypto
.createHash('md5')
.update(content)
.digest('hex');
buffer = name + '\n' + hash + '\n\n';
console.log(buffer);
ws.write(buffer);
}
}
ws.end();
});


}());

Or if you have a file with a single line that is not terminated with a \n, then lineReader will behave as if the file is empty. The following code generates hash values for all files in a file directory. If there are two single line files, their hash will be the same value and this value will also be the same as a file that is totally empty. ```/_global require, console_/ (function () { 'use strict'; var fs = require('fs'), wrench = require('wrench'), util = require('util'), crypto = require('crypto'), allFiles, fd, ws; ``` allFiles = wrench.readdirSyncRecursive('.'); allFiles.sort(); allFiles = allFiles.filter(function (element) { return (element.indexOf('.git') === -1 && element.indexOf('.log') === -1); }); if (allFiles.indexOf('review') === -1) { // create the review directory wrench.mkdirSyncRecursive('review', '0700'); } else { // don't want to create hashes for ourselves or git files allFiles.splice(allFiles.indexOf('review'), 1); allFiles.splice(allFiles.indexOf('review/hashes.txt'), 1); } ws = fs.createWriteStream('review/hashes.txt', { flags: 'w', encoding: null, mode: '0600' }); ws.on('open', function (fd) { var i, ilen, name, stats, f, content, hash, buffer; for (i = 0, ilen = allFiles.length; i < ilen; i += 1) { name = allFiles[i]; stats = fs.statSync(name); console.log('processing file [' + name + ']'); if (stats.isFile()) { f = new wrench.LineReader(name); content = ''; while (f.hasNextLine()) { content += f.getNextLine(); } fs.closeSync( f.fd); hash = crypto .createHash('md5') .update(content) .digest('hex'); buffer = name + '\n' + hash + '\n\n'; console.log(buffer); ws.write(buffer); } } ws.end(); }); ``` }()); ``` ```
ryanmcgrath commented 2013-05-03 13:18:54 -07:00 (Migrated from github.com)

Months late, but should work. Let me know if you have issues, thanks and sorry!

Months late, but should work. Let me know if you have issues, thanks and sorry!
This repository is archived. You cannot comment on issues.
No milestone
No project
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: code/wrench-js#49
No description provided.