def decrypt_write_file(localindex, fdir, fhash, secret): """ @param localindex: dict @type localindex: @param fdir: str or unicode @type fdir: @param fhash: str or unicode @type fhash: @param secret: str or unicode @type secret: """ cpath = os.path.join(fdir, fhash[2:]) enc_file_parts = open(cpath).read().split("\n") enc_file_parts = tuple([x for x in enc_file_parts if x]) data = decrypt_file_smp(secret, enc_files=enc_file_parts, progress_callback=update_item_progress) os.remove(cpath) for fp in enc_file_parts: os.remove(fp) files_left = get_files_dir(fdir) if len(files_left) == 0: os.rmdir(fdir) if len(files_left) == 1: dstorp = os.path.join(fdir, ".DS_Store") if os.path.exists(dstorp): os.remove(dstorp) files_left = get_files_dir(fdir) if len(files_left) == 0: os.rmdir(fdir) file_blob = {"data": data} paths = [] for dirhash in localindex["dirnames"]: for cfile in localindex["dirnames"][dirhash]["filenames"]: if strcmp(fhash, cfile["hash"]): fpath = os.path.join( localindex["dirnames"][dirhash]["dirname"], cfile["name"]) if not os.path.exists(fpath): ft = localindex["filestats"][fpath] file_blob["st_atime"] = int(ft["st_atime"]) file_blob["st_mtime"] = int(ft["st_mtime"]) file_blob["st_mode"] = int(ft["st_mode"]) file_blob["st_uid"] = int(ft["st_uid"]) file_blob["st_gid"] = int(ft["st_gid"]) write_fdict_to_file(file_blob, fpath) paths.append(fpath) return paths
def decrypt_write_file(localindex, fdir, fhash, secret): """ @param localindex: dict @type localindex: @param fdir: str or unicode @type fdir: @param fhash: str or unicode @type fhash: @param secret: str or unicode @type secret: """ cpath = os.path.join(fdir, fhash[2:]) enc_file_parts = open(cpath).read().split("\n") enc_file_parts = tuple([x for x in enc_file_parts if x]) data = decrypt_file_smp(secret, enc_files=enc_file_parts, progress_callback=update_item_progress) os.remove(cpath) for fp in enc_file_parts: os.remove(fp) files_left = get_files_dir(fdir) if len(files_left) == 0: os.rmdir(fdir) if len(files_left) == 1: dstorp = os.path.join(fdir, ".DS_Store") if os.path.exists(dstorp): os.remove(dstorp) files_left = get_files_dir(fdir) if len(files_left) == 0: os.rmdir(fdir) file_blob = {"data": data} paths = [] for dirhash in localindex["dirnames"]: for cfile in localindex["dirnames"][dirhash]["filenames"]: if strcmp(fhash, cfile["hash"]): fpath = os.path.join(localindex["dirnames"][dirhash]["dirname"], cfile["name"]) if not os.path.exists(fpath): ft = localindex["filestats"][fpath] file_blob["st_atime"] = int(ft["st_atime"]) file_blob["st_mtime"] = int(ft["st_mtime"]) file_blob["st_mode"] = int(ft["st_mode"]) file_blob["st_uid"] = int(ft["st_uid"]) file_blob["st_gid"] = int(ft["st_gid"]) write_fdict_to_file(file_blob, fpath) paths.append(fpath) return paths
def test_hide_config(self): """ test_hide_config """ self.delete_hidden_configs() self.do_wait_for_tasks = False self.unzip_testfiles_configonly() p = os.path.join(os.path.join(os.getcwd(), "testdata"), "test") org_files = get_files_dir(p) org_files = [x for x in org_files if "memory.pickle.json" not in x] salt = "123" secret = base64.decodestring('Ea9fxt0JExxPqkbbIAFggRz0DIsFumuXX/xnuARPOTw=\n') hide_config(self.cboptions, salt, secret) restore_hidden_config(self.cboptions) org_files2 = get_files_dir(p) self.assertEqual(set(org_files), set(org_files2))
def test_encrypt_hide_decrypt(self): """ encrypt_hide_decrypt """ self.do_wait_for_tasks = False encrypt = 1 decrypt = 1 self.reset_cb_dir() self.unzip_testfiles_synced() p = os.path.join(os.path.join(os.getcwd(), "testdata"), "test") org_files = get_files_dir(p, ignore_hidden=True) org_files = [x for x in org_files if "memory.pickle.json" not in x] #decrypt_and_build_filetree, hide_config if encrypt: self.delete_hidden_configs() self.do_wait_for_tasks = False self.cboptions.remove = True salt, secret, self.cbmemory, localindex = index_and_encrypt(self.cbmemory, self.cboptions) datadir = get_data_dir(self.cboptions) self.cbmemory.save(datadir) hide_config(self.cboptions, salt, secret) self.assertEqual(count_files_dir(get_blob_dir(self.cboptions)), 0) if decrypt: os.system("rm -Rf testdata/test") if not encrypt: os.system("cd testdata; unzip -o hidden_config.zip > /dev/null") options = self.cboptions options.encrypt = False options.decrypt = True options.remove = False secret = restore_hidden_config(options) datadir = get_data_dir(self.cboptions) memory = Memory() memory.load(datadir) decrypt_and_build_filetree(memory, options, secret) org_files2 = get_files_dir(p, ignore_hidden=True) self.assertEqual(set(org_files), set(org_files2))
def test_super_large_file(self): """ test_super_large_file """ self.reset_cb_db_clean() self.unzip_testfiles_clean() self.make_testfile("3000MB.txt", 3000) os.system("rm testdata/test/all_types/*") os.system("rm -Rf testdata/test/smalltest") os.system("cp testdata/3000MB.txt testdata/test/all_types/") localindex, self.cbmemory = sync_server(self.cbmemory, self.cboptions) self.assertTrue(self.files_synced()) datadir = get_data_dir(self.cboptions) self.cbmemory.save(datadir) p = os.path.join(os.path.join(os.getcwd(), "testdata"), "test") org_files = get_files_dir(p, ignore_hidden=True) org_files = [x for x in org_files if "memory.pickle.json" not in x] org_files1 = [make_sha1_hash_file(fpath=x) for x in org_files] self.delete_hidden_configs() self.do_wait_for_tasks = False self.cboptions.remove = True salt, secret, self.cbmemory, localindex = index_and_encrypt(self.cbmemory, self.cboptions) datadir = get_data_dir(self.cboptions) self.cbmemory.save(datadir) hide_config(self.cboptions, salt, secret) self.assertEqual(count_files_dir(get_blob_dir(self.cboptions)), 0) options = self.cboptions options.encrypt = False options.decrypt = True options.remove = False secret = restore_hidden_config(options) datadir = get_data_dir(self.cboptions) memory = Memory() memory.load(datadir) decrypt_and_build_filetree(memory, options, secret) org_files2 = get_files_dir(p, ignore_hidden=True) org_files3 = [make_sha1_hash_file(fpath=x) for x in org_files2] self.assertEqual(set(org_files1), set(org_files3)) os.system("rm -Rf testdata/test") os.system("rm testdata/3000MB.txt")
def count_files_dir(fpath): """ count_files_dir @type fpath: str, unicode """ return len(get_files_dir(fpath))