def read_upload_file(self, data): fp = self.get_path(data['path']) f = os.open(fp, os.O_WRONLY | os.O_CREAT, self.permissions_file) xattr_data = pickle.loads(self.inpipe.read(data['xattr_size'])) if os.path.exists(fp) and self.allowdelete: if os.path.islink(fp): os.remove(fp) elif os.path.isdir(fp): shutil.rmtree(fp, ignore_errors=True) with os.fdopen(os.dup(f), 'wb') as output: util.copy_file_limited(self.inpipe, output, data['size']) self._set_stat_and_xattr(fp, data['stat'], xattr_data) parent_dir = self.filedb.get_path(os.path.dirname(data['path'])) ent = FileDbEntry(os.path.basename(data['path']), parent_dir) ent.size = data['size'] ent.mtime = data['stat']['mtime'] with open(fp, 'rb') as fh: ent.sha256 = util.sha256_file(fh) self.filedb.append(ent) os.close(f) return True
def test_simple(self): if os.path.exists('test.bin'): raise FileExistsError() try: db = FileDb('test.bin') ent = FileDbEntry('test_dir', db.root) ent.set_directory() db.append(ent) db.close() db = FileDb('test.bin') dir = db.get_path('test_dir') self.assertTrue(dir.is_directory()) finally: os.remove('test.bin')
def get_file_db(self): self.write_command({'op': 'getdb'}) data = json.loads(self.read_line()) ret = FileDb(None) for i in range(1, data['count']): ent = FileDbEntry.decode(self.inpipe, ret.db) if ent.is_removed(): raise ValueError( 'Server db data cannot contain removed entries') ret.db[ent.id] = ent ent.parent.add_child(ent) return ret
def read_symlink(self, data): xattr_data = pickle.loads(self.inpipe.read(data['xattr_size'])) fp = self.get_path(data['path']) to_fp = data['to'] if to_fp[0] == '/': to_fp = self.get_path(to_fp) if os.path.exists(fp): if os.path.islink(fp): os.remove(fp) elif os.path.isdir(fp) and self.allowdelete: shutil.rmtree(fp, ignore_errors=True) elif self.allowdelete: os.remove(fp) os.symlink(to_fp, fp) # self._set_stat_and_xattr(fp, data['stat'], xattr_data) parent_dir = self.filedb.get_path(os.path.dirname(data['path'])) ent = FileDbEntry(os.path.basename(data['path']), parent_dir) ent.symlink = data['to'] ent.mtime = data['stat']['mtime'] self.filedb.append(ent) return True
def read_mkdir(self, data): xattr_data = pickle.loads(self.inpipe.read(data['xattr_size'])) fp = self.get_path(data['path']) if os.path.isfile(fp) and self.allowdelete: print(f"mkdir {fp} - already exists; deleting", file=sys.stderr) os.remove(fp) try: os.mkdir(fp, mode=self.permissions_dir) except FileExistsError: pass self._set_stat_and_xattr(fp, data['stat'], xattr_data) parent_dir = self.filedb.get_path(os.path.dirname(data['path'])) ent = parent_dir.get_child(os.path.basename(data['path'])) if ent is not None: if not ent.is_directory(): raise ValueError('Dir already exists as a file in the db') else: ent = FileDbEntry(os.path.basename(data['path']), parent_dir) ent.set_directory() ent.mtime = data['stat']['mtime'] self.filedb.append(ent) return True
def test_append(self): if os.path.exists('test.bin'): raise FileExistsError() try: db = FileDb('test.bin') ent = FileDbEntry('test.txt', db.root) ent.set_directory() db.append(ent) db.close() db = FileDb('test.bin') ent = db.get_path('test.txt') ent.reset_meta() ent.sha256 = b'1234' db.append(ent) db.close() db = FileDb('test.bin') ent = db.get_path('test.txt') self.assertFalse(ent.is_directory()) self.assertEqual(ent.sha256, b'1234') db.close() finally: os.remove('test.bin')
def test_delete(self): if os.path.exists('test.bin'): raise FileExistsError() try: db = FileDb('test.bin') dir = FileDbEntry('test_dir', db.root) dir.set_directory() db.append(dir) file1 = FileDbEntry('file1.txt', dir) file1.sha256 = b'1234' db.append(file1) file2 = FileDbEntry('file2.txt', dir) file2.sha256 = b'4567' db.append(file2) db.close() db = FileDb('test.bin') file1 = db.get_path('test_dir/file1.txt') self.assertEqual(file1.sha256, b'1234') file1.set_removed() db.append(file1) db.close() db = FileDb('test.bin') with self.assertRaises(KeyError): db.get_path('test_dir/file1.txt') file2 = db.get_path('test_dir/file2.txt') self.assertEqual(file2.sha256, b'4567') file2.set_removed() db.append(file2) dir = file2.parent dir.set_removed() db.append(dir) db.close() db = FileDb('test.bin') with self.assertRaises(KeyError): db.get_path('test_dir/file1.txt') with self.assertRaises(KeyError): db.get_path('test_dir/file2.txt') with self.assertRaises(KeyError): db.get_path('test_dir') db.close() finally: os.remove('test.bin')