@@ -1168,34 +1168,51 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f
1168
1168
# ifdef EXDEV
1169
1169
if (errno == EXDEV ) {
1170
1170
zend_stat_t sb ;
1171
+ # if !defined(ZTS ) && !defined(TSRM_WIN32 ) && !defined(NETWARE )
1172
+ /* not sure what to do in ZTS case, umask is not thread-safe */
1173
+ int oldmask = umask (077 );
1174
+ # endif
1175
+ int success = 0 ;
1171
1176
if (php_copy_file (url_from , url_to ) == SUCCESS ) {
1172
1177
if (VCWD_STAT (url_from , & sb ) == 0 ) {
1178
+ success = 1 ;
1173
1179
# if !defined(TSRM_WIN32 ) && !defined(NETWARE )
1174
- if (VCWD_CHMOD (url_to , sb .st_mode )) {
1175
- if (errno == EPERM ) {
1176
- php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1177
- VCWD_UNLINK (url_from );
1178
- return 1 ;
1179
- }
1180
+ /*
1181
+ * Try to set user and permission info on the target.
1182
+ * If we're not root, then some of these may fail.
1183
+ * We try chown first, to set proper group info, relying
1184
+ * on the system environment to have proper umask to not allow
1185
+ * access to the file in the meantime.
1186
+ */
1187
+ if (VCWD_CHOWN (url_to , sb .st_uid , sb .st_gid )) {
1180
1188
php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1181
- return 0 ;
1189
+ if (errno != EPERM ) {
1190
+ success = 0 ;
1191
+ }
1182
1192
}
1183
- if (VCWD_CHOWN (url_to , sb .st_uid , sb .st_gid )) {
1184
- if (errno == EPERM ) {
1193
+
1194
+ if (success ) {
1195
+ if (VCWD_CHMOD (url_to , sb .st_mode )) {
1185
1196
php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1186
- VCWD_UNLINK (url_from );
1187
- return 1 ;
1197
+ if (errno != EPERM ) {
1198
+ success = 0 ;
1199
+ }
1188
1200
}
1189
- php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1190
- return 0 ;
1191
1201
}
1192
1202
# endif
1193
- VCWD_UNLINK (url_from );
1194
- return 1 ;
1203
+ if (success ) {
1204
+ VCWD_UNLINK (url_from );
1205
+ }
1206
+ } else {
1207
+ php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1195
1208
}
1209
+ } else {
1210
+ php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1196
1211
}
1197
- php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1198
- return 0 ;
1212
+ # if !defined(ZTS ) && !defined(TSRM_WIN32 ) && !defined(NETWARE )
1213
+ umask (oldmask );
1214
+ # endif
1215
+ return success ;
1199
1216
}
1200
1217
# endif
1201
1218
#endif
0 commit comments