~ubuntu-branches/ubuntu/natty/zziplib/natty

« back to all changes in this revision

Viewing changes to zzip/write.c

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2009-03-23 21:13:57 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090323211357-6j35t8532ie9wlxf
Tags: 0.13.50-1
* New upstream version 
  - Update 01-fetch.patch
* debhelper compat version is 7
* Standards version is 3.8.1
* Run dh_prep instead of dh_clean -k
* Add debian/watch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
1
2
/*
2
3
 * The write-support in zziplib is not a full-flegded interface to the
3
4
 * internals that zip file-header or zip archive an contain. It's
26
27
 
27
28
#define _ZZIP_WRITE_SOURCE
28
29
 
29
 
#if defined DDDD || defined DDDDD || defined DDDDDD || defined DDDDDDD 
 
30
#if defined DDDD || defined DDDDD || defined DDDDDD || defined DDDDDDD
30
31
#define _ZZIP_ENABLE_WRITE
31
32
#else /* per default, we add support for passthrough to posix write */
32
33
#define _ZZIP_POSIX_WRITE
33
34
#endif
34
35
 
35
 
#include <zzip/write.h>                   /* #includes <zzip/lib.h> */
 
36
#include <zzip/write.h>         /* #includes <zzip/lib.h> */
36
37
#include <zzip/file.h>
37
38
 
38
39
#include <string.h>
96
97
 * EEXIST errors from => mkdir(2) are suppressed. (fixme: delete the
97
98
 * given subtree? like suggested by O_TRUNC? not done so far!)
98
99
 */
99
 
ZZIP_DIR*
100
 
zzip_dir_creat(zzip_char_t* name, int o_mode)
 
100
ZZIP_DIR *
 
101
zzip_dir_creat(zzip_char_t * name, int o_mode)
101
102
{
102
 
    return zzip_dir_creat_ext_io (name, o_mode, 0, 0);
 
103
    return zzip_dir_creat_ext_io(name, o_mode, 0, 0);
103
104
}
104
105
 
105
106
/** => zzip_dir_creat
116
117
 * Write-support will extend => zzip_closedir with semantics to finalize the
117
118
 * zip-archive by writing the zip-trailer and closing the archive file.
118
119
 */
119
 
ZZIP_DIR*
120
 
zzip_dir_creat_ext_io(zzip_char_t* name, int o_mode, 
121
 
                      zzip_strings_t* ext, zzip_plugin_io_t io)
 
120
ZZIP_DIR *
 
121
zzip_dir_creat_ext_io(zzip_char_t * name, int o_mode,
 
122
                      zzip_strings_t * ext, zzip_plugin_io_t io)
122
123
{
123
 
    if (! io) io = zzip_get_default_io ();
 
124
    if (! io)
 
125
        io = zzip_get_default_io();
124
126
 
125
127
    if (io != zzip_get_default_io())
126
 
    {  /* the current io-structure does not contain a "write" entry,
127
 
        * and therefore this parameter is useless. Anyone to expect
128
 
        * some behavior should be warned, so here we let the function
129
 
        * fail bluntly - and leaving the recovery to the application
130
 
        */
 
128
    {
 
129
        /* the current io-structure does not contain a "write" entry,
 
130
         * and therefore this parameter is useless. Anyone to expect
 
131
         * some behavior should be warned, so here we let the function
 
132
         * fail bluntly - and leaving the recovery to the application
 
133
         */
131
134
        errno = EINVAL;
132
135
        return 0;
133
136
    }
134
137
 
135
138
 
136
139
    if (! _ZZIP_TRY)
137
 
    {  /* not implemented - however, we respect that a null argument to 
138
 
        * zzip_mkdir and zzip_creat works, so we silently still do the mkdir 
139
 
        */
140
 
        if (! _mkdir (name, o_mode) || errno == EEXIST)
 
140
    {
 
141
        /* not implemented - however, we respect that a null argument to 
 
142
         * zzip_mkdir and zzip_creat works, so we silently still do the mkdir 
 
143
         */
 
144
        if (! _mkdir(name, o_mode) || errno == EEXIST)
141
145
            errno = EROFS;
142
146
        return 0;
143
 
    } else {
 
147
    } else
 
148
    {
144
149
#       define MAX_EXT_LEN 10
145
 
        ZZIP_DIR* dir = zzip_dir_alloc (ext);
 
150
        ZZIP_DIR *dir = zzip_dir_alloc(ext);
146
151
        int name_len = strlen(name);
147
 
        dir->realname = malloc (name_len+MAX_EXT_LEN); 
148
 
        if (! dir->realname) goto error;
149
 
 
150
 
        memcpy (dir->realname, name, name_len+1);
151
 
        ___ int fd = __zzip_try_open (
152
 
            dir->realname, O_EXCL|O_TRUNC|O_WRONLY, ext, io);
153
 
        if (fd != -1) { dir->fd = fd; return dir; }
154
 
 
155
 
        ___ zzip_strings_t* exx = ext; int exx_len;
156
 
        for (; *exx ; exx++)
 
152
        dir->realname = malloc(name_len + MAX_EXT_LEN);
 
153
        if (! dir->realname)
 
154
            goto error;
 
155
 
 
156
        memcpy(dir->realname, name, name_len + 1);
 
157
        ___ int fd =
 
158
            __zzip_try_open(dir->realname, O_EXCL | O_TRUNC | O_WRONLY, ext,
 
159
                            io);
 
160
        if (fd != -1)
 
161
            { dir->fd = fd; return dir; }
 
162
 
 
163
        ___ zzip_strings_t *exx = ext;
 
164
        int exx_len;
 
165
        for (; *exx; exx++)
157
166
        {
158
 
            if ((exx_len = strlen (*exx)+1) <= name_len &&
159
 
                ! memcmp (dir->realname+(name_len-exx_len), *exx, exx_len))
160
 
                break; /* keep unmodified */
161
 
            exx++; if (*exx) continue;
 
167
            if ((exx_len = strlen(*exx) + 1) <= name_len &&
 
168
                ! memcmp(dir->realname + (name_len - exx_len), *exx, exx_len))
 
169
                break;          /* keep unmodified */
 
170
            exx++;
 
171
            if (*exx)
 
172
                continue;
162
173
 
163
 
            if (! (exx_len = strlen (*exx)) || exx_len >= MAX_EXT_LEN) break; 
164
 
            memcpy (dir->realname+name_len, exx, exx_len); /* append! */
165
 
        }____;
166
 
        fd  = io->fd.open (dir->realname, O_CREAT|O_TRUNC|O_WRONLY, o_mode);
 
174
            if (! (exx_len = strlen(*exx)) || exx_len >= MAX_EXT_LEN)
 
175
                break;
 
176
            memcpy(dir->realname + name_len, exx, exx_len);     /* append! */
 
177
        }
 
178
        ____;
 
179
        fd = io->fd.open(dir->realname, O_CREAT | O_TRUNC | O_WRONLY, o_mode);
167
180
        dir->realname[name_len] = '\0'; /* keep ummodified */
168
 
        if (fd != -1) { dir->fd = fd; return dir; } 
169
 
     error:
170
 
        zzip_dir_free (dir); return 0;
 
181
        if (fd != -1)
 
182
            { dir->fd = fd; return dir; }
 
183
      error:
 
184
        zzip_dir_free(dir);
 
185
        return 0;
171
186
        ____;
172
187
    }
173
188
}
203
218
 * EEXIST errors from => mkdir(2) are suppressed. (fixme: delete the
204
219
 * given subtree? like suggested by O_TRUNC? not done so far!)
205
220
 */
206
 
ZZIP_DIR*
207
 
zzip_createdir(zzip_char_t* name, int o_mode)
 
221
ZZIP_DIR *
 
222
zzip_createdir(zzip_char_t * name, int o_mode)
208
223
{
209
224
    if (o_mode & S_IWGRP)
210
225
    {
211
 
        if (-1 == _mkdir(name, o_mode) && errno != EEXIST) /* fail */
 
226
        if (-1 == _mkdir(name, o_mode) && errno != EEXIST)      /* fail */
212
227
            return 0;
213
 
        return zzip_opendir (name);
 
228
        return zzip_opendir(name);
214
229
    } else
215
 
        return zzip_dir_creat (name, o_mode);
 
230
        return zzip_dir_creat(name, o_mode);
216
231
}
217
232
 
218
233
/** => zzip_file_creat              also: mkdir(2), creat(2), zzip_dir_creat
248
263
   zzip_closedir (zip_savefile);
249
264
 */
250
265
int
251
 
zzip_file_mkdir(ZZIP_DIR* dir, zzip_char_t* name, int o_mode)
 
266
zzip_file_mkdir(ZZIP_DIR * dir, zzip_char_t * name, int o_mode)
252
267
{
253
268
    if (! dir)
254
269
        return _mkdir(name, o_mode);
255
270
 
256
271
    if (! _ZZIP_TRY)
257
 
    {/* not implemented */
 
272
    {                           /* not implemented */
258
273
        errno = EROFS;
259
274
        return -1;
260
 
    } else {
 
275
    } else
 
276
    {
261
277
        errno = EROFS;
262
278
        return -1;
263
279
    }
279
295
 * a zip archive using O_RDONLY (and similar stuff) will surely lead to 
280
296
 * an error.
281
297
 */
282
 
ZZIP_FILE*
283
 
zzip_file_creat(ZZIP_DIR* dir, zzip_char_t* name, int o_mode)
 
298
ZZIP_FILE *
 
299
zzip_file_creat(ZZIP_DIR * dir, zzip_char_t * name, int o_mode)
284
300
{
285
301
    if (! dir)
286
 
        return zzip_open (name, o_mode);
 
302
        return zzip_open(name, o_mode);
287
303
 
288
304
    if (! _ZZIP_TRY)
289
 
    {/* not implemented */
 
305
    {                           /* not implemented */
290
306
        errno = EROFS;
291
307
        return 0;
292
 
    } else {
 
308
    } else
 
309
    {
293
310
        errno = EROFS;
294
311
        return 0;
295
312
    }
304
321
 * using => zlib(3) and appended to the zip archive file.
305
322
 */
306
323
zzip_ssize_t
307
 
zzip_write(ZZIP_FILE* file, const void* ptr, zzip_size_t len) 
 
324
zzip_write(ZZIP_FILE * file, const void *ptr, zzip_size_t len)
308
325
{
309
326
    if (zzip_file_real(file))
310
 
        return write (zzip_realfd (file), ptr, len);
 
327
        return write(zzip_realfd(file), ptr, len);
311
328
    else
312
 
        return zzip_file_write (file, ptr, len);
 
329
        return zzip_file_write(file, ptr, len);
313
330
}
314
331
 
315
332
/** => zzip_write                            also: zzip_file_creat
323
340
 * It returns immediately -1 and sets errno=EROFS for indication.
324
341
 */
325
342
zzip_ssize_t
326
 
zzip_file_write(ZZIP_FILE* file, const void* ptr, zzip_size_t len) 
 
343
zzip_file_write(ZZIP_FILE * file, const void *ptr, zzip_size_t len)
327
344
{
328
345
    if (! _ZZIP_TRY)
329
 
    {/* not implemented */
 
346
    {                           /* not implemented */
330
347
        errno = EROFS;
331
348
        return -1;
332
 
    } else {
 
349
    } else
 
350
    {
333
351
        /* add calls to zlib here... */
334
352
        errno = EROFS;
335
353
        return -1;
348
366
 * the (still-open) ZZIP_DIR handle.
349
367
 */
350
368
zzip_size_t
351
 
zzip_fwrite(const void* ptr, zzip_size_t len, zzip_size_t multiply, 
352
 
            ZZIP_FILE* file) 
 
369
zzip_fwrite(const void *ptr, zzip_size_t len, zzip_size_t multiply,
 
370
            ZZIP_FILE * file)
353
371
{
354
 
    zzip_ssize_t value = zzip_write (file, ptr, len * multiply);
355
 
    if (value == -1) 
 
372
    zzip_ssize_t value = zzip_write(file, ptr, len * multiply);
 
373
    if (value == -1)
356
374
        value = 0;
357
375
    return (zzip_size_t) value;
358
376
}
359
377
 
360
 
#if 0 /* pure documentation */
 
378
#if 0                           /* pure documentation */
 
379
 
361
380
/** create a zipped file/directory            also: zzip_dir_creat, mkdir(2)
362
381
 *
363
382
 * This function creates a directory entry in the default zip-archive. 
376
395
 *
377
396
 */
378
397
int inline
379
 
zzip_mkdir(zzip_char_t* name, int o_mode)
 
398
zzip_mkdir(zzip_char_t * name, int o_mode)
380
399
{
381
400
    return zzip_file_creat(zzip_savefile, name, mode);
382
401
}
383
402
#endif
384
403
 
385
 
#if 0 /* pure documentation */
 
404
#if 0                           /* pure documentation */
 
405
 
386
406
/** => zzip_mkdir                 also: creat(2), zzip_start
387
407
 *
388
408
 * This function creates a file in the default zip-archive. 
398
418
 #define zzip_creat(name,mode) \ -
399
419
         zzip_file_creat(zzip_savefile,name,mode)
400
420
 */
401
 
ZZIP_FILE* inline
402
 
zzip_creat(zzip_char_t* name, int o_mode)
 
421
ZZIP_FILE *inline
 
422
zzip_creat(zzip_char_t * name, int o_mode)
403
423
{
404
424
    return zzip_file_creat(zzip_savefile, name, mode);
405
425
}
406
426
#endif
407
427
 
408
428
 
409
 
#if 0 /* pure documentation */
 
429
#if 0                           /* pure documentation */
 
430
 
410
431
/** start writing to the magic zzip_savefile   also: zzip_creat, zzip_write
411
432
 * 
412
433
 * open a zip archive for writing via the magic zzip_savefile macro
431
452
 * on the real filesystem.
432
453
 */
433
454
void inline
434
 
zzip_mkfifo(zzip_char_t* name, int o_mode)
435
 
{   
436
 
    if (zzip_savefile) zzip_closedir (zzip_savefile); 
 
455
zzip_mkfifo(zzip_char_t * name, int o_mode)
 
456
{
 
457
    if (zzip_savefile)
 
458
        zzip_closedir(zzip_savefile);
437
459
    zzip_savefile = zzip_createdir(name, o_mode);
438
460
}
439
461
#endif
440
462
 
441
 
#if 0 /* pure documentation */
 
463
#if 0                           /* pure documentation */
 
464
 
442
465
/** => zzip_mkfifo                        also: zzip_closedir, sync(2)
443
466
 * 
444
467
 * finalize a zip archive thereby writing the central directory to
456
479
 *
457
480
 */
458
481
void inline
459
 
zzip_sync(void) 
460
 
{   
461
 
    zzip_closedir (zzip_savefile); zzip_savefile = 0; 
 
482
zzip_sync(void)
 
483
{
 
484
    zzip_closedir(zzip_savefile);
 
485
    zzip_savefile = 0;
462
486
}
463
487
#endif
464
488