~ubuntu-branches/ubuntu/trusty/landscape-client/trusty-proposed

1.1.1 by Rick Clark
Import upstream version 1.0.18
1
from __future__ import division
2
3
import os
4
import statvfs
5
6
1.1.14 by Free Ekanayaka
Import upstream version 1.5.0
7
def get_mount_info(mounts_file, statvfs_, filesystems_whitelist=None):
1.1.1 by Rick Clark
Import upstream version 1.0.18
8
    """
9
    Given a mounts file (e.g., /proc/mounts), generate dicts with the following
10
    keys:
11
1.1.14 by Free Ekanayaka
Import upstream version 1.5.0
12
    @param filesystems_whitelist: if provided, the list of file systems which
13
        we're allowed to stat.
14
1.1.1 by Rick Clark
Import upstream version 1.0.18
15
     - device: The device file which is mounted.
16
     - mount-point: The path at which the filesystem is mounted.
17
     - filesystem: The filesystem type.
18
     - total-space: The capacity of the filesystem in megabytes.
19
     - free-space: The amount of space available in megabytes.
20
    """
21
    for line in open(mounts_file):
22
        try:
23
            device, mount_point, filesystem = line.split()[:3]
24
            mount_point = mount_point.decode("string-escape")
25
        except ValueError:
26
            continue
1.1.14 by Free Ekanayaka
Import upstream version 1.5.0
27
        if (filesystems_whitelist is not None and
28
            filesystem not in filesystems_whitelist):
29
            continue
1.1.1 by Rick Clark
Import upstream version 1.0.18
30
        megabytes = 1024 * 1024
31
        stats = statvfs_(mount_point)
32
        block_size = stats[statvfs.F_BSIZE]
33
        total_space = (stats[statvfs.F_BLOCKS] * block_size) // megabytes
34
        free_space = (stats[statvfs.F_BFREE] * block_size) // megabytes
35
        yield {"device": device, "mount-point": mount_point,
36
               "filesystem": filesystem, "total-space": total_space,
37
               "free-space": free_space}
38
39
1.1.14 by Free Ekanayaka
Import upstream version 1.5.0
40
def get_filesystem_for_path(path, mounts_file, statvfs_,
41
                            filesystems_whitelist=None):
1.1.1 by Rick Clark
Import upstream version 1.0.18
42
    candidate = None
43
    path = os.path.realpath(path)
44
    path_segments = path.split("/")
1.1.14 by Free Ekanayaka
Import upstream version 1.5.0
45
    for info in get_mount_info(mounts_file, statvfs_, filesystems_whitelist):
1.1.1 by Rick Clark
Import upstream version 1.0.18
46
        mount_segments = info["mount-point"].split("/")
47
        if path.startswith(info["mount-point"]):
48
            if ((not candidate)
49
                or path_segments[:len(mount_segments)] == mount_segments):
50
                candidate = info
51
    return candidate