2019-06-26 20:13:36 +00:00
'''
Onionr - Private P2P Communication
import new blocks from disk , providing transport agnosticism
'''
'''
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < https : / / www . gnu . org / licenses / > .
'''
2019-06-23 17:41:07 +00:00
import glob
2019-07-19 19:49:56 +00:00
import logger
2019-06-25 08:21:36 +00:00
from onionrutils import blockmetadata
2019-07-17 16:25:29 +00:00
from coredb import blockmetadb
2019-07-27 02:42:55 +00:00
import filepaths
import onionrcrypto as crypto
2020-03-30 08:23:59 +00:00
from etc . onionrvalues import BLOCK_EXPORT_FILE_EXT
2019-07-19 19:49:56 +00:00
def import_new_blocks ( scanDir = ' ' ) :
2019-06-23 17:41:07 +00:00
'''
This function is intended to scan for new blocks ON THE DISK and import them
'''
2019-07-17 16:25:29 +00:00
blockList = blockmetadb . get_block_list ( )
2019-06-23 17:41:07 +00:00
exist = False
if scanDir == ' ' :
2019-07-19 19:49:56 +00:00
scanDir = filepaths . block_data_location
2019-06-23 17:41:07 +00:00
if not scanDir . endswith ( ' / ' ) :
scanDir + = ' / '
2020-03-30 08:23:59 +00:00
for block in glob . glob ( scanDir + " * %s " % ( BLOCK_EXPORT_FILE_EXT , ) ) :
if block . replace ( scanDir , ' ' ) . replace ( BLOCK_EXPORT_FILE_EXT , ' ' ) not in blockList :
2019-06-23 17:41:07 +00:00
exist = True
2019-06-28 21:10:29 +00:00
logger . info ( ' Found new block on dist %s ' % block , terminal = True )
2019-06-23 17:41:07 +00:00
with open ( block , ' rb ' ) as newBlock :
2020-03-30 08:23:59 +00:00
block = block . replace ( scanDir , ' ' ) . replace ( BLOCK_EXPORT_FILE_EXT , ' ' )
if crypto . hashers . sha3_hash ( newBlock . read ( ) ) == block . replace ( BLOCK_EXPORT_FILE_EXT , ' ' ) :
blockmetadb . add_to_block_DB ( block . replace ( BLOCK_EXPORT_FILE_EXT , ' ' ) , dataSaved = True )
logger . info ( ' Imported block %s ' % block , terminal = True )
2019-11-20 10:52:50 +00:00
blockmetadata . process_block_metadata ( block )
2019-06-23 17:41:07 +00:00
else :
2019-06-28 21:10:29 +00:00
logger . warn ( ' Failed to verify hash for %s ' % block , terminal = True )
2019-06-23 17:41:07 +00:00
if not exist :
2019-08-09 20:41:27 +00:00
logger . info ( ' No blocks found to import ' , terminal = True )
2019-09-21 05:06:49 +00:00
2020-03-30 08:23:59 +00:00
import_new_blocks . onionr_help = f " Scans the Onionr data directory under { filepaths . block_data_location } for new block files (.db not supported) to import "