Dieter Baron | 504965c | 2013-09-18 19:37:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | zip_file_set_external_attributes.c -- set external attributes for entry |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 3 | Copyright (C) 2013-2014 Dieter Baron and Thomas Klausner |
Dieter Baron | 504965c | 2013-09-18 19:37:22 +0200 | [diff] [blame] | 4 | |
| 5 | This file is part of libzip, a library to manipulate ZIP archives. |
| 6 | The authors can be contacted at <libzip@nih.at> |
| 7 | |
| 8 | Redistribution and use in source and binary forms, with or without |
| 9 | modification, are permitted provided that the following conditions |
| 10 | are met: |
| 11 | 1. Redistributions of source code must retain the above copyright |
| 12 | notice, this list of conditions and the following disclaimer. |
| 13 | 2. Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in |
| 15 | the documentation and/or other materials provided with the |
| 16 | distribution. |
| 17 | 3. The names of the authors may not be used to endorse or promote |
| 18 | products derived from this software without specific prior |
| 19 | written permission. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
| 22 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 25 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 27 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 29 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 30 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 31 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include "zipint.h" |
| 35 | |
Dieter Baron | 3e00f02 | 2014-10-11 01:12:19 +0200 | [diff] [blame] | 36 | ZIP_EXTERN int |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 37 | zip_file_set_external_attributes(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_uint8_t opsys, zip_uint32_t attributes) |
Dieter Baron | 504965c | 2013-09-18 19:37:22 +0200 | [diff] [blame] | 38 | { |
Dieter Baron | 1d9dfeb | 2014-09-28 23:02:54 +0200 | [diff] [blame] | 39 | zip_entry_t *e; |
Dieter Baron | 504965c | 2013-09-18 19:37:22 +0200 | [diff] [blame] | 40 | int changed; |
| 41 | zip_uint8_t unchanged_opsys; |
| 42 | zip_uint32_t unchanged_attributes; |
| 43 | |
| 44 | if (_zip_get_dirent(za, idx, 0, NULL) == NULL) |
| 45 | return -1; |
| 46 | |
| 47 | if (ZIP_IS_RDONLY(za)) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 48 | zip_error_set(&za->error, ZIP_ER_RDONLY, 0); |
Dieter Baron | 504965c | 2013-09-18 19:37:22 +0200 | [diff] [blame] | 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | e = za->entry+idx; |
| 53 | |
Thomas Klausner | 85dc6a5 | 2014-09-24 01:10:05 +0200 | [diff] [blame] | 54 | unchanged_opsys = (e->orig ? (zip_uint8_t)(e->orig->version_madeby>>8) : (zip_uint8_t)ZIP_OPSYS_DEFAULT); |
Dieter Baron | 504965c | 2013-09-18 19:37:22 +0200 | [diff] [blame] | 55 | unchanged_attributes = e->orig ? e->orig->ext_attrib : ZIP_EXT_ATTRIB_DEFAULT; |
| 56 | |
| 57 | changed = (opsys != unchanged_opsys || attributes != unchanged_attributes); |
| 58 | |
| 59 | if (changed) { |
| 60 | if (e->changes == NULL) { |
| 61 | if ((e->changes=_zip_dirent_clone(e->orig)) == NULL) { |
Thomas Klausner | ea8ba49 | 2014-09-23 16:54:47 +0200 | [diff] [blame] | 62 | zip_error_set(&za->error, ZIP_ER_MEMORY, 0); |
Dieter Baron | 504965c | 2013-09-18 19:37:22 +0200 | [diff] [blame] | 63 | return -1; |
| 64 | } |
| 65 | } |
Thomas Klausner | 03ca1c1 | 2014-09-24 01:02:15 +0200 | [diff] [blame] | 66 | e->changes->version_madeby = (zip_uint16_t)((opsys << 8) | (e->changes->version_madeby & 0xff)); |
Dieter Baron | 504965c | 2013-09-18 19:37:22 +0200 | [diff] [blame] | 67 | e->changes->ext_attrib = attributes; |
| 68 | e->changes->changed |= ZIP_DIRENT_ATTRIBUTES; |
| 69 | } |
| 70 | else if (e->changes) { |
| 71 | e->changes->changed &= ~ZIP_DIRENT_ATTRIBUTES; |
| 72 | if (e->changes->changed == 0) { |
| 73 | _zip_dirent_free(e->changes); |
| 74 | e->changes = NULL; |
| 75 | } |
| 76 | else { |
Thomas Klausner | 5d56de9 | 2014-10-08 14:39:52 +0200 | [diff] [blame] | 77 | e->changes->version_madeby = (zip_uint16_t)((unchanged_opsys << 8) | (e->changes->version_madeby & 0xff)); |
Dieter Baron | 504965c | 2013-09-18 19:37:22 +0200 | [diff] [blame] | 78 | e->changes->ext_attrib = unchanged_attributes; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |