Skip to content

Commit e851f3a

Browse files
committed
Packages (Linux): optimises pacman detection code
1 parent 3a865df commit e851f3a

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

src/detection/packages/packages_linux.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -414,29 +414,41 @@ static uint32_t getFlatpakPackages(FFstrbuf* baseDir, const char* dirname)
414414

415415
static uint32_t getPacmanPackages(FFstrbuf* baseDir)
416416
{
417-
FF_STRBUF_AUTO_DESTROY pacmanDir = ffStrbufCreate();
417+
FF_STRBUF_AUTO_DESTROY dbPath = ffStrbufCreate();
418+
FF_STRBUF_AUTO_DESTROY rootDir = ffStrbufCreate();
419+
418420
// Get path to pacman.conf
419421
uint32_t baseDirLen = baseDir->length;
420422
ffStrbufAppendS(baseDir, "/etc/pacman.conf");
421423

422-
// Get DBPath for packages
423-
ffParsePropFile(baseDir->chars, "DBPath =", &pacmanDir);
424-
// If DBPath is specified
425-
if (pacmanDir.length == 0) {
426-
ffParsePropFile(baseDir->chars, "RootDir =", &pacmanDir);
427-
// If DBpath is not specified and RootDir is specified
428-
if (pacmanDir.length ==0)
429-
// If both are not specified
430-
ffStrbufClear(&pacmanDir);
431-
ffStrbufTrimRight(&pacmanDir, '/');
432-
ffStrbufAppendS(&pacmanDir, "/var/lib/pacman");
433-
}
424+
bool confFound = ffParsePropFileValues(baseDir->chars, 2, (FFpropquery[]){
425+
{ "DBPath =", &dbPath },
426+
{ "RootDir =", &rootDir },
427+
});
434428
ffStrbufSubstrBefore(baseDir, baseDirLen);
435429

436-
ffStrbufTrimRight(&pacmanDir, '/');
437-
ffStrbufAppendS(&pacmanDir, "/local");
430+
if (confFound)
431+
{
432+
if (dbPath.length > 0)
433+
{
434+
// If DBPath is specified, use it
435+
ffStrbufEnsureEndsWithC(&dbPath, '/');
436+
ffStrbufAppendS(&dbPath, "local");
437+
}
438+
else if (rootDir.length > 0)
439+
{
440+
// ... otherwise, use RootDir
441+
ffStrbufDestroy(&dbPath);
442+
ffStrbufInitMove(&dbPath, &rootDir);
443+
ffStrbufEnsureEndsWithC(&dbPath, '/');
444+
ffStrbufAppendS(&dbPath, "var/lib/pacman/local");
445+
}
446+
}
447+
448+
if (dbPath.length == 0)
449+
ffStrbufSetStatic(&dbPath, "/var/lib/pacman/local");
438450

439-
return getNumElements(baseDir, pacmanDir.chars, true);
451+
return getNumElements(baseDir, dbPath.chars, true);
440452
}
441453

442454
static void getPackageCounts(FFstrbuf* baseDir, FFPackagesResult* packageCounts, FFPackagesOptions* options)

0 commit comments

Comments
 (0)