o c@snddlmZddlZddlZddlZddlmZegdZe dZ efddZ dd Z d d Z d d ZdS))divisionN)_PY3)extext2ext3ext4reiserfsntfsmsdosdosvfatxfshpfsjfsufshfshfsplussimfsdrvfslxfsz([a-z]+)[0-9]*c cst|D]]}z|dd\}}}trt|d}nt|d}Wn ty,Yqw|dur6||vr6qd}z||}Wn tyGYqw|j} |j| |} |j | |} |||| | dVqdS)aZ This is a generator that yields information about mounted filesystems. @param mounts_file: A file with information about mounted filesystems, such as C{/proc/mounts}. @param statvfs_: A function to get file status information. @param filesystems_whitelist: Optionally, a list of which filesystems to stat. @return: A C{dict} with C{device}, C{mount-point}, C{filesystem}, C{total-space} and C{free-space} keys. If the filesystem information is not available, C{None} is returned. Both C{total-space} and C{free-space} are in megabytes. Nunicode_escape string_escapei)device mount-point filesystemz total-spacez free-space) opensplitrcodecsdecode ValueErrorOSErrorf_bsizef_blocksf_bfree) mounts_filestatvfs_filesystems_whitelistliner mount_pointr megabytesstats block_size total_space free_spacer/4/usr/lib/python3/dist-packages/landscape/lib/disk.pyget_mount_infos8      r1cCsfd}tj|}|d}t||D]}|dd}||dr0|r.|dt||kr0|}q|S)a Tries to determine to which of the mounted filesystem C{path} belongs to, and then returns information about that filesystem or C{None} if it couldn't be determined. @param path: The path we want filesystem information about. @param mounts_file: A file with information about mounted filesystems, such as C{/proc/mounts}. @param statvfs_: A function to get file status information. @return: A C{dict} with C{device}, C{mount-point}, C{filesystem}, C{total-space} and C{free-space} keys. If the filesystem information is not available, C{None} is returned. Both C{total-space} and C{free-space} are in megabytes. N/r)ospathrealpathrr1 startswithlen)r4r%r& candidate path_segmentsinfomount_segmentsr/r/r0get_filesystem_for_path<s  r<cCstj|dr dSt|}|sdSd}zt|d }|}Wdn1s*wYWn ty:YdSw|dkrCdSdS)z This function returns whether a given device is removable or not by looking at the corresponding /sys/block//removable file @param device: The filesystem path to the device, e.g. /dev/sda1 mmcblkTFNr1) r3r4basenamer6_get_device_removable_file_pathrreadlineIOErrorstrip)rr4contentsfr/r/r0is_device_removableXs"     rGcCs\tj|r t|}|ddd\}t|}|sdS|d}tjd|d}|S)a5 Get a device's "removable" file path. This function figures out the C{/sys/block//removable} path associated with the given device. The file at that path contains either a "0" if the device is not removable, or a "1" if it is. @param device: File system path of the device. r2Nrz /sys/block/ removable) r3r4islinkreadlinkrEXTRACT_DEVICEmatchgroupsjoin)r device_namematchedremovable_filer/r/r0rAus   rA) __future__rr3rerlandscape.lib.compatr frozensetSTABLE_FILESYSTEMScompilerLr1r<rGrAr/r/r/r0s    )