Fixed missing 'GNOME_VFS_OPEN_RANDOM' for the HTTP cabinets reading.
[captive.git] / src / install / acquire / cabinet.c
1 /* $Id$
2  * cabextract interface for acquiration installation utility
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "cabinet.h"    /* self */
23 #include <glib/gmessages.h>
24 #include <libgnomevfs/gnome-vfs-file-size.h>
25 #include <libgnomevfs/gnome-vfs-ops.h>
26 #include "cabextract/cabextract.h"
27 #include "captivemodid.h"
28 #include "moduriload.h"
29 #include <sys/mman.h>
30 #include <unistd.h>
31 #include "main.h"
32 #include <signal.h>
33 #include <setjmp.h>
34 #include <sys/time.h>
35 #include "cabinet-memory.h"
36
37 #include <captive/macros.h>
38
39
40 /* Config: */
41 #define ACQUIRE_CABINET_READ_RAW_READ_TRY_MAX 5
42 #define ACQUIRE_CABINET_READ_RAW_READ_TIMEOUT 20
43 #define ACQUIRE_CABINET_READ_RAW_READ_ITER_SEC 0
44 #define ACQUIRE_CABINET_READ_RAW_READ_ITER_USEC 100000
45
46
47 #define ACQUIRE_CABINET_READ_RAW_READ_TIMEOUT_ITERS \
48                 ((ACQUIRE_CABINET_READ_RAW_READ_TIMEOUT *1000000LL) \
49                 /(ACQUIRE_CABINET_READ_RAW_READ_ITER_SEC*1000000LL+ACQUIRE_CABINET_READ_RAW_READ_ITER_USEC))
50
51
52 void acquire_cabinet_seek(struct acquire_cabinet *acquire_cabinet,GnomeVFSFileOffset offset)
53 {
54         g_return_if_fail(acquire_cabinet!=NULL);
55
56         /* Do not: (*ui_progress)(acquire_cabinet->uri);
57          * as we currently extract some specific file out of it.
58          */
59         (*ui_progress)(NULL);
60
61         acquire_cabinet->offset=offset;
62 }
63
64 void acquire_cabinet_seek_skip(struct acquire_cabinet *acquire_cabinet,GnomeVFSFileOffset offset)
65 {
66         g_return_if_fail(acquire_cabinet!=NULL);
67
68         (*ui_progress)(NULL);
69
70         acquire_cabinet->offset+=offset;
71 }
72
73 GnomeVFSFileOffset acquire_cabinet_tell(struct acquire_cabinet *acquire_cabinet)
74 {
75         g_return_val_if_fail(acquire_cabinet!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
76
77         (*ui_progress)(NULL);
78
79         return acquire_cabinet->offset;
80 }
81
82 static guint handler_SIGALRM_hits;
83 static sigjmp_buf handler_SIGALRM_sigjmp_buf;
84
85 static void handler_SIGALRM(int signo)
86 {
87         g_return_if_fail(signo==SIGALRM);
88
89         /* Try to abort the read(2) call first.
90          * If it already read something it will return the partially read data.
91          * Otherwise gnome_vfs_inet_connection_read() will loop back to retry read(2)
92          * and we will abort it after 1 second. OK, some data may be read that time
93          * but who cares.
94          */
95         if (handler_SIGALRM_hits++<ACQUIRE_CABINET_READ_RAW_READ_TIMEOUT_ITERS
96                         && !(*ui_progress)(NULL))
97                 return;
98
99         siglongjmp(handler_SIGALRM_sigjmp_buf,1);       /* 1; meaning: !=0 */
100 }
101
102 /* FIXME: This is hack.
103  * Correct way would be to use 'GnomeVFSCancellation'
104  * to abort 'GnomeVFSInetConnection' acting as 'GnomeVFSSocket'.
105  * This abort should be handled from 'http' handler
106  * but gnome_vfs_cancellation_cancel() cannot be invoked from
107  * the asynchronous slave thread.
108  */
109 static GnomeVFSResult acquire_cabinet_read_raw
110                 (struct acquire_cabinet *acquire_cabinet,gpointer buffer,GnomeVFSFileSize bytes,GnomeVFSFileSize *bytes_read,
111                 GnomeVFSFileOffset offset)
112 {
113 gint try=0;
114
115         g_return_val_if_fail(acquire_cabinet!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
116         g_return_val_if_fail(buffer!=NULL || bytes==0,GNOME_VFS_ERROR_BAD_PARAMETERS);
117         g_return_val_if_fail(bytes_read!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
118
119         *bytes_read=0;
120
121         if (!bytes)
122                 return GNOME_VFS_ERROR_EOF;
123
124         while (try++<=ACQUIRE_CABINET_READ_RAW_READ_TRY_MAX) {
125 GnomeVFSResult errvfsresult;
126 GnomeVFSHandle *handle_new;
127 struct sigaction oldact;
128 int errint;
129 struct itimerval itimerval;
130
131                 if ((*ui_progress)(NULL))
132                         return GNOME_VFS_ERROR_INTERRUPTED;
133
134                 if (!sigsetjmp(
135                                 handler_SIGALRM_sigjmp_buf,     /* env */
136                                 TRUE)) {        /* savesigs */
137                         handler_SIGALRM_hits=0;
138                         errint=sigaction(
139                                         SIGALRM,        /* signum */
140                                         NULL,   /* act */
141                                         &oldact);       /* oldact */
142                         g_assert(errint==0);
143                         signal(SIGALRM,handler_SIGALRM);
144                         itimerval.it_interval.tv_sec=ACQUIRE_CABINET_READ_RAW_READ_ITER_SEC;
145                         itimerval.it_interval.tv_usec=ACQUIRE_CABINET_READ_RAW_READ_ITER_USEC;
146                         itimerval.it_value=itimerval.it_interval;
147                         errint=setitimer(
148                                         ITIMER_REAL,    /* which */
149                                         &itimerval,     /* value */
150                                         NULL);  /* ovalue */
151                         g_assert(errint==0);
152                         errvfsresult=gnome_vfs_seek(*acquire_cabinet->handlep,GNOME_VFS_SEEK_START,offset);
153                         if (GNOME_VFS_OK==errvfsresult)
154                                 errvfsresult=gnome_vfs_read(*acquire_cabinet->handlep,buffer,bytes,bytes_read);
155                         }
156                 else
157                         errvfsresult=GNOME_VFS_ERROR_INTERRUPTED;
158                 itimerval.it_interval.tv_sec=0;
159                 itimerval.it_interval.tv_usec=0;
160                 itimerval.it_value=itimerval.it_interval;
161                 errint=setitimer(
162                                 ITIMER_REAL,    /* which */
163                                 &itimerval,     /* value */
164                                 NULL);  /* ovalue */
165                 g_assert(errint==0);
166                 errint=sigaction(
167                                 SIGALRM,        /* signum */
168                                 &oldact,        /* act */
169                                 NULL);  /* oldact */
170                 g_assert(errint==0);
171                 if (errvfsresult==GNOME_VFS_OK) {
172                         g_assert(*bytes_read>0);
173                         return GNOME_VFS_OK;
174                         }
175
176                 /* Reopen '*acquire_cabinet->handlep' */
177                 g_assert(acquire_cabinet->handle_uri!=NULL);
178                 if (GNOME_VFS_OK==(errvfsresult=gnome_vfs_open_uri(&handle_new,acquire_cabinet->handle_uri,
179                                         GNOME_VFS_OPEN_READ|GNOME_VFS_OPEN_RANDOM))) {
180                         gnome_vfs_close(*acquire_cabinet->handlep);     /* errors ignored */
181                         *acquire_cabinet->handlep=handle_new;
182                         }
183                 }
184
185         return GNOME_VFS_ERROR_IO;
186 }
187
188 #define ACQUIRE_CABINET_BYTE_CACHED(acquire_cabinet,pos)     (!(acquire_cabinet)->base_cached \
189                                                            || (acquire_cabinet)->base_cached[(pos)/8] &  1<<((pos)&7))
190 #define ACQUIRE_CABINET_SET_BYTE_CACHED(acquire_cabinet,pos) ((acquire_cabinet)->base_cached[(pos)/8] |= 1<<((pos)&7))
191
192 GnomeVFSResult acquire_cabinet_read
193                 (struct acquire_cabinet *acquire_cabinet,gpointer buffer,GnomeVFSFileSize bytes,GnomeVFSFileSize *bytes_read)
194 {
195 GnomeVFSFileOffset offset_start,offset_end,read_behind;
196 GnomeVFSResult errvfsresult;
197 GnomeVFSFileSize bytes_read_now;
198
199         g_return_val_if_fail(acquire_cabinet!=NULL,GNOME_VFS_ERROR_BAD_PARAMETERS);
200         g_return_val_if_fail(buffer!=NULL || bytes==0,GNOME_VFS_ERROR_BAD_PARAMETERS);
201
202         *bytes_read=0;
203
204         if ((*ui_progress)(NULL))
205                 return GNOME_VFS_ERROR_INTERRUPTED;
206
207         bytes=MAX(0,MIN(bytes,acquire_cabinet->size-acquire_cabinet->offset));
208         if (!bytes)
209                 return GNOME_VFS_ERROR_EOF;
210
211         while (bytes) {
212                 read_behind =acquire_cabinet->offset+bytes;
213
214                 /* GnomeVFS block transfer: */
215                 offset_start=acquire_cabinet->offset;
216                 offset_end  =acquire_cabinet->offset;
217                 while (offset_end<read_behind && !ACQUIRE_CABINET_BYTE_CACHED(acquire_cabinet,offset_end))
218                         offset_end++;
219                 if (offset_end>offset_start) {
220                         errvfsresult=acquire_cabinet_read_raw(acquire_cabinet,
221                                         acquire_cabinet->base+offset_start,offset_end-offset_start,&bytes_read_now,offset_start);
222                         if (errvfsresult!=GNOME_VFS_OK)
223                                 return errvfsresult;
224                         g_assert(bytes_read_now>0);
225                         acquire_cabinet->cabinet_done+=bytes_read_now;
226                         if (ui_progress_bar)
227                                 (*ui_progress_bar)(acquire_cabinet->cabinet_done,acquire_cabinet->cabinet_used);
228                         while (bytes_read_now) {
229                                 ACQUIRE_CABINET_SET_BYTE_CACHED(acquire_cabinet,offset_start);
230                                 offset_start++;
231                                 bytes_read_now--;
232                                 }
233                         }
234
235                 /* Memory block transfer: */
236                 offset_start=acquire_cabinet->offset;
237                 offset_end  =acquire_cabinet->offset;
238                 while (offset_end<read_behind && ACQUIRE_CABINET_BYTE_CACHED(acquire_cabinet,offset_end))
239                         offset_end++;
240                 memcpy(buffer,acquire_cabinet->base+offset_start,offset_end-offset_start);
241                 if (bytes_read)
242                         *bytes_read+=offset_end-offset_start;
243                 buffer+=offset_end-offset_start;
244                 bytes-=offset_end-offset_start;
245                 acquire_cabinet->offset=offset_end;
246                 }
247
248         return GNOME_VFS_OK;
249 }
250
251 static void acquire_cabinet_set_uri(struct acquire_cabinet *acquire_cabinet,GnomeVFSURI *uri)
252 {
253 GnomeVFSURI *uri_cabextract;
254
255         g_return_if_fail(acquire_cabinet!=NULL);
256         g_return_if_fail(uri!=NULL);
257
258         /* FIXME: HACK: Use proper 'cabextract' scheme after it gets implemented.
259          * GnomeVFS will return NULL on gnome_vfs_uri_new() with scheme not available.
260          */
261         uri_cabextract=gnome_vfs_uri_new("file://");
262         g_assert(uri_cabextract->parent==NULL);
263         /* Do not: g_assert(!strcmp(uri_cabextract->method_string,"file"));
264          *         uri_cabextract->method_string=g_strdup("cabextract");
265          * as it will just strip such anchor. FIXME: Why?
266          */
267
268         uri_cabextract->parent=gnome_vfs_uri_dup(uri);
269
270         acquire_cabinet->uri=uri_cabextract;
271         acquire_cabinet->handle_uri=gnome_vfs_uri_ref(uri);
272         acquire_cabinet->filename=gnome_vfs_uri_to_string(acquire_cabinet->uri,GNOME_VFS_URI_HIDE_PASSWORD);
273 }
274
275 struct acquire_cabinet *acquire_cabinet_new_from_memory
276                 (gconstpointer file_base,size_t file_length,GnomeVFSURI *uri,gint cabinet_used)
277 {
278 struct acquire_cabinet *r;
279
280         g_return_val_if_fail(file_base!=NULL,NULL);
281         g_return_val_if_fail(uri!=NULL,NULL);
282         
283         captive_new(r);
284         r->base=(/* de-const */ gpointer)file_base;
285         r->base_cached=NULL;
286         r->offset=0;
287         r->handlep=NULL;
288         r->size=file_length;
289         acquire_cabinet_set_uri(r,uri);
290         r->cabinet_done=0;
291         r->cabinet_used=cabinet_used;
292         r->memory=acquire_cabinet_memory_object_new();
293
294         return r;
295 }
296
297 struct acquire_cabinet *acquire_cabinet_new_from_handle
298                 (GnomeVFSHandle **handlep,GnomeVFSFileInfo *file_info,GnomeVFSURI *uri,gint cabinet_used)
299 {
300 struct acquire_cabinet *r;
301
302         g_return_val_if_fail(handlep!=NULL,NULL);
303         g_return_val_if_fail(*handlep!=NULL,NULL);
304         g_return_val_if_fail(file_info!=NULL,NULL);
305         g_return_val_if_fail(uri!=NULL,NULL);
306         
307         captive_new(r);
308         if (MAP_FAILED==(r->base=mmap(
309                         NULL,   /* start */
310                         CAPTIVE_ROUND_UP64(file_info->size,getpagesize()),      /* length */
311                         PROT_READ|PROT_WRITE,
312                         MAP_ANONYMOUS|MAP_PRIVATE       /* flags */
313                                         |MAP_NORESERVE, /* We will not probably not read the whole cabinet. */
314                         -1,     /* fd; ignored due to MAP_ANONYMOUS */
315                         0))) {  /* offset; ignored due to MAP_ANONYMOUS */
316                 g_free(r);
317                 g_return_val_if_reached(NULL);
318                 }
319         captive_new0n(r->base_cached,CAPTIVE_ROUND_UP64(file_info->size,8)/8);
320         r->offset=0;
321         r->handlep=handlep;
322         r->size=file_info->size;
323         r->cabinet_done=0;
324         r->cabinet_used=cabinet_used;
325
326         acquire_cabinet_set_uri(r,uri);
327         r->memory=acquire_cabinet_memory_object_new();
328
329         return r;
330 }
331
332 void acquire_cabinet_free(struct acquire_cabinet *acquire_cabinet)
333 {
334         g_return_if_fail(acquire_cabinet!=NULL);
335
336         if (acquire_cabinet->base_cached) {
337                 munmap(acquire_cabinet->base,CAPTIVE_ROUND_UP64(acquire_cabinet->size,getpagesize()));  /* errors ignored */
338                 g_free(acquire_cabinet->base_cached);
339                 }
340         g_free((/* de-const */ gchar *)acquire_cabinet->filename);
341         gnome_vfs_uri_unref(acquire_cabinet->uri);
342         gnome_vfs_uri_unref(acquire_cabinet->handle_uri);
343         g_object_unref(acquire_cabinet->memory);
344         g_free(acquire_cabinet);
345 }
346
347 static struct file *file_write_fi_assertion;
348 static GByteArray *file_write_bytearray;
349
350 int file_write(struct file *fi, UBYTE *buf, size_t length)
351 {
352         g_return_val_if_fail(fi!=NULL,0);
353         g_return_val_if_fail(buf!=NULL || length==0,0);
354
355         g_return_val_if_fail(fi==file_write_fi_assertion,0);
356         g_return_val_if_fail(file_write_bytearray!=NULL,0);
357
358         if ((*ui_progress)(NULL))
359                 return 0;
360
361         g_byte_array_append(file_write_bytearray,buf,length);
362
363         return 1;       /* success */
364 }
365
366 void acquire_cabinet_load(struct acquire_cabinet *acquire_cabinet)
367 {
368 struct cabinet *basecab;
369 struct file *filelist,*fi;
370
371         g_return_if_fail(acquire_cabinet!=NULL);
372
373         if (ui_progress_bar)
374                 (*ui_progress_bar)(acquire_cabinet->cabinet_done,acquire_cabinet->cabinet_used);
375
376         if ((*ui_progress)(acquire_cabinet->uri))
377                 return;
378
379         acquire_cabinet_memory_object_push(acquire_cabinet->memory);
380
381         basecab=find_cabs_in_file(acquire_cabinet);
382         if (!basecab)
383                 goto fail_memory_pop;
384         if (basecab->next)
385                 goto fail_memory_pop;
386         if (basecab->prevcab || basecab->nextcab)
387                 goto fail_memory_pop;
388
389         filelist=process_files(basecab);
390
391         for (fi=filelist;fi;fi=fi->next) {
392 gpointer file_buffer;
393 GnomeVFSURI *uri_fi;
394 int errint;
395
396                 if (!captivemodid_module_length_is_valid(fi->length))
397                         continue;
398
399                 uri_fi=gnome_vfs_uri_append_file_name(acquire_cabinet->uri,fi->filename);
400                 if ((*ui_progress)(uri_fi)) {
401                         gnome_vfs_uri_unref(uri_fi);
402                         goto fail_memory_pop;
403                         }
404
405                 file_write_fi_assertion=fi;
406                 file_write_bytearray=g_byte_array_new();
407                 /* extract_file() returns 1 for success. */
408                 errint=extract_file(fi,
409                                 0,      /* lower; ignored now */
410                                 FALSE,  /* fix */
411                                 NULL);  /* dir; ignored now */
412                 if (!errint || fi->length!=file_write_bytearray->len) {
413                         g_byte_array_free(file_write_bytearray,
414                                         TRUE);  /* free_segment */
415                         gnome_vfs_uri_unref(uri_fi);
416                         if (!errint)
417                                 goto fail_memory_pop;
418                         continue;
419                         }
420                 file_buffer=g_byte_array_free(file_write_bytearray,
421                                 FALSE); /* free_segment */
422                 mod_uri_load_file_from_memory(file_buffer,fi->length,uri_fi);
423                 gnome_vfs_uri_unref(uri_fi);
424                 g_free(file_buffer);
425     }
426
427         /* FALLTHRU */
428
429 fail_memory_pop:
430         acquire_cabinet_memory_object_pop(acquire_cabinet->memory);
431 }