1""" 2Common functions for managing mounts 3""" 4 5 6import logging 7import os 8 9import salt.utils.files 10import salt.utils.stringutils 11import salt.utils.versions 12import salt.utils.yaml 13 14log = logging.getLogger(__name__) 15 16 17def _read_file(path): 18 """ 19 Reads and returns the contents of a text file 20 """ 21 try: 22 with salt.utils.files.fopen(path, "rb") as contents: 23 return salt.utils.yaml.safe_load(contents) 24 except OSError: 25 return {} 26 27 28def get_cache(opts): 29 """ 30 Return the mount cache file location. 31 """ 32 return os.path.join(opts["cachedir"], "mounts") 33 34 35def read_cache(opts): 36 """ 37 Write the mount cache file. 38 """ 39 cache_file = get_cache(opts) 40 return _read_file(cache_file) 41 42 43def write_cache(cache, opts): 44 """ 45 Write the mount cache file. 46 """ 47 cache_file = get_cache(opts) 48 49 try: 50 _cache = salt.utils.stringutils.to_bytes(salt.utils.yaml.safe_dump(cache)) 51 with salt.utils.files.fopen(cache_file, "wb+") as fp_: 52 fp_.write(_cache) 53 return True 54 except OSError: 55 log.error("Failed to cache mounts", exc_info_on_loglevel=logging.DEBUG) 56 return False 57