2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FIo_FileEventManager.cpp
19 * @brief This is the implementation file for FileEventManager class.
23 #include <sys/inotify.h>
26 #include <unique_ptr.h>
28 #include <FBaseInteger.h>
29 #include <FBaseString.h>
30 #include <FBaseSysLog.h>
31 #include <FBaseColAllElementsDeleter.h>
32 #include <FBaseRtIEventArg.h>
34 #include <FIoIFileEventListener.h>
35 #include <FIoFileEventManager.h>
37 #include <FBase_StringConverter.h>
38 #include <FBase_NativeError.h>
39 #include <FIo_FileEventManagerImpl.h>
41 #define INOTIFY_BUFFER_LEN (1024*(sizeof(struct inotify_event) + 16))
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Runtime;
46 using namespace Tizen::Base::Collection;
48 namespace Tizen { namespace Io
55 _FileEventArg(unsigned long event, String path, unsigned int eventId)
61 unsigned long __event;
63 unsigned int __eventId;
75 virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
77 IFileEventListener* pListener = dynamic_cast<IFileEventListener*> (&listener);
78 if (pListener != null)
80 const _FileEventArg* pArg = dynamic_cast<const _FileEventArg*>(&arg);
83 pListener->OnFileEventOccured(pArg->__event, pArg->__path, pArg->__eventId);
89 _FileEventManagerImpl::_FileEventManagerImpl(void)
90 : __pMonitorFileList(null)
98 _FileEventManagerImpl::~_FileEventManagerImpl(void)
100 if (__inotifyFd != -1)
105 if (__pGIOChannel != null)
107 g_io_channel_unref(__pGIOChannel);
110 if (__pGSource != null)
112 g_source_destroy(__pGSource);
113 g_source_unref(__pGSource);
116 if (__pMonitorFileList != null)
118 __pMonitorFileList->RemoveAll(true);
119 delete __pMonitorFileList;
126 _FileEventManagerImpl::OnFileEventOccured(GIOChannel* source, GIOCondition condition, gpointer data)
128 _FileEventManagerImpl* pFileEventManagerImpl = (_FileEventManagerImpl*)data;
130 pFileEventManagerImpl->SendEvent();
136 _FileEventManagerImpl::SendEvent(void)
138 unsigned long iter = 0;
140 char buffer[INOTIFY_BUFFER_LEN + 1] = {0,};
142 unsigned long event = 0;
144 unsigned int eventId = 0;
147 SysTryReturnResult(NID_IO, __inotifyFd != -1, E_IO, "Failed to inotify.");
149 length = read(__inotifyFd, buffer, INOTIFY_BUFFER_LEN);
150 SysTryReturnResult(NID_IO, length < (long)INOTIFY_BUFFER_LEN, E_IO, "Read buffer has failed.");
152 while (iter < static_cast<unsigned long> (length))
154 struct inotify_event* pEvent = (struct inotify_event*)&buffer[iter];
155 SysTryReturnResult(NID_IO, pEvent != null, E_IO, "Event info not found.");
156 event = (unsigned long)pEvent->mask;
157 eventId = pEvent->cookie;
165 path = GetRegisteredPath(pEvent->wd);
168 _FileEventArg* pEventArg= new (std::nothrow) _FileEventArg(event, path, eventId);
169 if(pEventArg != null)
171 SysLog(NID_IO, "_FileEventManagerImpl::SendEvent Event Fire");
172 __pEvent->FireAsync(*pEventArg);
176 iter += sizeof(struct inotify_event) + pEvent->len;
183 _FileEventManagerImpl::Construct(IFileEventListener& listener)
185 GMainContext* pGContext = null;
186 result r = E_SUCCESS;
187 _FileEvent* pEvent = null;
188 __inotifyFd = inotify_init();
190 if (__inotifyFd == -1 && errno == EMFILE)
192 SysLogException(NID_IO, E_MAX_EXCEEDED, "The number of opened files has exceeded the maximum limit.");
193 return E_MAX_EXCEEDED;
195 SysTryReturnResult(NID_IO, __inotifyFd != -1, E_IO, "Failed to init inotify.");
197 unique_ptr<HashMap, AllElementsDeleter> pMonitorFileList(new (std::nothrow) HashMap());
198 SysTryCatch(NID_IO, pMonitorFileList != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to create monitored file list.");
200 r = pMonitorFileList->Construct();
201 SysTryCatch(NID_IO, r == E_SUCCESS, , r, "[%s] Propagated.", GetErrorMessage(r));
203 pGContext = g_main_context_get_thread_default(); //get own gmain context except default thread
205 if (pGContext == null)
207 pGContext = g_main_context_default(); //get gmain context from me (default)
208 SysTryCatch(NID_IO, pGContext != null, r = E_IO, E_IO, "[E_IO] Failed to get glib context.");
211 __pEvent = new (std::nothrow) _FileEvent();
212 SysTryReturnResult(NID_IO, __pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
214 __pEvent->AddListener(listener);
216 __pGIOChannel = g_io_channel_unix_new(__inotifyFd); //fd wrapping
217 SysTryCatch(NID_IO, __pGIOChannel != null, r = E_IO, E_IO, "[E_IO] Failed to create glib channel.");
219 __pGSource = g_io_create_watch(__pGIOChannel, (GIOCondition)(G_IO_IN | G_IO_ERR | G_IO_NVAL | G_IO_HUP));
220 SysTryCatch(NID_IO, __pGSource != null, r = E_IO, E_IO, "[E_IO] Failed to create glib watch.");
222 g_source_set_callback(__pGSource, (GSourceFunc)OnFileEventOccured, this, NULL);
223 g_source_attach(__pGSource, pGContext);
225 __pMonitorFileList = pMonitorFileList.release();
231 if (__inotifyFd != -1)
236 if (__pGIOChannel != null)
238 g_io_channel_unref(__pGIOChannel);
239 __pGIOChannel = null;
242 if (__pGSource != null)
244 g_source_destroy(__pGSource);
245 g_source_unref(__pGSource);
254 _FileEventManagerImpl::AddPath(const String& path, unsigned long eventsToMonitor)
257 result r = E_SUCCESS;
259 SysTryReturnResult(NID_IO, File::IsFileExist(path) == true, E_FILE_NOT_FOUND,
260 "path[%ls] dose not exist.", path.GetPointer());
262 SysLog(NID_IO, "path:%ls", path.GetPointer());
264 unique_ptr<char[]> pPath(_StringConverter::CopyToCharArrayN(path));
265 SysTryReturnResult(NID_IO, pPath != null, E_IO, "String covert is failed.");
267 monitoredFd = inotify_add_watch(__inotifyFd, pPath.get(), eventsToMonitor);
269 if (monitoredFd == -1)
275 SysLogException(NID_IO, E_IO, "Read access to the given file[%s] is not permitted.", pPath.get());
276 r = E_ILLEGAL_ACCESS;
279 SysLogException(NID_IO, E_IO, "The path[%s] points outside of the process's accessible address space.", pPath.get());
280 r = E_ILLEGAL_ACCESS;
283 SysLogException(NID_IO, E_IO, "The given file descriptor is not valid.");
286 SysLogException(NID_IO, E_INVALID_ARG, "The given event mask contains no valid events; or fd is not an inotify file descriptor.");
290 SysLogException(NID_IO, E_IO, "Insufficient kernel memory was available.");
293 SysLogException(NID_IO, E_IO, "The user limit on the total number of inotify watches was reached or the kernel failed to allocate a needed resource");
302 unique_ptr<String> pMonitoredPath(new (std::nothrow) String(path));
303 unique_ptr<Integer> pMonitoredFd(new (std::nothrow) Integer(monitoredFd));
304 SysTryReturnResult(NID_IO, pMonitoredPath != null && pMonitoredFd != null, E_IO, "Failed to allocate memory");
306 SysTryReturnResult(NID_IO, __pMonitorFileList != null, E_IO, "Monitored file list is null");
308 r = __pMonitorFileList->Add(*pMonitoredPath.release(), *pMonitoredFd.release());
309 SysTryReturnResult(NID_IO, r == E_SUCCESS || r == E_OBJ_ALREADY_EXIST, E_IO, "Failed to add on monitored list.");
315 _FileEventManagerImpl::RemovePath(const String& path)
317 Integer* pMonitoredFd = null;
319 SysTryReturnResult(NID_IO, __pMonitorFileList != null, E_IO, "Monitored file list is null");
321 pMonitoredFd = (Integer*)__pMonitorFileList->GetValue(path);
323 SysTryReturnResult(NID_IO, pMonitoredFd != null, E_IO, "path[%ls] is not registered.", path.GetPointer());
325 SysTryReturnResult(NID_IO, inotify_rm_watch(__inotifyFd, pMonitoredFd->ToInt()) == 0,
326 E_IO, "path[%ls] is not registered.", path.GetPointer());
328 SysTryReturnResult(NID_IO, __pMonitorFileList->Remove(path, true) == E_SUCCESS,
329 E_IO, "path[%ls] is not registered.", path.GetPointer());
334 _FileEventManagerImpl::GetRegisteredPath(int fd)
336 String* pRegisteredPath = null;
337 Integer* pRegisteredFD = null;
339 if (__pMonitorFileList == null)
341 SysLog(NID_IO, "There is no path for fd:%d", fd);
345 unique_ptr<IMapEnumerator> pEnumerator(__pMonitorFileList->GetMapEnumeratorN());
346 SysTryReturn(NID_IO, pEnumerator != null, L"", GetLastResult(), "[%s] Monitored file list is empty", GetErrorMessage(GetLastResult()));
348 while (pEnumerator && pEnumerator->MoveNext() == E_SUCCESS)
350 pRegisteredPath = static_cast<String*> (pEnumerator->GetKey());
351 pRegisteredFD = static_cast<Integer*> (pEnumerator->GetValue());
353 if (fd == pRegisteredFD->ToInt())
355 SysLog(NID_IO, "Path is %ls", pRegisteredPath->GetPointer());
356 return *pRegisteredPath;
361 SysLog(NID_IO, "There is no path for fd:%d", fd);
365 _FileEventManagerImpl*
366 _FileEventManagerImpl::GetInstance(FileEventManager& fileEventManager)
368 return fileEventManager.__pFileEventManagerImpl;
371 const _FileEventManagerImpl*
372 _FileEventManagerImpl::GetInstance(const FileEventManager& fileEventManager)
374 return fileEventManager.__pFileEventManagerImpl;