1 /*
2  * Copyright (C) 2022 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 FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_MTP_FILE_OBSERVER_H_
16 #define FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_MTP_FILE_OBSERVER_H_
17 #include "mtp_event.h"
18 #include <map>
19 #include <memory>
20 #include <mutex>
21 #include <sys/inotify.h>
22 #include <thread>
23 #include <vector>
24 #include "singleton.h"
25 namespace OHOS {
26 namespace Media {
27 using ContextSptr = std::shared_ptr<MtpOperationContext>;
28 class MtpFileObserver : public Singleton<MtpFileObserver> {
29 public:
30     void AddFileInotify(const std::string &path, const std::string &realPath, const ContextSptr &context);
31     bool StartFileInotify();
32     bool StopFileInotify();
33     void AddPathToWatchMap(const std::string &path);
34 
35 private:
36     static bool AddInotifyEvents(const int &inotifyFd, const ContextSptr &context);
37     static bool WatchPathThread(const ContextSptr &context);
38     static void SendBattery(const ContextSptr &context);
39     static void SendEvent(const inotify_event &event, const std::string &path, const ContextSptr &context);
40     static void EraseFromWatchMap(const std::string &path);
41     static void UpdateWatchMap(const std::string &path);
42     static void DealWatchMap(const inotify_event &event, const std::string &path);
43     static bool isRunning_;
44     static std::map<int, std::string> watchMap_;
45     static int inotifyFd_;
46     static std::mutex eventLock_;
47     bool startThread_;
48     bool inotifySuccess_;
49 };
50 } // namespace Media
51 } // namespace OHOS
52 #endif // FRAMEWORKS_SERVICES_MEDIA_MTP_INCLUDE_MTP_FILE_OBSERVER_H_
53