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_NS_UtilityCenter
19 /// \brief This file contains API implementation for handling file and folder functionalities.
22 //////////////////////////////////////////////////////////////////////////////////////////////////
24 #include <native_service/ns_util_directory.h>
28 #include <sys/types.h>
34 ////////////////////////////////////////////////////////////////////////////////////////////////
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;
42 struct dirent *l_pt_dirent = NULL;
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);
52 l_c_file_name = new(std::nothrow) CHAR[l_ui_str_length + 1];
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)));
57 std::strncpy(l_c_file_name, l_pt_dirent->d_name, l_ui_str_length);
59 f_pv_tfile_list->push_back(l_c_file_name); // LCOV_EXCL_BR_LINE 11: not a branch
61 delete[] l_c_file_name; // LCOV_EXCL_BR_LINE 11: not a branch
64 l_e_status = eFrameworkunifiedStatusNullPointer;
70 l_e_status = eFrameworkunifiedStatusFail;
73 l_e_status = eFrameworkunifiedStatusInvldParam;
80 ////////////////////////////////////////////////////////////////////////////////////////////////
82 /// Method to create a directory.
83 ////////////////////////////////////////////////////////////////////////////////////////////////
84 EFrameworkunifiedStatus CreateDirectory(std::string f_c_dir_path) {
85 EFrameworkunifiedStatus l_e_status = eFrameworkunifiedStatusOK;
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();
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) {
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;
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;
109 *l_c_parsed_dir_path = '/';
111 l_c_temp_dir_path = l_c_parsed_dir_path + 1;
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;
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;
126 l_e_status = eFrameworkunifiedStatusFail;
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;
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;
143 return l_b_directory_status;