def test_subvolume_path(self): btrfsutil.create_subvolume(os.path.join(self.mountpoint, 'subvol1')) os.mkdir(os.path.join(self.mountpoint, 'dir1')) os.mkdir(os.path.join(self.mountpoint, 'dir1/dir2')) btrfsutil.create_subvolume(os.path.join(self.mountpoint, 'dir1/dir2/subvol2')) btrfsutil.create_subvolume(os.path.join(self.mountpoint, 'dir1/dir2/subvol2/subvol3')) os.mkdir(os.path.join(self.mountpoint, 'subvol1/dir3')) btrfsutil.create_subvolume(os.path.join(self.mountpoint, 'subvol1/dir3/subvol4')) for arg in self.path_or_fd(self.mountpoint): with self.subTest(type=type(arg)): self.assertEqual(btrfsutil.subvolume_path(arg), '') self.assertEqual(btrfsutil.subvolume_path(arg, 5), '') self.assertEqual(btrfsutil.subvolume_path(arg, 256), 'subvol1') self.assertEqual(btrfsutil.subvolume_path(arg, 257), 'dir1/dir2/subvol2') self.assertEqual(btrfsutil.subvolume_path(arg, 258), 'dir1/dir2/subvol2/subvol3') self.assertEqual(btrfsutil.subvolume_path(arg, 259), 'subvol1/dir3/subvol4') pwd = os.getcwd() try: os.chdir(self.mountpoint) path = '' for i in range(26): name = chr(ord('a') + i) * 255 path = os.path.join(path, name) btrfsutil.create_subvolume(name) os.chdir(name) self.assertEqual(btrfsutil.subvolume_path('.'), path) finally: os.chdir(pwd)
def get_subvolume_from(self, filesystem_path: Path) -> Optional[Subvolume]: logger = self._logger if not filesystem_path.exists(): raise SubvolumeError( f"The '{filesystem_path}' path does not exist!") if not filesystem_path.is_dir(): raise SubvolumeError( f"The '{filesystem_path}' path does not represent a directory!" ) try: filesystem_path_str = str(filesystem_path) if btrfsutil.is_subvolume(filesystem_path_str): subvolume_id = btrfsutil.subvolume_id(filesystem_path_str) subvolume_path = btrfsutil.subvolume_path( filesystem_path_str, subvolume_id) subvolume_read_only = btrfsutil.get_subvolume_read_only( filesystem_path_str) subvolume_info = btrfsutil.subvolume_info( filesystem_path_str, subvolume_id) self_uuid = default_if_none( try_convert_bytes_to_uuid(subvolume_info.uuid), constants.EMPTY_UUID, ) parent_uuid = default_if_none( try_convert_bytes_to_uuid(subvolume_info.parent_uuid), constants.EMPTY_UUID, ) return Subvolume( filesystem_path, subvolume_path, datetime.fromtimestamp(subvolume_info.otime), UuidRelation(self_uuid, parent_uuid), NumIdRelation(subvolume_info.id, subvolume_info.parent_id), subvolume_read_only, ) except btrfsutil.BtrfsUtilError as e: logger.exception("btrfsutil call failed!") raise SubvolumeError( f"Could not initialize the subvolume for '{filesystem_path}'!" ) from e return None