1 /*
2  * Copyright (C) 2023 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 #ifndef OHOS_MEDIALIBRARY_INOTIFY_H
16 #define OHOS_MEDIALIBRARY_INOTIFY_H
17 
18 #include <sys/inotify.h>
19 #include <atomic>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <unordered_map>
24 
25 #include "userfile_manager_types.h"
26 
27 namespace OHOS {
28 namespace Media {
29 #define EXPORT __attribute__ ((visibility ("default")))
30 struct WatchInfo {
WatchInfoWatchInfo31     WatchInfo(const std::string &path, const std::string &uri, const std::string &bundleName,
32         MediaLibraryApi api, const int64_t &currentTime): path_(path), uri_(uri),
33         bundleName_(bundleName), api_(api), meetEvent_(0), currentTime_(currentTime){};
34     std::string path_;
35     std::string uri_;
36     std::string bundleName_;
37     MediaLibraryApi api_;
38     uint32_t meetEvent_;
39     int64_t currentTime_;
40 };
41 
42 struct WatchBundleInfo {
WatchBundleInfoWatchBundleInfo43     WatchBundleInfo(const int32_t count, const int64_t firstEntryTime,
44         const std::string &firstUri, const std::string &bundleName): count(count),
45         firstEntryTime(firstEntryTime), firstUri(firstUri), bundleName(bundleName){};
46     int32_t count;
47     int64_t firstEntryTime;
48     std::string firstUri;
49     std::string bundleName;
DumpWatchBundleInfo50     std::string Dump() const
51     {
52         return "count:" + std::to_string(count) + ", firstEntryTime:" + std::to_string(firstEntryTime) +
53             ", firstUri:" + firstUri + ", bundleName:" + bundleName;
54     }
55 };
56 
57 class MediaLibraryInotify {
58 public:
59     EXPORT static std::shared_ptr<MediaLibraryInotify> GetInstance();
60     EXPORT int32_t AddWatchList(const std::string &path, const std::string &uri,
61         MediaLibraryApi api = MediaLibraryApi::API_OLD);
62     EXPORT MediaLibraryInotify() = default;
63     EXPORT ~MediaLibraryInotify() = default;
64     EXPORT int32_t RemoveByFileUri(const std::string &uri, MediaLibraryApi api = MediaLibraryApi::API_OLD);
65     EXPORT void DoAging();
66     void DoStop();
67     EXPORT const std::string BuildDfxInfo();
68 
69 private:
70     int32_t Remove(int wd);
71     void WatchCallBack();
72     int32_t Init();
73     void Restart();
74     int32_t GetBundleCount(const std::string &bundleName);
75 
76 private:
77     static std::shared_ptr<MediaLibraryInotify> instance_;
78     static std::mutex mutex_;
79     static inline std::unordered_map<int, struct WatchInfo> watchList_;
80     static inline int inotifyFd_ = 0;
81     static inline std::atomic<bool> isWatching_ = false;
82 };
83 } // namespace Media
84 } // namespace OHOS
85 #endif // OHOS_MEDIALIBRARY_INOTIFY_H