import os

AUFS_METADATA = u'.wh..wh.'
AUFS_WHITEOUT = u'.wh.'
AUFS_WHITEOUT_PREFIX_LENGTH = len(AUFS_WHITEOUT)

def is_aufs_metadata(filepath):
  """ Returns whether the given filepath references an AUFS metadata file. """
  filename = os.path.basename(filepath)
  return filename.startswith(AUFS_METADATA) or filepath.startswith(AUFS_METADATA)

def get_deleted_filename(filepath):
  """ Returns the name of the deleted file referenced by the AUFS whiteout file at
      the given path or None if the file path does not reference a whiteout file.
  """
  filename = os.path.basename(filepath)
  if not filename.startswith(AUFS_WHITEOUT):
    return None

  return filename[AUFS_WHITEOUT_PREFIX_LENGTH:]

def get_deleted_prefix(filepath):
  """ Returns the path prefix of the deleted file referenced by the AUFS whiteout file at
     the given path or None if the file path does not reference a whiteout file.
  """
  deleted_filename = get_deleted_filename(filepath)
  if deleted_filename is None:
    return None

  dirname = os.path.dirname(filepath)
  return os.path.join('/', dirname, deleted_filename)
