Skip to content

Commit

Permalink
Merge pull request #454 from oliverkurth/topic/okurth/avoid-fail-if-c…
Browse files Browse the repository at this point in the history
…achedir-doesnt-exist

avoid failure when checking free space if cache directory doesn't exist
  • Loading branch information
oliverkurth authored Dec 1, 2023
2 parents 13751a8 + 9d470e2 commit 680496a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
13 changes: 10 additions & 3 deletions client/packageutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1000,21 +1000,28 @@ TDNFGetAvailableCacheBytes(
)
{
uint32_t dwError = 0;
struct statfs tmpStatfsBuffer = {0};
struct statfs stfs = {0};
struct stat st = {0};

if(!pConf || !pConf->pszCacheDir || !pqwAvailCacheDirBytes)
{
dwError = ERROR_TDNF_INVALID_PARAMETER;
BAIL_ON_TDNF_ERROR(dwError);
}

if (statfs(pConf->pszCacheDir, &tmpStatfsBuffer) != 0)
if (stat(pConf->pszCacheDir, &st) != 0) {
/* avoid failure when checking space, and dir doesn't exist */
dwError = TDNFUtilsMakeDirs(pConf->pszCacheDir);
BAIL_ON_TDNF_ERROR(dwError);
}

if (statfs(pConf->pszCacheDir, &stfs) != 0)
{
dwError = errno;
BAIL_ON_TDNF_SYSTEM_ERROR(dwError);
}

*pqwAvailCacheDirBytes = tmpStatfsBuffer.f_bsize * tmpStatfsBuffer.f_bavail;
*pqwAvailCacheDirBytes = stfs.f_bsize * stfs.f_bavail;

cleanup:
return dwError;
Expand Down
13 changes: 13 additions & 0 deletions pytests/tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import fnmatch
import pytest
import shutil


@pytest.fixture(scope='module', autouse=True)
Expand Down Expand Up @@ -203,3 +204,15 @@ def test_cache_directory_out_of_disk_space(utils):
clean_cache(utils)
clean_small_cache(utils)
assert ret['retval'] == 1036


# see https://github.com/vmware/tdnf/pull/454
# tdnf should not fail if cache dir does not exist
def test_cachedir_removed(utils):
pkgname = utils.config["sglversion_pkgname"]
utils.install_package(pkgname)

cache_dir = utils.tdnf_config.get('main', 'cachedir')
shutil.rmtree(cache_dir)
ret = utils.run(["tdnf", "-y", "--disablerepo=*", "remove", pkgname])
assert ret['retval'] == 0

0 comments on commit 680496a

Please sign in to comment.