Re-organized sub-directory by category
[staging/basesystem.git] / service / native / notification_persistent_service / server / src / ns_npp_fs_directory.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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 /// \defgroup <<Group Tag>> <<Group Name>>
19 /// \ingroup  tag_NS_NPPService
20 ///
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 /// \ingroup  tag_NS_NPPService
25 /// \brief    This file contains the implementation for class CFSDirectory.
26 ///
27 ////////////////////////////////////////////////////////////////////////////////////////////////////
28
29 #ifdef AGL_STUB
30 #include <other_service/PosixBasedOS001TimeApi.h>
31 #endif
32
33 #include <stdio.h>
34 #include <dirent.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39
40 #ifdef AGL_STUB
41 #include <cstring>
42 #endif
43 #include <string>
44
45 #include "ns_npp_notificationpersistentservicelog.h"
46 #include "ns_npp_types.h"
47 #include "ns_npp_fs_directory.h"
48
49 ////////////////////////////////////////////////////////////////////////////////////////////////////
50 /// CFSDirectory
51 /// Constructor of CFSDirectory class
52 ////////////////////////////////////////////////////////////////////////////////////////////////////
53 CFSDirectory::CFSDirectory() {  // LCOV_EXCL_START 8: all methods are static
54   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
55 }
56 // LCOV_EXCL_STOP
57
58 ////////////////////////////////////////////////////////////////////////////////////////////////////
59 /// CFSDirectory
60 /// Destructor of CFSDirectory class
61 ////////////////////////////////////////////////////////////////////////////////////////////////////
62 CFSDirectory::~CFSDirectory() {   // LCOV_EXCL_START 8: all methods are static
63   AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
64 }
65 // LCOV_EXCL_STOP
66
67 ////////////////////////////////////////////////////////////////////////////////////////////////////
68 /// CreateDirectory
69 /// Method to create a directory.
70 ////////////////////////////////////////////////////////////////////////////////////////////////////
71 EFrameworkunifiedStatus CFSDirectory::CreateDirectory(const std::string &f_cdirpath) {
72   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
73   EFrameworkunifiedStatus   l_estatus = eFrameworkunifiedStatusOK;
74
75   if (!f_cdirpath.empty()) {
76     PSTR     l_cTempDirPath;
77     PSTR     l_cParsedDirPath;
78     PCSTR      l_cCopypath = f_cdirpath.c_str();
79
80     l_cTempDirPath =  const_cast<PSTR >(f_cdirpath.c_str());
81     while (l_estatus == eFrameworkunifiedStatusOK && (l_cParsedDirPath = std::strchr(l_cTempDirPath, '/')) != 0) {
82       if (l_cParsedDirPath != l_cTempDirPath) {
83         /* Neither root nor double slash in path */
84         *l_cParsedDirPath = '\0';
85         if (0 != mkdir(l_cCopypath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {  // LCOV_EXCL_BR_LINE 5: mkdir's error case  // NOLINT[whitespace/line_length]
86           if (EEXIST != errno) {  // LCOV_EXCL_BR_LINE 5: errno can't be changed
87             // LCOV_EXCL_START 5: errno can't be changed
88             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
89             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error Cannot Create directory %s", l_cCopypath);
90             l_estatus = eFrameworkunifiedStatusFail;
91             // LCOV_EXCL_STOP
92           } else {
93             // file already exist
94           }
95         }
96         *l_cParsedDirPath = '/';
97       }
98       l_cTempDirPath = l_cParsedDirPath + 1;
99     }
100     if (eFrameworkunifiedStatusOK == l_estatus) {  // LCOV_EXCL_BR_LINE 200: l_estatus must be eFrameworkunifiedStatusOK
101       if (0 != mkdir(l_cCopypath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {  // LCOV_EXCL_BR_LINE 5: mkdir's error case  // NOLINT[whitespace/line_length]
102         if (EEXIST != errno) {  // LCOV_EXCL_BR_LINE 5: errno can't be changed
103           // LCOV_EXCL_START 5: errno can't be changed
104           AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
105           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error Cannot Create directory %s", l_cCopypath);
106           l_estatus = eFrameworkunifiedStatusFail;
107           // LCOV_EXCL_STOP
108         } else {
109           // file already exist
110         }
111       }
112     }
113   } else {
114     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Directory path is empty.");
115     l_estatus = eFrameworkunifiedStatusFail;
116   }
117   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
118   return l_estatus;
119 }
120
121 ////////////////////////////////////////////////////////////////////////////////////////////////////
122 /// DoesDirecotryExist
123 /// Method to check if a directory exists.
124 ////////////////////////////////////////////////////////////////////////////////////////////////////
125 BOOL CFSDirectory::DoesDirecotryExist(const std::string &f_cdirpath) {
126   DIR *l_pDirDescriptor = opendir(f_cdirpath.c_str());
127   if (NULL != l_pDirDescriptor) {
128     closedir(l_pDirDescriptor);
129     return TRUE;
130   }
131   return FALSE;
132 }
133
134 ////////////////////////////////////////////////////////////////////////////////////////////////////
135 /// IsDirectory
136 /// Method to check if the entity is a directory.
137 ////////////////////////////////////////////////////////////////////////////////////////////////////
138 BOOL CFSDirectory::IsDirectory(const std::string &f_cpath) {
139   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
140   BOOL l_bReturn = FALSE;
141
142   if (!f_cpath.empty()) {  // LCOV_EXCL_BR_LINE 200: f_cpath can't be empty
143     struct stat st_buf;
144
145     if (0 == stat(f_cpath.c_str(), &st_buf)) {  // LCOV_EXCL_BR_LINE 5: stat's error case
146       // Get the status of the file
147       if (S_ISREG(st_buf.st_mode)) {
148         l_bReturn = FALSE;  // return false if f_cpath is a regular file
149       }
150       if (S_ISDIR(st_buf.st_mode)) {
151         l_bReturn = TRUE;  // return true if f_cpath is a directory
152       }
153     } else {
154       AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
155       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error retrieving status of path : %s", f_cpath.c_str());  // LCOV_EXCL_LINE 5: stat's error case  // NOLINT[whitespace/line_length]
156     }
157   } else {
158     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
159     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Directory path is empty.");  // LCOV_EXCL_LINE 200: f_cpath can't be empty
160   }
161   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
162   return l_bReturn;
163 }
164
165 ////////////////////////////////////////////////////////////////////////////////////////////////////
166 /// RemoveDirectory
167 /// Method to remove a directory.
168 ////////////////////////////////////////////////////////////////////////////////////////////////////
169 BOOL CFSDirectory::RemoveDirectory(std::string f_cpath, const UI_32 f_uidelay) {
170   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
171   BOOL l_bReturn = FALSE;
172
173   if (!f_cpath.empty()) {  // LCOV_EXCL_BR_LINE 200: f_cpath can't be empty.
174     std::string l_cFilePath = "";
175
176     struct dirent *l_pdirent = NULL;
177     DIR *l_pdir = NULL;
178
179     l_pdir = opendir(f_cpath.c_str());  // LCOV_EXCL_BR_LINE 11:unexpected branch
180     if (NULL != l_pdir) {
181       if ('/' != f_cpath[f_cpath.length() - 1]) {
182         f_cpath.append("/");
183       }
184
185       while (NULL != (l_pdirent = readdir(l_pdir))) {
186         if (0 != std::strcmp(l_pdirent->d_name, ".") &&
187             0 != std::strcmp(l_pdirent->d_name, "..")) {
188           l_cFilePath.assign(f_cpath);
189           l_cFilePath.append(l_pdirent->d_name);  // concatenate the strings to get the complete f_cpath
190
191           if (TRUE == IsDirectory(l_cFilePath)) {
192             (void)RemoveDirectory(l_cFilePath, f_uidelay);
193           } else {
194             // it's a file, we can use unlink
195             unlink(l_cFilePath.c_str());
196
197             if (0 < f_uidelay) {  // LCOV_EXCL_START 200: f_uidelay is 0
198               AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
199 #ifdef AGL_PosixBasedOS001LEGACY_USED
200               delay(f_uidelay);
201 #endif
202             }
203             // LCOV_EXCL_STOP
204           }
205         }
206       }
207       closedir(l_pdir);  // close the directory
208     } else {
209       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "l_pdir is NULL");
210     }
211
212     // delete the directory
213     if (0 == rmdir(f_cpath.c_str())) {
214       if (0 < f_uidelay) {  // LCOV_EXCL_START 200: f_uidelay is 0
215         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
216 #ifdef AGL_PosixBasedOS001LEGACY_USED
217         delay(f_uidelay);
218 #endif
219       }
220       // LCOV_EXCL_STOP
221       l_bReturn = TRUE;
222     }
223   } else {
224     // LCOV_EXCL_START 200: f_cpath can't be empty.
225     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
226     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Directory path is empty.");
227     l_bReturn = FALSE;
228     // LCOV_EXCL_STOP
229   }
230   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
231   return l_bReturn;
232 }