summaryrefslogtreecommitdiff
path: root/nova/filesystem.py
blob: 5394d2d8351c243dc5712a1654cf33a9a45e6e84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Functions to address filesystem calls, particularly sysfs."""

import os

from oslo_log import log as logging

from nova import exception

LOG = logging.getLogger(__name__)


SYS = '/sys'


# NOTE(bauzas): this method is deliberately not wrapped in a privsep entrypoint
def read_sys(path: str) -> str:
    """Reads the content of a file in the sys filesystem.

    :param path: relative or absolute. If relative, will be prefixed by /sys.
    :returns: contents of that file.
    :raises: nova.exception.FileNotFound if we can't read that file.
    """
    try:
        # The path can be absolute with a /sys prefix but that's fine.
        with open(os.path.join(SYS, path), mode='r') as data:
            return data.read()
    except (OSError, ValueError) as exc:
        raise exception.FileNotFound(file_path=path) from exc


# NOTE(bauzas): this method is deliberately not wrapped in a privsep entrypoint
# In order to correctly use it, you need to decorate the caller with a specific
# privsep entrypoint.
def write_sys(path: str, data: str) -> None:
    """Writes the content of a file in the sys filesystem with data.

    :param path: relative or absolute. If relative, will be prefixed by /sys.
    :param data: the data to write.
    :returns: contents of that file.
    :raises: nova.exception.FileNotFound if we can't write that file.
    """
    try:
        # The path can be absolute with a /sys prefix but that's fine.
        with open(os.path.join(SYS, path), mode='w') as fd:
            fd.write(data)
    except (OSError, ValueError) as exc:
        raise exception.FileNotFound(file_path=path) from exc