• 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_watcher.h"
17 
18 #include "file_utils.h"
19 #include "filemgmt_libhilog.h"
20 #include "fs_file_watcher.h"
21 #include "fs_watch_entity.h"
22 #include "securec.h"
23 
24 namespace OHOS::FileManagement::ModuleFileIO {
25 using namespace std;
26 
Constructor()27 FsResult<FsWatcher *> FsWatcher::Constructor()
28 {
29     auto watchEntity = CreateUniquePtr<FsWatchEntity>();
30     if (watchEntity == nullptr) {
31         HILOGE("Failed to request heap memory.");
32         return FsResult<FsWatcher *>::Error(ENOMEM);
33     }
34 
35     FsWatcher *watcherPtr = new FsWatcher(move(watchEntity));
36 
37     if (watcherPtr == nullptr) {
38         HILOGE("Failed to create FsWatcher object on heap.");
39         return FsResult<FsWatcher *>::Error(ENOMEM);
40     }
41 
42     return FsResult<FsWatcher *>::Success(move(watcherPtr));
43 }
44 
Stop()45 FsResult<void> FsWatcher::Stop()
46 {
47     if (!watchEntity) {
48         HILOGE("Failed to get watchEntity when stop.");
49         return FsResult<void>::Error(EINVAL);
50     }
51     int ret = FsFileWatcher::GetInstance().StopNotify(watchEntity->data_);
52     if (ret != ERRNO_NOERR) {
53         HILOGE("Failed to stopNotify errno:%{public}d", errno);
54         return FsResult<void>::Error(ret);
55     }
56     return FsResult<void>::Success();
57 }
58 
Start()59 FsResult<void> FsWatcher::Start()
60 {
61     if (!watchEntity) {
62         HILOGE("Failed to get watchEntity when start.");
63         return FsResult<void>::Error(EINVAL);
64     }
65 
66     shared_ptr<WatcherInfo> info = watchEntity->data_;
67     int ret = FsFileWatcher::GetInstance().StartNotify(info);
68     if (ret != ERRNO_NOERR) {
69         HILOGE("Failed to startNotify.");
70         return FsResult<void>::Error(ret);
71     }
72 
73     FsFileWatcher::GetInstance().AsyncGetNotifyEvent();
74 
75     return FsResult<void>::Success();
76 }
77 
78 } // namespace OHOS::FileManagement::ModuleFileIO
79