ResourceStateManager.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. let ResourceStateManager
  2. const Path = require('path')
  3. const fs = require('fs')
  4. const logger = require('logger-sharelatex')
  5. const Errors = require('./Errors')
  6. const SafeReader = require('./SafeReader')
  7. module.exports = ResourceStateManager = {
  8. // The sync state is an identifier which must match for an
  9. // incremental update to be allowed.
  10. //
  11. // The initial value is passed in and stored on a full
  12. // compile, along with the list of resources..
  13. //
  14. // Subsequent incremental compiles must come with the same value - if
  15. // not they will be rejected with a 409 Conflict response. The
  16. // previous list of resources is returned.
  17. //
  18. // An incremental compile can only update existing files with new
  19. // content. The sync state identifier must change if any docs or
  20. // files are moved, added, deleted or renamed.
  21. SYNC_STATE_FILE: '.project-sync-state',
  22. SYNC_STATE_MAX_SIZE: 128 * 1024,
  23. saveProjectState(state, resources, basePath, callback) {
  24. const stateFile = Path.join(basePath, this.SYNC_STATE_FILE)
  25. if (state == null) {
  26. // remove the file if no state passed in
  27. logger.log({ state, basePath }, 'clearing sync state')
  28. fs.unlink(stateFile, function (err) {
  29. if (err && err.code !== 'ENOENT') {
  30. return callback(err)
  31. } else {
  32. return callback()
  33. }
  34. })
  35. } else {
  36. logger.log({ state, basePath }, 'writing sync state')
  37. const resourceList = resources.map((resource) => resource.path)
  38. fs.writeFile(
  39. stateFile,
  40. [...resourceList, `stateHash:${state}`].join('\n'),
  41. callback
  42. )
  43. }
  44. },
  45. checkProjectStateMatches(state, basePath, callback) {
  46. const stateFile = Path.join(basePath, this.SYNC_STATE_FILE)
  47. const size = this.SYNC_STATE_MAX_SIZE
  48. SafeReader.readFile(stateFile, size, 'utf8', function (
  49. err,
  50. result,
  51. bytesRead
  52. ) {
  53. if (err) {
  54. return callback(err)
  55. }
  56. if (bytesRead === size) {
  57. logger.error(
  58. { file: stateFile, size, bytesRead },
  59. 'project state file truncated'
  60. )
  61. }
  62. const array = result.toString().split('\n')
  63. const adjustedLength = Math.max(array.length, 1)
  64. const resourceList = array.slice(0, adjustedLength - 1)
  65. const oldState = array[adjustedLength - 1]
  66. const newState = `stateHash:${state}`
  67. logger.log(
  68. { state, oldState, basePath, stateMatches: newState === oldState },
  69. 'checking sync state'
  70. )
  71. if (newState !== oldState) {
  72. return callback(
  73. new Errors.FilesOutOfSyncError('invalid state for incremental update')
  74. )
  75. } else {
  76. const resources = resourceList.map((path) => ({ path }))
  77. callback(null, resources)
  78. }
  79. })
  80. },
  81. checkResourceFiles(resources, allFiles, basePath, callback) {
  82. // check the paths are all relative to current directory
  83. const containsRelativePath = (resource) => {
  84. const dirs = resource.path.split('/')
  85. return dirs.indexOf('..') !== -1
  86. }
  87. if (resources.some(containsRelativePath)) {
  88. return callback(new Error('relative path in resource file list'))
  89. }
  90. // check if any of the input files are not present in list of files
  91. const seenFiles = new Set(allFiles)
  92. const missingFiles = resources
  93. .map((resource) => resource.path)
  94. .filter((path) => !seenFiles.has(path))
  95. if (missingFiles.length > 0) {
  96. logger.err(
  97. { missingFiles, basePath, allFiles, resources },
  98. 'missing input files for project'
  99. )
  100. return callback(
  101. new Errors.FilesOutOfSyncError(
  102. 'resource files missing in incremental update'
  103. )
  104. )
  105. } else {
  106. callback()
  107. }
  108. },
  109. }