Delete a GRR temp file.

  To limit possible damage the path must be absolute and either the
  file must be within any of the Client.tempdir_roots or the file name
  must begin with Client.tempfile_prefix.

  Args:
    path: path string to file to be deleted.

  Raises:
    OSError: Permission denied, or file not found.
    ErrorBadPath: Path must be absolute.
    ErrorNotTempFile: Filename must start with Client.tempfile_prefix.
    ErrorNotAFile: File to delete does not exist.

def DeleteGRRTempFile(path):
  """Delete a GRR temp file.

  To limit possible damage the path must be absolute and either the
  file must be within any of the Client.tempdir_roots or the file name
  must begin with Client.tempfile_prefix.

  Args:
    path: path string to file to be deleted.

  Raises:
    OSError: Permission denied, or file not found.
    ErrorBadPath: Path must be absolute.
    ErrorNotTempFile: Filename must start with Client.tempfile_prefix.
    ErrorNotAFile: File to delete does not exist.
  """
  precondition.AssertType(path, Text)

  if not os.path.isabs(path):
    raise ErrorBadPath("Path must be absolute")

  prefix = config.CONFIG["Client.tempfile_prefix"]
  directories = [
      GetTempDirForRoot(root) for root in config.CONFIG["Client.tempdir_roots"]
  ]
  if not _CheckIfPathIsValidForDeletion(
      path, prefix=prefix, directories=directories):
    msg = ("Can't delete temp file %s. Filename must start with %s "
           "or lie within any of %s.")
    raise ErrorNotTempFile(msg % (path, prefix, ";".join(directories)))

  if os.path.exists(path):
    # Clear our file handle cache so the file can be deleted.
    files.FILE_HANDLE_CACHE.Flush()
    os.remove(path)
  else:
    raise ErrorNotAFile("%s does not exist." % path)