Esempio n. 1
0
def cifs_umount(mount):
    def _cb(success, output, exit_code):
        if not success:
            mount.ui.notify_user("Umount failure :<br>{}".format(output))

    cmd = [OSX_CONST.CMD_UMOUNT, "\"{local_path}\"".format(**mount.settings)]
    Output.verbose("cmd: " + " ".join(cmd))
    NonBlockingQtProcess(
        cmd,
        _cb,
    )
Esempio n. 2
0
def cifs_is_mounted(mount, cb):
    def _cb(success, output, exit_code):
        lines = output.split("\n")
        items = re.findall(r"(\S+)", lines[0])
        caption = "Caption"
        after_caption = items[items.index("Caption") + 1]
        providername = "ProviderName"
        after_providername = items[items.index("ProviderName") + 1]
        caption_start = lines[0].index(caption)
        caption_end = lines[0].index(after_caption)
        providername_start = lines[0].index(providername)
        providername_end = lines[0].index(after_providername)

        # Look for 'server' and for 'server.domain.name'
        server_names = [
            mount.settings["server_name"],
            re.findall(r'[^.]+', mount.settings["server_name"])[0],
        ]
        for server_name in server_names:
            i_search = r"^\\{server_name}\{server_path}".format(
                server_name=server_name,
                server_path=mount.settings['server_path'],
            )
            i_search = i_search.replace("\\", "\\\\")
            i_search = i_search.replace("$", r"\$")
            i_search += "$"
            # Output.debug("i_search='{0}'".format(i_search))
            for l in lines[1:]:
                try:
                    drive_letter = l[caption_start:caption_end].strip()
                    provider = l[providername_start:providername_end].strip()
                    # Output.debug("{} <- {}".format(drive_letter, provider))
                    if re.search(i_search, provider, flags=re.IGNORECASE):
                        mount.settings["Windows_letter"] = drive_letter
                        # Output.debug("MATCH!")
                        cb(True)
                        return
                except IndexError:
                    pass
        cb(False)

    cmd = ["wmic", "logicaldisk"]  # List all Logical Disks
    NonBlockingQtProcess(cmd, _cb, cache=True)
Esempio n. 3
0
def open_file_manager(mount):
    def _cb(success, output, exit_code):
        pass

    if (mount.settings["Linux_CIFS_method"] == "gvfs"
            and not mount.settings["Linux_gvfs_symlink"]):
        path = None
        for f in os.listdir(LIN_CONST.GVFS_DIR):
            if LIN_CONST.GVFS_GENERATION == 1:
                if re.match(
                        r"{server_share} \S+ {server_name}".format(
                            **mount.settings), f):
                    path = os.path.join(LIN_CONST.GVFS_DIR, f,
                                        mount.settings["server_subdir"])
            else:
                if (re.match(r"^smb-share:", f) and re.search(
                        r"domain={realm_domain}(,|$)".format(**mount.settings),
                        f,
                        flags=re.IGNORECASE) and re.search(
                            r"server={server_name}(,|$)".format(
                                **mount.settings), f) and re.search(
                                    r"share={server_share}(,|$)".format(
                                        **mount.settings), f)
                        and re.search(
                            r"user={realm_username}(,|$)".format(
                                **mount.settings), f)):
                    path = os.path.join(LIN_CONST.GVFS_DIR, f,
                                        mount.settings["server_subdir"])
        if path is None:
            raise Exception("Error: Could not find the GVFS mountpoint.")
    else:
        path = mount.settings["local_path"]
    if LIN_CONST.CMD_OPEN is None:
        Output.warning(
            "Cannot open location '{}', xdg-open not found.".format(path))
        return
    cmd = [s.format(path=path) for s in LIN_CONST.CMD_OPEN.split(" ")]
    Output.verbose("cmd: " + " ".join(cmd))
    NonBlockingQtProcess(
        cmd,
        _cb,
    )
Esempio n. 4
0
def cifs_is_mounted(mount, cb=None):
    """
        evaluate if this mount is mounted
        if cb is None make it synchronously (CLI)
        else make it asynchronously (GUI)
    """
    def _cb_gvfs(success, output, exit_code):
        # Output.debug("lin_stack._cb_gvfs")
        # Output.debug("-> gvfs-mount -l : \n{0}\n\n".format(output))
        share_format1 = re.sub(r"\$", r"\\$", mount.settings["server_share"])
        share_format2 = re.sub(r" ", r"%20", mount.settings["server_share"])
        share_format2 = re.sub(r"\$", r"\\$", share_format2)
        i_search = r"{share_format1} .+ {server_name} -> smb://{realm_domain};{realm_username}@{server_name}/{share_format2}".format(
            share_format1=share_format1,
            share_format2=share_format2,
            **mount.settings)
        for l in output.split("\n"):
            if re.search(i_search, l, flags=re.IGNORECASE):
                # Output.debug(l)
                if cb is None:
                    return True
                else:
                    cb(True)
                    return
        if cb is None:
            return False
        else:
            cb(False)

    def _target_mountcifs():
        return os.path.ismount(mount.settings["local_path"])

    # Output.debug("lin_stack.cifs_is_mounted")
    if mount.settings["Linux_CIFS_method"] == "gvfs":
        if LIN_CONST.GVFS_GENERATION <= 3:
            if LIN_CONST.CMD_GVFS_MOUNT is None:
                Output.error("'gvfs-mount' not installed.")
                return _cb_gvfs(False, "'gvfs-mount' not installed.", 1)
            cmd = [LIN_CONST.CMD_GVFS_MOUNT, "-l"]
        else:
            if LIN_CONST.CMD_GIO_MOUNT is None:
                Output.error("'gio' not installed.")
                return _cb_gvfs(False, "'gio' not installed.", 1)
            cmd = [LIN_CONST.CMD_GIO_MOUNT, "mount", "-l"]
        # Output.debug("cmd: " + " ".join(cmd))
        if cb is None:
            return _cb_gvfs(**BlockingProcess.run(
                cmd,
                env=dict(os.environ, LANG="C", LC_ALL="C", LANGUAGE="C"),
                cache=True,
            ))
        else:
            NonBlockingQtProcess(
                cmd,
                _cb_gvfs,
                env=dict(os.environ, LANG="C", LC_ALL="C", LANGUAGE="C"),
                cache=True,
            )
    else:  # "mount.cifs"
        if cb is None:
            return _target_mountcifs()
        else:
            NonBlockingQtThread(
                "os.path.ismounted.{}".format(mount.settings["local_path"]),
                _target_mountcifs, cb)
Esempio n. 5
0
def cifs_umount(mount):
    def _cb_gvfs(success, output, exit_code):
        if not success:
            mount.ui.notify_user("Umount failure :<br>{}".format(output))
        cifs_uncache_is_mounted(mount)

    if mount.settings["Linux_CIFS_method"] == "gvfs":
        # gvfs umount apparently never locks on open files.

        # 1) Umount
        share = re.sub(r" ", r"%20", mount.settings["server_share"])
        if LIN_CONST.GVFS_GENERATION <= 3:
            cmd = [
                LIN_CONST.CMD_GVFS_MOUNT, "-u",
                r"smb://{realm_domain};{realm_username}@{server_name}/{share}".
                format(share=share, **mount.settings)
            ]
        else:
            cmd = [
                LIN_CONST.CMD_GIO_MOUNT, "mount", "-u",
                r"smb://{realm_domain};{realm_username}@{server_name}/{share}".
                format(share=share, **mount.settings)
            ]
        Output.verbose("cmd: " + " ".join(cmd))

        if mount.ui.UI_TYPE == "GUI":
            NonBlockingQtProcess(
                cmd,
                _cb_gvfs,
                env=dict(os.environ, LANG="C", LC_ALL="C", LANGUAGE="C"),
            )
        else:
            _cb_gvfs(**BlockingProcess.run(
                cmd,
                env=dict(os.environ, LANG="C", LC_ALL="C", LANGUAGE="C"),
            ))

    else:  # "mount.cifs"
        # 1) uMount
        cmd = [LIN_CONST.CMD_UMOUNT, "{local_path}"]
        if CONST.LOCAL_UID != 0:
            cmd.insert(0, "sudo")
        cmd = [s.format(**mount.settings) for s in cmd]
        Output.verbose("cmd: " + " ".join(cmd))
        # for i in xrange(3): # 3 attempts (for passwords mistyped)
        process_meta = {
            "was_cancelled": False,
        }
        (output, exit_status) = pexpect.runu(
            command=" ".join(cmd),
            events={
                "(?i)password": pexpect_ask_password,
            },
            extra_args={
                "auth_realms": [
                    (r"\[sudo\] password", "sudo"),
                ],
                "key_chain": mount.key_chain,
                "process_meta": process_meta,
            },
            env=dict(os.environ, LANG="C", LC_ALL="C"),
            withexitstatus=True,
            timeout=CONST.UMOUNT_TIMEOUT,
        )
        if exit_status == 0:
            mount.key_chain.ack_password("sudo")
        else:
            if process_meta["was_cancelled"]:
                mount.key_chain.invalidate_if_no_ack_password("sudo")
            elif "device is busy" in output:
                mount.key_chain.ack_password("sudo")
                mount.ui.notify_user("Umount failure: Device is busy.")
                return False
            else:
                mount.key_chain.invalidate_if_no_ack_password("sudo")
                mount.ui.notify_user("Umount failure")
                return False
Esempio n. 6
0
def cifs_uncache_is_mounted(mount):
    NonBlockingQtProcess.invalidate_cmd_cache(["wmic", "logicaldisk"])