# Buggy code
def file_hash(load, fnd):
    """
    Return a file hash, the hash type is set in the master config file
    """
    if "path" not in load or "env" not in load:
        return ""
    path = fnd["path"]
    ret = {}

    # if the file doesn't exist, we can't get a hash
    if not path or not os.path.isfile(path):
        return ret

    # set the hash_type as it is determined by config-- so mechanism won't change that
    ret["hash_type"] = __opts__["hash_type"]

    # check if the hash is cached
    # cache file's contents should be "hash:mtime"
    cache_path = os.path.join(
        __opts__["cachedir"],
        "roots/hash",
        load["env"],
        "{0}.hash.{1}".format(fnd["rel"], __opts__["hash_type"]),
    )
    # if we have a cache, serve that if the mtime hasn't changed
    if os.path.exists(cache_path):
        with salt.utils.fopen(cache_path, "rb") as fp_:
            try:
                hsum, mtime = fp_.read().split(":")
                if os.path.getmtime(path) == mtime:
                    # check if mtime changed
                    ret["hsum"] = hsum
                    return ret
            except IOError:  # Can't use Python select() because we need Windows support
                file_hash(load, fnd)

    # if we don't have a cache entry-- lets make one
    ret["hsum"] = salt.utils.get_hash(path, __opts__["hash_type"])
    cache_dir = os.path.dirname(cache_path)
    # make cache directory if it doesn't exist
    if not os.path.exists(cache_dir):
        os.makedirs(cache_dir)
    # save the cache object "hash:mtime"
    if HAS_FCNTL:
        with salt.utils.flopen(cache_path, "w") as fp_:
            fp_.write("{0}:{1}".format(ret["hsum"], os.path.getmtime(path)))
            fcntl.flock(fp_.fileno(), fcntl.LOCK_UN)
        return ret
    else:
        with salt.utils.fopen(cache_path, "w") as fp_:
            fp_.write("{0}:{1}".format(ret["hsum"], os.path.getmtime(path)))
        return ret


# Traceback (012ValueError)
[ERROR   ] Error in function _file_hash:
#012Traceback (most recent call last):
#012  File "/usr/lib/python2.6/site-packages/salt/master.py", line 1444, in run_func
#012    ret = getattr(self, func)(load)
#012  File "/usr/lib/python2.6/site-packages/salt/fileserver/__init__.py", line 233, in file_hash
#012    return self.servers[fstr](load, fnd)
#012  File "/usr/lib/python2.6/site-packages/salt/fileserver/roots.py", line 153, in file_hash
#012    hsum, mtime = fp_.read().split(':')
#012ValueError: need more than 1 value to unpack

# Fixed code
def file_hash(load, fnd):
    """
    Return a file hash, the hash type is set in the master config file
    """
    if "path" not in load or "env" not in load:
        return ""
    path = fnd["path"]
    ret = {}

    # if the file doesn't exist, we can't get a hash
    if not path or not os.path.isfile(path):
        return ret

    # set the hash_type as it is determined by config-- so mechanism won't change that
    ret["hash_type"] = __opts__["hash_type"]

    # check if the hash is cached
    # cache file's contents should be "hash:mtime"
    cache_path = os.path.join(
        __opts__["cachedir"],
        "roots/hash",
        load["env"],
        "{0}.hash.{1}".format(fnd["rel"], __opts__["hash_type"]),
    )
    # if we have a cache, serve that if the mtime hasn't changed
    if os.path.exists(cache_path):
        try:
            with salt.utils.fopen(cache_path, "rb") as fp_:
                try:
                    hsum, mtime = fp_.read().split(":")
                except ValueError:
                    log.debug(
                        "Fileserver attempted to read incomplete cache file. Retrying."
                    )
                    file_hash(load, fnd)
                    return ret
                if os.path.getmtime(path) == mtime:
                    # check if mtime changed
                    ret["hsum"] = hsum
                    return ret
        except os.error:  # Can't use Python select() because we need Windows support
            log.debug("Fileserver encountered lock when reading cache file. Retrying.")
            file_hash(load, fnd)
            return ret

    # if we don't have a cache entry-- lets make one
    ret["hsum"] = salt.utils.get_hash(path, __opts__["hash_type"])
    cache_dir = os.path.dirname(cache_path)
    # make cache directory if it doesn't exist
    if not os.path.exists(cache_dir):
        os.makedirs(cache_dir)
    # save the cache object "hash:mtime"
    if HAS_FCNTL:
        with salt.utils.flopen(cache_path, "w") as fp_:
            fp_.write("{0}:{1}".format(ret["hsum"], os.path.getmtime(path)))
            fcntl.flock(fp_.fileno(), fcntl.LOCK_UN)
        return ret
    else:
        with salt.utils.fopen(cache_path, "w") as fp_:
            fp_.write("{0}:{1}".format(ret["hsum"], os.path.getmtime(path)))
        return ret