|
| 1 | +/* |
| 2 | + $NiH: zip_add_dir.c,v 1.1 2006/10/03 12:23:13 dillo Exp $ |
| 3 | +
|
| 4 | + zip_add_dir.c -- add directory |
| 5 | + Copyright (C) 1999-2006 Dieter Baron and Thomas Klausner |
| 6 | +
|
| 7 | + This file is part of libzip, a library to manipulate ZIP archives. |
| 8 | + The authors can be contacted at <nih@giga.or.at> |
| 9 | +
|
| 10 | + Redistribution and use in source and binary forms, with or without |
| 11 | + modification, are permitted provided that the following conditions |
| 12 | + are met: |
| 13 | + 1. Redistributions of source code must retain the above copyright |
| 14 | + notice, this list of conditions and the following disclaimer. |
| 15 | + 2. Redistributions in binary form must reproduce the above copyright |
| 16 | + notice, this list of conditions and the following disclaimer in |
| 17 | + the documentation and/or other materials provided with the |
| 18 | + distribution. |
| 19 | + 3. The names of the authors may not be used to endorse or promote |
| 20 | + products derived from this software without specific prior |
| 21 | + written permission. |
| 22 | + |
| 23 | + THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
| 24 | + OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 25 | + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 | + ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 27 | + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 | + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 29 | + GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 30 | + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 31 | + IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 32 | + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 33 | + IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | +*/ |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +#include <stdlib.h> |
| 39 | +#include <string.h> |
| 40 | + |
| 41 | +#include "zip.h" |
| 42 | +#include "zipint.h" |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +int |
| 47 | +zip_add_dir(struct zip *za, const char *name) |
| 48 | +{ |
| 49 | + int len, ret; |
| 50 | + char *s; |
| 51 | + struct zip_source *source; |
| 52 | + |
| 53 | + if (name == NULL) { |
| 54 | + _zip_error_set(&za->error, ZIP_ER_INVAL, 0); |
| 55 | + return -1; |
| 56 | + } |
| 57 | + |
| 58 | + s = NULL; |
| 59 | + len = strlen(name); |
| 60 | + |
| 61 | + if (name[len-1] != '/') { |
| 62 | + if ((s=(char *)malloc(len+2)) == NULL) { |
| 63 | + _zip_error_set(&za->error, ZIP_ER_MEMORY, 0); |
| 64 | + return -1; |
| 65 | + } |
| 66 | + strcpy(s, name); |
| 67 | + s[len] = '/'; |
| 68 | + s[len+1] = '\0'; |
| 69 | + } |
| 70 | + |
| 71 | + if ((source=zip_source_buffer(za, NULL, 0, 0)) == NULL) { |
| 72 | + free(s); |
| 73 | + return -1; |
| 74 | + } |
| 75 | + |
| 76 | + ret = _zip_replace(za, -1, s ? s : name, source); |
| 77 | + |
| 78 | + free(s); |
| 79 | + if (ret < 0) |
| 80 | + zip_source_free(source); |
| 81 | + |
| 82 | + return ret; |
| 83 | +} |
0 commit comments