aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/utils/posix.py
blob: a601dee466acce9ba6ca9ee9054a08371ba36a91 (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
# SPDX-License-Identifier: Apache-2.0
# Copyright 2012-2021 The Meson development team
# Copyright © 2021-2023 Intel Corporation

from __future__ import annotations

"""Posix specific implementations of mesonlib functionality."""

import fcntl
import typing as T

from .core import MesonException
from .platform import DirectoryLockBase, DirectoryLockAction

__all__ = ['DirectoryLock', 'DirectoryLockAction']

class DirectoryLock(DirectoryLockBase):

    def __enter__(self) -> None:
        self.lockfile = open(self.lockpath, 'w+', encoding='utf-8')
        try:
            flags = fcntl.LOCK_EX
            if self.action != DirectoryLockAction.WAIT:
                flags = flags | fcntl.LOCK_NB
            fcntl.flock(self.lockfile, flags)
        except BlockingIOError:
            self.lockfile.close()
            if self.action == DirectoryLockAction.IGNORE:
                return
            raise MesonException(self.err)
        except PermissionError:
            self.lockfile.close()
            raise MesonException(self.err)
        except OSError as e:
            self.lockfile.close()
            raise MesonException(f'Failed to lock directory {self.lockpath}: {e.strerror}')

    def __exit__(self, *args: T.Any) -> None:
        if self.lockfile is None or self.lockfile.closed:
            return
        fcntl.flock(self.lockfile, fcntl.LOCK_UN)
        self.lockfile.close()