2 * Copyright (c) 2013-2017 Samsung Electronics Co., Ltd All Rights Reserved
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.
26 #include <sys/types.h>
32 pthread_mutex_t delLock = PTHREAD_MUTEX_INITIALIZER;
33 pthread_mutex_t createLock = PTHREAD_MUTEX_INITIALIZER;
37 int file_op::write_file(const char* filename, unsigned char* buffer,
39 SLOGI("entering file_op::write_file");
41 return SS_RET_INVALID_PARAM;
46 if (is_valid_filename(filename)) {
47 SLOGD("[%s][%d] NOT valid file : [ %s ]", __FUNCTION__, __LINE__, filename);
48 return SS_RET_INVALID_PARAM;
51 // create folder if not exist.
52 char base_path[255] = {0};
53 get_base_path(filename, base_path);
54 SLOGI("base_path %s.", base_path);
55 if (!is_folder_exists(base_path)) {
56 if (0 != create_folder(base_path)) {
62 file = fopen(filename, "wb");
64 SLOGD("[%s][%d] fopen(..) == NULL , Can't open [ %s ] file. ", __FUNCTION__,
66 return SS_RET_CANT_FIND_REQUESTED_DATA;
69 if (size != fwrite(buffer, 1, size, file)) {
70 SLOGD("[%s][%d]!!!DIFFERENT!!!", __FUNCTION__, __LINE__);
77 //sync(fileno(file)); // sync blocked
81 return SS_RET_SUCCESS;
84 int file_op::read_file(const char* filename, unsigned char** buffer,
86 SLOGI("Entering file_op::read_file...");
92 if (is_valid_filename(filename)) {
93 SLOGD("[%s][%d] NOT valid file : [ %s ]", __FUNCTION__, __LINE__, filename);
94 return SS_RET_INVALID_PARAM;
98 file = fopen(filename, "rb");
100 SLOGD("[%s][%d] fopen(..) == NULL , Can't open [ %s ] file. ", __FUNCTION__,
102 return SS_RET_CANT_FIND_REQUESTED_DATA;
105 /* computing file size */
106 if(fseek(file, 0, SEEK_END) != 0){
114 if( fseek(file, 0, SEEK_SET) != 0){
119 /* allocating data buffer with enough size */
120 *buffer = (unsigned char *)OsaMalloc(size);
122 SLOGD("[%s][%d] Can't malloc pBuffer to 'fread'. ", __FUNCTION__, __LINE__);
124 return SS_RET_MALLOC_FAILED;
127 /* reading data from file */
128 if (size != fread(*buffer, size, 1, file)) {
129 SLOGD("[%s][%d] Read size is not equal to required size %d.", __FUNCTION__,
136 SLOGI("[%s][%d] File read successfully", __FUNCTION__, __LINE__);
137 return SS_RET_SUCCESS;
140 int file_op::remove_file(const char* filename) {
141 SLOGI("[%s][%d] Entering file_op::remove_file", __FUNCTION__, __LINE__);
142 pthread_mutex_lock(&delLock);
143 bool ret = is_file_exists(filename);
145 ret = remove(filename);
147 SLOGD("[%s][%d] remove , nRet : %d , file [ %s ] not removed",
148 __FUNCTION__, __LINE__, ret, filename);
149 pthread_mutex_unlock(&delLock);
153 SLOGD("[%s][%d] access not ok , nRet : %d , There's NO file [ %s ] ",
154 __FUNCTION__, __LINE__, ret, filename);
155 pthread_mutex_unlock(&delLock);
156 return SS_RET_CANT_FIND_REQUESTED_DATA;
158 pthread_mutex_unlock(&delLock);
159 SLOGI("[%s][%d] Succeed to remove file...", __FUNCTION__, __LINE__);
161 return SS_RET_SUCCESS;
164 int file_op::is_valid_filename(const char* filename) {
165 // path shall not contain any "strange" characters
166 const char* validated_path = strpbrk(filename, "&;`'\"|*?~<>^()[]{}$\n \r");
167 if (validated_path != NULL) {
169 "[%s][%d] There are some forbidden charactors in path.(&;`'\"|*?~<>^()[]{}$\n \r) ",
170 __FUNCTION__, __LINE__);
171 return SS_RET_INVALID_PARAM;
174 return SS_RET_SUCCESS;
177 int file_op::create_folder(const char* folder) {
178 SLOGI("[%s][%d] START", __FUNCTION__, __LINE__);
180 char base_p[256] = {0};
181 get_base_path(folder, base_p);
182 if (!is_folder_exists(base_p)) {
183 if (0 != create_folder(base_p)) {
184 SLOGE("Failed to create folder %s.", base_p);
189 uint32_t result = SS_RET_SUCCESS;
191 memset(&st, 0, sizeof(st));
192 pthread_mutex_lock(&createLock);
193 int res = stat(folder, &st);
196 res = mkdir(folder, S_IRWXU);
198 result = SS_RET_FAIL;
199 SLOGE("Failed to create folder %s.", folder);
202 } else if (!(S_ISDIR(st.st_mode))) {
203 result = SS_RET_FAIL;
205 pthread_mutex_unlock(&createLock);
207 SLOGI("[%s][%d] END", __FUNCTION__, __LINE__);
212 int file_op::remove_folder(const char* folder) {
213 SLOGI("[%s][%d] START", __FUNCTION__, __LINE__);
217 SLOGI("[%s][%d] END", __FUNCTION__, __LINE__);
218 return SS_RET_SUCCESS;
221 bool file_op::is_folder_exists(const char* folder) {
222 return is_file_exists(folder);
225 bool file_op::is_file_exists(const char* file) {
227 memset(&st, 0, sizeof(st));
228 int res = stat(file, &st);
232 void file_op::get_base_path(const char* filename, char* base_path) {
233 for (int i = strlen(filename) - 1; i >= 0; --i) {
234 if ('/' == filename[i]) {
235 memcpy(base_path, filename, i);