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 /// \ingroup tag_SS_LoggerService
19 /// \brief This file contains declaration of class CFSDirectory.
21 ///////////////////////////////////////////////////////////////////////////////
22 #include "ss_logger_fs_directory.h"
24 #include <sys/types.h>
30 #include "loggerservicedebug_loggerservicelog.h"
31 #include "ss_logger_types.h"
32 #include "ss_logger_util.h"
34 ////////////////////////////////////////////////////////////////////////////////////////////////////
36 /// Constructor of CFSDirectory class
37 ////////////////////////////////////////////////////////////////////////////////////////////////////
38 CFSDirectory::CFSDirectory() { // LCOV_EXCL_START 14:static instance
39 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
43 ////////////////////////////////////////////////////////////////////////////////////////////////////
45 /// Destructor of CFSDirectory class
46 ////////////////////////////////////////////////////////////////////////////////////////////////////
47 CFSDirectory::~CFSDirectory() { // LCOV_EXCL_START 14:static instance
48 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
52 ////////////////////////////////////////////////////////////////////////////////////////////////////
54 /// Method to create a directory.
55 ////////////////////////////////////////////////////////////////////////////////////////////////////
56 EFrameworkunifiedStatus CFSDirectory::CreateDirectory(std::string &f_cDirPath) {
57 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
58 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
60 if (!f_cDirPath.empty()) { // LCOV_EXCL_BR_LINE 6: f_cDirPath is aways not empty
62 PSTR l_cParsedDirPath;
63 PCSTR l_cCopypath = f_cDirPath.c_str();
65 l_cTempDirPath = const_cast<PSTR>(f_cDirPath.c_str());
66 while (l_eStatus == eFrameworkunifiedStatusOK
67 && (l_cParsedDirPath = std::strchr(l_cTempDirPath, '/')) != 0) {
68 if (l_cParsedDirPath != l_cTempDirPath) {
69 /* Neither root nor double slash in path */
70 *l_cParsedDirPath = '\0';
73 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
74 if (EEXIST != errno) {
75 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error Cannot Create directory %s",
77 l_eStatus = eFrameworkunifiedStatusFail;
82 *l_cParsedDirPath = '/';
84 l_cTempDirPath = l_cParsedDirPath + 1;
86 if (eFrameworkunifiedStatusOK == l_eStatus) {
89 S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
90 if (EEXIST != errno) {
91 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Error Cannot Create directory %s",
93 l_eStatus = eFrameworkunifiedStatusFail;
100 // LCOV_EXCL_START 6: f_cDirPath is aways not empty
101 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
102 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "String Empty.");
103 l_eStatus = eFrameworkunifiedStatusFail;
106 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
110 ////////////////////////////////////////////////////////////////////////////////////////////////////
111 /// DoesDirectoryExist
112 /// Method to check if a directory exists.
113 ////////////////////////////////////////////////////////////////////////////////////////////////////
114 BOOL CFSDirectory::DoesDirectoryExist(std::string &f_cDirPath) {
115 DIR* l_pDirDescriptor = opendir(f_cDirPath.c_str());
116 if (NULL != l_pDirDescriptor) {
117 closedir(l_pDirDescriptor);
123 ////////////////////////////////////////////////////////////////////////////////////////////////////
125 /// Method to check if the entity is a directory.
126 ////////////////////////////////////////////////////////////////////////////////////////////////////
127 BOOL CFSDirectory::IsDirectory(std::string &f_cPath) {
128 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
129 BOOL l_bReturn = FALSE;
131 if (!f_cPath.empty()) { // LCOV_EXCL_BR_LINE 6: f_cPath can not be empty
133 if (-1 == stat(f_cPath.c_str(), &st_buf)) { // LCOV_EXCL_BR_LINE 5: c code.
134 // LCOV_EXCL_START 5: c code.
135 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
136 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
137 "Error: stat failed for path/file %s, errno %d", f_cPath.c_str(),
139 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
145 // Get the status of the file
146 if (S_ISREG(st_buf.st_mode)) {
147 l_bReturn = FALSE; // return false if f_cPath is a regular file
149 if (S_ISDIR(st_buf.st_mode)) {
150 l_bReturn = TRUE; // return true if f_cPath is a directory
153 // LCOV_EXCL_START 6: f_cPath can not be empty
154 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
155 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Pathname empty.");
159 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
163 ////////////////////////////////////////////////////////////////////////////////////////////////////
165 /// Method to remove a directory.
166 ////////////////////////////////////////////////////////////////////////////////////////////////////
167 BOOL CFSDirectory::RemoveDirectory(std::string &f_cPath) {
168 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
169 BOOL l_bReturn = FALSE;
171 if (RemoveSubDirectory(f_cPath)) {
172 // delete the parent directory
173 if (0 == rmdir(f_cPath.c_str())) { // LCOV_EXCL_BR_LINE 5: c code error case
177 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
181 ////////////////////////////////////////////////////////////////////////////////////////////////////
182 /// RemoveSubDirectory
183 /// Method to remove a sub directory.
184 ////////////////////////////////////////////////////////////////////////////////////////////////////
185 BOOL CFSDirectory::RemoveSubDirectory(std::string &f_cPath) {
186 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
187 BOOL l_bReturn = TRUE;
189 if (!f_cPath.empty()) { // LCOV_EXCL_BR_LINE 6: f_cPath can not be empty
190 std::string l_cFilePath = "";
192 struct dirent l_Dirent;
196 l_pDir = opendir(f_cPath.c_str());
197 if (NULL != l_pDir) { // LCOV_EXCL_BR_LINE 5: c code.
198 if ('/' != f_cPath[f_cPath.length() - 1]) {
202 while (0 == readdir_r(l_pDir, &l_Dirent, &next) && next != NULL) {
203 if (0 != std::strcmp(l_Dirent.d_name, ".")
204 && 0 != std::strcmp(l_Dirent.d_name, "..")
205 && 0 != std::strcmp(l_Dirent.d_name, "lost+found")) {
206 l_cFilePath.assign(f_cPath);
207 l_cFilePath.append(l_Dirent.d_name); // concatenate the strings to get the complete f_cPath
209 if (TRUE == IsDirectory(l_cFilePath)) {
210 l_bReturn = RemoveDirectory(l_cFilePath);
212 // it's a file, we can use unlink
213 if (unlink(l_cFilePath.c_str()) == -1) {
219 closedir(l_pDir); // close the directory
220 CLoggerUtil::SyncDir(f_cPath);
222 // LCOV_EXCL_START 5: c code.
223 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
224 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "l_pDir is NULL");
228 // LCOV_EXCL_START 6: f_cPath can not be empty
229 AGL_ASSERT_NOT_TESTED(); // LCOV_EXCL_LINE 200: test assert
230 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Pathname empty.");
234 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");