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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
* Copyright (C) 2012 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <utime.h>
#include <zip.h>
#include "XBMCApp.h"
typedef struct stat Stat;
static int do_mkdir(const char *path)
{
Stat st;
int status = 0;
if (stat(path, &st) != 0)
{
/* Directory does not exist */
if (mkdir(path, 0755) != 0)
status = -1;
}
else if (!S_ISDIR(st.st_mode))
{
errno = ENOTDIR;
status = -1;
}
return(status);
}
int mkpath(const char *path)
{
char *pp;
char *sp;
int status;
char *copypath = strdup(path);
status = 0;
pp = copypath;
while (status == 0 && (sp = strchr(pp, '/')) != 0)
{
if (sp != pp)
{
/* Neither root nor double slash in path */
*sp = '\0';
status = do_mkdir(copypath);
*sp = '/';
}
pp = sp + 1;
}
if (status == 0)
status = do_mkdir(path);
free(copypath);
return (status);
}
int extract_to_cache(const char *archive, const char *cache_path)
{
struct zip *ziparchive;
struct zip_file *zipfile;
struct zip_stat zipstat;
char buf[4096];
int err;
int len;
int fd;
int dirname_len;
uint64_t sum;
char *full_path;
char *dir_name;
Stat localfile;
utimbuf modified;
CXBMCApp::android_printf("unzip: Preparing to cache. This could take a while...");
if ((ziparchive = zip_open(archive, 0, &err)) == NULL)
{
zip_error_to_str(buf, sizeof(buf), err, errno);
CXBMCApp::android_printf("unzip error: can't open archive %s/n",archive);
return 1;
}
mkpath(cache_path);
for (size_t i = 0; i < zip_get_num_entries(ziparchive, 0); i++)
{
if (zip_stat_index(ziparchive, i, 0, &zipstat) != 0)
{
CXBMCApp::android_printf("unzip error: can't open entry: %i/n",i);
continue;
}
if(strncmp (zipstat.name,"assets/",7) != 0)
continue;
if(strncmp (zipstat.name,"assets/python2.6",16) == 0)
continue;
modified.modtime = modified.actime = zipstat.mtime;
full_path = (char*)malloc( sizeof(char*) * (strlen(cache_path) + 1 + strlen(zipstat.name) + 1));
sprintf(full_path, "%s/%s",cache_path,zipstat.name);
if (zipstat.name[strlen(zipstat.name) - 1] == '/')
{
mkpath(full_path);
free(full_path);
continue;
}
zipfile = zip_fopen_index(ziparchive, i, 0);
if (!zipfile)
{
CXBMCApp::android_printf("unzip error: can't open index");
free(full_path);
continue;
}
if (stat(full_path, &localfile) != 0)
{
dirname_len = strrchr(zipstat.name,'/') - zipstat.name;
dir_name = (char*)malloc( sizeof(char*) * (strlen(cache_path) + 1 + dirname_len + 1));
strncpy(dir_name, full_path, strlen(cache_path) + dirname_len + 1);
dir_name[strlen(cache_path) + dirname_len + 1] = '\0';
mkpath(dir_name);
free(dir_name);
}
// watch this compare, zipstat.mtime is time_t which is a signed long
else if (localfile.st_mtime == (unsigned long)zipstat.mtime)
{
free(full_path);
continue;
}
fd = open(full_path, O_RDWR | O_TRUNC | O_CREAT, 0644);
if(fd < 0)
{
CXBMCApp::android_printf("unzip error: could not open %s",full_path);
free(full_path);
continue;
}
sum = 0;
while (sum != zipstat.size)
{
len = zip_fread(zipfile, buf, 4096);
if (len < 0)
{
CXBMCApp::android_printf("unzip error: no data in %s",full_path);
free(full_path);
continue;
}
write(fd, buf, len);
sum += len;
}
close(fd);
zip_fclose(zipfile);
if (stat(full_path, &localfile) == 0)
{
// save the zip time. this way we know for certain if we need to refresh.
utime(full_path, &modified);
}
else
{
CXBMCApp::android_printf("unzip error: failed to extract %s",full_path);
}
free(full_path);
}
if (zip_close(ziparchive) == -1)
CXBMCApp::android_printf("unzip error: can't close zip archive `%s'/n", archive);
return 0;
}
|