 Q = require 'q'
 _ = require 'underscore-plus'
 fs = require 'fs-plus'
+path = require 'path'
 
 Point = require './point'
 Range = require './range'

    Q(@file?.read(flushCache) ? "").then (contents) =>
      @cachedDiskContents = contents
      callback?()

  ###
  Section: Private Utility Methods

    @emit 'will-be-saved', this if Grim.includeDeprecatedAPIs
    @setPath(filePath)

    if options?.backup
      backupFilePath = @backupFileContentsBeforeWriting()

    try
      if backupFilePath
        fd = fs.openSync(filePath, 'w') # We open fd so we can call fdatasync
        @file.writeSync(@getText()) # File::writeSync does privelege escalation, so we don't use fd here
        fs.fdatasyncSync(fd) # Ensure file is written to disk before proceeding
        fs.closeSync(fd)
        fs.removeSync(backupFilePath)
      else
        @file.writeSync(@getText())

    catch error
      if backupFilePath?
        fs.writeFileSync(filePath, fs.readFileSync(backupFilePath))
