Skip to content

Commit f63094e

Browse files
committed
ext/opcache/zend_file_cache: move code to zend_file_cache_script_write()
This eliminates duplicate error handling code.
1 parent d65d21f commit f63094e

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

ext/opcache/zend_file_cache.c

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,29 @@ static char *zend_file_cache_get_bin_file_path(zend_string *script_path)
10091009
return filename;
10101010
}
10111011

1012+
/**
1013+
* Helper function for zend_file_cache_script_store().
1014+
*
1015+
* @return true on success, false on error
1016+
*/
1017+
static bool zend_file_cache_script_write(int fd, const zend_persistent_script *script, const zend_file_cache_metainfo *info, const void *buf, const zend_string *s)
1018+
{
1019+
#ifdef HAVE_SYS_UIO_H
1020+
const struct iovec vec[] = {
1021+
{ .iov_base = (void *)info, .iov_len = sizeof(*info) },
1022+
{ .iov_base = (void *)buf, .iov_len = script->size },
1023+
{ .iov_base = (void *)ZSTR_VAL(s), .iov_len = info->str_size },
1024+
};
1025+
1026+
return writev(fd, vec, sizeof(vec) / sizeof(vec[0])) == (ssize_t)(sizeof(*info) + script->size + info->str_size);
1027+
#else
1028+
return ZEND_LONG_MAX >= (zend_long)(sizeof(*info) + script->size + info->str_size) &&
1029+
write(fd, info, sizeof(*info)) == sizeof(*info) &&
1030+
write(fd, buf, script->size) == script->size &&
1031+
write(fd, ZSTR_VAL(s), info->str_size) == info->str_size;
1032+
#endif
1033+
}
1034+
10121035
int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm)
10131036
{
10141037
int fd;
@@ -1079,14 +1102,7 @@ int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm)
10791102
__msan_unpoison(buf, script->size);
10801103
#endif
10811104

1082-
#ifdef HAVE_SYS_UIO_H
1083-
const struct iovec vec[] = {
1084-
{ .iov_base = (void *)&info, .iov_len = sizeof(info) },
1085-
{ .iov_base = buf, .iov_len = script->size },
1086-
{ .iov_base = ZSTR_VAL(s), .iov_len = info.str_size },
1087-
};
1088-
1089-
if (writev(fd, vec, sizeof(vec) / sizeof(vec[0])) != (ssize_t)(sizeof(info) + script->size + info.str_size)) {
1105+
if (!zend_file_cache_script_write(fd, script, &info, buf, s)) {
10901106
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot write to file '%s'\n", filename);
10911107
zend_string_release_ex(s, 0);
10921108
close(fd);
@@ -1095,21 +1111,6 @@ int zend_file_cache_script_store(zend_persistent_script *script, bool in_shm)
10951111
efree(filename);
10961112
return FAILURE;
10971113
}
1098-
#else
1099-
if (ZEND_LONG_MAX < (zend_long)(sizeof(info) + script->size + info.str_size) ||
1100-
write(fd, &info, sizeof(info)) != sizeof(info) ||
1101-
write(fd, buf, script->size) != script->size ||
1102-
write(fd, ZSTR_VAL(s), info.str_size) != info.str_size
1103-
) {
1104-
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot write to file '%s'\n", filename);
1105-
zend_string_release_ex(s, 0);
1106-
close(fd);
1107-
efree(mem);
1108-
zend_file_cache_unlink(filename);
1109-
efree(filename);
1110-
return FAILURE;
1111-
}
1112-
#endif
11131114

11141115
zend_string_release_ex(s, 0);
11151116
efree(mem);

0 commit comments

Comments
 (0)