Merge pull request #115946 from bruvzg/zip_pack_pt
[ZIPPacker] Add support for Unix permissions and modification time.
This commit is contained in:
commit
a1604ab74c
5 changed files with 162 additions and 16 deletions
5
misc/extension_api_validation/4.6-stable/GH-115946.txt
Normal file
5
misc/extension_api_validation/4.6-stable/GH-115946.txt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
GH-115946
|
||||
---------
|
||||
Validate extension JSON: Error: Field 'classes/ZIPPacker/methods/start_file/arguments': size changed value in new API, from 1 to 3.
|
||||
|
||||
Optional argument added. Compatibility method registered.
|
||||
|
|
@ -23,6 +23,16 @@
|
|||
<tutorials>
|
||||
</tutorials>
|
||||
<methods>
|
||||
<method name="add_directory">
|
||||
<return type="int" enum="Error" />
|
||||
<param index="0" name="path" type="String" />
|
||||
<param index="1" name="permissions" type="int" enum="FileAccess.UnixPermissionFlags" is_bitfield="true" default="493" />
|
||||
<param index="2" name="modified_time" type="int" default="0" />
|
||||
<description>
|
||||
Adds directory to the archive. If [param modified_time] is set to [code]0[/code], current system time is used.
|
||||
[b]Note:[/b] Directories are automatically created when [method start_file] is called, use this function before adding files to create directories with custom permissions and modification time.
|
||||
</description>
|
||||
</method>
|
||||
<method name="close">
|
||||
<return type="int" enum="Error" />
|
||||
<description>
|
||||
|
|
@ -48,8 +58,10 @@
|
|||
<method name="start_file">
|
||||
<return type="int" enum="Error" />
|
||||
<param index="0" name="path" type="String" />
|
||||
<param index="1" name="permissions" type="int" enum="FileAccess.UnixPermissionFlags" is_bitfield="true" default="420" />
|
||||
<param index="2" name="modified_time" type="int" default="0" />
|
||||
<description>
|
||||
Starts writing to a file within the archive. Only one file can be written at the same time.
|
||||
Starts writing to a file within the archive. Only one file can be written at the same time. If [param modified_time] is set to [code]0[/code], current system time is used.
|
||||
Must be called after [method open].
|
||||
</description>
|
||||
</method>
|
||||
|
|
|
|||
41
modules/zip/zip_packer.compat.inc
Normal file
41
modules/zip/zip_packer.compat.inc
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/**************************************************************************/
|
||||
/* zip_packer.compat.inc */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
|
||||
Error ZIPPacker::_start_file_bind_compat_115946(const String &p_path) {
|
||||
return start_file(p_path, 0644, 0);
|
||||
}
|
||||
|
||||
void ZIPPacker::_bind_compatibility_methods() {
|
||||
ClassDB::bind_compatibility_method(D_METHOD("start_file", "path"), &ZIPPacker::_start_file_bind_compat_115946);
|
||||
}
|
||||
|
||||
#endif // DISABLE_DEPRECATED
|
||||
|
|
@ -29,9 +29,10 @@
|
|||
/**************************************************************************/
|
||||
|
||||
#include "zip_packer.h"
|
||||
#include "zip_packer.compat.inc"
|
||||
|
||||
#include "core/io/zip_io.h"
|
||||
#include "core/os/os.h"
|
||||
#include "core/os/time.h"
|
||||
|
||||
Error ZIPPacker::open(const String &p_path, ZipAppend p_append) {
|
||||
if (fa.is_valid()) {
|
||||
|
|
@ -49,6 +50,7 @@ Error ZIPPacker::close() {
|
|||
Error err = zipClose(zf, nullptr) == ZIP_OK ? OK : FAILED;
|
||||
if (err == OK) {
|
||||
DEV_ASSERT(fa.is_null());
|
||||
directories.clear();
|
||||
zf = nullptr;
|
||||
}
|
||||
|
||||
|
|
@ -64,22 +66,40 @@ int ZIPPacker::get_compression_level() const {
|
|||
return compression_level;
|
||||
}
|
||||
|
||||
Error ZIPPacker::start_file(const String &p_path) {
|
||||
Error ZIPPacker::start_file(const String &p_path, BitField<FileAccess::UnixPermissionFlags> p_permissions, uint64_t p_modified_time) {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
|
||||
|
||||
if (!p_path.get_base_dir().is_empty() && !directories.has(p_path.get_base_dir())) {
|
||||
add_directory(p_path.get_base_dir(), 0755, p_modified_time);
|
||||
}
|
||||
|
||||
uint64_t time = p_modified_time;
|
||||
if (time == 0) {
|
||||
time = Time::get_singleton()->get_unix_time_from_system();
|
||||
}
|
||||
Dictionary tz = Time::get_singleton()->get_time_zone_from_system();
|
||||
time += tz["bias"].operator int() * 60;
|
||||
Dictionary dt = Time::get_singleton()->get_datetime_dict_from_unix_time(time);
|
||||
|
||||
zip_fileinfo zipfi;
|
||||
|
||||
OS::DateTime time = OS::get_singleton()->get_datetime();
|
||||
|
||||
zipfi.tmz_date.tm_sec = time.second;
|
||||
zipfi.tmz_date.tm_min = time.minute;
|
||||
zipfi.tmz_date.tm_hour = time.hour;
|
||||
zipfi.tmz_date.tm_mday = time.day;
|
||||
zipfi.tmz_date.tm_mon = time.month - 1;
|
||||
zipfi.tmz_date.tm_year = time.year;
|
||||
zipfi.tmz_date.tm_year = dt["year"];
|
||||
zipfi.tmz_date.tm_mon = dt["month"].operator int() - 1; // Note: "tm" month range - 0..11, Godot month range - 1..12, https://www.cplusplus.com/reference/ctime/tm/
|
||||
zipfi.tmz_date.tm_mday = dt["day"];
|
||||
zipfi.tmz_date.tm_hour = dt["hour"];
|
||||
zipfi.tmz_date.tm_min = dt["minute"];
|
||||
zipfi.tmz_date.tm_sec = dt["second"];
|
||||
zipfi.dosDate = 0;
|
||||
|
||||
// 0100000: regular file type
|
||||
// 0000644: permissions rw-r--r--
|
||||
uint32_t _mode = p_permissions;
|
||||
if (_mode == 0) {
|
||||
_mode = 0100644;
|
||||
} else {
|
||||
_mode |= 0100000;
|
||||
}
|
||||
zipfi.external_fa = (_mode << 16L) | ((_mode & 0200) ? 0 : 1); // UUUUUUUU UUUUUUUU 00000000 00ADVSHR: Unix permissions (U) + DOS read-only flag (R).
|
||||
zipfi.internal_fa = 0;
|
||||
zipfi.external_fa = 0;
|
||||
|
||||
int err = zipOpenNewFileInZip4(zf,
|
||||
p_path.utf8().get_data(),
|
||||
|
|
@ -97,7 +117,7 @@ Error ZIPPacker::start_file(const String &p_path) {
|
|||
Z_DEFAULT_STRATEGY,
|
||||
nullptr,
|
||||
0,
|
||||
0, // "version made by", indicates the compatibility of the file attribute information (the `external_fa` field above).
|
||||
0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
|
||||
1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
|
||||
return err == ZIP_OK ? OK : FAILED;
|
||||
}
|
||||
|
|
@ -114,12 +134,72 @@ Error ZIPPacker::close_file() {
|
|||
return zipCloseFileInZip(zf) == ZIP_OK ? OK : FAILED;
|
||||
}
|
||||
|
||||
Error ZIPPacker::add_directory(const String &p_path, BitField<FileAccess::UnixPermissionFlags> p_permissions, uint64_t p_modified_time) {
|
||||
ERR_FAIL_COND_V_MSG(fa.is_null(), FAILED, "ZIPPacker must be opened before use.");
|
||||
ERR_FAIL_COND_V_MSG(directories.has(p_path), ERR_CANT_CREATE, vformat("Directory '%s' already exists.", p_path));
|
||||
|
||||
uint64_t time = p_modified_time;
|
||||
if (time == 0) {
|
||||
time = Time::get_singleton()->get_unix_time_from_system();
|
||||
}
|
||||
Dictionary tz = Time::get_singleton()->get_time_zone_from_system();
|
||||
time += tz["bias"].operator int() * 60;
|
||||
Dictionary dt = Time::get_singleton()->get_datetime_dict_from_unix_time(time);
|
||||
|
||||
zip_fileinfo zipfi;
|
||||
zipfi.tmz_date.tm_year = dt["year"];
|
||||
zipfi.tmz_date.tm_mon = dt["month"].operator int() - 1; // Note: "tm" month range - 0..11, Godot month range - 1..12, https://www.cplusplus.com/reference/ctime/tm/
|
||||
zipfi.tmz_date.tm_mday = dt["day"];
|
||||
zipfi.tmz_date.tm_hour = dt["hour"];
|
||||
zipfi.tmz_date.tm_min = dt["minute"];
|
||||
zipfi.tmz_date.tm_sec = dt["second"];
|
||||
zipfi.dosDate = 0;
|
||||
|
||||
// 0040000: directory file type
|
||||
// 0000755: permissions rwxr-xr-x
|
||||
uint32_t _mode = p_permissions;
|
||||
if (_mode == 0) {
|
||||
_mode = 0040755;
|
||||
} else {
|
||||
_mode |= 0040000;
|
||||
}
|
||||
zipfi.external_fa = (_mode << 16L) | 0x10 | ((_mode & 0200) ? 0 : 1); // UUUUUUUU UUUUUUUU 00000000 00ADVSHR: Unix permissions (U) + DOS directory flag (D) + DOS read-only flag (R).
|
||||
zipfi.internal_fa = 0;
|
||||
|
||||
int err = zipOpenNewFileInZip4(zf,
|
||||
p_path.utf8().get_data(),
|
||||
&zipfi,
|
||||
nullptr,
|
||||
0,
|
||||
nullptr,
|
||||
0,
|
||||
nullptr,
|
||||
Z_DEFLATED,
|
||||
compression_level,
|
||||
0,
|
||||
-MAX_WBITS,
|
||||
DEF_MEM_LEVEL,
|
||||
Z_DEFAULT_STRATEGY,
|
||||
nullptr,
|
||||
0,
|
||||
0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions
|
||||
1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8.
|
||||
zipCloseFileInZip(zf);
|
||||
if (err != ZIP_OK) {
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
directories.insert(p_path);
|
||||
return OK;
|
||||
}
|
||||
|
||||
void ZIPPacker::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("open", "path", "append"), &ZIPPacker::open, DEFVAL(Variant(APPEND_CREATE)));
|
||||
ClassDB::bind_method(D_METHOD("set_compression_level", "compression_level"), &ZIPPacker::set_compression_level);
|
||||
ClassDB::bind_method(D_METHOD("get_compression_level"), &ZIPPacker::get_compression_level);
|
||||
ADD_PROPERTY(PropertyInfo(Variant::INT, "compression_level"), "set_compression_level", "get_compression_level");
|
||||
ClassDB::bind_method(D_METHOD("start_file", "path"), &ZIPPacker::start_file);
|
||||
ClassDB::bind_method(D_METHOD("add_directory", "path", "permissions", "modified_time"), &ZIPPacker::add_directory, DEFVAL(0755), DEFVAL(0));
|
||||
ClassDB::bind_method(D_METHOD("start_file", "path", "permissions", "modified_time"), &ZIPPacker::start_file, DEFVAL(0644), DEFVAL(0));
|
||||
ClassDB::bind_method(D_METHOD("write_file", "data"), &ZIPPacker::write_file);
|
||||
ClassDB::bind_method(D_METHOD("close_file"), &ZIPPacker::close_file);
|
||||
ClassDB::bind_method(D_METHOD("close"), &ZIPPacker::close);
|
||||
|
|
|
|||
|
|
@ -41,10 +41,16 @@ class ZIPPacker : public RefCounted {
|
|||
Ref<FileAccess> fa;
|
||||
zipFile zf = nullptr;
|
||||
int compression_level = Z_DEFAULT_COMPRESSION;
|
||||
HashSet<String> directories;
|
||||
|
||||
protected:
|
||||
static void _bind_methods();
|
||||
|
||||
#ifndef DISABLE_DEPRECATED
|
||||
Error _start_file_bind_compat_115946(const String &p_path);
|
||||
static void _bind_compatibility_methods();
|
||||
#endif
|
||||
|
||||
public:
|
||||
enum ZipAppend {
|
||||
APPEND_CREATE = 0,
|
||||
|
|
@ -65,10 +71,12 @@ public:
|
|||
void set_compression_level(int p_compression_level);
|
||||
int get_compression_level() const;
|
||||
|
||||
Error start_file(const String &p_path);
|
||||
Error start_file(const String &p_path, BitField<FileAccess::UnixPermissionFlags> p_permissions = 0644, uint64_t p_modified_time = 0);
|
||||
Error write_file(const Vector<uint8_t> &p_data);
|
||||
Error close_file();
|
||||
|
||||
Error add_directory(const String &p_path, BitField<FileAccess::UnixPermissionFlags> p_permissions = 0755, uint64_t p_modified_time = 0);
|
||||
|
||||
ZIPPacker();
|
||||
~ZIPPacker();
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue