Home>
I would like to confirm what kind of processing is performed inside the rename process of PHP 5.3.3. Can you tell me how I can confirm it?

https://www.php.net/manual/en/function.rename.php

What you did

Download PHP 5.3.3 (tar.bz2) and search full-text with rename
https://www.php.net/releases/index.php

Result

I found the following file.c, but I don't know if the policy is correct.

PHP_FUNCTION (rename)
{
    char * old_name, * new_name;
    int old_name_len, new_name_len;
    zval * zcontext = NULL;
    php_stream_wrapper * wrapper;
    php_stream_context * context;
    if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "ss | r",&old_name,&old_name_len,&new_name,&new_name_len,&zcontext) == FAILURE) {
        RETURN_FALSE;
    }
    wrapper = php_stream_locate_url_wrapper (old_name, NULL, 0 TSRMLS_CC);
    if (! wrapper ||! wrapper->wops) {
        php_error_docref (NULL TSRMLS_CC, E_WARNING, "Unable to locate stream wrapper");
        RETURN_FALSE;
    }
    if (! wrapper->wops->rename) {
        php_error_docref (NULL TSRMLS_CC, E_WARNING, "% s wrapper does not support renaming", wrapper->wops->label? wrapper->wops->label: "Source");
        RETURN_FALSE;
    }
    if (wrapper! = php_stream_locate_url_wrapper (new_name, NULL, 0 TSRMLS_CC)) {
        php_error_docref (NULL TSRMLS_CC, E_WARNING, "Cannot rename a file across wrapper types");
        RETURN_FALSE;
    }
    context = php_stream_context_from_zval (zcontext, 0);
    RETURN_BOOL (wrapper->wops->rename (wrapper, old_name, new_name, 0, context TSRMLS_CC));
}
php
  • Answer # 1

      

    I asked the above question to see if the rename process is atomic.

    Since it will be sent to the OS system call after all, I think we can only get a conclusion that isdepending on the OS or device.

  • Answer # 2

    You may want to take a look while checking the specifications.
    If it is old PHP, I think that it was only passed to the API if the argument was processed so that it could be handled in C language, but is it a bit troublesome because it became complicated, such as accepting a URL?

    if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "ss | r",&old_name,&old_name_len,&new_name,&new_name_len,&zcontext) == FAILURE) {
            RETURN_FALSE;
        }


    It feels like it's handling arguments and handling in C here

    wrapper = php_stream_locate_url_wrapper (old_name, NULL, 0 TSRMLS_CC);


    As I wrote in ↑, this is the part where the wrapper has been created because the specification has become complicated, such as "Accept URL"

    if (! wrapper ||! wrapper->wops) {
            php_error_docref (NULL TSRMLS_CC, E_WARNING, "Unable to locate stream wrapper");
            RETURN_FALSE;
        }
        if (! wrapper->wops->rename) {
            php_error_docref (NULL TSRMLS_CC, E_WARNING, "% s wrapper does not support renaming", wrapper->wops->label? wrapper->wops->label: "Source");
            RETURN_FALSE;
        }
        if (wrapper! = php_stream_locate_url_wrapper (new_name, NULL, 0 TSRMLS_CC)) {
            php_error_docref (NULL TSRMLS_CC, E_WARNING, "Cannot rename a file across wrapper types");
            RETURN_FALSE;
        }


    As you can see, did you get a wrapper? (Does the specified path support?) Does the wrapper support rename?

    context = php_stream_context_from_zval (zcontext, 0);
        RETURN_BOOL (wrapper->wops->rename (wrapper, old_name, new_name, 0, context TSRMLS_CC));


    (I don't know what the context itself is) This is probably renaming with wrapper->wops->rename ().

    So, after examining php_stream_locate_url_wrapper () and understanding what instance is returned, if you look at wrapper->wops->rename () of the type of wrapper you want to know what you are doing internally Is it? I think you can understand.