File: index.js

package info (click to toggle)
node-file-sync-cmp 0.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 116 kB
  • sloc: makefile: 2
file content (37 lines) | stat: -rw-r--r-- 942 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';

var fs = require('fs');

var BUF_SIZE = 16 * 1024;

/* Compare two files by content. */
function equalFiles(pathA, pathB) {
    var statA = fs.lstatSync(pathA);
    var statB = fs.lstatSync(pathB);
    if (statA.size !== statB.size) {
        return false;
    }
    var fdA = fs.openSync(pathA, 'r');
    var fdB = fs.openSync(pathB, 'r');
    var bufA = new Buffer.alloc(BUF_SIZE);
    var bufB = new Buffer.alloc(BUF_SIZE);
    var readA = 1;
    var readB = 1;
    while (readA > 0) {
        readA = fs.readSync(fdA, bufA, 0, bufA.length, null);
        readB = fs.readSync(fdB, bufB, 0, bufB.length, null);
        if (readA !== readB) {
            return false;
        }
        for (var i = 0; i < readA; i++) {
            if (bufA[i] !== bufB[i]) {
                return false;
            }
        }
    }
    fs.closeSync(fdA);
    fs.closeSync(fdB);
    return true;
}

module.exports.equalFiles = equalFiles;