| # Copyright 2019 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| |
| class FvmImage(object): |
| """Represents an FVM (Fuchsia Volume Manager) image.""" |
| |
| def __init__(self, api, path, fvm_tool): |
| """ |
| Args: |
| api (recipe_api.RecipeApi): A recipe API. |
| path (Path): The path to the image. |
| fvm_tool (Path): The path to the `fvm` tool. |
| """ |
| self._api = api |
| self._path = path |
| self._tool = fvm_tool |
| self._size = None |
| |
| @property |
| def required_size(self): |
| """The size in bytes that the paved image would require. |
| |
| Only supported for sparse images. |
| """ |
| if not self._size: |
| basename = self._api.path.basename(self._path) |
| self._size = self._api.file.filesizes('%s size' % basename, |
| [self._path])[0] |
| return self._size |
| |
| def extend(self, factor): |
| """Extends the size of the image by a scaling factor. |
| |
| This operation is only valid for non-sparse images. |
| |
| Args: |
| factor (int or float): A factor by which to scale the size of the image. |
| """ |
| assert factor > 1, 'scaling factor must be greater than one' |
| new_size = int(self.required_size * factor) |
| size_result = self._api.step( |
| '%s extend' % self._api.path.basename(self._path), |
| [self._tool, self._path, 'extend', '--length', new_size]) |
| self._size = new_size |
| return size_result |