1518351ea8ef4c6b8bb62d238a59ff42b3c220fb
[framework/web/wrt-plugins-tizen.git] / src / Filesystem / Manager.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18
19 #include "Manager.h"
20
21 #include <unistd.h>
22 #include <sys/stat.h>
23 #include <errno.h>
24 #include <pcrecpp.h>
25 #include <ctime>
26 #include <sstream>
27 #include <dirent.h>
28 #include <dpl/scoped_ptr.h>
29
30 #include <Commons/Exception.h>
31 #include <Commons/Regex.h>
32 #include "PathUtils.h"
33 #include "StorageProperties.h"
34 #include "Node.h"
35 #include "Utils.h"
36 #include <app.h>
37 #include <Logger.h>
38
39 namespace {
40 const char* PATH_DOWNLOADS = "/opt/usr/media/Downloads";
41 const char* PATH_DOCUMENTS = "/opt/usr/media/Documents";
42 const char* PATH_SOUNDS = "/opt/usr/media/Sounds";
43 const char* PATH_IMAGES = "/opt/usr/media/Images";
44 const char* PATH_VIDEOS = "/opt/usr/media/Videos";
45 const char* PATH_RINGTONE = "/opt/usr/share/settings/Ringtones";
46 }
47
48 using namespace WrtDeviceApis;
49 using namespace WrtDeviceApis::Commons;
50
51 namespace DeviceAPI {
52 namespace Filesystem {
53 Manager::Locations Manager::m_locations;
54 Manager::RootList Manager::m_rootlist;
55 Manager::SubRootList Manager::m_subrootlist;
56 const std::size_t Manager::m_maxPathLength = 256;
57 NodeList Manager::m_openedNodes;
58 std::vector<Manager::WatcherPtr> Manager::m_watchers;
59
60 bool Manager::fileExists(const std::string &file)
61 {
62     errno = 0;
63     struct stat info;
64     memset(&info, 0, sizeof(struct stat));
65     int status = lstat(file.c_str(), &info);
66     if (status == 0) {
67         return true;
68     } else if (errno == ENOENT) {
69         return false;
70     }
71     ThrowMsg(Commons::PlatformException, "Cannot stat file.");
72 }
73
74 bool Manager::getSupportedDeviceCB(int storage, storage_type_e type, storage_state_e state, const char *path, void *user_data)
75 {
76         std::vector<StoragePropertiesPtr>* storageVector = (std::vector<StoragePropertiesPtr>*)user_data;
77         StoragePropertiesPtr storageItem(new StorageProperties());
78
79         int size = 12;
80         char buf[size];
81         std::string lable;
82         std::string fullpath(path);
83         if (type == STORAGE_TYPE_INTERNAL) {
84                 snprintf(buf, size, "internal%d", storage);
85                 lable.append(buf);
86         } else if (type == STORAGE_TYPE_EXTERNAL) {
87                 snprintf(buf, size, "removable%d", storage);
88                 lable.append(buf);
89         }
90         LoggerD(lable << "state" << state);
91
92         storageItem->setId(storage);
93         storageItem->setLabel(lable);
94         storageItem->setType((short)type);
95         storageItem->setState((short)state);
96         storageItem->setFullPath(fullpath);
97
98         storageVector->insert(storageVector->end(), storageItem);
99
100         return true;
101 }
102
103 void Manager::storageStateChangedCB(int storage, storage_state_e state, void *user_data)
104 {
105         if (user_data != NULL) {
106                 watcherList* m_watcherList = (watcherList *)user_data;
107                 watcherList::iterator it = m_watcherList->begin();
108                 for(; it!= m_watcherList->end(); ++it) {
109                         (*it)->StorageStateHasChanged(storage, state);
110                 }
111         }
112 }
113
114 void Manager::addOpenedNode(const INodePtr& node)
115 {
116     NodeListIterator it = m_openedNodes.begin();
117     for (; it != m_openedNodes.end(); ++it) {
118         if (node.Get() == (*it).Get()) {
119             //node is added already
120             return;
121         }
122     }
123     m_openedNodes.push_back(node);
124 }
125
126 void Manager::removeOpenedNode(const INodePtr& node)
127 {
128     NodeListIterator it = m_openedNodes.begin();
129     for (; it != m_openedNodes.end(); ++it) {
130         if ((*it).Get() == node.Get()) {
131             m_openedNodes.erase(it);
132             break;
133         }
134     }
135 }
136
137 bool Manager::checkIfOpened(const IPathPtr& path) const
138 {
139     NodeListIterator it = m_openedNodes.begin();
140     for (; it != m_openedNodes.end(); ++it) {
141         if (*path == *(*it)->getPath()) {
142             return true;
143         }
144     }
145     return false;
146 }
147
148 Manager::Manager()
149 {
150     static bool initialized = init();
151     (void) initialized;
152 }
153
154 Manager::~Manager()
155 {
156 }
157
158 StorageList Manager::getStorageList() const
159 {
160     return m_locations;
161 }
162
163 IPathPtr Manager::getBasePath() const
164 {
165     Locations::const_iterator it = m_locations.find(m_subrootlist.find(LT_ROOT)->second);
166     if (it == m_locations.end()) {
167         ThrowMsg(Commons::PlatformException, "Base path not available.");
168     }
169     return it->second;
170 }
171
172 IPathPtr Manager::getLocationPath(LocationType type) const
173 {
174     Locations::const_iterator it = m_locations.find(m_subrootlist.find(type)->second);
175     if (it != m_locations.end()) {
176         return it->second->clone();
177     }
178     return IPathPtr();
179 }
180
181 LocationPaths Manager::getLocationPaths() const
182 {
183     LocationPaths result;
184     Locations::const_iterator it = m_locations.begin();
185     for (; it != m_locations.end(); ++it) {
186         result.push_back(it->second->clone());
187     }
188     return result;
189 }
190
191 LocationTypes Manager::getLocations() const
192 {
193     LocationTypes result;
194     SubRootList::const_iterator it = m_subrootlist.begin();
195     for (; it != m_subrootlist.end(); ++it) {
196         result.push_back(it->first);
197     }
198     return result;
199 }
200
201 void Manager::getNode(const EventResolvePtr& event)
202 {
203     EventRequestReceiver<EventResolve>::PostRequest(event);
204 }
205
206 void Manager::getStorage(const EventGetStoragePtr& event)
207 {
208         EventRequestReceiver<EventGetStorage>::PostRequest(event);
209 }
210
211 void Manager::listStorages(const EventListStoragesPtr& event)
212 {
213         EventRequestReceiver<EventListStorages>::PostRequest(event);
214 }
215
216 std::size_t Manager::getMaxPathLength() const
217 {
218     return m_maxPathLength;
219 }
220
221 void Manager::copy(const EventCopyPtr& event)
222 {
223     EventRequestReceiver<EventCopy>::PostRequest(event);
224 }
225
226 void Manager::move(const EventMovePtr& event)
227 {
228     EventRequestReceiver<EventMove>::PostRequest(event);
229 }
230
231 void Manager::create(const EventCreatePtr& event)
232 {
233     EventRequestReceiver<EventCreate>::PostRequest(event);
234 }
235
236 void Manager::remove(const EventRemovePtr& event)
237 {
238     EventRequestReceiver<EventRemove>::PostRequest(event);
239 }
240
241 void Manager::find(const EventFindPtr& event)
242 {
243     EventRequestReceiver<EventFind>::PostRequest(event);
244 }
245
246 void Manager::find(const IPathPtr& path,
247         const FiltersMap& filters,
248         NodeList& result,
249         const EventFindPtr& event)
250 {
251     Try {
252         DIR* dir = opendir(path->getFullPath().c_str());
253         if (!dir) {
254             return;
255         }
256
257         errno = 0;
258         struct dirent* entry = NULL;
259         while ((entry = readdir(dir))) {
260             if (event && event->checkCancelled()) {
261                 break;
262             }
263             if (!strcmp(entry->d_name, ".") || !strncmp(entry->d_name, "..", 2)) {
264                 continue;
265             }
266             IPathPtr childPath = *path + entry->d_name;
267             struct stat info;
268             memset(&info, 0, sizeof(struct stat));
269             if (lstat(childPath->getFullPath().c_str(), &info) == 0) {
270                 if (matchFilters(entry->d_name, info, filters)) {
271                     result.push_back(Node::resolve(childPath));
272                 }
273                 if (S_ISDIR(info.st_mode)) {
274                     find(childPath, filters, result, event);
275                 }
276             }
277         }
278
279         if (errno != 0) {
280             ThrowMsg(Commons::PlatformException,
281                      "Error while reading directory.");
282         }
283
284         if (closedir(dir) != 0) {
285             ThrowMsg(Commons::PlatformException,
286                      "Could not close platform node.");
287         }
288     }
289     Catch(WrtDeviceApis::Commons::Exception) {
290     }
291 }
292
293 void Manager::copyElement(
294         const std::string &src, const std::string &dest, bool recursive) const
295 {
296     LoggerD("Copying src: " << src << " to: " << dest);
297
298     //element is a file:
299     if (EINA_TRUE != ecore_file_is_dir(src.c_str())) {
300         if (EINA_TRUE != ecore_file_cp(src.c_str(), dest.c_str())) {
301             ThrowMsg(Commons::PlatformException, "Failed to copy file");
302         }
303         return;
304     }
305     //element is a directory -> create it:
306     if (EINA_TRUE != ecore_file_is_dir(dest.c_str())) {
307         if (EINA_TRUE != ecore_file_mkdir(dest.c_str())) {
308             LoggerD("Failed to create destination directory");
309             ThrowMsg(Commons::PlatformException, "Failed to copy directory");
310         }
311     }
312     //copy all elements of directory:
313     if (recursive) {
314         Eina_List* list = ecore_file_ls(src.c_str());
315         void* data;
316         EINA_LIST_FREE(list, data)
317         {
318             Try
319             {
320                 copyElement((src + '/' + static_cast<char*>(data)).c_str(),
321                             (dest + '/' + static_cast<char*>(data)).c_str());
322             }
323                         
324             Catch (Commons::Exception) 
325             {
326                  //remove rest of the list
327                 EINA_LIST_FREE(list, data)
328                 {
329                     free(data);
330                 }
331                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
332                 ReThrowMsg(Commons::PlatformException, "Failed to copy element");
333             }
334             free(data);
335         }
336     }
337
338 }
339
340 /*bool Manager::access(const IPathPtr& path,
341         int accessType) const
342 {
343     int amode = 0;
344     if (accessType & AT_EXISTS) { amode |= F_OK; }
345     if (accessType & AT_READ) { amode |= R_OK; }
346     if (accessType & AT_WRITE) { amode |= W_OK; }
347     if (accessType & AT_EXEC) { amode |= X_OK; }
348     return (::access(path->getFullPath().c_str(), amode) == 0);
349 }*/
350
351 long Manager::addStorageStateChangeListener(
352         const EventStorageStateChangedEmitterPtr& emitter)
353 {
354         RootList::const_iterator it = m_rootlist.begin();
355         WatcherPtr watcher(new Watcher(emitter));
356         Manager::m_watchers.push_back(watcher);
357
358         for (; it != m_rootlist.end(); ++it) {
359                 storage_set_state_changed_cb(it->second, Manager::storageStateChangedCB, (void *)(&m_watchers));
360         }
361
362         watcher->getCurrentStoargeStateForWatch();
363         return static_cast<long>(emitter->getId());
364 }
365
366 void Manager::removeStorageStateChangeListener(EventStorageStateChangedEmitter::IdType id)
367 {
368         watcherList::iterator itWatcher = Manager::m_watchers.begin();
369         bool found = false;
370         for (;itWatcher != Manager::m_watchers.end();) {
371                 if (id == (*itWatcher)->getEmitter()->getId()) {
372                         itWatcher = Manager::m_watchers.erase(itWatcher);
373                         found = true;
374                         continue;
375                 }
376                 ++itWatcher;
377         }
378
379         if (Manager::m_watchers.size() == 0) {
380                 RootList::const_iterator itRoot;
381                 for (itRoot = m_rootlist.begin(); itRoot != m_rootlist.end(); ++itRoot) {
382                         storage_unset_state_changed_cb(itRoot->second);
383                 }
384         }
385
386         if (found == false) {
387 //              LoggerD("no exist" << id);
388                 ThrowMsg(Commons::NotFoundException, "The " << id << "is not exist");
389         }
390 }
391
392 bool Manager::matchFilters(const std::string& name,
393         const struct stat& info,
394         const FiltersMap& filters)
395 {
396     FiltersMap::const_iterator it = filters.begin();
397     for (; it != filters.end(); ++it) {
398         if (it->first == FF_NAME) {
399             if (!pcrecpp::RE(it->second).PartialMatch(name)) { return false; }
400         } else if (it->first == FF_SIZE) {
401             std::size_t size;
402             std::stringstream ss(it->second);
403             ss >> size;
404             if (!ss ||
405                 (size != static_cast<size_t>(info.st_size))) { return false; }
406         } else if (it->first == FF_CREATED) {
407             std::time_t created;
408             std::stringstream ss(it->second);
409             ss >> created;
410             if (!ss || (created != info.st_ctime)) { return false; }
411         } else if (it->first == FF_MODIFIED) {
412             std::time_t modified;
413             std::stringstream ss(it->second);
414             ss >> modified;
415             if (!ss || (modified != info.st_mtime)) { return false; }
416         }
417     }
418     return true;
419 }
420
421 void Manager::OnRequestReceived(const EventResolvePtr& event)
422 {
423     try {
424         INodePtr node = Node::resolve(event->getPath());
425         if (node->checkPermission(event->getPath(), event->getMode(), node->getType()) == false)
426         {
427             ThrowMsg(Commons::SecurityException, "Permission Denied Error");
428         }
429         event->setResult(node);
430     }
431     catch (const Commons::Exception& ex) 
432     {
433         LoggerE("Exception: " << ex.GetMessage());
434         event->setExceptionCode(ex.getCode());
435     }
436 }
437
438 void Manager::OnRequestReceived(const EventGetStoragePtr& event)
439 {
440         try {
441
442                 StoragePropertiesPtr storage(new StorageProperties());
443
444                 RootList::const_iterator it = m_rootlist.find(event->getLabel());
445                 if (it == m_rootlist.end()) {
446                         Locations::const_iterator itL = m_locations.find(event->getLabel());
447                         if (itL == m_locations.end()) {
448                                 ThrowMsg(Commons::NotFoundException, "Base path not available.");
449                         } else {
450                                 storage->setId(0xff);
451                                 storage->setLabel(event->getLabel());
452                                 storage->setType(StorageProperties::TYPE_INTERNAL);
453                                 storage->setState(StorageProperties::STATE_MOUNTED);
454                         }
455                 } else {
456                         int id = it->second;
457
458                         storage_type_e currentType;
459                         storage_state_e currentState;
460
461                         storage_get_type(id, &currentType);
462                         storage_get_state(id, &currentState);
463
464                         storage->setId(id);
465                         storage->setLabel(event->getLabel());
466                         storage->setType((short)currentType);
467                         storage->setState((short)currentState);
468                 }
469
470                 event->setResult(storage);
471     } catch (const Commons::Exception& ex) 
472     {
473         event->setExceptionCode(ex.getCode());
474     }
475 }
476
477 void Manager::OnRequestReceived(const EventListStoragesPtr& event)
478 {
479         try {
480                 std::vector<StoragePropertiesPtr> storageList;
481                         
482                 storage_foreach_device_supported(Manager::getSupportedDeviceCB, &storageList);
483
484                 SubRootList::const_iterator it = m_subrootlist.begin();
485                 for (; it != m_subrootlist.end(); ++it) {
486                         if (it->first == LT_ROOT) 
487                                 continue;
488                         if (it->first == LT_SDCARD) 
489                                 continue;
490                         if (it->first == LT_USBHOST) 
491                                 continue;                       
492
493                         addLocalStorage(it->second, storageList);
494                 }
495                 
496
497                 event->setResult(storageList);
498     }   catch (const Commons::Exception& ex) 
499     {
500         event->setExceptionCode(ex.getCode());
501     }
502 }
503
504 void Manager::OnRequestReceived(const EventCopyPtr& event)
505 {
506     Try {
507         INodePtr srcNode = Node::resolve(event->getSource());
508                 LoggerD(std::hex << srcNode->getMode() << " " << std::hex << PM_USER_READ);
509         if ((srcNode->getMode() & PM_USER_READ/*PERM_READ*/) == 0) {
510             ThrowMsg(Commons::SecurityException,
511                      "Not enough permissions to read source node.");
512         }
513
514         IPathPtr src = event->getSource();
515         IPathPtr dest = event->getDestination();
516         if (!dest->isAbsolute()) {
517             dest = src->getPath() + *dest;
518         }
519   
520         INodePtr parent;
521         Try {
522             parent = Node::resolve(IPath::create(dest->getPath()));
523         }
524         Catch(Commons::NotFoundException) 
525         {
526             event->setExceptionCode(_rethrown_exception.getCode());
527             ReThrowMsg(Commons::NotFoundException, "could not find a destination path.");
528         }
529         Catch (Commons::Exception) 
530         {
531             event->setExceptionCode(_rethrown_exception.getCode());
532             ReThrowMsg(Commons::PlatformException,
533                        "Could not get destination's parent node.");
534         }
535
536         if (parent->getType() != NT_DIRECTORY) {
537             ThrowMsg(Commons::PlatformException,
538                      "Destination's parent node is not directory.");
539         }
540
541                 std::string realSrc = src->getFullPath();
542                 std::string realDest = dest->getFullPath();
543
544                 if (realSrc == realDest) {
545                         ThrowMsg(Commons::PlatformException,
546                         "Destination is same as source.");
547                 }
548                 
549
550         errno = 0;
551         struct stat info;
552         memset(&info, 0, sizeof(struct stat));
553         int status = lstat(realDest.c_str(), &info);
554         if ((status != 0) && (errno != ENOENT)) {
555             ThrowMsg(Commons::PlatformException,
556                      "No access to platform destination node.");
557         }
558                 
559         if (S_ISDIR(info.st_mode) && srcNode->getType() == NT_FILE) {
560             dest->append("/" + src->getName());
561             realDest = dest->getFullPath();
562             memset(&info, 0, sizeof(struct stat));
563             status = lstat(realDest.c_str(), &info);
564             if ((status != 0) && (errno != ENOENT)) {
565                 ThrowMsg(Commons::PlatformException,
566                              "No access to platform destination node.");
567             }
568         }
569
570         if (0 == status) {
571             //no owerwrite flag setted -> exception
572             if ((event->getOptions() & OPT_OVERWRITE) == 0) {
573                 ThrowMsg(Commons::PlatformException, "Overwrite is not set.");
574             }
575
576             if (event->checkCancelled()) {
577                 //file is not copied yet, so we can cancel it now.
578                 event->setCancelAllowed(true);
579                 return;
580             }
581
582             //destination exist. Need to be removed
583             Try {
584                 INodePtr node = Node::resolve(dest);
585
586                 // only remove if dest is file.
587                 if (node->getType() == NT_FILE) {
588                     node->remove(event->getOptions());
589                 } 
590                 else {
591                         // destination exist and src & dest are directory and dest path ends with '/'
592                     if (srcNode->getType() == NT_DIRECTORY && realDest[realDest.length() - 1] == '/') {
593                         realDest += src->getName();
594                     }
595                 }
596             }
597             catch (const Commons::Exception& ex) 
598             {
599                 LoggerE("Exception: " << ex.GetMessage());
600                 event->setExceptionCode(ex.getCode());
601              }
602         }
603         //Destination is not exist. Start copy now.
604                 LoggerD(dest->getFullPath().c_str());
605         copyElement(realSrc, realDest);
606
607         event->setResult(Node::resolve(dest));
608     }
609     catch (const Commons::Exception& ex) 
610     {
611         LoggerE("Exception: " << ex.GetMessage());
612         event->setExceptionCode(ex.getCode());
613     }
614     //file is copied already so we don't allow cancelling anymore.
615     event->setCancelAllowed(false);
616 }
617
618 void Manager::OnRequestReceived(const EventMovePtr& event)
619 {
620     try {
621         IPathPtr src = event->getSource();
622         IPathPtr dest = event->getDestination();
623
624         INodePtr srcNode = Node::resolve(src);
625                 LoggerD(std::hex << srcNode->getMode() << " " << std::hex << PM_USER_WRITE);
626         if ((srcNode->getMode() & PM_USER_WRITE/*PERM_WRITE*/) == 0)
627         {
628             ThrowMsg(Commons::SecurityException,
629                      "Not enough permissions to move source node.");
630         }
631
632         if (!dest->isAbsolute()) {
633             dest = src->getPath() + *dest;
634         }
635
636         if (src == dest) {
637             ThrowMsg(Commons::PlatformException,
638                      "Destination is same as source.");
639         }
640
641         INodePtr parent;
642         Try {
643             parent = Node::resolve(IPath::create(dest->getPath()));
644         }
645                 
646         Catch(Commons::NotFoundException) 
647         {
648             event->setExceptionCode(_rethrown_exception.getCode());
649             ReThrowMsg(Commons::NotFoundException, "could not find a destination path.");
650         }
651         Catch(Commons::Exception) 
652         {
653             LoggerE("Exception: " << _rethrown_exception.GetMessage());
654             ReThrowMsg(Commons::PlatformException,
655                        "Could not get destination's parent node.");
656         }
657
658         if (parent->getType() != NT_DIRECTORY) {
659             ThrowMsg(Commons::PlatformException,
660                      "Destination's parent node is not directory.");
661         }
662
663         errno = 0;
664         struct stat info;
665         memset(&info, 0, sizeof(info));
666         int status = lstat(dest->getFullPath().c_str(), &info);
667         if ((status != 0) && (errno != ENOENT)) {
668             ThrowMsg(Commons::PlatformException,
669                      "No access to platform destination node.");
670         }
671
672         LoggerD(dest->getFullPath().c_str());
673                 
674         if (S_ISDIR(info.st_mode) && srcNode->getType() == NT_FILE) {
675                         dest->append("/" + src->getName());
676                         memset(&info, 0, sizeof(info));
677                         status = lstat(dest->getFullPath().c_str(), &info);
678                         if ((status != 0) && (errno != ENOENT)) {
679                     ThrowMsg(Commons::PlatformException,
680                             "No access to platform destination node.");
681                         }
682         } 
683
684                 if (status == 0 && 0 == (event->getOptions() & OPT_OVERWRITE)) {
685             ThrowMsg(Commons::PlatformException, "Overwrite is not set.");
686         }
687
688         if (event->checkCancelled()) {
689             //file is not moved yet, so we can cancel it now.
690             event->setCancelAllowed(true);
691             return;
692         }
693
694         errno = 0;
695         
696         LoggerD(dest->getFullPath().c_str());
697                 
698         if (0 != ::rename(src->getFullPath().c_str(), dest->getFullPath().c_str()))
699         {
700             int error = errno;
701             switch (error)
702             {
703             case EXDEV:
704                 {
705                                         LoggerD(std::hex << srcNode->getMode() << " " << std::hex << PM_USER_READ);
706                     if ((srcNode->getMode() & PM_USER_READ /*PERM_READ*/) == 0)
707                     {
708                         ThrowMsg(Commons::SecurityException,
709                                  "Not enough permissions to move source node.");
710                     }
711                     if (0 == status) {
712                         //destination exist. Need to be removed
713                         Try {
714                             INodePtr node = Node::resolve(dest);
715                             node->remove(event->getOptions());
716                         }
717                         catch (const Commons::Exception& ex) 
718                         {
719                             LoggerE("Exception: " << ex.GetMessage());
720                             event->setExceptionCode(ex.getCode());
721                             LoggerE("Exception while removing dest directory");
722                         }
723                     }
724
725                     copyElement(src->getFullPath(),
726                                 dest->getFullPath());
727                     //remove source files
728                     Try {
729                         INodePtr node = Node::resolve(event->getSource());
730                         node->remove(event->getOptions());
731                     }
732                     Catch(Commons::Exception) {
733                         LoggerE("Exception: "
734                                  << _rethrown_exception.GetMessage());
735                     }
736                     break;
737                 }
738             default:
739                                 // needtofix
740                 ThrowMsg(Commons::PlatformException,
741                          "Error on rename: " /*<< DPL::GetErrnoString(error)*/);
742                 break;
743             }
744         }
745
746         event->setResult(Node::resolve(dest));
747     }
748     catch (const Commons::Exception& ex) 
749     {
750         LoggerE("Exception: " << ex.GetMessage());
751         event->setExceptionCode(ex.getCode());
752     }
753
754     event->setCancelAllowed(false);
755 }
756
757 void Manager::OnRequestReceived(const EventCreatePtr& event)
758 {
759     Try {
760     }
761     catch (const Commons::Exception& ex) 
762     {
763         LoggerE("Exception: " << ex.GetMessage());
764         event->setExceptionCode(ex.getCode());
765     }
766 }
767
768 void Manager::OnRequestReceived(const EventRemovePtr& event)
769 {
770     if (!event->checkCancelled()) {
771         Try {
772             INodePtr node = Node::resolve(event->getPath());
773             node->remove(event->getOptions());
774         }
775         catch (const Commons::Exception& ex) 
776         {
777             LoggerE("Exception: " << ex.GetMessage());
778             event->setExceptionCode(ex.getCode());
779         }
780         event->setCancelAllowed(false);
781     } else {
782         event->setCancelAllowed(true);
783     }
784 }
785
786 void Manager::OnRequestReceived(const EventFindPtr& event)
787 {
788     try {
789         NodeList result;
790         find(event->getPath(), event->getFilters(), result, event);
791         event->setResult(result);
792     }
793     catch (const Commons::Exception& ex) {
794         LoggerE("Exception: " << ex.GetMessage());
795         event->setExceptionCode(Commons::ExceptionCodes::PlatformException);
796     }
797     event->setCancelAllowed(true);
798 }
799
800 void Manager::addLocalStorage(std::string label, std::vector<StoragePropertiesPtr> &storageList)
801 {
802         StoragePropertiesPtr storage(new StorageProperties());
803         storage->setId(0xff);
804         storage->setLabel(label);
805         storage->setType(StorageProperties::TYPE_INTERNAL);
806
807         storage_state_e currentState;
808         storage_get_state(0, &currentState);
809         storage->setState((short)currentState);
810
811         storageList.insert(storageList.end(), storage);
812 }
813
814 void Manager::addWidgetStorage(const std::string &key, const std::string &value)
815 {
816         setupLocation(key, value.c_str());
817
818         if (key == "wgt-package")
819         {
820                 m_subrootlist[LT_WGTPKG] = key;
821         }
822         else if (key == "wgt-private")
823         {
824                 m_subrootlist[LT_WGTPRV] = key;
825         }
826         else if (key == "wgt-private-tmp")
827         {
828                 m_subrootlist[LT_WGTPRVTMP] = key;
829         }
830 }
831
832 bool Manager::init()
833 {
834         std::vector<StoragePropertiesPtr> storageList;
835         storage_foreach_device_supported(Manager::getSupportedDeviceCB, &storageList);
836
837         for (size_t i = 0; i < storageList.size(); i++) {
838                 m_locations[storageList[i]->getLabel()] = IPath::create(storageList[i]->getFullPath());
839                 m_rootlist[storageList[i]->getLabel()] = storageList[i]->getId();
840         }
841
842         /* for Tizen */
843         setupLocation("downloads", PATH_DOWNLOADS);
844         setupLocation("documents", PATH_DOCUMENTS);
845         setupLocation("music", PATH_SOUNDS);
846         setupLocation("images", PATH_IMAGES);
847         setupLocation("videos", PATH_VIDEOS);
848         setupLocation("ringtones", PATH_RINGTONE);
849
850         m_subrootlist[LT_ROOT] = "internal0";
851         m_subrootlist[LT_SDCARD] = "removable1";
852         m_subrootlist[LT_USBHOST] = "removable2";
853         m_subrootlist[LT_DOWNLOADS] = "downloads";
854         m_subrootlist[LT_DOCUMENTS] = "documents";
855         m_subrootlist[LT_SOUNDS] = "music";
856         m_subrootlist[LT_IMAGES] = "images";
857         m_subrootlist[LT_VIDEOS] = "videos";
858         m_subrootlist[LT_RINGTONES] = "ringtones";
859         return true;
860
861 }
862
863 void Manager::setupLocation(std::string location, const char* path)
864 {
865     if (!nodeExists(path)) {
866         try {
867             makePath(path, 0755);
868         }
869         catch (const Commons::Exception& ex) 
870         {
871             LoggerE("Exception: " << ex.GetMessage());
872             return;
873         }
874     }
875     m_locations[location] = IPath::create(path);
876 }
877
878 void Manager::Watcher::getCurrentStoargeStateForWatch()
879 {
880         std::string label("");
881         storage_type_e type;
882         storage_state_e state;
883         RootList::const_iterator it = m_rootlist.begin();
884         for (; it != m_rootlist.end(); ++it) {
885                 label = it->first;
886                 if (label.compare("") != 0) {
887                         StoragePropertiesPtr storageItem(new StorageProperties());
888
889                         if (storage_get_type(it->second, &type) != STORAGE_ERROR_NONE) {
890                                 Throw(Commons::PlatformException);
891                         }
892                         if (storage_get_state(it->second, &state) != STORAGE_ERROR_NONE) {
893                                 Throw(Commons::PlatformException);
894                         }
895
896                         storageItem->setId(it->second);
897                         storageItem->setLabel(it->first);
898                         storageItem->setType(static_cast<short>(type));
899                         storageItem->setState(static_cast<short>(state));
900
901                         EventStorageStateChangedPtr event(new EventStorageStateChanged());
902
903                         event->setResult(storageItem);
904                         emit(event);
905                 }
906         }
907 }
908
909 void Manager::Watcher::StorageStateHasChanged(int storage, storage_state_e state)
910 {
911         StoragePropertiesPtr storageItem(new StorageProperties());
912
913         std::string label;
914         storage_type_e type;
915
916         RootList::const_iterator it = m_rootlist.begin();
917         for (; it != m_rootlist.end(); ++it) {
918                 if (it->second == storage) {
919                         label = it->first;
920                         break;
921                 }
922         }
923
924         if (storage_get_type(storage, &type) != STORAGE_ERROR_NONE) {
925                 Throw(Commons::PlatformException);
926         }
927
928         if (label.compare("") != 0) {
929                 storageItem->setId(storage);
930                 storageItem->setLabel(label);
931                 storageItem->setType(static_cast<short>(type));
932                 storageItem->setState(static_cast<short>(state));
933
934                 EventStorageStateChangedPtr event(new EventStorageStateChanged());
935
936                 event->setResult(storageItem);
937                 emit(event);
938         }
939 }
940
941 }
942 }