From 041bbff9b074a05885426bd2b66e1092d6811b6b Mon Sep 17 00:00:00 2001 From: Andrew Lindesay Date: Fri, 29 Nov 2019 21:11:01 +1300 Subject: [PATCH] HaikuDepot: Better Logging of Cache File Creation A user has reported having some problems around initial use of HaikuDepot and this seems to be related to setup of the directories etc... at start time. This change should improve the logging so that it is easier to identify the cause. Also some additional locking has been introduced as there might be a problem where two threads are creating the same directory at the same time. Relates to #15493 Change-Id: I4cbfda7c2ce87b8509ceb78788b7995ee3185050 Reviewed-on: https://review.haiku-os.org/c/haiku/+/1980 Reviewed-by: Adrien Destugues --- src/apps/haikudepot/model/Model.cpp | 90 ++++++++++--------- .../server/ServerIconExportUpdateProcess.cpp | 1 + .../server/ServerPkgDataUpdateProcess.cpp | 1 + .../ServerReferenceDataUpdateProcess.cpp | 1 + .../ServerRepositoryDataUpdateProcess.cpp | 1 + 5 files changed, 54 insertions(+), 40 deletions(-) diff --git a/src/apps/haikudepot/model/Model.cpp b/src/apps/haikudepot/model/Model.cpp index 2031168c1e..1e25fef781 100644 --- a/src/apps/haikudepot/model/Model.cpp +++ b/src/apps/haikudepot/model/Model.cpp @@ -850,24 +850,30 @@ Model::SetAuthorization(const BString& nickname, const BString& passwordClear, status_t Model::_LocalDataPath(const BString leaf, BPath& path) const { - BString leafAssembled(leaf); - leafAssembled.ReplaceAll("%languageCode%", - LanguageModel().PreferredLanguage().Code()); + BPath resultPath; + status_t result = B_OK; - BPath repoDataPath; + if (result == B_OK) + result = find_directory(B_USER_CACHE_DIRECTORY, &resultPath); - if (find_directory(B_USER_CACHE_DIRECTORY, &repoDataPath) == B_OK - && repoDataPath.Append("HaikuDepot") == B_OK - && create_directory(repoDataPath.Path(), 0777) == B_OK - && repoDataPath.Append(leafAssembled) == B_OK) { - path.SetTo(repoDataPath.Path()); - return B_OK; + if (result == B_OK) + result = resultPath.Append("HaikuDepot"); + + if (result == B_OK) + result = create_directory(resultPath.Path(), 0777); + + if (result == B_OK) + result = resultPath.Append(leaf); + + if (result == B_OK) + path.SetTo(resultPath.Path()); + else { + path.Unset(); + fprintf(stdout, "unable to find the user cache file for " + "[%s] data; %s\n", leaf.String(), strerror(result)); } - path.Unset(); - fprintf(stdout, "unable to find the user cache file for [%s] data", - leaf.String()); - return B_ERROR; + return result; } @@ -880,7 +886,10 @@ Model::_LocalDataPath(const BString leaf, BPath& path) const status_t Model::DumpExportRepositoryDataPath(BPath& path) const { - return _LocalDataPath("repository-all_%languageCode%.json.gz", path); + BString leaf; + leaf.SetToFormat("repository-all_%s.json.gz", + LanguageModel().PreferredLanguage().Code()); + return _LocalDataPath(leaf, path); } @@ -892,7 +901,10 @@ Model::DumpExportRepositoryDataPath(BPath& path) const status_t Model::DumpExportReferenceDataPath(BPath& path) const { - return _LocalDataPath("reference-all_%languageCode%.json.gz", path); + BString leaf; + leaf.SetToFormat("reference-all_%s.json.gz", + LanguageModel().PreferredLanguage().Code()); + return _LocalDataPath(leaf, path); } @@ -900,18 +912,29 @@ status_t Model::IconStoragePath(BPath& path) const { BPath iconStoragePath; + status_t result = B_OK; - if (find_directory(B_USER_CACHE_DIRECTORY, &iconStoragePath) == B_OK - && iconStoragePath.Append("HaikuDepot") == B_OK - && iconStoragePath.Append("__allicons") == B_OK - && create_directory(iconStoragePath.Path(), 0777) == B_OK) { + if (result == B_OK) + result = find_directory(B_USER_CACHE_DIRECTORY, &iconStoragePath); + + if (result == B_OK) + result = iconStoragePath.Append("HaikuDepot"); + + if (result == B_OK) + result = iconStoragePath.Append("__allicons"); + + if (result == B_OK) + result = create_directory(iconStoragePath.Path(), 0777); + + if (result == B_OK) path.SetTo(iconStoragePath.Path()); - return B_OK; + else { + path.Unset(); + fprintf(stdout, "unable to find the user cache directory for " + "icons; %s\n", strerror(result)); } - path.Unset(); - fprintf(stdout, "unable to find the user cache directory for icons"); - return B_ERROR; + return result; } @@ -919,23 +942,10 @@ status_t Model::DumpExportPkgDataPath(BPath& path, const BString& repositorySourceCode) const { - BPath repoDataPath; - BString leafName; - - leafName.SetToFormat("pkg-all-%s-%s.json.gz", repositorySourceCode.String(), + BString leaf; + leaf.SetToFormat("pkg-all-%s-%s.json.gz", repositorySourceCode.String(), LanguageModel().PreferredLanguage().Code()); - - if (find_directory(B_USER_CACHE_DIRECTORY, &repoDataPath) == B_OK - && repoDataPath.Append("HaikuDepot") == B_OK - && create_directory(repoDataPath.Path(), 0777) == B_OK - && repoDataPath.Append(leafName.String()) == B_OK) { - path.SetTo(repoDataPath.Path()); - return B_OK; - } - - path.Unset(); - fprintf(stdout, "unable to find the user cache file for pkgs' data"); - return B_ERROR; + return _LocalDataPath(leaf, path); } diff --git a/src/apps/haikudepot/server/ServerIconExportUpdateProcess.cpp b/src/apps/haikudepot/server/ServerIconExportUpdateProcess.cpp index 1d1878ade0..a8d14bae05 100644 --- a/src/apps/haikudepot/server/ServerIconExportUpdateProcess.cpp +++ b/src/apps/haikudepot/server/ServerIconExportUpdateProcess.cpp @@ -38,6 +38,7 @@ ServerIconExportUpdateProcess::ServerIconExportUpdateProcess( fModel(model), fCountIconsSet(0) { + AutoLocker locker(fModel->Lock()); if (fModel->IconStoragePath(fLocalIconStoragePath) != B_OK) { printf("[%s] unable to obtain the path for storing icons\n", Name()); fLocalIconStoragePath.Unset(); diff --git a/src/apps/haikudepot/server/ServerPkgDataUpdateProcess.cpp b/src/apps/haikudepot/server/ServerPkgDataUpdateProcess.cpp index 10bdfe2f9b..6e4695a998 100644 --- a/src/apps/haikudepot/server/ServerPkgDataUpdateProcess.cpp +++ b/src/apps/haikudepot/server/ServerPkgDataUpdateProcess.cpp @@ -284,6 +284,7 @@ ServerPkgDataUpdateProcess::GetLocalPath(BPath& path) const BString webAppRepositorySourceCode = _DeriveWebAppRepositorySourceCode(); if (!webAppRepositorySourceCode.IsEmpty()) { + AutoLocker locker(fModel->Lock()); return fModel->DumpExportPkgDataPath(path, webAppRepositorySourceCode); } diff --git a/src/apps/haikudepot/server/ServerReferenceDataUpdateProcess.cpp b/src/apps/haikudepot/server/ServerReferenceDataUpdateProcess.cpp index 97acc8928a..ce0fa0e051 100644 --- a/src/apps/haikudepot/server/ServerReferenceDataUpdateProcess.cpp +++ b/src/apps/haikudepot/server/ServerReferenceDataUpdateProcess.cpp @@ -74,6 +74,7 @@ ServerReferenceDataUpdateProcess::UrlPathComponent() status_t ServerReferenceDataUpdateProcess::GetLocalPath(BPath& path) const { + AutoLocker locker(fModel->Lock()); return fModel->DumpExportReferenceDataPath(path); } diff --git a/src/apps/haikudepot/server/ServerRepositoryDataUpdateProcess.cpp b/src/apps/haikudepot/server/ServerRepositoryDataUpdateProcess.cpp index e7395e9275..673e8a4bd8 100644 --- a/src/apps/haikudepot/server/ServerRepositoryDataUpdateProcess.cpp +++ b/src/apps/haikudepot/server/ServerRepositoryDataUpdateProcess.cpp @@ -189,6 +189,7 @@ ServerRepositoryDataUpdateProcess::UrlPathComponent() status_t ServerRepositoryDataUpdateProcess::GetLocalPath(BPath& path) const { + AutoLocker locker(fModel->Lock()); return fModel->DumpExportRepositoryDataPath(path); }