File php-build-reproducible-phar.patch of Package php7
diff -purN php-7.4.14-orig/ext/phar/phar.c php-7.4.14/ext/phar/phar.c
--- php-7.4.14-orig/ext/phar/phar.c 2021-01-05 11:45:11.000000000 +0100
+++ php-7.4.14/ext/phar/phar.c 2021-01-05 15:10:44.038518333 +0100
@@ -2954,7 +2954,7 @@ int phar_flush(phar_archive_data *phar,
4: metadata-len
+: metadata
*/
- mytime = time(NULL);
+ mytime = source_date_epoch_time(NULL);
phar_set_32(entry_buffer, entry->uncompressed_filesize);
phar_set_32(entry_buffer+4, mytime);
phar_set_32(entry_buffer+8, entry->compressed_filesize);
diff -purN php-7.4.14-orig/ext/phar/phar_internal.h php-7.4.14/ext/phar/phar_internal.h
--- php-7.4.14-orig/ext/phar/phar_internal.h 2021-01-05 11:45:11.000000000 +0100
+++ php-7.4.14/ext/phar/phar_internal.h 2021-01-06 11:14:35.635724835 +0100
@@ -417,6 +417,22 @@ static inline enum phar_fp_type phar_get
return PHAR_G(cached_fp)[entry->phar->phar_pos].manifest[entry->manifest_pos].fp_type;
}
+static inline time_t source_date_epoch_time(time_t *tloc)
+{
+ zend_string *str = php_getenv("SOURCE_DATE_EPOCH", sizeof("SOURCE_DATE_EPOCH")-1);
+
+ if (str) {
+ time_t t = strtoul(ZSTR_VAL(str), NULL, 10);
+
+ zend_string_release(str);
+ if (tloc) {
+ *tloc = t;
+ }
+ return t;
+ }
+ return time(tloc);
+}
+
static inline zend_off_t phar_get_fp_offset(phar_entry_info *entry)
{
if (!entry->is_persistent) {
diff -purN php-7.4.14-orig/ext/phar/stream.c php-7.4.14/ext/phar/stream.c
--- php-7.4.14-orig/ext/phar/stream.c 2021-01-05 11:45:11.000000000 +0100
+++ php-7.4.14/ext/phar/stream.c 2021-01-05 15:10:44.042518386 +0100
@@ -466,7 +466,7 @@ static int phar_stream_flush(php_stream
phar_entry_data *data = (phar_entry_data *) stream->abstract;
if (data->internal_file->is_modified) {
- data->internal_file->timestamp = time(0);
+ data->internal_file->timestamp = source_date_epoch_time(0);
ret = phar_flush(data->phar, 0, 0, 0, &error);
if (error) {
php_stream_wrapper_log_error(stream->wrapper, REPORT_ERRORS, "%s", error);
diff -purN php-7.4.14-orig/ext/phar/tar.c php-7.4.14/ext/phar/tar.c
--- php-7.4.14-orig/ext/phar/tar.c 2021-01-05 11:45:11.000000000 +0100
+++ php-7.4.14/ext/phar/tar.c 2021-01-05 15:10:44.042518386 +0100
@@ -977,7 +977,7 @@ int phar_tar_flush(phar_archive_data *ph
char halt_stub[] = "__HALT_COMPILER();";
entry.flags = PHAR_ENT_PERM_DEF_FILE;
- entry.timestamp = time(NULL);
+ entry.timestamp = source_date_epoch_time(NULL);
entry.is_modified = 1;
entry.is_crc_checked = 1;
entry.is_tar = 1;
diff -purN php-7.4.14-orig/ext/phar/util.c php-7.4.14/ext/phar/util.c
--- php-7.4.14-orig/ext/phar/util.c 2021-01-05 11:45:11.000000000 +0100
+++ php-7.4.14/ext/phar/util.c 2021-01-05 15:10:44.042518386 +0100
@@ -574,7 +574,7 @@ phar_entry_data *phar_get_or_create_entr
phar_add_virtual_dirs(phar, path, path_len);
etemp.is_modified = 1;
- etemp.timestamp = time(0);
+ etemp.timestamp = source_date_epoch_time(0);
etemp.is_crc_checked = 1;
etemp.phar = phar;
etemp.filename = estrndup(path, path_len);
diff -purN php-7.4.14-orig/ext/phar/zip.c php-7.4.14/ext/phar/zip.c
--- php-7.4.14-orig/ext/phar/zip.c 2021-01-05 11:45:11.000000000 +0100
+++ php-7.4.14/ext/phar/zip.c 2021-01-05 15:10:44.042518386 +0100
@@ -1210,7 +1210,7 @@ int phar_zip_flush(phar_archive_data *ph
pass.error = &temperr;
entry.flags = PHAR_ENT_PERM_DEF_FILE;
- entry.timestamp = time(NULL);
+ entry.timestamp = source_date_epoch_time(NULL);
entry.is_modified = 1;
entry.is_zip = 1;
entry.phar = phar;
diff -purN php-7.4.14-orig/ext/standard/basic_functions.c php-7.4.14/ext/standard/basic_functions.c
--- php-7.4.14-orig/ext/standard/basic_functions.c 2021-01-05 11:45:13.000000000 +0100
+++ php-7.4.14/ext/standard/basic_functions.c 2021-01-07 21:37:59.962314567 +0100
@@ -4058,99 +4058,98 @@ PHP_FUNCTION(long2ip)
* System Functions *
********************/
-/* {{{ proto string getenv(string varname[, bool local_only]
- Get the value of an environment variable or every available environment variable
- if no varname is present */
-PHP_FUNCTION(getenv)
-{
- char *ptr, *str = NULL;
- size_t str_len;
- zend_bool local_only = 0;
-
- ZEND_PARSE_PARAMETERS_START(0, 2)
- Z_PARAM_OPTIONAL
- Z_PARAM_STRING(str, str_len)
- Z_PARAM_BOOL(local_only)
- ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
-
- if (!str) {
- array_init(return_value);
- php_import_environment_variables(return_value);
- return;
- }
-
- if (!local_only) {
- /* SAPI method returns an emalloc()'d string */
- ptr = sapi_getenv(str, str_len);
- if (ptr) {
- // TODO: avoid realocation ???
- RETVAL_STRING(ptr);
- efree(ptr);
- return;
- }
- }
+PHPAPI zend_string *php_getenv(const char *str, size_t str_len) {
#ifdef PHP_WIN32
{
- wchar_t dummybuf;
- DWORD size;
- wchar_t *keyw, *valw;
-
- keyw = php_win32_cp_conv_any_to_w(str, str_len, PHP_WIN32_CP_IGNORE_LEN_P);
+ wchar_t *keyw = php_win32_cp_conv_any_to_w(str, str_len, PHP_WIN32_CP_IGNORE_LEN_P);
if (!keyw) {
- RETURN_FALSE;
+ return NULL;
}
SetLastError(0);
- /*If the given bugger is not large enough to hold the data, the return value is
- the buffer size, in characters, required to hold the string and its terminating
- null character. We use this return value to alloc the final buffer. */
- size = GetEnvironmentVariableW(keyw, &dummybuf, 0);
+ /* If the given buffer is not large enough to hold the data, the return value is
+ * the buffer size, in characters, required to hold the string and its terminating
+ * null character. We use this return value to alloc the final buffer. */
+ wchar_t dummybuf;
+ DWORD size = GetEnvironmentVariableW(keyw, &dummybuf, 0);
if (GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
- /* The environment variable doesn't exist. */
- free(keyw);
- RETURN_FALSE;
+ /* The environment variable doesn't exist. */
+ free(keyw);
+ return NULL;
}
if (size == 0) {
- /* env exists, but it is empty */
- free(keyw);
- RETURN_EMPTY_STRING();
+ /* env exists, but it is empty */
+ free(keyw);
+ return ZSTR_EMPTY_ALLOC();
}
- valw = emalloc((size + 1) * sizeof(wchar_t));
+ wchar_t *valw = emalloc((size + 1) * sizeof(wchar_t));
size = GetEnvironmentVariableW(keyw, valw, size);
if (size == 0) {
- /* has been removed between the two calls */
- free(keyw);
- efree(valw);
- RETURN_EMPTY_STRING();
+ /* has been removed between the two calls */
+ free(keyw);
+ efree(valw);
+ return ZSTR_EMPTY_ALLOC();
} else {
- ptr = php_win32_cp_w_to_any(valw);
- RETVAL_STRING(ptr);
+ char *ptr = php_win32_cp_w_to_any(valw);
+ zend_string *result = zend_string_init(ptr, strlen(ptr), 0);
free(ptr);
free(keyw);
efree(valw);
- return;
+ return result;
}
}
#else
-
tsrm_env_lock();
/* system method returns a const */
- ptr = getenv(str);
-
+ const char *ptr = getenv(str);
+ zend_string *result = NULL;
if (ptr) {
- RETVAL_STRING(ptr);
+ result = zend_string_init(ptr, strlen(ptr), 0);
}
tsrm_env_unlock();
+ return result;
+#endif
+}
- if (ptr) {
- return;
- }
+/* {{{ Get the value of an environment variable or every available environment variable
+ if no varname is present */
+PHP_FUNCTION(getenv)
+{
+ char *str = NULL;
+ size_t str_len;
+ zend_bool local_only = 0;
-#endif
+ ZEND_PARSE_PARAMETERS_START(0, 2)
+ Z_PARAM_OPTIONAL
+ Z_PARAM_STRING(str, str_len)
+ Z_PARAM_BOOL(local_only)
+ ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
+
+ if (!str) {
+ array_init(return_value);
+ php_import_environment_variables(return_value);
+ return;
+ }
+
+ if (!local_only) {
+ /* SAPI method returns an emalloc()'d string */
+ char *ptr = sapi_getenv(str, str_len);
+ if (ptr) {
+ // TODO: avoid reallocation ???
+ RETVAL_STRING(ptr);
+ efree(ptr);
+ return;
+ }
+ }
+
+ zend_string *res = php_getenv(str, str_len);
+ if (res) {
+ RETURN_STR(res);
+ }
RETURN_FALSE;
}
/* }}} */
diff -purN php-7.4.14-orig/ext/standard/basic_functions.h php-7.4.14/ext/standard/basic_functions.h
--- php-7.4.14-orig/ext/standard/basic_functions.h 2021-01-05 11:45:13.000000000 +0100
+++ php-7.4.14/ext/standard/basic_functions.h 2021-01-06 08:54:42.610099184 +0100
@@ -255,6 +255,8 @@ typedef struct {
} putenv_entry;
#endif
+PHPAPI zend_string *php_getenv(const char *str, size_t str_len);
+
PHPAPI double php_get_nan(void);
PHPAPI double php_get_inf(void);