jrgp/linfo

OS\Linux wrong distro name

kmuharam opened this issue · 3 comments

Hello,

Running Fedora 30 (Thirty) Workstation, I was trying to call getDistro(), I got:

array:2 [▼
  "name" => "CentOS"
  "version" => "Fedora release 30 (Thirty)"
]

The problem that Fedora has redhat-release file in /etc and looking at src/Linfo/OS/Linux.php line 1406 is considered the first match and at line 1471 gets returned as the first correct match.

The last condition is always true since all $contents_distros have distro index (except ones with closure).

            } elseif (isset($distro['distro'])) {
                return array(
                    'name' => $distro['distro'],
                    'version' => $contents,
                );
            }

Edit:

Adding isset($distro['distro']) && (strpos($distro['distro'], $contents) !== false) to the last condition seems to solve the issue for me.

        foreach ($contents_distros as $distro) {
            if (!($contents = Common::getContents($distro['file'], false))) {
                continue;
            }

            if (isset($distro['closure']) && ($info = $distro['closure']($contents))) {
                return array(
                    'name' => ucfirst($info['distro']),
                    'version' => $info['version'].(isset($info['codename']) ? ' ('.ucfirst($info['codename']).')' : ''),
                );
            } elseif (isset($distro['regex']) && preg_match($distro['regex'], $contents, $info)) {
                return array(
                    'name' => $distro['distro'],
                    'version' => $info['version'].(isset($info['codename']) ? ' ('.ucfirst($info['codename']).')' : ''),
                );
            } elseif (isset($distro['distro']) && (strpos($distro['distro'], $contents) !== false)) {
                return array(
                    'name' => $distro['distro'],
                    'version' => $contents,
                );
            }
        }
jrgp commented

Hmm we really should have a unit test for all of these scenarios.

jrgp commented

Hopefully c8537c7 should fix it. Let me know if it doesn't.

jrgp commented

This should be definitely fixed per 22fd93e