/src/libzip/lib/zip_file_set_external_attributes.c
Line | Count | Source |
1 | | /* |
2 | | zip_file_set_external_attributes.c -- set external attributes for entry |
3 | | Copyright (C) 2013-2024 Dieter Baron and Thomas Klausner |
4 | | |
5 | | This file is part of libzip, a library to manipulate ZIP archives. |
6 | | The authors can be contacted at <info@libzip.org> |
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 | | |
36 | 6.80k | ZIP_EXTERN int zip_file_set_external_attributes(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_uint8_t opsys, zip_uint32_t attributes) { |
37 | 6.80k | zip_entry_t *e; |
38 | 6.80k | int changed; |
39 | 6.80k | zip_uint8_t unchanged_opsys; |
40 | 6.80k | zip_uint32_t unchanged_attributes; |
41 | | |
42 | 6.80k | if (_zip_get_dirent(za, idx, 0, NULL) == NULL) { |
43 | 0 | return -1; |
44 | 0 | } |
45 | | |
46 | 6.80k | if (ZIP_IS_RDONLY(za)) { |
47 | 0 | zip_error_set(&za->error, ZIP_ER_RDONLY, 0); |
48 | 0 | return -1; |
49 | 0 | } |
50 | 6.80k | if (ZIP_WANT_TORRENTZIP(za)) { |
51 | 0 | zip_error_set(&za->error, ZIP_ER_NOT_ALLOWED, 0); |
52 | 0 | return -1; |
53 | 0 | } |
54 | | |
55 | 6.80k | e = za->entry + idx; |
56 | | |
57 | 6.80k | unchanged_opsys = (e->orig ? (zip_uint8_t)(e->orig->version_madeby >> 8) : (zip_uint8_t)ZIP_OPSYS_DEFAULT); |
58 | 6.80k | unchanged_attributes = e->orig ? e->orig->ext_attrib : ZIP_EXT_ATTRIB_DEFAULT; |
59 | | |
60 | 6.80k | changed = (opsys != unchanged_opsys || attributes != unchanged_attributes); |
61 | | |
62 | 6.80k | if (changed) { |
63 | 6.80k | if (e->changes == NULL) { |
64 | 0 | if ((e->changes = _zip_dirent_clone(e->orig)) == NULL) { |
65 | 0 | zip_error_set(&za->error, ZIP_ER_MEMORY, 0); |
66 | 0 | return -1; |
67 | 0 | } |
68 | 0 | } |
69 | 6.80k | e->changes->version_madeby = (zip_uint16_t)((opsys << 8) | (e->changes->version_madeby & 0xff)); |
70 | 6.80k | e->changes->ext_attrib = attributes; |
71 | 6.80k | e->changes->changed |= ZIP_DIRENT_ATTRIBUTES; |
72 | 6.80k | } |
73 | 0 | else if (e->changes) { |
74 | 0 | e->changes->changed &= ~ZIP_DIRENT_ATTRIBUTES; |
75 | 0 | if (e->changes->changed == 0) { |
76 | 0 | _zip_dirent_free(e->changes); |
77 | 0 | e->changes = NULL; |
78 | 0 | } |
79 | 0 | else { |
80 | 0 | e->changes->version_madeby = (zip_uint16_t)((unchanged_opsys << 8) | (e->changes->version_madeby & 0xff)); |
81 | 0 | e->changes->ext_attrib = unchanged_attributes; |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | 6.80k | return 0; |
86 | 6.80k | } |