• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "fs_error.h"
17 #include "uv.h"
18 
19 namespace OHOS::FileManagement::ModuleFileIO {
20 using namespace std;
21 
ConvertUVCode2ErrCode(int errCode)22 static int ConvertUVCode2ErrCode(int errCode)
23 {
24     if (errCode >= 0) {
25         return errCode;
26     }
27     auto uvCode = string_view(uv_err_name(errCode));
28     if (uvCode2ErrCodeTable.find(uvCode) != uvCode2ErrCodeTable.end()) {
29         return uvCode2ErrCodeTable.at(uvCode);
30     }
31     return UNKNOWN_ERR;
32 }
33 
FsError(int errCode)34 FsError::FsError(int errCode)
35 {
36     int genericCode = ConvertUVCode2ErrCode(errCode);
37     auto it = errCodeTable.find(genericCode);
38     if (it != errCodeTable.end()) {
39         errno_ = it->second.first;
40         errMsg_ = it->second.second;
41     } else {
42         errno_ = errCodeTable.at(UNKNOWN_ERR).first;
43         errMsg_ = errCodeTable.at(UNKNOWN_ERR).second + ", errno is " + to_string(abs(errCode));
44     }
45 }
46 
GetErrNo() const47 int FsError::GetErrNo() const
48 {
49     return errno_;
50 }
51 
GetErrMsg() const52 const std::string &FsError::GetErrMsg() const
53 {
54     return errMsg_;
55 }
56 
operator bool() const57 FsError::operator bool() const
58 {
59     return errno_ != ERRNO_NOERR;
60 }
61 
62 } // namespace OHOS::FileManagement::ModuleFileIO
63