示例#1
0
    def FullUpdateFromMap(self, cache, new_map, force_write=False):
        """Write a new map into the provided cache (overwrites).

        Args:
          cache: A nss_cache.caches.Cache object.
          new_map: A nss_cache.maps.Map object.
          force_write: A boolean indicating empty maps are okay to write, defaults
            to False which means do not write them.

        Returns:
          0 if succesful, non-zero indicating number of failures otherwise.

        Raises:
          EmptyMap: Update is an empty map, not raised if force_write=True.
        """
        return_val = 0

        if len(new_map) == 0 and not force_write:
            raise error.EmptyMap(
                'Source map empty during full update, aborting. '
                'Use --force-write to override.')

        return_val = cache.WriteMap(map_data=new_map)

        # We did an update, write our timestamps unless there is an error.
        if return_val == 0:
            self.WriteModifyTimestamp(new_map.GetModifyTimestamp())
            self.WriteUpdateTimestamp()

        return return_val
示例#2
0
    def GetAutomountFile(self, dst_file, current_file, location):
        """Retrieve automount file via zsync.

    Args:
      dst_file: Destination file (temp)
      current_file: path to the current cache file.
      location: name of the automount
    Returns:
      path to cache
    """
        self.log.debug('Automount location: %s', location)
        if location is None:
            self.log.error(
                'A location is required to retrieve an automount map!')
            raise error.EmptyMap()
        automount_url = urlparse.urljoin(self.conf['automount_base_url'],
                                         location)
        tmp = self._GetFile(automount_url, dst_file, current_file)
        return tmp
示例#3
0
  def _FullUpdateFromFile(self, cache, source_map, force_write=False):
    """Write a new map into the provided cache (overwrites).

    Args:
      cache: A nss_cache.caches.Cache object.
      source_map: The map whose contents we're replacing the cache with, that is
        used for verification.
      force_write: A boolean flag forcing empty map updates when False,
        defaults to False.

    Returns:
      0 if succesful, non-zero indicating number of failures otherwise.

    Raises:
      EmptyMap: Update is an empty map, not raised if force_write=True.
      InvalidMap:
    """
    return_val = 0

    for entry in source_map:
      if not entry.Verify():
        raise error.InvalidMap('Map is not valid. Aborting')

    if len(source_map) is 0 and not force_write:
      raise error.EmptyMap('Source map empty during full update, aborting. '
                           'Use --force-write to override.')

    return_val += cache.WriteMap(map_data=source_map)

    # We did an update, write our timestamps unless there is an error.
    if return_val is 0:
      mtime = os.stat(cache.GetCacheFilename()).st_mtime
      gmtime = time.gmtime(mtime)
      self.log.debug('Cache filename %s has mtime %d, gmtime %r',
                     cache.GetCacheFilename(), mtime, gmtime)
      self.WriteModifyTimestamp(gmtime)
      self.WriteUpdateTimestamp()

    return return_val
示例#4
0
文件: files.py 项目: foxpass/nsscache
    def Verify(self, written_keys):
        """Verify that the cache is correct.

        Perform some unit tests on the written data, such as reading it
        back and verifying that it parses and has the entries we expect.

        Args:
          written_keys: a set of keys that should have been written to disk.

        Returns:
          a boolean indicating success.

        Raises:
          EmptyMap: The cache being verified is empty.
        """
        self.log.debug('verification starting on %r', self.temp_cache_filename)

        cache_data = self.GetMap(self.temp_cache_filename)
        map_entry_count = len(cache_data)
        self.log.debug('entry count: %d', map_entry_count)

        if map_entry_count <= 0:
            # We have read in an empty map, yet we expect that earlier we
            # should have written more. Uncaught disk full or other error?
            self.log.error('The files cache being verified "%r" is empty.',
                           self.temp_cache_filename)
            raise error.EmptyMap(self.temp_cache_filename + ' is empty')

        cache_keys = set()
        # Use PopItem() so we free our memory if multiple maps are Verify()ed.
        try:
            while 1:
                entry = cache_data.PopItem()
                cache_keys.update(self._ExpectedKeysForEntry(entry))
        except KeyError:
            # expected when PopItem() is done, and breaks our loop for us.
            pass

        missing_from_cache = written_keys - cache_keys
        if missing_from_cache:
            self.log.warning(
                'verify failed: %d missing from the on-disk cache',
                len(missing_from_cache))
            if len(missing_from_cache) < 1000:
                self.log.debug('keys missing from the on-disk cache: %r',
                               missing_from_cache)
            else:
                self.log.debug('More than 1000 keys missing from cache. '
                               'Not printing.')
            self._Rollback()
            return False

        missing_from_map = cache_keys - written_keys
        if missing_from_map:
            self.log.warning(
                'verify failed: %d keys found, unexpected in the on-disk '
                'cache', len(missing_from_map))
            if len(missing_from_map) < 1000:
                self.log.debug('keys missing from map: %r', missing_from_map)
            else:
                self.log.debug(
                    'More than 1000 keys missing from map.  Not printing.')
            self._Rollback()
            return False

        return True
示例#5
0
      local path
    """
        self.log.debug('ZSync URL: %s', remote)
        zs = zsync.Zsync(conn=self.conn,
                         retry_max=self.conf['retry_max'],
                         retry_delay=self.conf['retry_delay'])
        try:
            zs.Begin(remote + self.conf['zsync_suffix'])
            if current_file and os.path.exists(current_file):
                zs.SubmitSource(current_file)
            zs.Fetch(local_path)
        except zsync.error.Error, e:
            raise error.InvalidMap('Unable to retrieve zsync file: %s' % e)

        if not os.path.exists(local_path):
            raise error.EmptyMap()

        if self.conf['gpg']:
            remote_sig = remote + self.conf['gpg_suffix']
            if not self._GPGVerify(local_path, remote_sig):
                self.log.warning('Invalid GPG signature for %s', remote)
                raise error.InvalidMap('Unable to verify map')

        return local_path

    def GetPasswdFile(self, dst_file, current_file):
        """Retrieve passwd file via zsync.

    Args:
      dst_file: Destination file (temp)
      current_file: path to the current cache file.