2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ////////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \defgroup <<Group Tag>> <<Group Name>>
19 /// \ingroup tag_NS_NPPService
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 /// \ingroup tag_NS_NPPService
25 /// \brief This file contains the implementation for class CFSDirectory.
27 ////////////////////////////////////////////////////////////////////////////////////////////////////
30 #include <other_service/PosixBasedOS001TimeApi.h>
37 #include <sys/types.h>
45 #include "ns_npp_notificationpersistentservicelog.h"
46 #include "ns_npp_types.h"
47 #include "ns_npp_fs_directory.h"
49 ////////////////////////////////////////////////////////////////////////////////////////////////////
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
58 ////////////////////////////////////////////////////////////////////////////////////////////////////
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
67 ////////////////////////////////////////////////////////////////////////////////////////////////////
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;
75 if (!f_cdirpath.empty()) {
77 PSTR l_cParsedDirPath;
78 PCSTR l_cCopypath = f_cdirpath.c_str();
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;
96 *l_cParsedDirPath = '/';
98 l_cTempDirPath = l_cParsedDirPath + 1;
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;
109 // file already exist
114 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Directory path is empty.");
115 l_estatus = eFrameworkunifiedStatusFail;
117 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
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);
134 ////////////////////////////////////////////////////////////////////////////////////////////////////
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;
142 if (!f_cpath.empty()) { // LCOV_EXCL_BR_LINE 200: f_cpath can't be empty
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
150 if (S_ISDIR(st_buf.st_mode)) {
151 l_bReturn = TRUE; // return true if f_cpath is a directory
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]
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
161 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
165 ////////////////////////////////////////////////////////////////////////////////////////////////////
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;
173 if (!f_cpath.empty()) { // LCOV_EXCL_BR_LINE 200: f_cpath can't be empty.
174 std::string l_cFilePath = "";
176 struct dirent *l_pdirent = NULL;
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]) {
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
191 if (TRUE == IsDirectory(l_cFilePath)) {
192 (void)RemoveDirectory(l_cFilePath, f_uidelay);
194 // it's a file, we can use unlink
195 unlink(l_cFilePath.c_str());
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
207 closedir(l_pdir); // close the directory
209 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "l_pdir is NULL");
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
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.");
230 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");