utils.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. var fs = require("./fileSystem").require(),
  2. pth = require('path');
  3. fs.existsSync = fs.existsSync || pth.existsSync;
  4. module.exports = (function() {
  5. var crcTable = [],
  6. Constants = require('./constants'),
  7. Errors = require('./errors'),
  8. PATH_SEPARATOR = pth.sep;
  9. function mkdirSync(/*String*/path) {
  10. var resolvedPath = path.split(PATH_SEPARATOR)[0];
  11. path.split(PATH_SEPARATOR).forEach(function(name) {
  12. if (!name || name.substr(-1,1) === ":") return;
  13. resolvedPath += PATH_SEPARATOR + name;
  14. var stat;
  15. try {
  16. stat = fs.statSync(resolvedPath);
  17. } catch (e) {
  18. fs.mkdirSync(resolvedPath);
  19. }
  20. if (stat && stat.isFile())
  21. throw Errors.FILE_IN_THE_WAY.replace("%s", resolvedPath);
  22. });
  23. }
  24. function findSync(/*String*/dir, /*RegExp*/pattern, /*Boolean*/recoursive) {
  25. if (typeof pattern === 'boolean') {
  26. recoursive = pattern;
  27. pattern = undefined;
  28. }
  29. var files = [];
  30. fs.readdirSync(dir).forEach(function(file) {
  31. var path = pth.join(dir, file);
  32. if (fs.statSync(path).isDirectory() && recoursive)
  33. files = files.concat(findSync(path, pattern, recoursive));
  34. if (!pattern || pattern.test(path)) {
  35. files.push(pth.normalize(path) + (fs.statSync(path).isDirectory() ? PATH_SEPARATOR : ""));
  36. }
  37. });
  38. return files;
  39. }
  40. return {
  41. makeDir : function(/*String*/path) {
  42. mkdirSync(path);
  43. },
  44. crc32 : function(buf) {
  45. if (typeof buf === 'string') {
  46. buf = Buffer.alloc(buf.length, buf);
  47. }
  48. var b = Buffer.alloc(4);
  49. if (!crcTable.length) {
  50. for (var n = 0; n < 256; n++) {
  51. var c = n;
  52. for (var k = 8; --k >= 0;) //
  53. if ((c & 1) !== 0) { c = 0xedb88320 ^ (c >>> 1); } else { c = c >>> 1; }
  54. if (c < 0) {
  55. b.writeInt32LE(c, 0);
  56. c = b.readUInt32LE(0);
  57. }
  58. crcTable[n] = c;
  59. }
  60. }
  61. var crc = 0, off = 0, len = buf.length, c1 = ~crc;
  62. while(--len >= 0) c1 = crcTable[(c1 ^ buf[off++]) & 0xff] ^ (c1 >>> 8);
  63. crc = ~c1;
  64. b.writeInt32LE(crc & 0xffffffff, 0);
  65. return b.readUInt32LE(0);
  66. },
  67. methodToString : function(/*Number*/method) {
  68. switch (method) {
  69. case Constants.STORED:
  70. return 'STORED (' + method + ')';
  71. case Constants.DEFLATED:
  72. return 'DEFLATED (' + method + ')';
  73. default:
  74. return 'UNSUPPORTED (' + method + ')';
  75. }
  76. },
  77. writeFileTo : function(/*String*/path, /*Buffer*/content, /*Boolean*/overwrite, /*Number*/attr) {
  78. if (fs.existsSync(path)) {
  79. if (!overwrite)
  80. return false; // cannot overwrite
  81. var stat = fs.statSync(path);
  82. if (stat.isDirectory()) {
  83. return false;
  84. }
  85. }
  86. var folder = pth.dirname(path);
  87. if (!fs.existsSync(folder)) {
  88. mkdirSync(folder);
  89. }
  90. var fd;
  91. try {
  92. fd = fs.openSync(path, 'w', 438); // 0666
  93. } catch(e) {
  94. fs.chmodSync(path, 438);
  95. fd = fs.openSync(path, 'w', 438);
  96. }
  97. if (fd) {
  98. try {
  99. fs.writeSync(fd, content, 0, content.length, 0);
  100. }
  101. catch (e){
  102. throw e;
  103. }
  104. finally {
  105. fs.closeSync(fd);
  106. }
  107. }
  108. fs.chmodSync(path, attr || 438);
  109. return true;
  110. },
  111. writeFileToAsync : function(/*String*/path, /*Buffer*/content, /*Boolean*/overwrite, /*Number*/attr, /*Function*/callback) {
  112. if(typeof attr === 'function') {
  113. callback = attr;
  114. attr = undefined;
  115. }
  116. fs.exists(path, function(exists) {
  117. if(exists && !overwrite)
  118. return callback(false);
  119. fs.stat(path, function(err, stat) {
  120. if(exists &&stat.isDirectory()) {
  121. return callback(false);
  122. }
  123. var folder = pth.dirname(path);
  124. fs.exists(folder, function(exists) {
  125. if(!exists)
  126. mkdirSync(folder);
  127. fs.open(path, 'w', 438, function(err, fd) {
  128. if(err) {
  129. fs.chmod(path, 438, function() {
  130. fs.open(path, 'w', 438, function(err, fd) {
  131. fs.write(fd, content, 0, content.length, 0, function() {
  132. fs.close(fd, function() {
  133. fs.chmod(path, attr || 438, function() {
  134. callback(true);
  135. })
  136. });
  137. });
  138. });
  139. })
  140. } else {
  141. if(fd) {
  142. fs.write(fd, content, 0, content.length, 0, function() {
  143. fs.close(fd, function() {
  144. fs.chmod(path, attr || 438, function() {
  145. callback(true);
  146. })
  147. });
  148. });
  149. } else {
  150. fs.chmod(path, attr || 438, function() {
  151. callback(true);
  152. })
  153. }
  154. }
  155. });
  156. })
  157. })
  158. })
  159. },
  160. findFiles : function(/*String*/path) {
  161. return findSync(path, true);
  162. },
  163. getAttributes : function(/*String*/path) {
  164. },
  165. setAttributes : function(/*String*/path) {
  166. },
  167. toBuffer : function(input) {
  168. if (Buffer.isBuffer(input)) {
  169. return input;
  170. } else {
  171. if (input.length === 0) {
  172. return Buffer.alloc(0)
  173. }
  174. return Buffer.alloc(input.length, input, 'utf8');
  175. }
  176. },
  177. Constants : Constants,
  178. Errors : Errors
  179. }
  180. })();