示例#1
0
	def close(self):
		""" Closes the file and returns a URI with the final address of the file.
		If save_metadata is set, the file metadata is saved in the DHT.
		You can always access to the metadata with File.metadata """
		if self.closed: return
		logger.info('Closing %s'%self.uri.get_readable())
		if self.mode == 'w':
			self.flush(True)
			self.metadata.set('Main:parts', len(self.parts))
			self.metadata.set('Main:length', self.filelength)
			self.metadata.set('Main:hash', self.hasher.hexdigest())
			self.metadata.set('Main:p', '')
			if self.save_metadata:
				# variables used to chain metadata blocks
				puri = self.uri
				pmeta = self.metadata
				not_saved = True
				# crypter used to encrypt the metadata. There is always
				# a crypter to protect against casual atackers, but
				# if there is no Kf the crypter is nearly useless
				if SECURED:
					if self.keys[4]:
						mdencrypter = AES.new(self.keys[4], AES.MODE_CBC, self.uri.get_hd())
					else:
						mdencrypter = AES.new(self.uri.get_hd(), AES.MODE_CBC, self.uri.get_hd())
				else:
					mdencrypter = DummyEncrypter()
				for i in range(0, len(self.parts)):
					pmeta.set('Part:%d'%i, self.parts[i])
					# chain the metadata blocks, each block only with
					# DESC_PER_METAPART references to parts of the file
					if i<len(self.parts)-1 and i%self.DESC_PER_METAPART == self.DESC_PER_METAPART-1:
						nuri=URI(self.uri.uid,utils.random_nick(),'',self.keys)
						nuri.hd=utils.random_string(16,False)
						pmeta.set('Main:n',nuri.get_static())
						m=pmeta.save()
						pmeta.set('Main:p',utils.random_string(self.BLOCK_SIZE-len(m)))
						m=mdencrypter.encrypt(pmeta.save())
						dfs.dht.put(puri.get_hd(),m,puri.nick)
						pmeta=utils.Config()
						pmeta.set('Main:p','')
						puri=nuri
						not_saved=False
					else:
						not_saved=True
				if not_saved:
					m=pmeta.save()
					pmeta.set('Main:p',utils.random_string(self.BLOCK_SIZE-len(m)))
					m=mdencrypter.encrypt(pmeta.save())
					dfs.dht.put(puri.get_hd(),m,puri.nick)

			# Create the final metadata
			for i in range(0,len(self.parts)):
				self.metadata.set('Part:%d'%i,self.parts[i])
		else:
			# In read, free the buffer
			self.buffer = None
		self.closed = True
		return self.uri
示例#2
0
文件: __init__.py 项目: Juanvvc/scfs
def init_default_conf():
    """ Sets up a default configuration for DFS: uses $HOME/.dfs as
	the config dir, reads the default configuration from
	$HOME/.dfs/dfsrc, and sets up the logging system to use the file
	dfs.log in the configuration directory """

    global default_config_dir, default_config, default_log_file, default_config_file, dht

    # Creates a directory to the default config, if it does not exists.
    default_config_dir = os.path.expanduser('~%s.dfs' % os.path.sep)
    # Create the default config path if it does not exists
    if not os.path.exists(default_config_dir): os.mkdir(default_config_dir)
    default_config_file = default_config_dir + os.path.sep + 'dfsrc'
    default_log_file = default_config_dir + os.path.sep + 'dfs.log'
    # Load the default config file
    if not os.path.exists(default_config_file):
        open(default_config_file, 'w').close()
    default_config = utils.Config()
    default_config.load(open(default_config_file, 'r'))

    # Configures the logging system
    utils.configure_logging(
        level=logging.INFO,
        format='%(asctime)s %(name)s %(levelname)s %(message)s',
        datefmt='%H:%M:%S',
        filename=default_log_file,
        filemode='w')

    logging.info('Default configuration: %s' % default_config_file)

    # sets default configuration, if not set
    changed = False
    if not default_config.get('DHT:datadir'):
        default_config.set('DHT:datadir',
                           default_config_dir + os.path.sep + 'dhtdata')
        changed = True
    if not default_config.get('Main:UID'):
        default_config.set('Main:uid', utils.random_string(16))
        changes = True
    if not default_config.get('Main:nick'):
        default_config.set('Main:nick', utils.random_nick())
        changed = True
    if not default_config.get('Keys:kf'):
        logging.warning('There are not file key')
    if not default_config.get('Keys:kd'):
        logging.warning('There are not description key')
    if changed:
        default_config.save(open(default_config_file, 'w'))

    # Default DHT: a local DHT
    dht = DHT.LocalDHT(default_config)
示例#3
0
文件: __init__.py 项目: Juanvvc/scfs
def init_default_conf():	
	""" Sets up a default configuration for DFS: uses $HOME/.dfs as
	the config dir, reads the default configuration from
	$HOME/.dfs/dfsrc, and sets up the logging system to use the file
	dfs.log in the configuration directory """
	
	global default_config_dir, default_config, default_log_file, default_config_file, dht
	
	# Creates a directory to the default config, if it does not exists.
	default_config_dir=os.path.expanduser('~%s.dfs'%os.path.sep)
	# Create the default config path if it does not exists
	if not os.path.exists(default_config_dir): os.mkdir(default_config_dir)
	default_config_file=default_config_dir+os.path.sep+'dfsrc'
	default_log_file=default_config_dir+os.path.sep+'dfs.log'
	# Load the default config file
	if not os.path.exists(default_config_file): open(default_config_file,'w').close()
	default_config=utils.Config()
	default_config.load(open(default_config_file,'r'))
	
	# Configures the logging system
	utils.configure_logging(level=logging.INFO,
		format='%(asctime)s %(name)s %(levelname)s %(message)s',
		datefmt='%H:%M:%S',
		filename=default_log_file,
		filemode='w')	
	
	logging.info('Default configuration: %s'%default_config_file)
	
	# sets default configuration, if not set
	changed=False
	if not default_config.get('DHT:datadir'):
		default_config.set('DHT:datadir',default_config_dir+os.path.sep+'dhtdata')
		changed=True
	if not default_config.get('Main:UID'):
		default_config.set('Main:uid',utils.random_string(16))
		changes=True
	if not default_config.get('Main:nick'):
		default_config.set('Main:nick',utils.random_nick())
		changed=True
	if not default_config.get('Keys:kf'):
		logging.warning('There are not file key')
	if not default_config.get('Keys:kd'):
		logging.warning('There are not description key')
	if changed:
		default_config.save(open(default_config_file,'w'))
		
	# Default DHT: a local DHT
	dht=DHT.LocalDHT(default_config)
示例#4
0
def random_uri(config=None, kd=None):
	""" Creates a random URI """
	u = URI('', utils.random_nick(), '', config=config, kd=kd)
	u.hd=utils.random_string(16, printable=False)
	return u