summaryrefslogtreecommitdiffstats
path: root/gluster/utils.py
blob: bc551848b11f3a8ce5dbbbbbb003806c93c47ca4 (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
# Copyright (c) 2016 Red Hat, Inc.
#
# This file is part of libgfapi-python project which is a
# subproject of GlusterFS ( www.gluster.org)
#
# This file is licensed to you under your choice of the GNU Lesser
# General Public License, version 3 or any later version (LGPLv3 or
# later), or the GNU General Public License, version 2 (GPLv2), in all
# cases as published by the Free Software Foundation.

import os
import errno
from functools import wraps
from gluster.exceptions import VolumeNotMounted


def validate_mount(func):
    """
    Decorator to assert that volume is initialized and mounted before any
    further I/O calls are invoked by methods.

    :param func: method to be decorated and checked.
    """
    def _exception(volname):
        raise VolumeNotMounted('Volume "%s" not mounted.' % (volname))

    @wraps(func)
    def wrapper(*args, **kwargs):
        self = args[0]
        if self.fs and self._mounted:
            return func(*args, **kwargs)
        else:
            return _exception(self.volname)
    wrapper.__wrapped__ = func

    return wrapper


def validate_glfd(func):
    """
    Decorator to assert that glfd is valid.

    :param func: method to be decorated and checked.
    """
    def _exception():
        raise OSError(errno.EBADF, os.strerror(errno.EBADF))

    @wraps(func)
    def wrapper(*args, **kwargs):
        self = args[0]
        if self.fd:
            return func(*args, **kwargs)
        else:
            return _exception()
    wrapper.__wrapped__ = func

    return wrapper