ResourceStateManager.coffee 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. Path = require "path"
  2. fs = require "fs"
  3. logger = require "logger-sharelatex"
  4. settings = require("settings-sharelatex")
  5. Errors = require "./Errors"
  6. 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. saveProjectState: (state, resources, basePath, callback = (error) ->) ->
  23. stateFile = Path.join(basePath, @SYNC_STATE_FILE)
  24. if not state? # remove the file if no state passed in
  25. logger.log state:state, basePath:basePath, "clearing sync state"
  26. fs.unlink stateFile, (err) ->
  27. if err? and err.code isnt 'ENOENT'
  28. return callback(err)
  29. else
  30. return callback()
  31. else
  32. logger.log state:state, basePath:basePath, "writing sync state"
  33. resourceList = (resource.path for resource in resources)
  34. fs.writeFile stateFile, [resourceList..., "stateHash:#{state}"].join("\n"), callback
  35. checkProjectStateMatches: (state, basePath, callback = (error, resources) ->) ->
  36. stateFile = Path.join(basePath, @SYNC_STATE_FILE)
  37. SafeReader.readFile stateFile, 128*1024, 'utf8', (err, result) ->
  38. return callback(err) if err?
  39. [resourceList..., oldState] = result?.toString()?.split("\n") or []
  40. newState = "stateHash:#{state}"
  41. logger.log state:state, oldState: oldState, basePath:basePath, stateMatches: (newState is oldState), "checking sync state"
  42. if newState isnt oldState
  43. return callback new Errors.FilesOutOfSyncError("invalid state for incremental update")
  44. else
  45. resources = ({path: path} for path in resourceList)
  46. callback(null, resources)
  47. checkResourceFiles: (resources, allFiles, basePath, callback = (error) ->) ->
  48. # check the paths are all relative to current directory
  49. for file in resources or []
  50. for dir in file?.path?.split('/')
  51. if dir == '..'
  52. return callback new Error("relative path in resource file list")
  53. # check if any of the input files are not present in list of files
  54. seenFile = {}
  55. for file in allFiles
  56. seenFile[file] = true
  57. missingFiles = (resource.path for resource in resources when not seenFile[resource.path])
  58. if missingFiles?.length > 0
  59. logger.err missingFiles:missingFiles, basePath:basePath, allFiles:allFiles, resources:resources, "missing input files for project"
  60. return callback new Errors.FilesOutOfSyncError("resource files missing in incremental update")
  61. else
  62. callback()