2 * Copyright (c) 2012 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.
21 #include "download-agent-client-mgr.h"
22 #include "download-agent-debug.h"
23 #include "download-agent-utils.h"
24 #include "download-agent-dl-mgr.h"
25 #include "download-agent-file.h"
26 #include "download-agent-mime-util.h"
27 #include "download-agent-http-mgr.h"
28 #include "download-agent-plugin-conf.h"
30 #ifdef _ENABLE_OMA_DRM
31 #include "download-agent-plugin-drm.h"
35 #define NO_NAME_TEMP_STR "No name"
37 static da_result_t __set_file_size(stage_info *stage);
38 static da_result_t __saved_file_open(stage_info *stage);
40 static char *__derive_extension(stage_info *stage);
41 static da_result_t __divide_file_name_into_pure_name_N_extesion(
42 const char *in_file_name,
43 char **out_pure_file_name,
44 char **out_extension);
45 static da_result_t __get_candidate_file_name(stage_info *stage,
46 char **out_pure_file_name, char **out_extension);
48 static da_result_t __file_write_buf_make_buf(file_info *file_storage);
49 static da_result_t __file_write_buf_destroy_buf(file_info *file_storage);
50 static da_result_t __file_write_buf_flush_buf(stage_info *stage,
51 file_info *file_storage);
52 static da_result_t __file_write_buf_copy_to_buf(file_info *file_storage,
53 char *body, int body_len);
54 static da_result_t __file_write_buf_directly_write(stage_info *stage,
55 file_info *file_storage, char *body, int body_len);
58 da_result_t create_saved_dir(void)
60 da_result_t ret = DA_RESULT_OK;
61 char *tmp_default_path = DA_NULL;
63 DA_LOG_FUNC_START(FileManager);
65 ret = get_default_install_dir(&tmp_default_path);
66 if (ret != DA_RESULT_OK)
69 if (!is_dir_exist(tmp_default_path)) {
70 ret = create_dir(tmp_default_path);
74 if (tmp_default_path) {
75 free(tmp_default_path);
76 tmp_default_path = DA_NULL;
81 da_result_t clean_files_from_dir(char *dir_path)
83 da_result_t ret = DA_RESULT_OK;
84 struct dirent *d = DA_NULL;
86 char file_path[DA_MAX_FULL_PATH_LEN] = { 0, };
88 DA_LOG_FUNC_START(FileManager);
90 if (dir_path == DA_NULL)
91 return DA_ERR_INVALID_ARGUMENT;
93 if (is_dir_exist(dir_path)) {
94 dir = opendir(dir_path);
96 DA_LOG_ERR(FileManager, "opendir() for %s is failed.", dir_path);
97 ret = DA_ERR_INVALID_INSTALL_PATH;
99 while (DA_NULL != (d = readdir(dir))) {
100 DA_LOG(FileManager, "%s",d->d_name);
101 if (0 == strncmp(d->d_name, ".", strlen("."))
102 || 0 == strncmp(d->d_name,
108 memset(file_path, 0x00, DA_MAX_FULL_PATH_LEN);
109 snprintf(file_path, DA_MAX_FULL_PATH_LEN,
110 "%s/%s", dir_path, d->d_name);
111 if (remove(file_path) < 0) {
112 DA_LOG_ERR(FileManager, "fail to remove file");
117 if (remove(dir_path) < 0) {
118 DA_LOG_ERR(FileManager, "fail to remove dir");
125 /* Priority to obtain MIME Type
126 * 1. HTTP response header's <Content-Type> field
127 * 2. from OMA descriptor file's <content-type> attribute (mandatory field)
128 * 3. Otherwise, leave blank for MIME Type
130 da_result_t get_mime_type(stage_info *stage, char **out_mime_type)
132 char *mime_type = DA_NULL;
134 if (!GET_STAGE_SOURCE_INFO(stage))
135 return DA_ERR_INVALID_ARGUMENT;
138 if (GET_REQUEST_HTTP_HDR_CONT_TYPE(GET_STAGE_TRANSACTION_INFO(stage))) {
139 mime_type = GET_REQUEST_HTTP_HDR_CONT_TYPE(GET_STAGE_TRANSACTION_INFO(stage));
140 DA_LOG(FileManager, "content type from HTTP response header [%s]", mime_type);
144 DA_LOG(FileManager, "no content type derived");
148 /* FIXME really need memory allocation? */
149 *out_mime_type = (char *) calloc(1, strlen(mime_type) + 1);
150 if (*out_mime_type) {
151 snprintf(*out_mime_type, strlen(mime_type) + 1, mime_type);
152 DA_LOG_VERBOSE(FileManager, "out_mime_type str[%s] ptr[%p] len[%d]",
153 *out_mime_type,*out_mime_type,strlen(*out_mime_type));
155 DA_LOG_ERR(FileManager, "fail to allocate memory");
156 return DA_ERR_FAIL_TO_MEMALLOC;
159 DA_LOG(FileManager, "mime type = %s", *out_mime_type);
163 da_bool_t is_file_exist(const char *file_path)
165 struct stat dir_state;
168 if (file_path == DA_NULL) {
169 DA_LOG_ERR(FileManager, "file path is DA_NULL");
173 stat_ret = stat(file_path, &dir_state);
176 if (dir_state.st_mode & S_IFREG) {
177 DA_LOG(FileManager, "Exist! %s is a regular file & its size = %lu", file_path, dir_state.st_size);
187 da_bool_t is_dir_exist(char *file_path)
189 struct stat dir_state;
192 if (file_path == DA_NULL) {
193 DA_LOG_ERR(FileManager, "file path is DA_NULL");
197 stat_ret = stat(file_path, &dir_state);
200 if (dir_state.st_mode & S_IFDIR) {
201 DA_LOG(FileManager, "Exist! %s is a directory.", file_path);
210 void get_file_size(char *file_path, unsigned long long *out_file_size)
212 struct stat dir_state;
217 if (file_path == DA_NULL) {
218 DA_LOG_ERR(FileManager, "file path is DA_NULL");
222 /* Please do not use ftell() to obtain file size, use stat instead.
223 * This is a guide from www.securecoding.cert.org
224 * : FIO19-C. Do not use fseek() and ftell() to compute the size of a file
226 stat_ret = stat(file_path, &dir_state);
228 if (dir_state.st_mode & S_IFREG) {
229 DA_LOG(FileManager, "size = %lu", dir_state.st_size);
230 *out_file_size = dir_state.st_size;
236 da_result_t __saved_file_open(stage_info *stage)
238 da_result_t ret = DA_RESULT_OK;
239 file_info *file_storage = DA_NULL;
240 char *actual_file_path = DA_NULL;
243 DA_LOG_FUNC_START(FileManager);
245 file_storage = GET_STAGE_CONTENT_STORE_INFO(stage);
247 return DA_ERR_INVALID_ARGUMENT;
249 actual_file_path = GET_CONTENT_STORE_ACTUAL_FILE_NAME(file_storage);
250 DA_LOG(FileManager, "actual_file_path = %s", actual_file_path);
251 if (!actual_file_path)
252 return DA_ERR_INVALID_ARGUMENT;
255 fd = fopen(actual_file_path, "a+"); // for resume
257 DA_LOG_ERR(FileManager, "File open failed");
258 ret = DA_ERR_FAIL_TO_ACCESS_FILE;
261 GET_CONTENT_STORE_FILE_HANDLE(file_storage) = fd;
263 DA_LOG(FileManager, "file path for saving = %s",
264 GET_CONTENT_STORE_ACTUAL_FILE_NAME(file_storage));
267 if (DA_RESULT_OK != ret) {
268 GET_CONTENT_STORE_FILE_HANDLE(file_storage) = DA_NULL;
273 da_result_t __set_file_size(stage_info *stage)
275 da_result_t ret = DA_RESULT_OK;
276 req_dl_info *stage_req_info = DA_NULL;
277 file_info *file_storage = DA_NULL;
279 DA_LOG_FUNC_START(FileManager);
282 ret = DA_ERR_INVALID_ARGUMENT;
286 stage_req_info = GET_STAGE_TRANSACTION_INFO(stage);
288 file_storage = GET_STAGE_CONTENT_STORE_INFO(stage);
292 if (GET_REQUEST_HTTP_HDR_CONT_LEN(stage_req_info) != DA_NULL) {
293 GET_CONTENT_STORE_FILE_SIZE(file_storage)
294 = GET_REQUEST_HTTP_HDR_CONT_LEN(stage_req_info);
296 GET_CONTENT_STORE_FILE_SIZE(file_storage) = 0;
298 DA_LOG(FileManager, "file size = %d", GET_CONTENT_STORE_FILE_SIZE(file_storage));
304 /* Priority to derive extension
305 * 1. according to MIME-Type
306 * 2. if MIME-Type is ambiguous or blank,
307 * 2-1. derived from <Content-Disposition> field's "filename" attribute
308 * 2-2. derived from url
309 * 3. if url does not have extension, leave blank for extension
311 char *__derive_extension(stage_info *stage)
316 source_info_t *source_info = GET_STAGE_SOURCE_INFO(stage);
317 req_dl_info *request_info = GET_STAGE_TRANSACTION_INFO(stage);
318 file_info *file_info_data = GET_STAGE_CONTENT_STORE_INFO(stage);
319 char *extension = DA_NULL;
323 char *mime_type = DA_NULL;
324 mime_type = GET_CONTENT_STORE_CONTENT_TYPE(file_info_data);
325 if (mime_type && !is_ambiguous_MIME_Type(mime_type)) {
326 char *extension = DA_NULL;
327 da_result_t ret = get_extension_from_mime_type(mime_type, &extension);
328 if (ret == DA_RESULT_OK && extension)
333 http_msg_response_t *http_msg_response = DA_NULL;
334 http_msg_response = request_info->http_info.http_msg_response;
335 if (http_msg_response) {
336 char *file_name = DA_NULL;
337 da_bool_t b_ret = http_msg_response_get_content_disposition(http_msg_response,
338 DA_NULL, &file_name);
339 if (b_ret && file_name) {
340 char *extension = DA_NULL;
341 DA_LOG(FileManager, "Name from Content-Disposition :[%s]", file_name);
342 __divide_file_name_into_pure_name_N_extesion(file_name, DA_NULL, &extension);
352 /* If there is location url from response header in case of redirection,
353 * it try to parse the extention name from the location url */
354 if (GET_REQUEST_HTTP_REQ_LOCATION(request_info))
355 url = GET_REQUEST_HTTP_REQ_LOCATION(request_info);
357 url = GET_SOURCE_BASIC_URL(source_info);
359 DA_LOG(FileManager, "url:[%s]", url);
360 da_bool_t b_ret = da_get_extension_name_from_url(url, &extension);
361 if (b_ret && extension)
368 /** Priority for deciding file name
369 * 1. what client wants, which is conveyed by DA_FEATURE_FILE_NAME
370 * 2. 'filename' option on HTTP response header's Content-Disposition field
372 * 4. Otherwise, define it as "No name"
374 da_result_t __get_candidate_file_name(stage_info *stage, char **out_pure_file_name, char **out_extension)
376 da_result_t ret = DA_RESULT_OK;
377 source_info_t *source_info = DA_NULL;
378 char *pure_file_name = DA_NULL;
379 char *extension = DA_NULL;
381 DA_LOG_FUNC_START(FileManager);
383 if (!stage || !out_pure_file_name)
384 return DA_ERR_INVALID_ARGUMENT;
386 source_info = GET_STAGE_SOURCE_INFO(stage);
388 return DA_ERR_INVALID_ARGUMENT;
391 if (!pure_file_name && GET_DL_USER_FILE_NAME(GET_STAGE_DL_ID(stage))) {
392 __divide_file_name_into_pure_name_N_extesion(
393 GET_DL_USER_FILE_NAME(GET_STAGE_DL_ID(stage)),
394 &pure_file_name, &extension);
398 if (!pure_file_name) {
399 req_dl_info *request_info = GET_STAGE_TRANSACTION_INFO(stage);
400 http_msg_response_t *http_msg_response = DA_NULL;
401 http_msg_response = request_info->http_info.http_msg_response;
402 if (http_msg_response) {
403 char *file_name = DA_NULL;
404 da_bool_t b_ret = http_msg_response_get_content_disposition(http_msg_response,
405 DA_NULL, &file_name);
406 if (b_ret && file_name) {
407 DA_LOG(FileManager, "Name from Content-Disposition :[%s]", file_name);
408 __divide_file_name_into_pure_name_N_extesion(file_name, &pure_file_name, DA_NULL);
418 if (!pure_file_name) {
420 req_dl_info *request_info = GET_STAGE_TRANSACTION_INFO(stage);
421 /* If there is location url from response header in case of redirection,
422 * it try to parse the file name from the location url */
423 if (GET_REQUEST_HTTP_REQ_LOCATION(request_info))
424 url = GET_REQUEST_HTTP_REQ_LOCATION(request_info);
426 url = GET_SOURCE_BASIC_URL(source_info);
428 DA_LOG(FileManager, "url: [%s]", url);
429 da_get_file_name_from_url(url, &pure_file_name);
434 if (!pure_file_name) {
435 pure_file_name = strdup(NO_NAME_TEMP_STR);
436 if (!pure_file_name) {
437 ret = DA_ERR_FAIL_TO_MEMALLOC;
442 *out_pure_file_name = pure_file_name;
443 pure_file_name = DA_NULL;
444 DA_LOG(FileManager, "candidate file name [%s]", *out_pure_file_name);
448 *out_extension = extension;
451 *out_extension = __derive_extension(stage);
452 DA_LOG(FileManager, "candidate extension [%s]", *out_extension);
468 da_result_t __decide_file_path(stage_info *stage)
470 da_result_t ret = DA_RESULT_OK;
471 char *temp_dir = DA_NULL;
472 char *extension = DA_NULL;
473 char *file_name_without_extension = DA_NULL;
474 char *tmp_file_path = DA_NULL;
475 char *user_install_path = DA_NULL;
476 file_info *file_info_data = DA_NULL;
478 DA_LOG_FUNC_START(FileManager);
480 file_info_data = GET_STAGE_CONTENT_STORE_INFO(stage);
482 return DA_ERR_INVALID_ARGUMENT;
485 /* If the installed path which user want is set, the temporary directory is same to the installation directory.
486 * Otherwise, the default temporary directory is used.
488 user_install_path = GET_DL_USER_INSTALL_PATH(GET_STAGE_DL_ID(stage));
489 if (user_install_path) {
490 len = strlen(user_install_path);
491 temp_dir = (char *)calloc(len + 1, sizeof(char));
493 ret = DA_ERR_FAIL_TO_MEMALLOC;
496 memcpy(temp_dir, user_install_path, len);
497 temp_dir[len] = '\0';
500 ret = get_default_install_dir(&temp_dir);
501 if (DA_RESULT_OK != ret || DA_NULL == temp_dir) {
506 ret = __get_candidate_file_name(stage, &file_name_without_extension, &extension);
507 if (ret != DA_RESULT_OK)
511 tmp_file_path = get_full_path_avoided_duplication(temp_dir, file_name_without_extension, extension);
513 GET_CONTENT_STORE_ACTUAL_FILE_NAME(GET_STAGE_CONTENT_STORE_INFO(stage))
515 tmp_file_path = DA_NULL;
517 ret = DA_ERR_FAIL_TO_ACCESS_FILE;
521 if (file_name_without_extension && !GET_CONTENT_STORE_PURE_FILE_NAME(file_info_data)) {
522 GET_CONTENT_STORE_PURE_FILE_NAME(file_info_data) = file_name_without_extension;
523 file_name_without_extension = DA_NULL;
526 if (extension && !GET_CONTENT_STORE_EXTENSION(file_info_data)) {
527 GET_CONTENT_STORE_EXTENSION(file_info_data) = extension;
532 DA_LOG(FileManager, "decided file path = %s", GET_CONTENT_STORE_ACTUAL_FILE_NAME(file_info_data));
537 if (file_name_without_extension) {
538 free(file_name_without_extension);
539 file_name_without_extension = DA_NULL;
548 char *get_full_path_avoided_duplication(char *in_dir, char *in_candidate_file_name, char *in_extension)
551 char *file_name = in_candidate_file_name;
552 char *extension = in_extension;
553 char *final_path = DA_NULL;
555 int final_path_len = 0;
556 int extension_len = 0;
558 int suffix_count = 0; /* means suffix on file name. up to "_99" */
559 const int max_suffix_count = 99;
560 int suffix_len = (int)log10(max_suffix_count+1) + 1; /* 1 means "_" */
562 if (!in_dir || !in_candidate_file_name)
565 // DA_LOG_FUNC_START(FileManager);
566 DA_LOG(FileManager, "in_candidate_file_name=[%s], in_extension=[%s]", in_candidate_file_name, in_extension);
569 extension_len = strlen(extension);
571 /* first 1 for "/", second 1 for ".", last 1 for DA_NULL */
572 final_path_len = strlen(dir) + 1 + strlen(file_name) + 1
573 + suffix_len + extension_len + 1;
575 final_path = (char*)calloc(1, final_path_len);
577 DA_LOG_ERR(FileManager, "DA_ERR_FAIL_TO_MEMALLOC");
583 * if there is no extension name, just make a file name without extension */
584 if (0 == extension_len) {
585 if (suffix_count == 0) {
586 snprintf(final_path, final_path_len,
587 "%s/%s", dir, file_name);
589 snprintf(final_path, final_path_len,
590 "%s/%s_%d", dir, file_name, suffix_count);
593 if (suffix_count == 0) {
594 snprintf(final_path, final_path_len,
595 "%s/%s.%s", dir, file_name, extension);
597 snprintf(final_path, final_path_len,
599 dir, file_name, suffix_count, extension);
603 if (is_file_exist(final_path)) {
605 if (suffix_count > max_suffix_count) {
607 final_path = DA_NULL;
610 memset(final_path, 0x00, final_path_len);
618 DA_LOG(FileManager, "decided path = [%s]", final_path);
622 da_result_t __divide_file_name_into_pure_name_N_extesion(const char *in_file_name, char **out_pure_file_name, char **out_extension)
624 char *file_name = DA_NULL;
625 char *tmp_ptr = DA_NULL;
626 char temp_file[DA_MAX_FILE_PATH_LEN] = {0,};
627 char tmp_ext[DA_MAX_STR_LEN] = {0,};
629 da_result_t ret = DA_RESULT_OK;
631 DA_LOG_FUNC_START(FileManager);
634 return DA_ERR_INVALID_ARGUMENT;
636 file_name = (char *)in_file_name;
637 tmp_ptr = strrchr(file_name, '.');
640 if (tmp_ptr && out_extension) {
641 strncpy((char*) tmp_ext, tmp_ptr, sizeof(tmp_ext) - 1);
642 *out_extension = strdup((const char*) tmp_ext);
643 DA_LOG(FileManager, "extension [%s]", *out_extension);
646 if (!out_pure_file_name)
650 len = tmp_ptr - file_name - 1;
652 len = strlen(file_name);
654 if (len >= DA_MAX_FILE_PATH_LEN) {
655 strncpy((char*) temp_file, file_name,
656 DA_MAX_FILE_PATH_LEN - 1);
658 strncpy((char*) temp_file, file_name, len);
661 delete_prohibited_char((char*) temp_file,
662 strlen((char*) temp_file));
663 if (strlen(temp_file) < 1) {
664 *out_pure_file_name = strdup(NO_NAME_TEMP_STR);
666 *out_pure_file_name = strdup(
667 (const char*) temp_file);
670 DA_LOG(FileManager, "pure file name [%s]", *out_pure_file_name);
674 da_result_t __file_write_buf_make_buf(file_info *file_storage)
676 da_result_t ret = DA_RESULT_OK;
677 char *buffer = DA_NULL;
679 DA_LOG_FUNC_START(FileManager);
681 buffer = (char*) calloc(DOWNLOAD_NOTIFY_LIMIT, 1);
682 if (DA_NULL == buffer) {
683 DA_LOG_ERR(FileManager, "Calloc failure ");
684 ret = DA_ERR_FAIL_TO_MEMALLOC;
686 GET_CONTENT_STORE_FILE_BUFF_LEN(file_storage) = 0;
687 GET_CONTENT_STORE_FILE_BUFFER(file_storage) = buffer;
693 da_result_t __file_write_buf_destroy_buf(file_info *file_storage)
695 da_result_t ret = DA_RESULT_OK;
697 DA_LOG_FUNC_START(FileManager);
699 if (GET_CONTENT_STORE_FILE_BUFFER(file_storage))
700 free(GET_CONTENT_STORE_FILE_BUFFER(file_storage));
702 GET_CONTENT_STORE_FILE_BUFFER(file_storage) = DA_NULL;
703 GET_CONTENT_STORE_FILE_BUFF_LEN(file_storage) = 0;
708 da_result_t __file_write_buf_flush_buf(stage_info *stage, file_info *file_storage)
710 da_result_t ret = DA_RESULT_OK;
711 char *buffer = DA_NULL;
713 int write_success_len = 0;
716 // DA_LOG_FUNC_START(FileManager);
718 buffer = GET_CONTENT_STORE_FILE_BUFFER(file_storage);
719 buffer_size = GET_CONTENT_STORE_FILE_BUFF_LEN(file_storage);
721 if (buffer_size == 0) {
722 DA_LOG_ERR(FileManager, "no data on buffer..");
726 fd = GET_CONTENT_STORE_FILE_HANDLE(file_storage);
728 DA_LOG_ERR(FileManager, "There is no file handle.");
730 ret = DA_ERR_FAIL_TO_ACCESS_FILE;
734 write_success_len = fwrite(buffer, sizeof(char), buffer_size,
736 /* FIXME : This can be necessary later due to progressive download.
737 * The solution for reducing fflush is needed */
738 //fflush((FILE *) fd);
739 if (write_success_len != buffer_size) {
740 DA_LOG_ERR(FileManager, "write fails ");
741 ret = DA_ERR_FAIL_TO_ACCESS_FILE;
744 GET_CONTENT_STORE_CURRENT_FILE_SIZE(GET_STAGE_CONTENT_STORE_INFO(stage))
745 += write_success_len;
746 DA_LOG(FileManager, "write %d bytes", write_success_len);
748 IS_CONTENT_STORE_FILE_BYTES_WRITTEN_TO_FILE(file_storage) = DA_TRUE;
749 GET_CONTENT_STORE_FILE_BUFF_LEN(file_storage) = 0;
755 da_result_t __file_write_buf_copy_to_buf(file_info *file_storage, char *body,
758 da_result_t ret = DA_RESULT_OK;
759 char *buffer = DA_NULL;
762 // DA_LOG_FUNC_START(FileManager);
764 buffer = GET_CONTENT_STORE_FILE_BUFFER(file_storage);
765 buffer_size = GET_CONTENT_STORE_FILE_BUFF_LEN(file_storage);
767 memcpy(buffer + buffer_size, body, body_len);
768 GET_CONTENT_STORE_FILE_BUFF_LEN(file_storage) += body_len;
773 da_result_t __file_write_buf_directly_write(stage_info *stage,
774 file_info *file_storage, char *body, int body_len)
776 da_result_t ret = DA_RESULT_OK;
777 int write_success_len = 0;
780 // DA_LOG_FUNC_START(FileManager);
782 fd = GET_CONTENT_STORE_FILE_HANDLE(file_storage);
784 DA_LOG_ERR(FileManager, "There is no file handle.");
786 ret = DA_ERR_FAIL_TO_ACCESS_FILE;
790 write_success_len = fwrite(body, sizeof(char), body_len,
792 /* FIXME : This can be necessary later due to progressive download.
793 * The solution for reducing fflush is needed */
794 //fflush((FILE *) fd);
795 if (write_success_len != body_len) {
796 DA_LOG_ERR(FileManager, "write fails ");
797 ret = DA_ERR_FAIL_TO_ACCESS_FILE;
800 GET_CONTENT_STORE_CURRENT_FILE_SIZE(GET_STAGE_CONTENT_STORE_INFO(stage))
801 += write_success_len;
802 DA_LOG(FileManager, "write %d bytes", write_success_len);
803 IS_CONTENT_STORE_FILE_BYTES_WRITTEN_TO_FILE(file_storage) = DA_TRUE;
809 da_result_t file_write_ongoing(stage_info *stage, char *body, int body_len)
811 da_result_t ret = DA_RESULT_OK;
812 file_info *file_storage = DA_NULL;
814 char *buffer = DA_NULL;
816 // DA_LOG_FUNC_START(FileManager);
818 file_storage = GET_STAGE_CONTENT_STORE_INFO(stage);
820 DA_LOG_ERR(FileManager, "file_info is empty.");
821 ret = DA_ERR_FAIL_TO_ACCESS_FILE;
825 buffer = GET_CONTENT_STORE_FILE_BUFFER(file_storage);
826 buffer_size = GET_CONTENT_STORE_FILE_BUFF_LEN(file_storage);
827 IS_CONTENT_STORE_FILE_BYTES_WRITTEN_TO_FILE(file_storage) = DA_FALSE;
829 if (DA_NULL == buffer) {
830 if (body_len < DOWNLOAD_NOTIFY_LIMIT) {
831 ret = __file_write_buf_make_buf(file_storage);
832 if (ret != DA_RESULT_OK)
835 __file_write_buf_copy_to_buf(file_storage, body, body_len);
837 ret = __file_write_buf_directly_write(stage,
838 file_storage, body, body_len);
839 if (ret != DA_RESULT_OK)
843 if (DOWNLOAD_NOTIFY_LIMIT <= body_len) {
844 ret = __file_write_buf_flush_buf(stage, file_storage);
845 if (ret != DA_RESULT_OK)
848 ret = __file_write_buf_directly_write(stage,
849 file_storage, body, body_len);
850 if (ret != DA_RESULT_OK)
853 } else if ((DOWNLOAD_NOTIFY_LIMIT - buffer_size) <= body_len) {
854 ret = __file_write_buf_flush_buf(stage, file_storage);
855 if (ret != DA_RESULT_OK)
858 __file_write_buf_copy_to_buf(file_storage, body, body_len);
860 __file_write_buf_copy_to_buf(file_storage, body, body_len);
865 if (ret != DA_RESULT_OK) {
867 GET_CONTENT_STORE_FILE_BUFF_LEN(file_storage) = 0;
868 if (GET_CONTENT_STORE_FILE_BUFFER(file_storage)) {
870 GET_CONTENT_STORE_FILE_BUFFER(file_storage));
871 GET_CONTENT_STORE_FILE_BUFFER(file_storage)
879 da_result_t file_write_complete(stage_info *stage)
881 da_result_t ret = DA_RESULT_OK;
882 file_info*file_storage = DA_NULL;
883 char *buffer = DA_NULL;
884 unsigned int buffer_size = 0;
887 DA_LOG_FUNC_START(FileManager);
889 file_storage = GET_STAGE_CONTENT_STORE_INFO(stage);
891 DA_LOG_ERR(FileManager, "file_info is DA_NULL.");
892 ret = DA_ERR_FAIL_TO_ACCESS_FILE;
896 buffer = GET_CONTENT_STORE_FILE_BUFFER(file_storage);
897 buffer_size = GET_CONTENT_STORE_FILE_BUFF_LEN(file_storage);
899 if (DA_NULL == buffer) {
900 DA_LOG_ERR(FileManager, "file buffer is DA_NULL");
902 if (buffer_size != 0) {
903 ret = __file_write_buf_flush_buf(stage, file_storage);
904 if (ret != DA_RESULT_OK)
907 __file_write_buf_destroy_buf(file_storage);
909 fd = GET_CONTENT_STORE_FILE_HANDLE(file_storage);
915 GET_CONTENT_STORE_FILE_HANDLE(file_storage) = DA_NULL;
920 da_result_t start_file_writing(stage_info *stage)
922 da_result_t ret = DA_RESULT_OK;
923 file_info *file_info_data = DA_NULL;
925 DA_LOG_FUNC_START(FileManager);
927 file_info_data = GET_STAGE_CONTENT_STORE_INFO(stage);
928 ret = get_mime_type(stage,
929 &GET_CONTENT_STORE_CONTENT_TYPE(file_info_data));
930 if (ret != DA_RESULT_OK)
933 ret = __decide_file_path(stage);
934 if (ret != DA_RESULT_OK)
937 ret = __set_file_size(stage);
938 if (DA_RESULT_OK != ret)
941 GET_CONTENT_STORE_CURRENT_FILE_SIZE(GET_STAGE_CONTENT_STORE_INFO(stage))
944 ret = __saved_file_open(stage);
951 da_result_t start_file_writing_append(stage_info *stage)
953 da_result_t ret = DA_RESULT_OK;
955 DA_LOG_FUNC_START(FileManager);
957 ret = __saved_file_open(stage);
962 // for resume with new download request
963 da_result_t start_file_writing_append_with_new_download(stage_info *stage)
965 da_result_t ret = DA_RESULT_OK;
966 file_info *file_storage = DA_NULL;
967 char *original_file_path = DA_NULL;
968 char *temp_file_path = DA_NULL;
969 char *extension = DA_NULL;
970 char *file_name_without_extension = DA_NULL;
971 req_dl_info *request_info = DA_NULL;
972 unsigned long long temp_file_size = 0;
974 DA_LOG_FUNC_START(FileManager);
976 file_storage = GET_STAGE_CONTENT_STORE_INFO(stage);
978 return DA_ERR_INVALID_ARGUMENT;
979 request_info = GET_STAGE_TRANSACTION_INFO(stage);
981 return DA_ERR_INVALID_ARGUMENT;
982 temp_file_path = GET_REQUEST_HTTP_USER_REQUEST_TEMP_FILE_PATH(request_info);
984 return DA_ERR_INVALID_ARGUMENT;
985 original_file_path = GET_CONTENT_STORE_ACTUAL_FILE_NAME(file_storage);
987 GET_CONTENT_STORE_ACTUAL_FILE_NAME(file_storage) = strdup(temp_file_path);
989 if (original_file_path)
990 free(original_file_path);
992 ret = get_mime_type(stage,
993 &GET_CONTENT_STORE_CONTENT_TYPE(file_storage));
994 if (ret != DA_RESULT_OK)
997 ret = __get_candidate_file_name(stage, &file_name_without_extension, &extension);
998 if (ret != DA_RESULT_OK)
1001 if (file_name_without_extension) {
1002 if (!GET_CONTENT_STORE_PURE_FILE_NAME(file_storage)) {
1003 GET_CONTENT_STORE_PURE_FILE_NAME(file_storage) = file_name_without_extension;
1004 file_name_without_extension = DA_NULL;
1006 free(file_name_without_extension);
1007 file_name_without_extension = DA_NULL;
1012 if (!GET_CONTENT_STORE_EXTENSION(file_storage)) {
1013 GET_CONTENT_STORE_EXTENSION(file_storage) = extension;
1014 extension = DA_NULL;
1017 extension = DA_NULL;
1021 ret = __set_file_size(stage);
1022 if (DA_RESULT_OK != ret)
1024 get_file_size(temp_file_path, &temp_file_size);
1025 if (temp_file_size < 1)
1028 GET_CONTENT_STORE_CURRENT_FILE_SIZE(GET_STAGE_CONTENT_STORE_INFO(stage))
1031 ret = __saved_file_open(stage);
1034 if (file_name_without_extension) {
1035 free(file_name_without_extension);
1036 file_name_without_extension = DA_NULL;
1041 extension = DA_NULL;
1046 da_result_t discard_download(stage_info *stage)
1048 da_result_t ret = DA_RESULT_OK;
1049 file_info *file_storage = DA_NULL;
1050 FILE *f_handle = DA_NULL;
1052 DA_LOG_FUNC_START(FileManager);
1054 file_storage = GET_STAGE_CONTENT_STORE_INFO(stage);
1056 f_handle = GET_CONTENT_STORE_FILE_HANDLE(file_storage);
1059 GET_CONTENT_STORE_FILE_HANDLE(file_storage) = DA_NULL;
1064 void clean_paused_file(stage_info *stage)
1066 file_info *file_info_data = DA_NULL;
1067 char *paused_file_path = DA_NULL;
1070 DA_LOG_FUNC_START(FileManager);
1072 file_info_data = GET_STAGE_CONTENT_STORE_INFO(stage);
1074 fd = GET_CONTENT_STORE_FILE_HANDLE(file_info_data);
1077 GET_CONTENT_STORE_FILE_HANDLE(file_info_data) = DA_NULL;
1080 paused_file_path = GET_CONTENT_STORE_ACTUAL_FILE_NAME(file_info_data);
1081 remove_file((const char*) paused_file_path);
1086 da_result_t replace_content_file_in_stage(stage_info *stage,
1087 const char *dest_dd_file_path)
1089 da_result_t ret = DA_RESULT_OK;
1090 char *dd_file_path = DA_NULL;
1093 DA_LOG_FUNC_START(FileManager);
1095 if (!dest_dd_file_path
1096 && (DA_FALSE == is_file_exist(dest_dd_file_path))) {
1097 ret = DA_ERR_INVALID_ARGUMENT;
1102 =GET_CONTENT_STORE_ACTUAL_FILE_NAME(GET_STAGE_CONTENT_STORE_INFO(stage));
1104 if (DA_NULL != dd_file_path) {
1105 remove_file((const char*) dd_file_path);
1108 len = strlen(dest_dd_file_path);
1109 dd_file_path = calloc(1, len + 1);
1110 if (!dd_file_path) {
1111 ret = DA_ERR_FAIL_TO_MEMALLOC;
1114 strncpy(dd_file_path, dest_dd_file_path, len);
1115 GET_CONTENT_STORE_ACTUAL_FILE_NAME(GET_STAGE_CONTENT_STORE_INFO(stage))
1123 da_result_t copy_file(const char *src, const char *dest)
1129 char buff[4096] = { 0, };
1131 DA_LOG_FUNC_START(FileManager);
1133 /* open files to copy */
1134 fs = fopen(src, "rb");
1136 DA_LOG_ERR(FileManager, "Fail to open src file");
1137 return DA_ERR_FAIL_TO_ACCESS_FILE;
1140 fd = fopen(dest, "wb");
1142 DA_LOG_ERR(FileManager, "Fail to open dest file");
1145 return DA_ERR_FAIL_TO_ACCESS_FILE;
1150 memset(buff, 0x00, 4096);
1151 freadnum = fread(buff, sizeof(char), sizeof(buff), fs);
1153 fwritenum = fwrite(buff, sizeof(char), freadnum, fd);
1154 if (fwritenum <= 0) {
1155 DA_LOG(FileManager, "written = %d",fwritenum);
1159 DA_LOG(FileManager, "read = %d",freadnum);
1167 return DA_RESULT_OK;
1170 da_result_t create_dir(const char *install_dir)
1172 da_result_t ret = DA_RESULT_OK;
1173 /* read/write/search permissions for owner and group,
1174 * and with read/search permissions for others. */
1175 if (mkdir(install_dir, S_IRWXU | S_IRWXG | S_IRWXO)) {
1176 DA_LOG_ERR(FileManager, "Fail to creaate directory [%s]", install_dir);
1177 ret = DA_ERR_FAIL_TO_ACCESS_STORAGE;
1179 DA_LOG(FileManager, "[%s] is created!", install_dir);
1185 da_result_t get_default_dir(char **out_path)
1187 char *tmp_default_path = DA_NULL;
1191 DA_LOG_ERR(ClientNoti, "DA_ERR_INVALID_ARGUMENT");
1192 return DA_ERR_INVALID_ARGUMENT;
1195 len = strlen(DA_DEFAULT_FILE_DIR_PATH);
1196 tmp_default_path = calloc(len + 1, sizeof(char));
1197 if (!tmp_default_path) {
1198 return DA_ERR_FAIL_TO_MEMALLOC;
1201 memcpy(tmp_default_path, DA_DEFAULT_FILE_DIR_PATH, len);
1202 tmp_default_path[len] = '\0';
1204 *out_path = tmp_default_path;
1206 DA_LOG_VERBOSE(FileManager, "default temp path = [%s]", *out_path);
1208 return DA_RESULT_OK;
1211 da_result_t get_default_install_dir(char **out_path)
1213 char *default_path = DA_NULL;
1214 da_storage_type_t type;
1215 da_result_t ret = DA_RESULT_OK;
1219 DA_LOG_ERR(ClientNoti, "DA_ERR_INVALID_ARGUMENT");
1220 return DA_ERR_INVALID_ARGUMENT;
1222 ret = get_storage_type(&type);
1223 if (DA_RESULT_OK != ret)
1225 if (type == DA_STORAGE_MMC)
1226 len = strlen(DA_DEFAULT_INSTALL_PATH_FOR_MMC);
1228 len = strlen(DA_DEFAULT_INSTALL_PATH_FOR_PHONE);
1230 default_path = calloc(len + 1, sizeof(char));
1231 if (!default_path) {
1232 return DA_ERR_FAIL_TO_MEMALLOC;
1234 if (type == DA_STORAGE_MMC)
1235 memcpy(default_path, DA_DEFAULT_INSTALL_PATH_FOR_MMC, len);
1236 else // DA_STROAGE_PHONE
1237 memcpy(default_path, DA_DEFAULT_INSTALL_PATH_FOR_PHONE, len);
1238 default_path[len] = '\0';
1240 *out_path = default_path;
1242 DA_LOG_VERBOSE(FileManager, "default temp path = [%s]", *out_path);
1243 return DA_RESULT_OK;