Newer
Older
CVSS_3.0_GUI / dist / CVSS_3.0_Calc-1.0.0-linux-x64 / node_modules / nwjs-builder-phoenix / node_modules / dir-compare / compareSync.js
root on 7 May 2019 7 KB Initial commit
  1. var fs = require('fs');
  2. var pathUtils = require('path');
  3. var common = require('./common');
  4.  
  5. /**
  6. * Returns the sorted list of entries in a directory.
  7. */
  8. var getEntries = function (path, options) {
  9. if (!path) {
  10. return [];
  11. } else {
  12. var statPath = fs.statSync(path);
  13. if (statPath.isDirectory()) {
  14. var entries = fs.readdirSync(path);
  15.  
  16. var res = [];
  17. entries.forEach(function (entryName) {
  18. var entryPath = path + '/' + entryName;
  19. var lstatEntry = fs.lstatSync(entryPath);
  20. var isSymlink = lstatEntry.isSymbolicLink();
  21. var statEntry;
  22. if(options.skipSymlinks && isSymlink){
  23. statEntry = undefined;
  24. } else{
  25. statEntry = fs.statSync(entryPath);
  26. }
  27. var entry = {
  28. name : entryName,
  29. path : entryPath,
  30. stat : statEntry,
  31. lstat : lstatEntry,
  32. symlink : isSymlink,
  33. toString : function () {
  34. return this.name;
  35. }
  36. };
  37. if (common.filterEntry(entry, options)){
  38. res.push(entry);
  39. }
  40. });
  41. return options.ignoreCase?res.sort(common.compareEntryIgnoreCase):res.sort(common.compareEntryCaseSensitive);
  42. } else {
  43. var name = pathUtils.basename(path);
  44. return [
  45. {
  46. name : name,
  47. path : path,
  48. stat : statPath,
  49. toString : function () {
  50. return this.name;
  51. }
  52. }
  53.  
  54. ];
  55. }
  56. }
  57. }
  58.  
  59. /**
  60. * Compares two directories synchronously.
  61. */
  62. var compare = function (rootEntry1, rootEntry2, level, relativePath, options, statistics, diffSet, symlinkCache) {
  63. symlinkCache = symlinkCache || {
  64. dir1 : {},
  65. dir2 : {}
  66. };
  67. var loopDetected1 = common.detectLoop(rootEntry1, symlinkCache.dir1);
  68. var loopDetected2 = common.detectLoop(rootEntry2, symlinkCache.dir2);
  69.  
  70. var symlinkCachePath1, symlinkCachePath2;
  71. if(rootEntry1 && !loopDetected1){
  72. symlinkCachePath1 = pathUtils.normalize(pathUtils.resolve(rootEntry1.symlink?fs.realpathSync(rootEntry1.path):rootEntry1.path)).toLowerCase();
  73. symlinkCache.dir1[symlinkCachePath1] = true;
  74. }
  75. if(rootEntry2 && !loopDetected2){
  76. symlinkCachePath2 = pathUtils.normalize(pathUtils.resolve(rootEntry2.symlink?fs.realpathSync(rootEntry2.path):rootEntry2.path)).toLowerCase();
  77. symlinkCache.dir2[symlinkCachePath2] = true;
  78. }
  79. var path1 = rootEntry1?rootEntry1.path:undefined;
  80. var path2 = rootEntry2?rootEntry2.path:undefined;
  81. var entries1 = loopDetected1?[]:getEntries(path1, options);
  82. var entries2 = loopDetected2?[]:getEntries(path2, options);
  83. var i1 = 0, i2 = 0;
  84. while (i1 < entries1.length || i2 < entries2.length) {
  85. var entry1 = entries1[i1];
  86. var entry2 = entries2[i2];
  87. var n1 = entry1 ? entry1.name : undefined;
  88. var n2 = entry2 ? entry2.name : undefined;
  89. var p1 = entry1 ? entry1.path : undefined;
  90. var p2 = entry2 ? entry2.path : undefined;
  91. var fileStat1 = entry1 ? entry1.stat : undefined;
  92. var fileStat2 = entry2 ? entry2.stat : undefined;
  93. var type1, type2;
  94.  
  95. // compare entry name (-1, 0, 1)
  96. var cmp;
  97. if (i1 < entries1.length && i2 < entries2.length) {
  98. cmp = options.ignoreCase?common.compareEntryIgnoreCase(entry1, entry2):common.compareEntryCaseSensitive(entry1, entry2);
  99. type1 = common.getType(fileStat1);
  100. type2 = common.getType(fileStat2);
  101. } else if (i1 < entries1.length) {
  102. type1 = common.getType(fileStat1);
  103. type2 = common.getType(undefined);
  104. cmp = -1;
  105. } else {
  106. type1 = common.getType(undefined);
  107. type2 = common.getType(fileStat2);
  108. cmp = 1;
  109. }
  110.  
  111. // process entry
  112. if (cmp === 0) {
  113. // Both left/right exist and have the same name
  114. if (type1 === type2) {
  115. var same;
  116. if(type1==='file'){
  117. if (options.compareSize && fileStat1.size !== fileStat2.size) {
  118. same = false;
  119. } else if(options.compareDate && !common.sameDate(fileStat1.mtime, fileStat2.mtime, options.dateTolerance)){
  120. same = false;
  121. } else if(options.compareContent){
  122. same = options.compareFileSync(p1, fileStat1, p2, fileStat2, options);
  123. } else{
  124. same = true;
  125. }
  126. } else{
  127. same = true;
  128. }
  129. if(!options.noDiffSet){
  130. options.resultBuilder(entry1, entry2, same ? 'equal' : 'distinct', level, relativePath, options, statistics, diffSet);
  131. }
  132. same ? statistics.equal++ : statistics.distinct++;
  133. if(type1==='file'){
  134. same ? statistics.equalFiles++ : statistics.distinctFiles++;
  135. } else{
  136. same ? statistics.equalDirs++ : statistics.distinctDirs++;
  137. }
  138. } else {
  139. // File and directory with same name
  140. if(!options.noDiffSet){
  141. options.resultBuilder(entry1, entry2, 'distinct', level, relativePath, options, statistics, diffSet);
  142. }
  143. statistics.distinct+=2;
  144. statistics.distinctFiles++;
  145. statistics.distinctDirs++;
  146. }
  147. i1++;
  148. i2++;
  149. if(!options.skipSubdirs){
  150. if (type1 === 'directory' && type2 === 'directory') {
  151. compare(entry1, entry2, level + 1, relativePath + '/' + entry1.name, options, statistics, diffSet, common.cloneSymlinkCache(symlinkCache));
  152. } else if (type1 === 'directory') {
  153. compare(entry1, undefined, level + 1, relativePath + '/' + entry1.name, options, statistics, diffSet, common.cloneSymlinkCache(symlinkCache));
  154. } else if (type2 === 'directory') {
  155. compare(undefined, entry2, level + 1, relativePath + '/' + entry2.name, options, statistics, diffSet, common.cloneSymlinkCache(symlinkCache));
  156. }
  157. }
  158. } else if (cmp < 0) {
  159. // Right missing
  160. if(!options.noDiffSet){
  161. options.resultBuilder(entry1, undefined, 'left', level, relativePath, options, statistics, diffSet);
  162. }
  163. statistics.left++;
  164. if(type1==='file'){
  165. statistics.leftFiles++;
  166. } else{
  167. statistics.leftDirs++;
  168. }
  169. i1++;
  170. if (type1 === 'directory' && !options.skipSubdirs) {
  171. compare(entry1, undefined, level + 1, relativePath + '/' + entry1.name, options, statistics, diffSet, common.cloneSymlinkCache(symlinkCache));
  172. }
  173. } else {
  174. // Left missing
  175. if(!options.noDiffSet){
  176. options.resultBuilder(undefined, entry2, 'right', level, relativePath, options, statistics, diffSet);
  177. }
  178. statistics.right++;
  179. if(type2==='file'){
  180. statistics.rightFiles++;
  181. } else{
  182. statistics.rightDirs++;
  183. }
  184. i2++;
  185. if (type2 === 'directory' && !options.skipSubdirs) {
  186. compare(undefined, entry2, level + 1, relativePath + '/' + entry2.name, options, statistics, diffSet, common.cloneSymlinkCache(symlinkCache));
  187. }
  188. }
  189. }
  190. };
  191.  
  192. module.exports = compare;
Buy Me A Coffee