add IsExistFile return code

Signed-off-by: xuqian <xuqian65@huawei.com>
This commit is contained in:
xuqian 2024-11-09 21:25:08 +08:00
parent 32b243a63f
commit 96d908855e
4 changed files with 37 additions and 7 deletions

View File

@ -41,6 +41,7 @@ public:
bool IsJsonFormat(const std::string &path);
bool IsJsonFileReady(const std::string &path);
bool IsExistDir(const std::string &path);
ErrCode CheckFileExistence(const std::string &path);
bool GetValidDeleteFileOperationFlag(const std::string &fileName);
void SetValidDeleteFileOperationFlag(const std::string &fileName, bool flag);
bool GetValidModifyFileOperationFlag(const std::string &fileName);

View File

@ -329,6 +329,35 @@ bool AccountFileOperator::IsExistFile(const std::string &path)
return false;
}
ErrCode AccountFileOperator::CheckFileExistence(const std::string &path)
{
if (path.empty()) {
ACCOUNT_LOGE("Path is empty.");
return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
}
std::shared_lock<std::shared_timed_mutex> lock(fileLock_);
uint32_t retryCount = 0;
while (retryCount < RETRY_TIMES) {
struct stat buf = {};
if (stat(path.c_str(), &buf) == 0) {
if (S_ISREG(buf.st_mode)) {
return ERR_OK;
}
ACCOUNT_LOGE("S_ISREG failed, errno=%{public}d.", errno);
return ERR_ACCOUNT_COMMON_FILE_OTHER_ERROR;
}
if (errno != ENOENT) {
ACCOUNT_LOGE("Stat %{public}s failed, errno=%{public}d. Retrying...", path.c_str(), errno);
std::this_thread::sleep_for(std::chrono::milliseconds(RETRY_SLEEP_MS));
retryCount++;
} else {
ACCOUNT_LOGE("Stat %{public}s failed, errno=%{public}d.", path.c_str(), errno);
return ERR_ACCOUNT_COMMON_FILE_NOT_EXIST;
}
}
return ERR_ACCOUNT_COMMON_FILE_OTHER_ERROR;
}
bool AccountFileOperator::IsJsonFormat(const std::string &path)
{
std::string content;

View File

@ -64,6 +64,8 @@ enum {
ERR_ACCOUNT_COMMON_FILE_OPEN_FAILED,
ERR_ACCOUNT_COMMON_FILE_WRITE_FAILED,
ERR_ACCOUNT_COMMON_FILE_READ_FAILED,
ERR_ACCOUNT_COMMON_FILE_NOT_EXIST,
ERR_ACCOUNT_COMMON_FILE_OTHER_ERROR,
ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR,
ERR_ACCOUNT_COMMON_OPERATION_TIMEOUT,
ERR_ACCOUNT_COMMON_NOT_AUTHENTICATED,

View File

@ -525,17 +525,15 @@ ErrCode OsAccountControlFileManager::GetOsAccountInfoById(const int id, OsAccoun
{
std::string path = Constants::USER_INFO_BASE + Constants::PATH_SEPARATOR + std::to_string(id) +
Constants::PATH_SEPARATOR + Constants::USER_INFO_FILE_NAME;
ErrCode err = ERR_ACCOUNT_COMMON_FILE_READ_FAILED;
if (!accountFileOperator_->IsExistFile(path)) {
if (errno == ENOENT) {
err = ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
}
ACCOUNT_LOGE("file %{public}s does not exist err", path.c_str());
ErrCode err = accountFileOperator_->CheckFileExistence(path);
if (err != ERR_OK) {
ACCOUNT_LOGE("file %{public}s does not exist err, errcode=%{public}d", path.c_str(), err);
if (GetOsAccountFromDatabase("", id, osAccountInfo) == ERR_OK) {
InsertOsAccount(osAccountInfo);
return ERR_OK;
}
return err;
return err == ERR_ACCOUNT_COMMON_FILE_NOT_EXIST ?
ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR : ERR_ACCOUNT_COMMON_FILE_READ_FAILED;
}
std::string accountInfoStr;
if (accountFileOperator_->GetFileContentByPath(path, accountInfoStr) != ERR_OK) {