common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / systemservice / logger_service / server / src / ss_logger_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 /// \ingroup  tag_SS_LoggerService
19 /// \brief    This file contains declaration of class CFSDirectory.
20 ///
21 ///////////////////////////////////////////////////////////////////////////////
22 #include "ss_logger_fs_directory.h"
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <string>
30 #include "loggerservicedebug_loggerservicelog.h"
31 #include "ss_logger_types.h"
32 #include "ss_logger_util.h"
33
34 ////////////////////////////////////////////////////////////////////////////////////////////////////
35 /// CFSDirectory
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
40 }
41 // LCOV_EXCL_STOP
42
43 ////////////////////////////////////////////////////////////////////////////////////////////////////
44 /// CFSDirectory
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
49 }
50 // LCOV_EXCL_STOP
51
52 ////////////////////////////////////////////////////////////////////////////////////////////////////
53 /// CreateDirectory
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;
59
60   if (!f_cDirPath.empty()) {  // LCOV_EXCL_BR_LINE 6: f_cDirPath is aways not empty
61     PSTR l_cTempDirPath;
62     PSTR l_cParsedDirPath;
63     PCSTR l_cCopypath = f_cDirPath.c_str();
64
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';
71         if (0
72             != mkdir(l_cCopypath,
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",
76                    l_cCopypath);
77             l_eStatus = eFrameworkunifiedStatusFail;
78           } else {
79             // file already exist
80           }
81         }
82         *l_cParsedDirPath = '/';
83       }
84       l_cTempDirPath = l_cParsedDirPath + 1;
85     }
86     if (eFrameworkunifiedStatusOK == l_eStatus) {
87       if (0
88           != mkdir(l_cCopypath,
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",
92                  l_cCopypath);
93           l_eStatus = eFrameworkunifiedStatusFail;
94         } else {
95           // file already exist
96         }
97       }
98     }
99   } else {
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;
104     // LCOV_EXCL_STOP
105   }
106   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
107   return l_eStatus;
108 }
109
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);
118     return TRUE;
119   }
120   return FALSE;
121 }
122
123 ////////////////////////////////////////////////////////////////////////////////////////////////////
124 /// IsDirectory
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;
130
131   if (!f_cPath.empty()) {  // LCOV_EXCL_BR_LINE 6: f_cPath can not be empty
132     struct stat st_buf;
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(),
138              errno);
139       FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
140       l_bReturn = FALSE;
141       return l_bReturn;
142       // LCOV_EXCL_STOP
143     }
144
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
148     }
149     if (S_ISDIR(st_buf.st_mode)) {
150       l_bReturn = TRUE;  // return true if f_cPath is a directory
151     }
152   } else {
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.");
156     l_bReturn = FALSE;
157     // LCOV_EXCL_STOP
158   }
159   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
160   return l_bReturn;
161 }
162
163 ////////////////////////////////////////////////////////////////////////////////////////////////////
164 /// RemoveDirectory
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;
170
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
174       l_bReturn = TRUE;
175     }
176   }
177   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
178   return l_bReturn;
179 }
180
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;
188
189   if (!f_cPath.empty()) {  // LCOV_EXCL_BR_LINE 6: f_cPath can not be empty
190     std::string l_cFilePath = "";
191
192     struct dirent l_Dirent;
193     struct dirent* next;
194     DIR *l_pDir = NULL;
195
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]) {
199         f_cPath.append("/");
200       }
201
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
208
209           if (TRUE == IsDirectory(l_cFilePath)) {
210             l_bReturn = RemoveDirectory(l_cFilePath);
211           } else {
212             // it's a file, we can use unlink
213             if (unlink(l_cFilePath.c_str()) == -1) {
214               l_bReturn = FALSE;
215             }
216           }
217         }
218       }
219       closedir(l_pDir);  // close the directory
220       CLoggerUtil::SyncDir(f_cPath);
221     } else {
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");
225       // LCOV_EXCL_STOP
226     }
227   } else {
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.");
231     l_bReturn = FALSE;
232     // LCOV_EXCL_STOP
233   }
234   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
235   return l_bReturn;
236 }