diff --git a/client/packageutils.c b/client/packageutils.c index 7c195eea..5bed7f4e 100644 --- a/client/packageutils.c +++ b/client/packageutils.c @@ -1000,7 +1000,8 @@ TDNFGetAvailableCacheBytes( ) { uint32_t dwError = 0; - struct statfs tmpStatfsBuffer = {0}; + struct statfs stfs = {0}; + struct stat st = {0}; if(!pConf || !pConf->pszCacheDir || !pqwAvailCacheDirBytes) { @@ -1008,13 +1009,19 @@ TDNFGetAvailableCacheBytes( 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; diff --git a/pytests/tests/test_cache.py b/pytests/tests/test_cache.py index 38701594..2621b590 100644 --- a/pytests/tests/test_cache.py +++ b/pytests/tests/test_cache.py @@ -9,6 +9,7 @@ import os import fnmatch import pytest +import shutil @pytest.fixture(scope='module', autouse=True) @@ -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