Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules / core / src / scoped_dir.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file        scoped_dir.cpp
18  * @author      Iwanek Tomasz (t.iwanek@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation scoped directory
21  */
22 #include <dpl/scoped_dir.h>
23 #include <dpl/errno_string.h>
24 #include <dpl/log/wrt_log.h>
25
26 #include <fts.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 namespace {
31
32 bool removeRecusive(const char * path)
33 {
34     FTS *fts;
35     FTSENT *ftsent;
36     bool rv = true;
37     char * const paths[] = { const_cast<char * const>(path), NULL };
38     if ((fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR, NULL)) == NULL) {
39         return false;
40     }
41     while ((ftsent = fts_read(fts)) != NULL) {
42         switch (ftsent->fts_info) {
43         case FTS_D:
44             break;
45         case FTS_DP:
46             if (rmdir(ftsent->fts_accpath) != 0) {
47                 rv = false;
48             }
49             break;
50         case FTS_DC:
51         case FTS_F:
52         case FTS_NSOK:
53         case FTS_SL:
54         case FTS_SLNONE:
55         case FTS_DEFAULT:
56             if (unlink(ftsent->fts_accpath) != 0) {
57                 rv = false;
58             }
59             break;
60         case FTS_NS:
61             rv = false;
62             break;
63         case FTS_DOT:
64         case FTS_DNR:
65         case FTS_ERR:
66         default:
67             rv = false;
68             break;
69         }
70     }
71     if (fts_close(fts) == -1) {
72         rv = false;
73     }
74     return rv;
75 }
76
77 }
78
79 namespace DPL {
80
81 ScopedDirPolicy::Type ScopedDirPolicy::NullValue()
82 {
83     return std::string();
84 }
85
86 void ScopedDirPolicy::Destroy(Type str)
87 {
88     if(!str.empty())
89     {
90         bool status = removeRecusive(str.c_str());
91         if(!status)
92         {
93             WrtLogE("Error while removing recursively: %s", str.c_str());
94         }
95     }
96 }
97
98 ScopedDir::ScopedDir(const std::string & str, mode_t mode) : BaseType(str)
99 {
100     if(!str.empty())
101     {
102         if (mkdir(str.c_str(), mode) == -1)
103         {
104             std::string errstr = DPL::GetErrnoString();
105             WrtLogE("Error while creating directory: %s [%s]",
106                 str.c_str(), errstr.c_str());
107         }
108     }
109 }
110
111 } // namespace DPL
112