statUtils.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.areStatsEqual = exports.convertToBigIntStats = exports.clearStats = exports.makeEmptyStats = exports.makeDefaultStats = exports.BigIntStatsEntry = exports.StatEntry = exports.DirEntry = exports.DEFAULT_MODE = void 0;
  4. const tslib_1 = require("tslib");
  5. const nodeUtils = tslib_1.__importStar(require("util"));
  6. const constants_1 = require("./constants");
  7. exports.DEFAULT_MODE = constants_1.S_IFREG | 0o644;
  8. class DirEntry {
  9. constructor() {
  10. this.name = ``;
  11. this.mode = 0;
  12. }
  13. isBlockDevice() {
  14. return false;
  15. }
  16. isCharacterDevice() {
  17. return false;
  18. }
  19. isDirectory() {
  20. return (this.mode & constants_1.S_IFMT) === constants_1.S_IFDIR;
  21. }
  22. isFIFO() {
  23. return false;
  24. }
  25. isFile() {
  26. return (this.mode & constants_1.S_IFMT) === constants_1.S_IFREG;
  27. }
  28. isSocket() {
  29. return false;
  30. }
  31. isSymbolicLink() {
  32. return (this.mode & constants_1.S_IFMT) === constants_1.S_IFLNK;
  33. }
  34. }
  35. exports.DirEntry = DirEntry;
  36. class StatEntry {
  37. constructor() {
  38. this.uid = 0;
  39. this.gid = 0;
  40. this.size = 0;
  41. this.blksize = 0;
  42. this.atimeMs = 0;
  43. this.mtimeMs = 0;
  44. this.ctimeMs = 0;
  45. this.birthtimeMs = 0;
  46. this.atime = new Date(0);
  47. this.mtime = new Date(0);
  48. this.ctime = new Date(0);
  49. this.birthtime = new Date(0);
  50. this.dev = 0;
  51. this.ino = 0;
  52. this.mode = exports.DEFAULT_MODE;
  53. this.nlink = 1;
  54. this.rdev = 0;
  55. this.blocks = 1;
  56. }
  57. isBlockDevice() {
  58. return false;
  59. }
  60. isCharacterDevice() {
  61. return false;
  62. }
  63. isDirectory() {
  64. return (this.mode & constants_1.S_IFMT) === constants_1.S_IFDIR;
  65. }
  66. isFIFO() {
  67. return false;
  68. }
  69. isFile() {
  70. return (this.mode & constants_1.S_IFMT) === constants_1.S_IFREG;
  71. }
  72. isSocket() {
  73. return false;
  74. }
  75. isSymbolicLink() {
  76. return (this.mode & constants_1.S_IFMT) === constants_1.S_IFLNK;
  77. }
  78. }
  79. exports.StatEntry = StatEntry;
  80. class BigIntStatsEntry {
  81. constructor() {
  82. this.uid = BigInt(0);
  83. this.gid = BigInt(0);
  84. this.size = BigInt(0);
  85. this.blksize = BigInt(0);
  86. this.atimeMs = BigInt(0);
  87. this.mtimeMs = BigInt(0);
  88. this.ctimeMs = BigInt(0);
  89. this.birthtimeMs = BigInt(0);
  90. this.atimeNs = BigInt(0);
  91. this.mtimeNs = BigInt(0);
  92. this.ctimeNs = BigInt(0);
  93. this.birthtimeNs = BigInt(0);
  94. this.atime = new Date(0);
  95. this.mtime = new Date(0);
  96. this.ctime = new Date(0);
  97. this.birthtime = new Date(0);
  98. this.dev = BigInt(0);
  99. this.ino = BigInt(0);
  100. this.mode = BigInt(exports.DEFAULT_MODE);
  101. this.nlink = BigInt(1);
  102. this.rdev = BigInt(0);
  103. this.blocks = BigInt(1);
  104. }
  105. isBlockDevice() {
  106. return false;
  107. }
  108. isCharacterDevice() {
  109. return false;
  110. }
  111. isDirectory() {
  112. return (this.mode & BigInt(constants_1.S_IFMT)) === BigInt(constants_1.S_IFDIR);
  113. }
  114. isFIFO() {
  115. return false;
  116. }
  117. isFile() {
  118. return (this.mode & BigInt(constants_1.S_IFMT)) === BigInt(constants_1.S_IFREG);
  119. }
  120. isSocket() {
  121. return false;
  122. }
  123. isSymbolicLink() {
  124. return (this.mode & BigInt(constants_1.S_IFMT)) === BigInt(constants_1.S_IFLNK);
  125. }
  126. }
  127. exports.BigIntStatsEntry = BigIntStatsEntry;
  128. function makeDefaultStats() {
  129. return new StatEntry();
  130. }
  131. exports.makeDefaultStats = makeDefaultStats;
  132. function makeEmptyStats() {
  133. return clearStats(makeDefaultStats());
  134. }
  135. exports.makeEmptyStats = makeEmptyStats;
  136. /**
  137. * Mutates the provided stats object to zero it out then returns it for convenience
  138. */
  139. function clearStats(stats) {
  140. for (const key in stats) {
  141. if (Object.prototype.hasOwnProperty.call(stats, key)) {
  142. const element = stats[key];
  143. if (typeof element === `number`) {
  144. // @ts-expect-error Typescript can't tell that stats[key] is a number
  145. stats[key] = 0;
  146. }
  147. else if (typeof element === `bigint`) {
  148. // @ts-expect-error Typescript can't tell that stats[key] is a bigint
  149. stats[key] = BigInt(0);
  150. }
  151. else if (nodeUtils.types.isDate(element)) {
  152. // @ts-expect-error Typescript can't tell that stats[key] is a bigint
  153. stats[key] = new Date(0);
  154. }
  155. }
  156. }
  157. return stats;
  158. }
  159. exports.clearStats = clearStats;
  160. function convertToBigIntStats(stats) {
  161. const bigintStats = new BigIntStatsEntry();
  162. for (const key in stats) {
  163. if (Object.prototype.hasOwnProperty.call(stats, key)) {
  164. const element = stats[key];
  165. if (typeof element === `number`) {
  166. // @ts-expect-error Typescript isn't able to tell this is valid
  167. bigintStats[key] = BigInt(element);
  168. }
  169. else if (nodeUtils.types.isDate(element)) {
  170. // @ts-expect-error Typescript isn't able to tell this is valid
  171. bigintStats[key] = new Date(element);
  172. }
  173. }
  174. }
  175. bigintStats.atimeNs = bigintStats.atimeMs * BigInt(1e6);
  176. bigintStats.mtimeNs = bigintStats.mtimeMs * BigInt(1e6);
  177. bigintStats.ctimeNs = bigintStats.ctimeMs * BigInt(1e6);
  178. bigintStats.birthtimeNs = bigintStats.birthtimeMs * BigInt(1e6);
  179. return bigintStats;
  180. }
  181. exports.convertToBigIntStats = convertToBigIntStats;
  182. function areStatsEqual(a, b) {
  183. if (a.atimeMs !== b.atimeMs)
  184. return false;
  185. if (a.birthtimeMs !== b.birthtimeMs)
  186. return false;
  187. if (a.blksize !== b.blksize)
  188. return false;
  189. if (a.blocks !== b.blocks)
  190. return false;
  191. if (a.ctimeMs !== b.ctimeMs)
  192. return false;
  193. if (a.dev !== b.dev)
  194. return false;
  195. if (a.gid !== b.gid)
  196. return false;
  197. if (a.ino !== b.ino)
  198. return false;
  199. if (a.isBlockDevice() !== b.isBlockDevice())
  200. return false;
  201. if (a.isCharacterDevice() !== b.isCharacterDevice())
  202. return false;
  203. if (a.isDirectory() !== b.isDirectory())
  204. return false;
  205. if (a.isFIFO() !== b.isFIFO())
  206. return false;
  207. if (a.isFile() !== b.isFile())
  208. return false;
  209. if (a.isSocket() !== b.isSocket())
  210. return false;
  211. if (a.isSymbolicLink() !== b.isSymbolicLink())
  212. return false;
  213. if (a.mode !== b.mode)
  214. return false;
  215. if (a.mtimeMs !== b.mtimeMs)
  216. return false;
  217. if (a.nlink !== b.nlink)
  218. return false;
  219. if (a.rdev !== b.rdev)
  220. return false;
  221. if (a.size !== b.size)
  222. return false;
  223. if (a.uid !== b.uid)
  224. return false;
  225. const aN = a;
  226. const bN = b;
  227. if (aN.atimeNs !== bN.atimeNs)
  228. return false;
  229. if (aN.mtimeNs !== bN.mtimeNs)
  230. return false;
  231. if (aN.ctimeNs !== bN.ctimeNs)
  232. return false;
  233. if (aN.birthtimeNs !== bN.birthtimeNs)
  234. return false;
  235. return true;
  236. }
  237. exports.areStatsEqual = areStatsEqual;