merge with master
[platform/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/log/log.h>
24
25 #include <fts.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 namespace {
30
31 bool removeRecusive(const char * path)
32 {
33     FTS *fts;
34     FTSENT *ftsent;
35     bool rv = true;
36     char * const paths[] = { const_cast<char * const>(path), NULL };
37     if ((fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR, NULL)) == NULL) {
38         return false;
39     }
40     while ((ftsent = fts_read(fts)) != NULL) {
41         switch (ftsent->fts_info) {
42         case FTS_D:
43             break;
44         case FTS_DP:
45             if (rmdir(ftsent->fts_accpath) != 0) {
46                 rv = false;
47             }
48             break;
49         case FTS_DC:
50         case FTS_F:
51         case FTS_NSOK:
52         case FTS_SL:
53         case FTS_SLNONE:
54         case FTS_DEFAULT:
55             if (unlink(ftsent->fts_accpath) != 0) {
56                 rv = false;
57             }
58             break;
59         case FTS_NS:
60             rv = false;
61             break;
62         case FTS_DOT:
63         case FTS_DNR:
64         case FTS_ERR:
65         default:
66             rv = false;
67             break;
68         }
69     }
70     if (fts_close(fts) == -1) {
71         rv = false;
72     }
73     return rv;
74 }
75
76 }
77
78 namespace DPL {
79
80 ScopedDirPolicy::Type ScopedDirPolicy::NullValue()
81 {
82     return std::string();
83 }
84
85 void ScopedDirPolicy::Destroy(Type str)
86 {
87     if(!str.empty())
88     {
89         bool status = removeRecusive(str.c_str());
90         if(!status)
91         {
92             LogError("Error while removing recursively: " << str);
93         }
94     }
95 }
96
97 ScopedDir::ScopedDir(const std::string & str, mode_t mode) : BaseType(str)
98 {
99     if(!str.empty())
100     {
101         mkdir(str.c_str(), mode);
102     }
103 }
104
105 } // namespace DPL
106