summaryrefslogtreecommitdiff
path: root/src/agent/download-agent-installation.c
blob: f0ffcfafd9eb0f1f8f16d600e7e106f5e5e65b10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Download Agent
 *
 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Jungki Kwak <jungki.kwak@samsung.com>, Keunsoon Lee <keunsoon.lee@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @file		download-agent-installation.c
 * @brief		Functions for Content Installation
 * @author		Keunsoon Lee(keunsoon.lee@samsung.com)
 * @author		Jungki Kwak(jungki.kwak@samsung.com)
 ***/

#include <sys/time.h>
#include <unistd.h>

#include "download-agent-client-mgr.h"
#include "download-agent-dl-info-util.h"
#include "download-agent-http-mgr.h"
#include "download-agent-http-misc.h"
#include "download-agent-installation.h"
#include "download-agent-file.h"
#include "download-agent-plugin-install.h"

da_result_t _extract_file_path_which_will_be_installed(char *in_file_name, char *in_extension, char *in_install_path_client_wants, char **out_will_install_path);

da_result_t install_content(stage_info *stage)
{
	da_result_t ret = DA_RESULT_OK;

	file_info *file_storage = DA_NULL;
	char *temp_saved_file_path = DA_NULL;
	char *install_file_path = DA_NULL;
	unsigned int start_time = 0;

	DA_LOG_FUNC_START(InstallManager);

	if (!stage)
		return DA_ERR_INVALID_ARGUMENT;

	file_storage = GET_STAGE_CONTENT_STORE_INFO(stage);
	if (!file_storage) {
		DA_LOG_ERR(InstallManager,"file_storage structure is NULL");
		ret = DA_ERR_INVALID_ARGUMENT;
		goto ERR;
	}

	temp_saved_file_path =
			GET_CONTENT_STORE_TMP_FILE_NAME(file_storage);
	DA_LOG(InstallManager,"Source path[%s]",temp_saved_file_path);

	ret = check_enough_storage(stage);
	if (ret != DA_RESULT_OK)
		goto ERR;

	ret = _extract_file_path_which_will_be_installed(
			GET_CONTENT_STORE_PURE_FILE_NAME(file_storage),
			GET_CONTENT_STORE_EXTENSION(file_storage),
			GET_DL_USER_INSTALL_PATH(GET_STAGE_DL_ID(stage)),
			&install_file_path);
	if (ret != DA_RESULT_OK)
		goto ERR;

	DA_LOG(InstallManager,"Installing path [%s]", install_file_path);
	DA_LOG(InstallManager,"Move start time : [%ld]",start_time=time(NULL));
	ret = move_file(temp_saved_file_path, install_file_path);
	if (ret != DA_RESULT_OK)
		goto ERR;

	if (GET_CONTENT_STORE_ACTUAL_FILE_NAME(file_storage))
		free(GET_CONTENT_STORE_ACTUAL_FILE_NAME(file_storage));
	GET_CONTENT_STORE_ACTUAL_FILE_NAME(file_storage) = install_file_path;
	install_file_path = DA_NULL;


ERR:
	if (ret != DA_RESULT_OK) {
		remove_file(GET_CONTENT_STORE_TMP_FILE_NAME(file_storage));
		remove_file(install_file_path);
	} else {
	}
	return ret;
}

da_result_t _extract_file_path_which_will_be_installed(char *in_file_name, char *in_extension, char *in_install_path_client_wants, char **out_will_install_path)
{
	da_result_t ret = DA_RESULT_OK;
	char *install_dir = NULL;
	char *default_install_dir = NULL;
	char *final_path = NULL;
	char *pure_file_name = in_file_name;
	char *extension = in_extension;

	if (!in_file_name || !out_will_install_path)
		return DA_ERR_INVALID_ARGUMENT;

	*out_will_install_path = DA_NULL;

	if (in_install_path_client_wants) {
		install_dir = in_install_path_client_wants;
	} else {
		default_install_dir = PI_get_default_install_dir();
		if (default_install_dir)
			install_dir = default_install_dir;
		else
			return DA_ERR_FAIL_TO_INSTALL_FILE;
	}

	if (DA_FALSE == is_dir_exist(install_dir)) {
		ret = create_dir(install_dir);
		if (ret != DA_RESULT_OK)
			return DA_ERR_FAIL_TO_INSTALL_FILE;
	}

	final_path = get_full_path_avoided_duplication(install_dir, pure_file_name, extension);
	if (!final_path)
		ret = DA_ERR_FAIL_TO_INSTALL_FILE;

	*out_will_install_path = final_path;

	DA_LOG(InstallManager,"Final install path[%s]", *out_will_install_path);

	return ret;
}