summaryrefslogtreecommitdiff
path: root/src/download-provider-utils.c
blob: 59b19ca95aded9af30fa2aedfff0b4e3f6274a79 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h>

#include "download-provider-config.h"
#include "download-provider-notification.h"
#include "download-provider-pthread.h"
#include "download-provider-log.h"

extern fd_set g_download_provider_socket_readset;
extern fd_set g_download_provider_socket_exceptset;

int get_download_request_id(void)
{
	int uniquetime = 0;
	struct timeval tval;
	static int last_uniquetime = 0;

	do {
		uniquetime = (int)time(NULL);
		gettimeofday(&tval, NULL);
		if (tval.tv_usec == 0)
			uniquetime = uniquetime + (tval.tv_usec + 1) % 0xfffff;
		else
			uniquetime = uniquetime + tval.tv_usec;
		TRACE_DEBUG_INFO_MSG("ID : %d", uniquetime);
	} while (last_uniquetime == uniquetime);
	last_uniquetime = uniquetime;	// store
	return uniquetime;
}

void clear_socket(download_clientinfo *clientinfo)
{
	if (!clientinfo)
		return;
	if (clientinfo->clientfd) {
		FD_CLR(clientinfo->clientfd, &g_download_provider_socket_readset);
		FD_CLR(clientinfo->clientfd, &g_download_provider_socket_exceptset);
		shutdown(clientinfo->clientfd, 0);
		fdatasync(clientinfo->clientfd);
		close(clientinfo->clientfd);
		clientinfo->clientfd = 0;
	}
}

void clear_clientinfo(download_clientinfo *clientinfo)
{
	TRACE_DEBUG_INFO_MSG("[TEST] clear [%p]",clientinfo);
	// clear this slot
	if (!clientinfo)
		return;

	clear_socket(clientinfo);

	CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
	if (clientinfo->requestinfo) {
		clientinfo->requestinfo->requestid = 0;
		if (clientinfo->requestinfo->client_packagename.length > 0
			&& clientinfo->requestinfo->client_packagename.str)
			free(clientinfo->requestinfo->client_packagename.str);
		clientinfo->requestinfo->client_packagename.str = NULL;
		if (clientinfo->requestinfo->url.str)
			free(clientinfo->requestinfo->url.str);
		clientinfo->requestinfo->url.str = NULL;
		if (clientinfo->requestinfo->install_path.str)
			free(clientinfo->requestinfo->install_path.str);
		clientinfo->requestinfo->install_path.str = NULL;
		if (clientinfo->requestinfo->filename.str)
			free(clientinfo->requestinfo->filename.str);
		clientinfo->requestinfo->filename.str = NULL;
		if (clientinfo->requestinfo->service_data.str)
			free(clientinfo->requestinfo->service_data.str);
		clientinfo->requestinfo->service_data.str = NULL;
		if (clientinfo->requestinfo->headers.rows) {
			int i = 0;
			for (i = 0; i < clientinfo->requestinfo->headers.rows;
				i++) {
				if (clientinfo->requestinfo->headers.str[i].str)
					free(clientinfo->requestinfo->headers.
						str[i].str);
				clientinfo->requestinfo->headers.str[i].str =
					NULL;
			}
			free(clientinfo->requestinfo->headers.str);
			clientinfo->requestinfo->headers.str = NULL;
		}
		free(clientinfo->requestinfo);
		clientinfo->requestinfo = NULL;
	}
	if (clientinfo->downloadinginfo)
		free(clientinfo->downloadinginfo);
	clientinfo->downloadinginfo = NULL;
	if (clientinfo->downloadinfo)
		free(clientinfo->downloadinfo);
	clientinfo->downloadinfo = NULL;
	if (clientinfo->tmp_saved_path)
		free(clientinfo->tmp_saved_path);
	clientinfo->tmp_saved_path = NULL;
	if (clientinfo->ui_notification_handle || clientinfo->service_handle)
		destroy_appfw_notification(clientinfo);
	CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
	CLIENT_MUTEX_DESTROY(&(clientinfo->client_mutex));
	memset(clientinfo, 0x00, sizeof(download_clientinfo));
	free(clientinfo);
	clientinfo = NULL;
}

void clear_clientinfoslot(download_clientinfo_slot *clientinfoslot)
{
	// clear this slot
	if (!clientinfoslot)
		return;
	download_clientinfo *clientinfo =
		(download_clientinfo *) clientinfoslot->clientinfo;
	clear_clientinfo(clientinfo);
	clientinfoslot->clientinfo = NULL;
}

int get_downloading_count(download_clientinfo_slot *clientinfo_list)
{
	int count = 0;
	int i = 0;
	for (i = 0; i < MAX_CLIENT; i++) {
		if (clientinfo_list[i].clientinfo) {
			if (clientinfo_list[i].clientinfo->state ==
				DOWNLOAD_STATE_DOWNLOADING
				|| clientinfo_list[i].clientinfo->state ==
				DOWNLOAD_STATE_INSTALLING
				|| clientinfo_list[i].clientinfo->state ==
				DOWNLOAD_STATE_READY)
				count++;
		}
	}
	return count;
}

int get_same_request_slot_index(download_clientinfo_slot *clientinfo_list,
				int requestid)
{
	int i = 0;

	if (!clientinfo_list || !requestid)
		return -1;

	for (i = 0; i < MAX_CLIENT; i++) {
		if (clientinfo_list[i].clientinfo
			&& clientinfo_list[i].clientinfo->requestinfo) {
			if (clientinfo_list[i].clientinfo->requestinfo->
				requestid == requestid) {
				return i;
			}
		}
	}
	return -1;
}

int get_empty_slot_index(download_clientinfo_slot *clientinfo_list)
{
	int i = 0;

	if (!clientinfo_list)
		return -1;

	for (i = 0; i < MAX_CLIENT; i++)
		if (!clientinfo_list[i].clientinfo)
			return i;
	return -1;
}