• 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 "fuse_assistant.h"
17 
18 #include <dlfcn.h>
19 
20 namespace OHOS {
21 namespace FileManagement {
22 namespace CloudFile {
23 
EnableMock()24 void FuseAssistantMock::EnableMock()
25 {
26     FuseAssistant::mockable = true;
27 }
28 
DisableMock()29 void FuseAssistantMock::DisableMock()
30 {
31     ins = nullptr;
32     FuseAssistant::mockable = false;
33 }
34 
IsMockable()35 bool FuseAssistantMock::IsMockable()
36 {
37     return FuseAssistant::mockable;
38 }
39 
40 } // namespace CloudFile
41 } // namespace FileManagement
42 } // namespace OHOS
43 
44 #ifdef __cplusplus
45 extern "C" {
46 using namespace OHOS::FileManagement::CloudFile;
47 
mkdir(const char * path,mode_t mode)48 int mkdir(const char *path, mode_t mode)
49 {
50     if (FuseAssistantMock::IsMockable()) {
51         return FuseAssistant::ins->mkdir(path, mode);
52     }
53 
54     static int (*realMkdir)() = []() {
55         auto func = (int (*)())dlsym(RTLD_NEXT, "mkdir");
56         if (!func) {
57             GTEST_LOG_(ERROR) << "Failed to resolve real mkdir: " << dlerror();
58         }
59         return func;
60     }();
61 
62     if (!realMkdir) {
63         return -1;
64     }
65 
66     return realMkdir();
67 }
68 
69 } // extern "C"
70 #endif