ACC SHELL

Path : /usr/share/YaST2/modules/
File Upload :
Current File : //usr/share/YaST2/modules/CheckMedia.ycp

/**
 * File:
 *   modules/CheckMedia.ycp
 *
 * Summary:
 *   Module for checking media intergrity
 *
 * Authors:
 *   Ladislav Slezak <lslezak@suse.cz>
 *
 * $Id: CheckMedia.ycp 44518 2008-02-15 13:36:34Z lslezak $
 *
 * Input and output routines.
 *
 */

{
    // Set the name of the module
    module "CheckMedia";
    import "Linuxrc";
    import "Report";

    textdomain "packager";

    global const string checkmedia = "/usr/bin/checkmedia";

    list<string> output = [];
    integer progress = 0;
    boolean inprogress = false;

    global string preferred_drive = "";

    // true if module start was forced - check=1 was found in the application area,
    // effects UI a little
    global boolean forced_start = false;

    // cache .probe.cdrom value
    // avoid reprobind during installation
    list<map> cd_cache = nil;

    integer process = nil;

    global define list<map> DetectedCDDevices()
    {
	if (cd_cache == nil)
	{
	    // the cache is not initialied, do it now
	    list<map> cds = (list<map>)SCR::Read(.probe.cdrom);

	    if (cds == nil)
	    {
		// initialize to empty list
		cds = [];
	    }

	    cd_cache = cds;
	}

	return cd_cache;
    }

    global define boolean Start(string device) {
	// reset values
	output = [];
	progress = 0;
	inprogress = false;

	process = (integer)SCR::Execute(.process.start_shell, checkmedia + " " + device);
	return true;
    }

    global define boolean Stop() {
	boolean ret = (boolean)SCR::Execute(.process.kill, process);

	// wait for the process
	SCR::Execute(.process.close, process);

	// release the process from the agent
	SCR::Execute(.process.release, process);
	process = nil;

	return ret;
    }

    global define void Process() {
	if (process == nil)
	{
	    return;
	}

	if (inprogress)
	{
	    // try to read whole lines
	    string out = (string)SCR::Read(.process.read_line, process);

	    if (out != nil)
	    {
		output = add(output, out);

		out = (string)SCR::Read(.process.read, process);

		if (out != nil)
		{
		    output = (list<string>)merge(output, splitstring(out, "\n"));
		}

		// finished
		progress = 100;
		inprogress = false;
	    }
	    else
	    {
		// read progress status
		string buffer = (string)SCR::Read(.process.read, process);

		if (buffer != nil)
		{
		    y2debug("buffer: %1", buffer);

		    string percent = regexpsub(buffer, "([0-9]*)%.*$", "\\1");

		    if (percent != nil)
		    {
			progress = tointeger(percent);
			y2milestone("progress: %1%%", progress);
		    }
		}
	    }
	}
	else
	{
	    string out = (string)SCR::Read(.process.read_line, process);

	    if (out != nil)
	    {
		output = add(output, out);

		// check whether we need to switch to progress mode
		if (regexpmatch(out, "^ *pad: "))
		{
		    inprogress = true;
		    y2milestone("Switching into progress mode");
		}
	    }
	}

	return;
    }

    global define boolean Running() {
	boolean ret = (boolean)SCR::Read(.process.running, process);

	return ret;
    }

    /**
     * Return information printed by checkmedia utility
     * @ret list<string> checkmedia output
     */
    global define list<string> Info() {
	list<string> ret = output;
	output = [];
	return ret;
    }

    global define integer Progress() {
	return progress;
    }

    /* Return list of ready CD devices for installation. It works properly only
     * in the first installation stage - it reads content of /etc/install.inf
     * file. It returns the installation (boot) CD device if it's known or it
     * probes for all CD devices and returns ready devices (the devices which
     * contain a medium). If repository is not CD/DVD it returns
     * empty list.
     *
     * @return list<string> List of CD/DVD device names
     */
    global define list<string> GetReadyCDs() {
	// check whether we are using CD repository
	string instmode = Linuxrc::InstallInf ("InstMode");
	y2milestone("Installation mode: %1", instmode);

	list<string> readycddrives = [];

	if (instmode == "cd" || instmode == "dvd")
	{
	    // get CD device name
	    string bootcd = Linuxrc::InstallInf ("Cdrom");

	    if (bootcd != nil && bootcd != "")
	    {
		readycddrives = [ sformat("/dev/%1", bootcd) ];
	    }
	    else
	    {
		y2milestone("CD device device is not known, probing...");
		// booted from another location (network), test all CD drives
		list<map> cds = DetectedCDDevices();

		if (cds != nil)
		{
		    foreach(map cd, cds, {
			    string devname = cd["dev_name"]:"";

			    // check whether the CD is ready
			    if (cd["notready"]:false == false && devname != nil && devname != "")
			    {
				readycddrives = add(readycddrives, devname);
			    }
			}
		    );
		}
	    }

	    y2milestone("Ready CD drives: %1", readycddrives);
	}

	return readycddrives;
    }

    /**
     * Release resources used by the subprocess
     */
    global void Release()
    {
	if (process != nil)
	{
	    SCR::Execute(.process.release, process);
	    process = nil;
	}
    }
}

ACC SHELL 2018