• 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 "watcher_core.h"
17 
18 #include <cstring>
19 #include <fcntl.h>
20 #include <tuple>
21 
22 #include "file_utils.h"
23 #include "filemgmt_libhilog.h"
24 #include "fs_file_watcher.h"
25 
26 namespace OHOS::FileManagement::ModuleFileIO {
27 using namespace std;
28 
InstantiateWatcher()29 static FsResult<FsWatcher *> InstantiateWatcher()
30 {
31     if (FsFileWatcher::GetInstance().GetNotifyId() < 0 && !FsFileWatcher::GetInstance().InitNotify()) {
32         HILOGE("Failed to get notifyId or initnotify fail");
33         return FsResult<FsWatcher *>::Error(errno);
34     }
35 
36     FsResult<FsWatcher *> result = FsWatcher::Constructor();
37     if (!result.IsSuccess()) {
38         HILOGE("Failed to instantiate watcher");
39         return FsResult<FsWatcher *>::Error(EIO);
40     }
41     return result;
42 }
43 
ToWatcherInfo(const string & path,const int32_t events,shared_ptr<IWatcherCallback> callback,int32_t & errCode)44 shared_ptr<WatcherInfo> ToWatcherInfo(
45     const string &path, const int32_t events, shared_ptr<IWatcherCallback> callback, int32_t &errCode)
46 {
47     if (events <= 0 || !FsFileWatcher::GetInstance().CheckEventValid(events)) {
48         HILOGE("Failed to get watcher event.");
49         errCode = EINVAL;
50         return nullptr;
51     }
52 
53     if (!callback) {
54         HILOGE("Failed to get callback");
55         errCode = EINVAL;
56         return nullptr;
57     }
58 
59     auto info = CreateSharedPtr<WatcherInfo>(callback);
60     if (info == nullptr) {
61         HILOGE("Failed to request heap memory.");
62         errCode = ENOMEM;
63         return nullptr;
64     }
65 
66     info->events = static_cast<uint32_t>(events);
67     info->fileName = move(path);
68     return info;
69 }
70 
DoCreateWatcher(const string & path,const int32_t events,shared_ptr<IWatcherCallback> callback)71 FsResult<FsWatcher *> WatcherCore::DoCreateWatcher(
72     const string &path, const int32_t events, shared_ptr<IWatcherCallback> callback)
73 {
74     int errCode = 0;
75     auto info = ToWatcherInfo(path, events, callback, errCode);
76     if (errCode != 0) {
77         HILOGE("Failed to parse param");
78         return FsResult<FsWatcher *>::Error(errCode);
79     }
80 
81     auto result = InstantiateWatcher();
82     if (!result.IsSuccess()) {
83         return result;
84     }
85 
86     const FsWatcher *objWatcher = result.GetData().value();
87     if (!objWatcher) {
88         HILOGE("Failed to get fsWatcher");
89         return FsResult<FsWatcher *>::Error(EIO);
90     }
91 
92     auto *watchEntity = objWatcher->GetWatchEntity();
93     if (!watchEntity) {
94         HILOGE("Failed to get watchEntity.");
95         delete objWatcher;
96         objWatcher = nullptr;
97         return FsResult<FsWatcher *>::Error(EIO);
98     }
99 
100     watchEntity->data_ = info;
101 
102     bool ret = FsFileWatcher::GetInstance().AddWatcherInfo(info);
103     if (!ret) {
104         HILOGE("Failed to add watcher info.");
105         return FsResult<FsWatcher *>::Error(EINVAL);
106     }
107     return result;
108 }
109 } // namespace OHOS::FileManagement::ModuleFileIO
110