Merge "Add a _LocalizedNumParser class and 4 static functions" into tizen_2.1
[platform/framework/native/appfw.git] / src / io / FIo_FileEventManagerImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file        FIo_FileEventManager.cpp
19  * @brief       This is the implementation file for _FileEventManager class.
20  */
21
22 #include <unique_ptr.h>
23 #include <unistd.h>
24 #include <sys/inotify.h>
25 #include <errno.h>
26
27 #include <FBaseInteger.h>
28 #include <FBaseString.h>
29 #include <FBaseColAllElementsDeleter.h>
30 #include <FBaseColIMapEnumerator.h>
31 #include <FBaseSysLog.h>
32 #include <FIoFile.h>
33 #include <FIoFileEventManager.h>
34 #include <FIoIFileEventListener.h>
35
36 #include <FBase_StringConverter.h>
37 #include <FBase_NativeError.h>
38 #include "FIo_FileEventManagerImpl.h"
39 #include "FIo_FileEventDispatcher.h"
40 #include "FIo_FileEvent.h"
41
42 using namespace std;
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Runtime;
45 using namespace Tizen::Base::Collection;
46
47 namespace Tizen { namespace Io
48 {
49
50 _FileEventManagerImpl::_FileEventManagerImpl(void)
51         : __pWatchList(null)
52         , __pEvent(null)
53         , __inotifyFd(-1)
54 {
55 }
56
57 _FileEventManagerImpl::~_FileEventManagerImpl(void)
58 {
59         SysLog(NID_IO, "_FileEventManagerImpl: 0x%x", this);
60
61         if (__pWatchList != null)
62         {
63                 _FileEventDispatcher* pDispatcher = _FileEventDispatcher::GetInstance();
64                 IMapEnumerator* pMapEnum = __pWatchList->GetMapEnumeratorN();
65                 while (pMapEnum->MoveNext() == E_SUCCESS)
66                 {
67                         Integer* pWd = dynamic_cast< Integer* >(pMapEnum->GetValue());
68                         if (pWd != null)
69                         {
70                                 SysLog(NID_IO, "inotify fd: %d, watch description: %d", __inotifyFd, pWd->ToInt());
71                                 int res = inotify_rm_watch(__inotifyFd, pWd->ToInt());
72                                 SysTryLog(NID_IO, res == 0, "inotify_rm_watch() failed, errno: %d (%s)", errno, strerror(errno));
73                                 pDispatcher->RemoveFileEventInfo(*pWd);
74                         }
75                 }
76         }
77
78         delete __pWatchList;
79         delete __pEvent;
80 }
81
82 result
83 _FileEventManagerImpl::Construct(IFileEventListener& listener)
84 {
85         unique_ptr< HashMap > pWatchList(new (std::nothrow) HashMap(SingleObjectDeleter));
86         SysTryReturnResult(NID_IO, pWatchList != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
87         pWatchList->Construct();
88
89         _FileEventDispatcher* pDispatcher = _FileEventDispatcher::GetInstance();
90
91         unique_ptr< _FileEvent > pEvent(new (std::nothrow) _FileEvent());
92         SysTryReturnResult(NID_IO, pEvent != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
93         result r = pEvent->Construct();
94         SysTryReturn(NID_IO, !IsFailed(r), E_IO, r, "[%s] The caller is a worker thread.", GetErrorMessage(r));
95
96         r = pEvent->AddListener(listener, true);
97         SysTryReturn(NID_IO, r == E_SUCCESS || r == E_OBJ_ALREADY_EXIST, E_IO, r,
98                         "[%s] The caller is a worker thread.", GetErrorMessage(r));
99
100         __inotifyFd = pDispatcher->GetInotifyFd();
101         __pWatchList = pWatchList.release();
102         __pEvent = pEvent.release();
103
104         SysLog(NID_IO, "_FileEventManagerImpl: 0x%x, _Event: 0x%x", this, __pEvent);
105         return E_SUCCESS;
106 }
107
108 result
109 _FileEventManagerImpl::AddPath(const String& path, unsigned long eventsToMonitor)
110 {
111         SysSecureLog(NID_IO, "Add watch path: %ls, _FileEventManagerImpl: 0x%x", path.GetPointer(), this);
112         result r = E_SUCCESS;
113
114         SysTryReturnResult(NID_IO, File::IsFileExist(path) == true, E_FILE_NOT_FOUND,
115                         "The path (%ls) dose not exist.", path.GetPointer());
116
117         unique_ptr< char[] > pPath(_StringConverter::CopyToCharArrayN(path));
118         SysTryReturnResult(NID_IO, pPath != null, GetLastResult(), "Propagating to caller...");
119
120         int watchDescription = inotify_add_watch(__inotifyFd, pPath.get(), eventsToMonitor & ~IN_IGNORED);
121         if (watchDescription == -1)
122         {
123                 r = E_IO;
124                 switch (errno)
125                 {
126                 case EACCES:
127                         SysLogException(NID_IO, E_IO, "Read access to the given file (%s) is not permitted.", pPath.get());
128                         r = E_ILLEGAL_ACCESS;
129                         break;
130                 case EFAULT:
131                         SysLogException(NID_IO, E_IO, "The path (%s) points outside of the process's accessible address space.", pPath.get());
132                         r = E_ILLEGAL_ACCESS;
133                         break;
134                 case EBADF:
135                         SysLogException(NID_IO, E_IO, "The given file descriptor is not valid.");
136                         break;
137                 case EINVAL:
138                         SysLogException(NID_IO, E_INVALID_ARG, "The given event mask contains no valid events; or fd is not an inotify file descriptor.");
139                         r = E_INVALID_ARG;
140                         break;
141                 case ENOMEM:
142                         SysLogException(NID_IO, E_IO, "Insufficient kernel memory was available.");
143                         break;
144                 case ENOSPC:
145                         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");
146                         break;
147                 default:
148                         break;
149                 }
150
151                 return r;
152         }
153
154         Integer* pWatchDescription = new (std::nothrow) Integer(watchDescription);
155         _FileEventInfo* pFileEventInfo = new (std::nothrow) _FileEventInfo();
156         String* pWatchPath = new (std::nothrow) String(path);
157         Integer* pWd = new (std::nothrow) Integer(watchDescription);
158         SysTryCatch(NID_IO, pWatchDescription != null && pFileEventInfo != null && pWatchPath != null && pWd != null,
159                         r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "The memory is insufficient.");
160
161         pFileEventInfo->pFileEventManagerImpl = this;
162         pFileEventInfo->path = path;
163
164         r = _FileEventDispatcher::GetInstance()->AddFileEventInfo(pWatchDescription, pFileEventInfo);
165         SysTryCatch(NID_IO, r == E_SUCCESS || r == E_OBJ_ALREADY_EXIST, r = E_IO, r,
166                         "[%s] Failed to add file event info.", GetErrorMessage(r));
167         if (r == E_OBJ_ALREADY_EXIST)
168         {
169                 SysLog(NID_IO, "FileEventInfo already exists.");
170                 delete pWatchDescription;
171                 delete pFileEventInfo;
172                 delete pWatchPath;
173                 delete pWd;
174         }
175         else
176         {
177                 SysLog(NID_IO, "Add to watch list");
178                 __pWatchList->Add(pWatchPath, pWd);
179         }
180
181         return E_SUCCESS;
182
183 CATCH:
184         delete pWatchDescription;
185         delete pFileEventInfo;
186         delete pWatchPath;
187         delete pWd;
188
189         return r;
190 }
191
192 result
193 _FileEventManagerImpl::RemovePath(const String& path)
194 {
195         SysSecureLog(NID_IO, "Remove watch path (%ls), _FileEventManagerImpl: 0x%x", path.GetPointer(), this);
196
197         Integer* pWd = dynamic_cast< Integer* >(__pWatchList->GetValue(path));
198         SysTryReturnResult(NID_IO, pWd != null, E_IO, "The path (%ls) is not registered.", path.GetPointer());
199
200         SysTryReturnResult(NID_IO, inotify_rm_watch(__inotifyFd, pWd->ToInt()) == 0,
201                         E_IO, "The path (%ls) is not registered.", path.GetPointer());
202
203         _FileEventDispatcher::GetInstance()->RemoveFileEventInfo(*pWd);
204         __pWatchList->Remove(path);
205
206         return E_SUCCESS;
207 }
208
209 _FileEventManagerImpl*
210 _FileEventManagerImpl::GetInstance(FileEventManager& fileEventManager)
211 {
212         return fileEventManager.__pFileEventManagerImpl;
213 }
214
215 const _FileEventManagerImpl*
216 _FileEventManagerImpl::GetInstance(const FileEventManager& fileEventManager)
217 {
218         return fileEventManager.__pFileEventManagerImpl;
219 }
220
221 }} // Tizen::Io
222