@@ -1198,34 +1198,51 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f
1198
1198
# ifdef EXDEV
1199
1199
if (errno == EXDEV ) {
1200
1200
zend_stat_t sb ;
1201
+ # if !defined(ZTS ) && !defined(TSRM_WIN32 )
1202
+ /* not sure what to do in ZTS case, umask is not thread-safe */
1203
+ int oldmask = umask (077 );
1204
+ # endif
1205
+ int success = 0 ;
1201
1206
if (php_copy_file (url_from , url_to ) == SUCCESS ) {
1202
1207
if (VCWD_STAT (url_from , & sb ) == 0 ) {
1203
- # ifndef TSRM_WIN32
1204
- if (VCWD_CHMOD (url_to , sb .st_mode )) {
1205
- if (errno == EPERM ) {
1206
- php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1207
- VCWD_UNLINK (url_from );
1208
- return 1 ;
1209
- }
1208
+ success = 1 ;
1209
+ # if !defined(TSRM_WIN32 )
1210
+ /*
1211
+ * Try to set user and permission info on the target.
1212
+ * If we're not root, then some of these may fail.
1213
+ * We try chown first, to set proper group info, relying
1214
+ * on the system environment to have proper umask to not allow
1215
+ * access to the file in the meantime.
1216
+ */
1217
+ if (VCWD_CHOWN (url_to , sb .st_uid , sb .st_gid )) {
1210
1218
php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1211
- return 0 ;
1219
+ if (errno != EPERM ) {
1220
+ success = 0 ;
1221
+ }
1212
1222
}
1213
- if (VCWD_CHOWN (url_to , sb .st_uid , sb .st_gid )) {
1214
- if (errno == EPERM ) {
1223
+
1224
+ if (success ) {
1225
+ if (VCWD_CHMOD (url_to , sb .st_mode )) {
1215
1226
php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1216
- VCWD_UNLINK (url_from );
1217
- return 1 ;
1227
+ if (errno != EPERM ) {
1228
+ success = 0 ;
1229
+ }
1218
1230
}
1219
- php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1220
- return 0 ;
1221
1231
}
1222
1232
# endif
1223
- VCWD_UNLINK (url_from );
1224
- return 1 ;
1233
+ if (success ) {
1234
+ VCWD_UNLINK (url_from );
1235
+ }
1236
+ } else {
1237
+ php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1225
1238
}
1239
+ } else {
1240
+ php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1226
1241
}
1227
- php_error_docref2 (NULL , url_from , url_to , E_WARNING , "%s" , strerror (errno ));
1228
- return 0 ;
1242
+ # if !defined(ZTS ) && !defined(TSRM_WIN32 )
1243
+ umask (oldmask );
1244
+ # endif
1245
+ return success ;
1229
1246
}
1230
1247
# endif
1231
1248
#endif
0 commit comments