def do_key_setup(ui_, update_sm, params, stored_cfg): """ INTERNAL: Handle inverting/updating keys before running a command.""" insert_uri = params.get('INSERT_URI') if not insert_uri is None and insert_uri.startswith('USK@/'): insert_uri = ('USK' + stored_cfg.defaults['DEFAULT_PRIVATE_KEY'][3:] + insert_uri[5:]) ui_.status("Filled in the insert URI using the default private key.\n") if insert_uri is None or not (is_usk(insert_uri) or is_ssk(insert_uri)): return (params.get('REQUEST_URI'), False) update_sm.start_inverting(insert_uri) run_until_quiescent(update_sm, params['POLL_SECS'], False) if update_sm.get_state(QUIESCENT).prev_state != INVERTING_URI: raise util.Abort("Couldn't invert private key:\n%s" % insert_uri) inverted_uri = update_sm.get_state(INVERTING_URI).get_request_uri() params['INVERTED_INSERT_URI'] = inverted_uri if is_usk(insert_uri): # Determine the highest known index for the insert uri. max_index = max(stored_cfg.get_index(inverted_uri), get_version(insert_uri)) # Update the insert uri to the latest known version. params['INSERT_URI'] = get_usk_for_usk_version(insert_uri, max_index) # Update the inverted insert URI to the latest known version. params['INVERTED_INSERT_URI'] = get_usk_for_usk_version( inverted_uri, max_index) # Update the index of the request uri using the stored config. request_uri = params.get('REQUEST_URI') if not request_uri is None and is_usk(request_uri): assert not params['NO_SEARCH'] or not request_uri is None if not params['NO_SEARCH']: max_index = max(stored_cfg.get_index(request_uri), get_version(request_uri)) request_uri = get_usk_for_usk_version(request_uri, max_index) if (params['NO_SEARCH'] and # Force the insert URI down to the version in the request URI. usks_equal(request_uri, params['INVERTED_INSERT_URI'])): params['INVERTED_INSERT_URI'] = request_uri params['INSERT_URI'] = get_usk_for_usk_version( insert_uri, get_version(request_uri)) # Skip key inversion if we already inverted the insert_uri. is_keypair = False if (request_uri is None and not params.get('INVERTED_INSERT_URI') is None): request_uri = params['INVERTED_INSERT_URI'] is_keypair = True return (request_uri, is_keypair)
def do_key_setup(ui_, update_sm, params, stored_cfg): """ INTERNAL: Handle inverting/updating keys before running a command.""" insert_uri = params.get('INSERT_URI') if not insert_uri is None and insert_uri.startswith('USK@/'): insert_uri = ('USK' + stored_cfg.defaults['DEFAULT_PRIVATE_KEY'][3:] + insert_uri[5:]) ui_.status("Filled in the insert URI using the default private key.\n") if insert_uri is None or not (is_usk(insert_uri) or is_ssk(insert_uri)): return (params.get('REQUEST_URI'), False) update_sm.start_inverting(insert_uri) run_until_quiescent(update_sm, params['POLL_SECS'], False) if update_sm.get_state(QUIESCENT).prev_state != INVERTING_URI: raise util.Abort("Couldn't invert private key:\n%s" % insert_uri) inverted_uri = update_sm.get_state(INVERTING_URI).get_request_uri() params['INVERTED_INSERT_URI'] = inverted_uri if is_usk(insert_uri): # Determine the highest known index for the insert uri. max_index = max(stored_cfg.get_index(inverted_uri), get_version(insert_uri)) # Update the insert uri to the latest known version. params['INSERT_URI'] = get_usk_for_usk_version(insert_uri, max_index) # Update the inverted insert URI to the latest known version. params['INVERTED_INSERT_URI'] = get_usk_for_usk_version( inverted_uri, max_index) # Update the index of the request uri using the stored config. request_uri = params.get('REQUEST_URI') if not request_uri is None and is_usk(request_uri): assert not params['NO_SEARCH'] or not request_uri is None if not params['NO_SEARCH']: max_index = max(stored_cfg.get_index(request_uri), get_version(request_uri)) request_uri = get_usk_for_usk_version(request_uri, max_index) if (params['NO_SEARCH'] and # Force the insert URI down to the version in the request URI. usks_equal(request_uri, params['INVERTED_INSERT_URI'])): params['INVERTED_INSERT_URI'] = request_uri params['INSERT_URI'] = get_usk_for_usk_version( insert_uri, get_version(request_uri)) # Skip key inversion if we already inverted the insert_uri. is_keypair = False if (request_uri is None and not params.get('INVERTED_INSERT_URI') is None): request_uri = params['INVERTED_INSERT_URI'] is_keypair = True return (request_uri, is_keypair)
def execute_info(ui_, repo, params, stored_cfg): """ Run the info command. """ request_uri = params['REQUEST_URI'] if request_uri is None or not is_usk_file(request_uri): ui_.status("Only works with USK file URIs.\n") return usk_hash = normalize(request_uri) max_index = stored_cfg.get_index(request_uri) if max_index is None: ui_.status(NO_INFO_FMT % usk_hash) return insert_uri = str(stored_cfg.get_insert_uri(usk_hash)) # fix index request_uri = get_usk_for_usk_version(request_uri, max_index) trusted = stored_cfg.trusted_notifiers(usk_hash) if not trusted: trusted = ' None' else: trusted = ' ' + '\n '.join(trusted) ui_.status(INFO_FMT % (usk_hash, max_index or -1, trusted, request_uri, insert_uri)) update_sm = setup(ui_, repo, params, stored_cfg) try: ui_.status('Freenet head(s): %s\n' % ' '.join([ ver[:12] for ver in read_freenet_heads(params, update_sm, request_uri) ])) finally: cleanup(update_sm)
def decide_next_state(self, from_state): """ DecisionState implementation. """ if not hasattr(from_state, 'get_top_key_tuple'): raise Exception("Illegal Transition from: %s" % from_state.name) top_key = from_state.get_top_key_tuple() #top_key = break_top_key(top_key) #archivetop.dump_top_key_tuple(top_key) uri = self.parent.ctx['REQUEST_URI'] # WRONG FOR INSERT (+1) # Hmmmm... push this into the context? ctx.request_uri() insert_uri = self.parent.ctx.get('INSERT_URI', None) if not insert_uri is None and insert_uri != 'CHK@': version = get_version(insert_uri) uri = get_usk_for_usk_version(uri, max(version, get_version(uri))) self.parent.ctx.arch_cache_top_key(uri, top_key) self.cached_blocks = self.parent.ctx.required_blocks(top_key) if len(self.cached_blocks) > 0: #print "NEEDS BLOCKS: ", len(self.cached_blocks) return self.needs_blocks_state #print "HAS BLOCKS" return self.has_blocks_state
def handle_announcement(self, clean_id, fms_id, usk): """ INTERNAL: process a single announcement. """ entry = self.table.get(clean_id, (set([]), {}, set([]))) entry[0].add(get_usk_for_usk_version(usk, 0)) entry[2].add(fms_id) self.table[clean_id] = entry
def handle_announcement(self, clean_id, fms_id, usk): """ INTERNAL: process a single announcement. """ entry = self.table.get(clean_id, (set([]), {}, set([]))) entry[0].add(get_usk_for_usk_version(usk, 0)) entry[2].add(fms_id) self.table[clean_id] = entry
def execute_info(ui_, repo, params, stored_cfg): """ Run the info command. """ request_uri = params['REQUEST_URI'] if request_uri is None or not is_usk_file(request_uri): ui_.status("Only works with USK file URIs.\n") return usk_hash = normalize(request_uri) max_index = stored_cfg.get_index(request_uri) if max_index is None: ui_.status(NO_INFO_FMT % usk_hash) return insert_uri = str(stored_cfg.get_insert_uri(usk_hash)) # fix index request_uri = get_usk_for_usk_version(request_uri, max_index) trusted = stored_cfg.trusted_notifiers(usk_hash) if not trusted: trusted = ' None' else: trusted = ' ' + '\n '.join(trusted) ui_.status(INFO_FMT % (usk_hash, max_index or -1, trusted, request_uri, insert_uri)) update_sm = setup(ui_, repo, params, stored_cfg) try: ui_.status('Freenet head(s): %s\n' % ' '.join([ver[:12] for ver in read_freenet_heads(params, update_sm, request_uri)])) finally: cleanup(update_sm)
def execute_arc_pull(ui_, params, stored_cfg): """ Update from an existing incremental archive in Freenet. """ update_sm = None top_key_state = None try: assert 'ARCHIVE_CACHE_DIR' in params assert not params['REQUEST_URI'] is None if not params['NO_SEARCH'] and is_usk_file(params['REQUEST_URI']): index = stored_cfg.get_index(params['REQUEST_URI']) if not index is None: if index >= get_version(params['REQUEST_URI']): # Update index to the latest known value # for the --uri case. params['REQUEST_URI'] = get_usk_for_usk_version( params['REQUEST_URI'], index) else: ui_.status(("Cached index [%i] < index in USK [%i]. " + "Using the index from the USK.\n" + "You're sure that index exists, right?\n") % (index, get_version(params['REQUEST_URI']))) update_sm = setup(ui_, None, params, stored_cfg) ui_.status( "%sRequest URI:\n%s\n" % (is_redundant(params['REQUEST_URI']), params['REQUEST_URI'])) # Pull changes into the local block cache. ctx = ArchiveUpdateContext(update_sm, ui_) ctx.update({ 'REQUEST_URI': params['REQUEST_URI'], 'ARCHIVE_CACHE_DIR': params['ARCHIVE_CACHE_DIR'] }) start_requesting_blocks(update_sm, ctx) run_until_quiescent(update_sm, params['POLL_SECS']) if update_sm.get_state(QUIESCENT).arrived_from(((FINISHING, ))): uri = update_sm.get_state(ARC_REQUESTING_URI).get_latest_uri() blocks = update_sm.get_state(ARC_CACHING_TOPKEY).get_blocks() plural = '' if len(blocks) != 1: plural = 's' ui_.status("Fetched %i bytes in %i CHK%s from:\n%s\n" % (sum([block[0] for block in blocks]), len(blocks), plural, uri)) ui_.status("Updating local directory...\n") local_synch( ui_, params['ARCHIVE_CACHE_DIR'], # Use the updated URI below so we get the # right cached topkey. uri, params['TO_DIR']) top_key_state = ARC_REQUESTING_URI else: ui_.status("Synchronize failed.\n") arc_handle_updating_config(update_sm, params, stored_cfg, True) finally: arc_cleanup(update_sm, top_key_state)
def execute_arc_pull(ui_, params, stored_cfg): """ Update from an existing incremental archive in Freenet. """ update_sm = None top_key_state = None try: assert 'ARCHIVE_CACHE_DIR' in params assert not params['REQUEST_URI'] is None if not params['NO_SEARCH'] and is_usk_file(params['REQUEST_URI']): index = stored_cfg.get_index(params['REQUEST_URI']) if not index is None: if index >= get_version(params['REQUEST_URI']): # Update index to the latest known value # for the --uri case. params['REQUEST_URI'] = get_usk_for_usk_version( params['REQUEST_URI'], index) else: ui_.status(("Cached index [%i] < index in USK [%i]. " + "Using the index from the USK.\n" + "You're sure that index exists, right?\n") % (index, get_version(params['REQUEST_URI']))) update_sm = setup(ui_, None, params, stored_cfg) ui_.status("%sRequest URI:\n%s\n" % ( is_redundant(params['REQUEST_URI']), params['REQUEST_URI'])) # Pull changes into the local block cache. ctx = ArchiveUpdateContext(update_sm, ui_) ctx.update({'REQUEST_URI':params['REQUEST_URI'], 'ARCHIVE_CACHE_DIR':params['ARCHIVE_CACHE_DIR']}) start_requesting_blocks(update_sm, ctx) run_until_quiescent(update_sm, params['POLL_SECS']) if update_sm.get_state(QUIESCENT).arrived_from(((FINISHING,))): uri = update_sm.get_state(ARC_REQUESTING_URI).get_latest_uri() blocks = update_sm.get_state(ARC_CACHING_TOPKEY).get_blocks() plural = '' if len(blocks) != 1: plural = 's' ui_.status("Fetched %i bytes in %i CHK%s from:\n%s\n" % (sum([block[0] for block in blocks]), len(blocks), plural, uri)) ui_.status("Updating local directory...\n") local_synch(ui_, params['ARCHIVE_CACHE_DIR'], # Use the updated URI below so we get the # right cached topkey. uri, params['TO_DIR']) top_key_state = ARC_REQUESTING_URI else: ui_.status("Synchronize failed.\n") arc_handle_updating_config(update_sm, params, stored_cfg, True) finally: arc_cleanup(update_sm, top_key_state)
def get_request_uri(self, for_dir): """ Get the repo USK used to pull changes into for_dir or None. """ uri = self.request_usks.get(norm_path(for_dir)) if uri is None: return None version = self.get_index(uri) if not version is None: if version > get_version(uri): uri = get_usk_for_usk_version(uri, version) return uri
def get_request_uri(self, for_dir): """ Get the repo USK used to pull changes into for_dir or None. """ uri = self.request_usks.get(norm_path(for_dir)) if uri is None: return None version = self.get_index(uri) if not version is None: if version > get_version(uri): uri = get_usk_for_usk_version(uri, version) return uri
def get_insert_uri(self, for_usk_or_id): """ Get the insert USK for the request USK or None. """ uri = self.insert_usks.get(normalize(for_usk_or_id)) if uri is None: return None version = self.get_index(for_usk_or_id) if not version is None: if version > get_version(uri): uri = get_usk_for_usk_version(uri, version) return uri
def get_insert_uri(self, for_usk_or_id): """ Get the insert USK for the request USK or None. """ uri = self.insert_usks.get(normalize(for_usk_or_id)) if uri is None: return None version = self.get_index(for_usk_or_id) if not version is None: if version > get_version(uri): uri = get_usk_for_usk_version(uri, version) return uri
def leave(self, to_state): """ Implementation of State virtual. """ if to_state.name == self.success_state: # Hmmm... what about chks? # Update the index in the insert_uri on success if (should_increment(self) and is_usk(self.parent.ctx['INSERT_URI'])): version = get_version(self.parent.ctx['INSERT_URI']) + 1 self.parent.ctx['INSERT_URI'] = (get_usk_for_usk_version( self.parent.ctx['INSERT_URI'], version)) if self.parent.params.get('DUMP_URIS', False): self.parent.ctx.ui_.status( ("INSERT UPDATED INSERT " + "URI:\n%s\n") % self.parent.ctx['INSERT_URI'])
def leave(self, to_state): """ Implementation of State virtual. """ if to_state.name == self.success_state: # Hmmm... what about chks? # Update the index in the insert_uri on success if (should_increment(self) and is_usk(self.parent.ctx['INSERT_URI'])): version = get_version(self.parent.ctx['INSERT_URI']) + 1 self.parent.ctx['INSERT_URI'] = ( get_usk_for_usk_version(self.parent.ctx['INSERT_URI'], version)) if self.parent.params.get('DUMP_URIS', False): self.parent.ctx.ui_.status(("INSERT UPDATED INSERT " + "URI:\n%s\n") % self.parent.ctx['INSERT_URI'])
def get_latest_uri(self): """ Returns the URI with the version part update if the URI is a USK.""" if (is_usk(self.parent.ctx['REQUEST_URI']) and self.parent.params['NO_SEARCH']): return self.parent.ctx['REQUEST_URI'] max_version = None for candidate in self.ordered: result = candidate[5] if result is None or result[0] != 'AllData': continue uri = result[1]['URI'] if not is_usk_file(uri): return uri max_version = max(max_version, abs(get_version(uri))) break assert not max_version is None # The .R1 URI is queued first. assert (len(self.ordered) < 2 or self.ordered[0][0].find('.R1') != -1) return get_usk_for_usk_version(self.ordered[0][0], max_version)
def execute_pull(ui_, repo, params, stored_cfg): """ Run the pull command. """ update_sm = None try: assert not params['REQUEST_URI'] is None if not params['NO_SEARCH'] and is_usk_file(params['REQUEST_URI']): index = stored_cfg.get_index(params['REQUEST_URI']) if not index is None: if index >= get_version(params['REQUEST_URI']): # Update index to the latest known value # for the --uri case. params['REQUEST_URI'] = get_usk_for_usk_version( params['REQUEST_URI'], index) else: ui_.status(("Cached index [%i] < index in USK [%i]. " + "Using the index from the USK.\n" + "You're sure that index exists, right?\n") % (index, get_version(params['REQUEST_URI']))) update_sm = setup(ui_, repo, params, stored_cfg) ui_.status("%sRequest URI:\n%s\n" % (is_redundant(params[ 'REQUEST_URI']), params['REQUEST_URI'])) #ui_.status("Current tip: %s\n" % hex_version(repo)[:12]) update_sm.start_pulling(params['REQUEST_URI']) run_until_quiescent(update_sm, params['POLL_SECS']) if update_sm.get_state(QUIESCENT).arrived_from(((FINISHING,))): ui_.status("Pulled from:\n%s\n" % update_sm.get_state('REQUESTING_URI'). get_latest_uri()) #ui_.status("New tip: %s\n" % hex_version(repo)[:12]) else: ui_.status("Pull failed.\n") handle_updating_config(repo, update_sm, params, stored_cfg, True) finally: cleanup(update_sm)
def get_latest_uri(self): """ Returns the URI with the version part update if the URI is a USK.""" if (is_usk(self.parent.ctx['REQUEST_URI']) and self.parent.params['NO_SEARCH']): return self.parent.ctx['REQUEST_URI'] max_version = None for candidate in self.ordered: result = candidate[5] if result is None or result[0] != 'AllData': continue uri = result[1]['URI'] if not is_usk_file(uri): return uri max_version = max(max_version, abs(get_version(uri))) break assert not max_version is None # The .R1 URI is queued first. assert (len(self.ordered) < 2 or self.ordered[0][0].find('.R1') != -1) return get_usk_for_usk_version(self.ordered[0][0], max_version)
def leave(self, to_state): """ Override to update REQUEST_URI in the parent's context. """ InsertingUri.leave(self, to_state) if to_state.name != self.success_state: return if self.parent.ctx['INSERT_URI'] is None: # Assert reinserting??? return # i.e. for reinserting. if not (is_usk(self.parent.ctx['INSERT_URI']) and is_usk(self.parent.ctx['REQUEST_URI'])): return if (get_version(self.parent.ctx['INSERT_URI']) > get_version(self.parent.ctx['REQUEST_URI'])): version = get_version(self.parent.ctx['INSERT_URI']) self.parent.ctx['REQUEST_URI'] = ( get_usk_for_usk_version(self.parent.ctx['REQUEST_URI'], version))
def execute_pull(ui_, repo, params, stored_cfg): """ Run the pull command. """ update_sm = None try: assert not params['REQUEST_URI'] is None if not params['NO_SEARCH'] and is_usk_file(params['REQUEST_URI']): index = stored_cfg.get_index(params['REQUEST_URI']) if not index is None: if index >= get_version(params['REQUEST_URI']): # Update index to the latest known value # for the --uri case. params['REQUEST_URI'] = get_usk_for_usk_version( params['REQUEST_URI'], index) else: ui_.status(("Cached index [%i] < index in USK [%i]. " + "Using the index from the USK.\n" + "You're sure that index exists, right?\n") % (index, get_version(params['REQUEST_URI']))) update_sm = setup(ui_, repo, params, stored_cfg) ui_.status( "%sRequest URI:\n%s\n" % (is_redundant(params['REQUEST_URI']), params['REQUEST_URI'])) #ui_.status("Current tip: %s\n" % hex_version(repo)[:12]) update_sm.start_pulling(params['REQUEST_URI']) run_until_quiescent(update_sm, params['POLL_SECS']) if update_sm.get_state(QUIESCENT).arrived_from(((FINISHING, ))): ui_.status("Pulled from:\n%s\n" % update_sm.get_state('REQUESTING_URI').get_latest_uri()) #ui_.status("New tip: %s\n" % hex_version(repo)[:12]) else: ui_.status("Pull failed.\n") handle_updating_config(repo, update_sm, params, stored_cfg, True) finally: cleanup(update_sm)
def leave(self, to_state): """ Implementation of State virtual. """ if to_state.name == self.success_state: self.parent.ctx['REQUEST_URI'] = self.get_latest_uri() if is_usk(self.parent.ctx['REQUEST_URI']): self.parent.ctx.ui_.status( "Current USK version: %i\n" % get_version(self.parent.ctx['REQUEST_URI'])) if (self.parent.ctx['IS_KEYPAIR'] and is_usk(self.parent.ctx['REQUEST_URI']) and # lose usk checks? is_usk(self.parent.ctx['INSERT_URI'])): version = get_version(self.parent.ctx['REQUEST_URI']) self.parent.ctx['INSERT_URI'] = (get_usk_for_usk_version( self.parent.ctx['INSERT_URI'], version)) #print "SEARCH UPDATED INSERT URI: ", \ # self.parent.ctx['INSERT_URI'] # Allow pending requests to run to completion. self.parent.ctx.orphan_requests(self) if self.parent.params.get('DUMP_TOP_KEY', False): self.topkey_funcs.dump_top_key_tuple( self.get_top_key_tuple(), self.parent.ctx.ui_.status)
def leave(self, to_state): """ Implementation of State virtual. """ if to_state.name == self.success_state: self.parent.ctx['REQUEST_URI'] = self.get_latest_uri() if is_usk(self.parent.ctx['REQUEST_URI']): self.parent.ctx.ui_.status("Current USK version: %i\n" % get_version(self.parent .ctx['REQUEST_URI'])) if (self.parent.ctx['IS_KEYPAIR'] and is_usk(self.parent.ctx['REQUEST_URI']) and # lose usk checks? is_usk(self.parent.ctx['INSERT_URI'])): version = get_version(self.parent.ctx['REQUEST_URI']) self.parent.ctx['INSERT_URI'] = ( get_usk_for_usk_version(self.parent.ctx['INSERT_URI'], version)) #print "SEARCH UPDATED INSERT URI: ", \ # self.parent.ctx['INSERT_URI'] # Allow pending requests to run to completion. self.parent.ctx.orphan_requests(self) if self.parent.params.get('DUMP_TOP_KEY', False): self.topkey_funcs.dump_top_key_tuple(self.get_top_key_tuple(), self.parent.ctx.ui_.status)
def usks_equal(usk_a, usk_b): """ Returns True if the USKs are equal disregarding version. """ return (get_usk_for_usk_version(usk_a, 0) == get_usk_for_usk_version(usk_b, 0))
def usks_equal(usk_a, usk_b): """ Returns True if the USKs are equal disregarding version. """ return (get_usk_for_usk_version(usk_a, 0) == get_usk_for_usk_version(usk_b, 0))
def request_uri(self): """ Return the repository request URI. """ return get_usk_for_usk_version(self.parent.params['REQUEST_URI'], self.store_info['LATEST_INDEX'])
def insert_uri(self): """ Return the repository insert URI. """ return get_usk_for_usk_version(self.parent.params['INSERT_URI'], self.store_info['LATEST_INDEX'])
def request_uri(self): """ Return the repository request URI. """ return get_usk_for_usk_version(self.parent.params['REQUEST_URI'], self.store_info['LATEST_INDEX'])
def insert_uri(self): """ Return the repository insert URI. """ return get_usk_for_usk_version(self.parent.params['INSERT_URI'], self.store_info['LATEST_INDEX'])