Init basesystem source codes.
[staging/basesystem.git] / nsframework / framework_unified / client / NS_UtilityCenter / src / ns_util_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_NS_UtilityCenter
19 /// \brief   This file contains API implementation for handling file and folder functionalities.
20 ///
21 ///
22 //////////////////////////////////////////////////////////////////////////////////////////////////
23
24 #include <native_service/ns_util_directory.h>
25
26 #include <dirent.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31 #include <cstring>
32 #include <string>
33
34 ////////////////////////////////////////////////////////////////////////////////////////////////
35 /// GetFileList
36 /// Get list of files present in a specific directory
37 ////////////////////////////////////////////////////////////////////////////////////////////////
38 EFrameworkunifiedStatus GetFileList(TFileList *f_pv_tfile_list, PCSTR f_pc_path) {
39   EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
40
41   DIR *l_p_dir = NULL;
42   struct dirent *l_pt_dirent = NULL;
43
44   if (NULL != f_pc_path && NULL != f_pv_tfile_list) {
45     if (NULL != (l_p_dir  = opendir(f_pc_path))) {
46       PCHAR l_c_file_name = NULL;
47       size_t l_ui_str_length = 0;
48       while (NULL != (l_pt_dirent = readdir(l_p_dir))) {  // LCOV_EXCL_BR_LINE 11: not a branch
49         if (!((std::strcmp(l_pt_dirent->d_name, ".") == 0) || (std::strcmp(l_pt_dirent->d_name, "..") == 0))) {
50           l_ui_str_length = std::strlen(l_pt_dirent->d_name);
51
52           l_c_file_name = new(std::nothrow) CHAR[l_ui_str_length + 1];
53
54           if (NULL != l_c_file_name) {  // LCOV_EXCL_BR_LINE 11: new's error case(c++)
55             std::memset(l_c_file_name, 0, (sizeof(CHAR) * (l_ui_str_length + 1)));
56
57             std::strncpy(l_c_file_name, l_pt_dirent->d_name, l_ui_str_length);
58
59             f_pv_tfile_list->push_back(l_c_file_name);  // LCOV_EXCL_BR_LINE 11: not a branch
60
61             delete[] l_c_file_name;  // LCOV_EXCL_BR_LINE 11: not a branch
62             l_c_file_name = NULL;
63           } else {
64             l_e_status = eFrameworkunifiedStatusNullPointer;
65           }
66         }
67       }
68       closedir(l_p_dir);
69     } else {
70       l_e_status = eFrameworkunifiedStatusFail;
71     }
72   } else {
73     l_e_status = eFrameworkunifiedStatusInvldParam;
74   }
75
76   return l_e_status;
77 }
78
79
80 ////////////////////////////////////////////////////////////////////////////////////////////////
81 /// CreateDirectory
82 /// Method to create a directory.
83 ////////////////////////////////////////////////////////////////////////////////////////////////
84 EFrameworkunifiedStatus CreateDirectory(std::string f_c_dir_path) {
85   EFrameworkunifiedStatus   l_e_status = eFrameworkunifiedStatusOK;
86
87   if (!f_c_dir_path.empty()) {
88     PSTR     l_c_temp_dir_path;
89     PSTR     l_c_parsed_dir_path;
90     PCSTR    l_c_copy_path = f_c_dir_path.c_str();
91
92     l_c_temp_dir_path =  const_cast<PSTR >(f_c_dir_path.c_str());
93     // LCOV_EXCL_BR_START 11: not a branch
94     while (l_e_status == eFrameworkunifiedStatusOK && (l_c_parsed_dir_path = std::strchr(l_c_temp_dir_path, '/')) != 0) {
95     // LCOV_EXCL_BR_STOP
96       if (l_c_parsed_dir_path != l_c_temp_dir_path) {
97         /* Neither root nor double slash in path */
98         *l_c_parsed_dir_path = '\0';
99         if (0 != mkdir(l_c_copy_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
100           if (EEXIST != errno) {  // LCOV_EXCL_BR_LINE 11: not a branch
101             l_e_status = eFrameworkunifiedStatusFail;
102           } else {
103             // file already exist
104             if (TRUE != DoesDirecotryExist(f_c_dir_path)) {  // LCOV_EXCL_BR_LINE 5: DoesDirecotryExist's error case
105               l_e_status = eFrameworkunifiedStatusFail;
106             }
107           }
108         }
109         *l_c_parsed_dir_path = '/';
110       }
111       l_c_temp_dir_path = l_c_parsed_dir_path + 1;
112     }
113     if (eFrameworkunifiedStatusOK == l_e_status) {  // LCOV_EXCL_BR_LINE 11: not a branch
114       if (0 != mkdir(l_c_copy_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
115         if (EEXIST != errno) {  // LCOV_EXCL_BR_LINE 11: not a branch
116           l_e_status = eFrameworkunifiedStatusFail;
117         } else {
118           // file already exist
119           if (TRUE != DoesDirecotryExist(f_c_dir_path)) {  // LCOV_EXCL_BR_LINE 5: DoesDirecotryExist's error case
120             l_e_status = eFrameworkunifiedStatusFail;
121           }
122         }
123       }
124     }
125   } else {
126     l_e_status = eFrameworkunifiedStatusFail;
127   }
128   return l_e_status;
129 }
130
131 ////////////////////////////////////////////////////////////////////////////////////////////////////
132 /// DoesDirecotryExist
133 /// Method to check if a directory exists.
134 ////////////////////////////////////////////////////////////////////////////////////////////////////
135 BOOL DoesDirecotryExist(std::string f_c_dir_path) {
136   BOOL l_b_directory_status = FALSE;
137
138   DIR *l_p_dir_descriptor = opendir(f_c_dir_path.c_str());
139   if (NULL != l_p_dir_descriptor) {
140     closedir(l_p_dir_descriptor);
141     l_b_directory_status =  TRUE;
142   }
143   return l_b_directory_status;
144 }
145