cvs: pecl /cvsclient cvsclient.c php_cvsclient.h
| From: | Sara Golemon | Date: | Sun, 30 Nov 2003 04:55:58 +0000 |
| Subject: | cvs: pecl /cvsclient cvsclient.c php_cvsclient.h | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-541@lists.php.net to get a copy of this message | ||
pollita Sat Nov 29 23:55:58 2003 EDT
Modified files:
/pecl/cvsclient cvsclient.c php_cvsclient.h
Log:
Some refactoring and addition of userland functions. No real added functionality.
Index: pecl/cvsclient/cvsclient.c diff -u pecl/cvsclient/cvsclient.c:1.1 pecl/cvsclient/cvsclient.c:1.2 --- pecl/cvsclient/cvsclient.c:1.1 Sat Nov 29 20:53:38 2003 +++ pecl/cvsclient/cvsclient.c Sat Nov 29 23:55:58 2003 @@ -46,6 +46,17 @@ #define PHP_CVSCLIENT_REQ_REMOVE 0x00000800 #define PHP_CVSCLIENT_REQ_HISTORY 0x00001000 +#define PHP_CVSCLIENT_DEFAULT_PORT 2401 + +static int le_cvsclient; +#define CVSCLIENT_RES_NAME "CVS pserver Client" + +typedef struct _php_cvsclient_resource { + php_stream *server; + char *cvsroot; + int requests; +} php_cvsclient_resource; + typedef struct _php_cvsclient_requests { long request; char *label; @@ -68,6 +79,10 @@ { 0, NULL } }; +/* ********************* */ +/* CVS pserver interface */ +/* ********************* */ + static char cvs_encode[96] = { 32, 120, 53, 35, 36, 109, 72, 108, 70, 64, 76, 67, 116, 74, 68, 87, @@ -82,31 +97,8 @@ 58, 113, 32, 90, 44, 98, 60, 51, 33, 97, 62, 123, 124, 125, 126, 127 }; -function_entry cvsclient_functions[] = { - /* TODO: Resource based single-session access and committment */ - {NULL, NULL, NULL} -}; - -zend_module_entry cvsclient_module_entry = { - STANDARD_MODULE_HEADER, - "cvsclient", - cvsclient_functions, - PHP_MINIT(cvsclient), - PHP_MSHUTDOWN(cvsclient), - NULL, /* RINIT */ - NULL, /* RSHUTDOWN */ - PHP_MINFO(cvsclient), - NO_VERSION_YET, - STANDARD_MODULE_PROPERTIES -}; - -#ifdef COMPILE_DL_CVSCLIENT -ZEND_GET_MODULE(cvsclient) -#endif - - /* {{{ php_cvsclient_do_connect */ -static php_stream * php_cvsclient_do_connect(char *path, int options, php_stream_context *context, php_url **presource STREAMS_DC TSRMLS_DC) +static php_stream * php_cvsclient_do_connect(char *path, int options, php_stream_context *context, php_url **presource TSRMLS_DC) { php_stream *stream = NULL; php_url *resource = NULL; @@ -173,7 +165,7 @@ /* {{{ php_cvsclient_authenticate */ -static int php_cvsclient_authenticate(php_stream *stream, const char *cvsroot, const char *user, const char *pass STREAMS_DC TSRMLS_DC) +static int php_cvsclient_authenticate(php_stream *stream, const char *cvsroot, const char *user, const char *pass TSRMLS_DC) { char *encpw; char response[128]; @@ -200,7 +192,7 @@ /* }}} */ /* {{{ php_cvsclient_negotiate */ -static int php_cvsclient_negotiate(php_stream *stream, const char *cvsroot STREAMS_DC TSRMLS_DC) +static int php_cvsclient_negotiate(php_stream *stream, const char *cvsroot TSRMLS_DC) { php_cvsclient_requests *verbs = cvsclient_requests; char response[4096]; @@ -233,12 +225,16 @@ } /* }}} */ +/* ********************** */ +/* Wrapper Implementation */ +/* ********************** */ + /* {{{ php_stream_url_wrap_cvsclient */ static php_stream * php_stream_url_wrap_cvsclient(php_stream_wrapper *wrapper, char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC) { php_cvsclient_requests *verbs = cvsclient_requests; - php_stream *stream, *memstream; + php_stream *stream = NULL, *memstream = NULL; php_url *resource = NULL; char *module = NULL; char *cvsroot = NULL; @@ -256,7 +252,7 @@ } /* Connect */ - stream = php_cvsclient_do_connect(path, options, context, &resource STREAMS_CC TSRMLS_CC); + stream = php_cvsclient_do_connect(path, options, context, &resource TSRMLS_CC); if (!stream || !resource->path) { goto wrap_errexit; } @@ -284,12 +280,12 @@ /* Authenticate */ if (resource && resource->user && resource->pass && - php_cvsclient_authenticate(stream, cvsroot, resource->user, resource->pass STREAMS_CC TSRMLS_CC) == FAILURE) { + php_cvsclient_authenticate(stream, cvsroot, resource->user, resource->pass TSRMLS_CC) == FAILURE) { goto wrap_errexit; } /* Negotiate */ - if ((valid_requests = php_cvsclient_negotiate(stream, cvsroot STREAMS_CC TSRMLS_CC)) == 0) { + if ((valid_requests = php_cvsclient_negotiate(stream, cvsroot TSRMLS_CC)) == 0) { goto wrap_errexit; } @@ -359,6 +355,10 @@ php_stream_write(memstream, response, read); filesize -= read; + if (read <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading remote file."); + goto wrap_errexit; + } } php_stream_rewind(memstream); php_stream_close(stream); @@ -389,6 +389,9 @@ if (stream) { php_stream_close(stream); } + if (memstream) { + php_stream_close(memstream); + } return NULL; } /* }}} */ @@ -424,9 +427,218 @@ 1 /* is_url */ }; +/* ****************** */ +/* Userland Functions */ +/* ****************** */ + +/* {{{ proto resource cvsclient_connect(string server, string cvsroot[, int port]) + Connect to CVS pserver */ +PHP_FUNCTION(cvsclient_connect) +{ + php_cvsclient_resource *cvsclient; + php_stream *stream; + char *server, *cvsroot; + long server_len, cvsroot_len, port = PHP_CVSCLIENT_DEFAULT_PORT; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &server, &server_len, &cvsroot, &cvsroot_len, &port) == FAILURE) { + RETURN_FALSE; + } + + stream = php_stream_sock_open_host(server, port, SOCK_STREAM, NULL, 0); + if (!stream) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to CVS pserver cvs://%s:%d", server, port); + RETURN_FALSE; + } + + cvsclient = emalloc(sizeof(php_cvsclient_resource)); + cvsclient->server = stream; + cvsclient->cvsroot = estrndup(cvsroot, cvsroot_len); + + ZEND_REGISTER_RESOURCE(return_value, cvsclient, le_cvsclient); +} +/* }}} */ + +/* {{{ proto bool cvsclient_login(resource cvsclient, string username, string password) + Authenticate to the CVS pserver */ +PHP_FUNCTION(cvsclient_login) +{ + zval *zcvsclient; + php_cvsclient_resource *cvsclient; + char *username, *password; + long username_len, password_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &zcvsclient, &username, &username_len, &password, &password_len) == FAILURE) { + RETURN_FALSE; + } + + ZEND_FETCH_RESOURCE(cvsclient, php_cvsclient_resource*, &zcvsclient, -1, CVSCLIENT_RES_NAME, le_cvsclient); + + /* Authenticate */ + if (php_cvsclient_authenticate(cvsclient->server, cvsclient->cvsroot, username, password TSRMLS_CC) == FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "CVS pserver authentication failure."); + RETURN_FALSE; + } + + /* Not really part of the authentication process, but the protocol expects it next. */ + cvsclient->requests = php_cvsclient_negotiate(cvsclient->server, cvsclient->cvsroot TSRMLS_CC); + + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto mixed cvsclient_retrieve(resource cvsclient, string module, string path[, string saveto[, string revision]]) + Retrieve specified <revision> (can also be tag or branch) of filename referred to by <path> in <module> */ +PHP_FUNCTION(cvsclient_retrieve) +{ + zval *zcvsclient; + php_cvsclient_resource *cvsclient; + char *p, *module, *path, *saveto = NULL, *revision = NULL, *file, response[4096]; + long module_len, path_len, saveto_len = 0, revision_len = 0, filesize = 0, count, i; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss|ss", &zcvsclient, &module, &module_len, &path, &path_len, &saveto, &saveto_len, &revision, &revision_len) == FAILURE) { + RETURN_FALSE; + } + + ZEND_FETCH_RESOURCE(cvsclient, php_cvsclient_resource*, &zcvsclient, -1, CVSCLIENT_RES_NAME, le_cvsclient); + + if (path[0] == '/') { + path++; + } + + p = strrchr(path, '/'); + if (revision) { + php_stream_printf(cvsclient->server TSRMLS_CC, "Argument -r\nArgument %s\n", revision); + } + if (p) { + /* path is non-root */ + php_stream_printf(cvsclient->server TSRMLS_CC, "Argument %s\n", p + 1); + php_stream_write_string(cvsclient->server, "Directory .\n"); + php_stream_printf(cvsclient->server TSRMLS_CC, "%s/%s/", cvsclient->cvsroot, module); + php_stream_write(cvsclient->server, path, path_len - (p - path)); + php_stream_putc(cvsclient->server, '\n'); + } else { + /* file is at root of cvs module */ + php_stream_printf(cvsclient->server TSRMLS_CC, "Argument %s\n", path); + php_stream_write_string(cvsclient->server, "Directory .\n"); + php_stream_printf(cvsclient->server TSRMLS_CC, "%s/%s\n", cvsclient->cvsroot, module); + } + php_stream_write_string(cvsclient->server, "update\n"); + + while (filesize == 0 && php_stream_gets(cvsclient->server, response, sizeof(response)-1) != NULL) { + /* Parse response and look for data */ + if (strncasecmp(response, "error", 5) == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unrecoverable error occured (%s)", response); + zend_list_delete(Z_LVAL_P(zcvsclient)); + RETURN_FALSE; + } + filesize = 1; + for(i=0; filesize && i<strlen(response); i++) { + if (!isdigit(response[i]) && !iscntrl(response[i])) { + filesize = 0; + } + } + } + + if (!filesize) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find document length."); + RETURN_FALSE; + } + + filesize = atoi(response); + if (filesize <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid filesize (%d)", filesize); + RETURN_FALSE; + } + + if (saveto && (saveto_len > 1 || (saveto_len == 1 && saveto[0] == '-'))) { + /* Save to a "local" file */ + php_stream *outfile; + + if (!(outfile = php_stream_open_wrapper(saveto, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL))) { + RETURN_FALSE; + } + while (filesize > 0) { + count = php_stream_read(cvsclient->server, response, filesize > (sizeof(response) - 1) ? sizeof(response) - 1 : filesize); + php_stream_write(outfile, response, count); + filesize -= count; + if (count <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading remote file."); + RETURN_FALSE; + } + } + RETURN_TRUE; + } else { + /* Return as a string */ + p = file = emalloc(filesize); + while (filesize > 0) { + count = php_stream_read(cvsclient->server, p, filesize); + filesize -= count; + p += count; + if (count <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading remote file."); + efree(file); + RETURN_FALSE; + } + } + RETURN_STRINGL(file, p - file, 0); + } +} +/* }}} */ + +/* ******************* */ +/* Module Housekeeping */ +/* ******************* */ + +function_entry cvsclient_functions[] = { + PHP_FE(cvsclient_connect, NULL) + PHP_FE(cvsclient_login, NULL) + PHP_FE(cvsclient_retrieve, NULL) + /* TODO: Resource based single-session access and committment */ + {NULL, NULL, NULL} +}; + +zend_module_entry cvsclient_module_entry = { + STANDARD_MODULE_HEADER, + "cvsclient", + cvsclient_functions, + PHP_MINIT(cvsclient), + PHP_MSHUTDOWN(cvsclient), + NULL, /* RINIT */ + NULL, /* RSHUTDOWN */ + PHP_MINFO(cvsclient), + NO_VERSION_YET, + STANDARD_MODULE_PROPERTIES +}; + +#ifdef COMPILE_DL_CVSCLIENT +ZEND_GET_MODULE(cvsclient) +#endif + +static void cvsclient_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) +{ + php_cvsclient_resource *cvsclient = (php_cvsclient_resource *)rsrc->ptr; + + if (cvsclient) { + if (cvsclient->server) { + php_stream_close(cvsclient->server); + cvsclient->server = NULL; + } + if (cvsclient->cvsroot) { + efree(cvsclient->cvsroot); + cvsclient->cvsroot = NULL; + } + efree(cvsclient); + cvsclient = NULL; + rsrc->ptr = NULL; + } +} + + PHP_MINIT_FUNCTION(cvsclient) { /* TODO: Readonly support for cvs.log, cvs.history, etc... */ + le_cvsclient = zend_register_list_destructors_ex(cvsclient_dtor, NULL, CVSCLIENT_RES_NAME, module_number); + return php_register_url_stream_wrapper("cvs", &php_stream_cvsclient_wrapper TSRMLS_CC); } @@ -441,6 +653,7 @@ php_info_print_table_start(); php_info_print_table_row(2, "CVS Client", "enabled"); php_info_print_table_row(2, "Wrapper", "cvs://"); + php_info_print_table_row(2, "Resource", CVSCLIENT_RES_NAME); php_info_print_table_end(); } Index: pecl/cvsclient/php_cvsclient.h diff -u pecl/cvsclient/php_cvsclient.h:1.1 pecl/cvsclient/php_cvsclient.h:1.2 --- pecl/cvsclient/php_cvsclient.h:1.1 Sat Nov 29 20:53:38 2003 +++ pecl/cvsclient/php_cvsclient.h Sat Nov 29 23:55:58 2003 @@ -28,6 +28,10 @@ PHP_MSHUTDOWN_FUNCTION(cvsclient); PHP_MINFO_FUNCTION(cvsclient); +PHP_FUNCTION(cvsclient_connect); +PHP_FUNCTION(cvsclient_login); +PHP_FUNCTION(cvsclient_retrieve); + #else #define phpext_cvsclient_ptr NULL
Index: pecl/cvsclient/cvsclient.c diff -u pecl/cvsclient/cvsclient.c:1.1 pecl/cvsclient/cvsclient.c:1.2 --- pecl/cvsclient/cvsclient.c:1.1 Sat Nov 29 20:53:38 2003 +++ pecl/cvsclient/cvsclient.c Sat Nov 29 23:55:58 2003 @@ -46,6 +46,17 @@ #define PHP_CVSCLIENT_REQ_REMOVE 0x00000800 #define PHP_CVSCLIENT_REQ_HISTORY 0x00001000 +#define PHP_CVSCLIENT_DEFAULT_PORT 2401 + +static int le_cvsclient; +#define CVSCLIENT_RES_NAME "CVS pserver Client" + +typedef struct _php_cvsclient_resource { + php_stream *server; + char *cvsroot; + int requests; +} php_cvsclient_resource; + typedef struct _php_cvsclient_requests { long request; char *label; @@ -68,6 +79,10 @@ { 0, NULL } }; +/* ********************* */ +/* CVS pserver interface */ +/* ********************* */ + static char cvs_encode[96] = { 32, 120, 53, 35, 36, 109, 72, 108, 70, 64, 76, 67, 116, 74, 68, 87, @@ -82,31 +97,8 @@ 58, 113, 32, 90, 44, 98, 60, 51, 33, 97, 62, 123, 124, 125, 126, 127 }; -function_entry cvsclient_functions[] = { - /* TODO: Resource based single-session access and committment */ - {NULL, NULL, NULL} -}; - -zend_module_entry cvsclient_module_entry = { - STANDARD_MODULE_HEADER, - "cvsclient", - cvsclient_functions, - PHP_MINIT(cvsclient), - PHP_MSHUTDOWN(cvsclient), - NULL, /* RINIT */ - NULL, /* RSHUTDOWN */ - PHP_MINFO(cvsclient), - NO_VERSION_YET, - STANDARD_MODULE_PROPERTIES -}; - -#ifdef COMPILE_DL_CVSCLIENT -ZEND_GET_MODULE(cvsclient) -#endif - - /* {{{ php_cvsclient_do_connect */ -static php_stream * php_cvsclient_do_connect(char *path, int options, php_stream_context *context, php_url **presource STREAMS_DC TSRMLS_DC) +static php_stream * php_cvsclient_do_connect(char *path, int options, php_stream_context *context, php_url **presource TSRMLS_DC) { php_stream *stream = NULL; php_url *resource = NULL; @@ -173,7 +165,7 @@ /* {{{ php_cvsclient_authenticate */ -static int php_cvsclient_authenticate(php_stream *stream, const char *cvsroot, const char *user, const char *pass STREAMS_DC TSRMLS_DC) +static int php_cvsclient_authenticate(php_stream *stream, const char *cvsroot, const char *user, const char *pass TSRMLS_DC) { char *encpw; char response[128]; @@ -200,7 +192,7 @@ /* }}} */ /* {{{ php_cvsclient_negotiate */ -static int php_cvsclient_negotiate(php_stream *stream, const char *cvsroot STREAMS_DC TSRMLS_DC) +static int php_cvsclient_negotiate(php_stream *stream, const char *cvsroot TSRMLS_DC) { php_cvsclient_requests *verbs = cvsclient_requests; char response[4096]; @@ -233,12 +225,16 @@ } /* }}} */ +/* ********************** */ +/* Wrapper Implementation */ +/* ********************** */ + /* {{{ php_stream_url_wrap_cvsclient */ static php_stream * php_stream_url_wrap_cvsclient(php_stream_wrapper *wrapper, char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC) { php_cvsclient_requests *verbs = cvsclient_requests; - php_stream *stream, *memstream; + php_stream *stream = NULL, *memstream = NULL; php_url *resource = NULL; char *module = NULL; char *cvsroot = NULL; @@ -256,7 +252,7 @@ } /* Connect */ - stream = php_cvsclient_do_connect(path, options, context, &resource STREAMS_CC TSRMLS_CC); + stream = php_cvsclient_do_connect(path, options, context, &resource TSRMLS_CC); if (!stream || !resource->path) { goto wrap_errexit; } @@ -284,12 +280,12 @@ /* Authenticate */ if (resource && resource->user && resource->pass && - php_cvsclient_authenticate(stream, cvsroot, resource->user, resource->pass STREAMS_CC TSRMLS_CC) == FAILURE) { + php_cvsclient_authenticate(stream, cvsroot, resource->user, resource->pass TSRMLS_CC) == FAILURE) { goto wrap_errexit; } /* Negotiate */ - if ((valid_requests = php_cvsclient_negotiate(stream, cvsroot STREAMS_CC TSRMLS_CC)) == 0) { + if ((valid_requests = php_cvsclient_negotiate(stream, cvsroot TSRMLS_CC)) == 0) { goto wrap_errexit; } @@ -359,6 +355,10 @@ php_stream_write(memstream, response, read); filesize -= read; + if (read <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading remote file."); + goto wrap_errexit; + } } php_stream_rewind(memstream); php_stream_close(stream); @@ -389,6 +389,9 @@ if (stream) { php_stream_close(stream); } + if (memstream) { + php_stream_close(memstream); + } return NULL; } /* }}} */ @@ -424,9 +427,218 @@ 1 /* is_url */ }; +/* ****************** */ +/* Userland Functions */ +/* ****************** */ + +/* {{{ proto resource cvsclient_connect(string server, string cvsroot[, int port]) + Connect to CVS pserver */ +PHP_FUNCTION(cvsclient_connect) +{ + php_cvsclient_resource *cvsclient; + php_stream *stream; + char *server, *cvsroot; + long server_len, cvsroot_len, port = PHP_CVSCLIENT_DEFAULT_PORT; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &server, &server_len, &cvsroot, &cvsroot_len, &port) == FAILURE) { + RETURN_FALSE; + } + + stream = php_stream_sock_open_host(server, port, SOCK_STREAM, NULL, 0); + if (!stream) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to CVS pserver cvs://%s:%d", server, port); + RETURN_FALSE; + } + + cvsclient = emalloc(sizeof(php_cvsclient_resource)); + cvsclient->server = stream; + cvsclient->cvsroot = estrndup(cvsroot, cvsroot_len); + + ZEND_REGISTER_RESOURCE(return_value, cvsclient, le_cvsclient); +} +/* }}} */ + +/* {{{ proto bool cvsclient_login(resource cvsclient, string username, string password) + Authenticate to the CVS pserver */ +PHP_FUNCTION(cvsclient_login) +{ + zval *zcvsclient; + php_cvsclient_resource *cvsclient; + char *username, *password; + long username_len, password_len; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &zcvsclient, &username, &username_len, &password, &password_len) == FAILURE) { + RETURN_FALSE; + } + + ZEND_FETCH_RESOURCE(cvsclient, php_cvsclient_resource*, &zcvsclient, -1, CVSCLIENT_RES_NAME, le_cvsclient); + + /* Authenticate */ + if (php_cvsclient_authenticate(cvsclient->server, cvsclient->cvsroot, username, password TSRMLS_CC) == FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "CVS pserver authentication failure."); + RETURN_FALSE; + } + + /* Not really part of the authentication process, but the protocol expects it next. */ + cvsclient->requests = php_cvsclient_negotiate(cvsclient->server, cvsclient->cvsroot TSRMLS_CC); + + RETURN_TRUE; +} +/* }}} */ + +/* {{{ proto mixed cvsclient_retrieve(resource cvsclient, string module, string path[, string saveto[, string revision]]) + Retrieve specified <revision> (can also be tag or branch) of filename referred to by <path> in <module> */ +PHP_FUNCTION(cvsclient_retrieve) +{ + zval *zcvsclient; + php_cvsclient_resource *cvsclient; + char *p, *module, *path, *saveto = NULL, *revision = NULL, *file, response[4096]; + long module_len, path_len, saveto_len = 0, revision_len = 0, filesize = 0, count, i; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss|ss", &zcvsclient, &module, &module_len, &path, &path_len, &saveto, &saveto_len, &revision, &revision_len) == FAILURE) { + RETURN_FALSE; + } + + ZEND_FETCH_RESOURCE(cvsclient, php_cvsclient_resource*, &zcvsclient, -1, CVSCLIENT_RES_NAME, le_cvsclient); + + if (path[0] == '/') { + path++; + } + + p = strrchr(path, '/'); + if (revision) { + php_stream_printf(cvsclient->server TSRMLS_CC, "Argument -r\nArgument %s\n", revision); + } + if (p) { + /* path is non-root */ + php_stream_printf(cvsclient->server TSRMLS_CC, "Argument %s\n", p + 1); + php_stream_write_string(cvsclient->server, "Directory .\n"); + php_stream_printf(cvsclient->server TSRMLS_CC, "%s/%s/", cvsclient->cvsroot, module); + php_stream_write(cvsclient->server, path, path_len - (p - path)); + php_stream_putc(cvsclient->server, '\n'); + } else { + /* file is at root of cvs module */ + php_stream_printf(cvsclient->server TSRMLS_CC, "Argument %s\n", path); + php_stream_write_string(cvsclient->server, "Directory .\n"); + php_stream_printf(cvsclient->server TSRMLS_CC, "%s/%s\n", cvsclient->cvsroot, module); + } + php_stream_write_string(cvsclient->server, "update\n"); + + while (filesize == 0 && php_stream_gets(cvsclient->server, response, sizeof(response)-1) != NULL) { + /* Parse response and look for data */ + if (strncasecmp(response, "error", 5) == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unrecoverable error occured (%s)", response); + zend_list_delete(Z_LVAL_P(zcvsclient)); + RETURN_FALSE; + } + filesize = 1; + for(i=0; filesize && i<strlen(response); i++) { + if (!isdigit(response[i]) && !iscntrl(response[i])) { + filesize = 0; + } + } + } + + if (!filesize) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find document length."); + RETURN_FALSE; + } + + filesize = atoi(response); + if (filesize <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid filesize (%d)", filesize); + RETURN_FALSE; + } + + if (saveto && (saveto_len > 1 || (saveto_len == 1 && saveto[0] == '-'))) { + /* Save to a "local" file */ + php_stream *outfile; + + if (!(outfile = php_stream_open_wrapper(saveto, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL))) { + RETURN_FALSE; + } + while (filesize > 0) { + count = php_stream_read(cvsclient->server, response, filesize > (sizeof(response) - 1) ? sizeof(response) - 1 : filesize); + php_stream_write(outfile, response, count); + filesize -= count; + if (count <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading remote file."); + RETURN_FALSE; + } + } + RETURN_TRUE; + } else { + /* Return as a string */ + p = file = emalloc(filesize); + while (filesize > 0) { + count = php_stream_read(cvsclient->server, p, filesize); + filesize -= count; + p += count; + if (count <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error reading remote file."); + efree(file); + RETURN_FALSE; + } + } + RETURN_STRINGL(file, p - file, 0); + } +} +/* }}} */ + +/* ******************* */ +/* Module Housekeeping */ +/* ******************* */ + +function_entry cvsclient_functions[] = { + PHP_FE(cvsclient_connect, NULL) + PHP_FE(cvsclient_login, NULL) + PHP_FE(cvsclient_retrieve, NULL) + /* TODO: Resource based single-session access and committment */ + {NULL, NULL, NULL} +}; + +zend_module_entry cvsclient_module_entry = { + STANDARD_MODULE_HEADER, + "cvsclient", + cvsclient_functions, + PHP_MINIT(cvsclient), + PHP_MSHUTDOWN(cvsclient), + NULL, /* RINIT */ + NULL, /* RSHUTDOWN */ + PHP_MINFO(cvsclient), + NO_VERSION_YET, + STANDARD_MODULE_PROPERTIES +}; + +#ifdef COMPILE_DL_CVSCLIENT +ZEND_GET_MODULE(cvsclient) +#endif + +static void cvsclient_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) +{ + php_cvsclient_resource *cvsclient = (php_cvsclient_resource *)rsrc->ptr; + + if (cvsclient) { + if (cvsclient->server) { + php_stream_close(cvsclient->server); + cvsclient->server = NULL; + } + if (cvsclient->cvsroot) { + efree(cvsclient->cvsroot); + cvsclient->cvsroot = NULL; + } + efree(cvsclient); + cvsclient = NULL; + rsrc->ptr = NULL; + } +} + + PHP_MINIT_FUNCTION(cvsclient) { /* TODO: Readonly support for cvs.log, cvs.history, etc... */ + le_cvsclient = zend_register_list_destructors_ex(cvsclient_dtor, NULL, CVSCLIENT_RES_NAME, module_number); + return php_register_url_stream_wrapper("cvs", &php_stream_cvsclient_wrapper TSRMLS_CC); } @@ -441,6 +653,7 @@ php_info_print_table_start(); php_info_print_table_row(2, "CVS Client", "enabled"); php_info_print_table_row(2, "Wrapper", "cvs://"); + php_info_print_table_row(2, "Resource", CVSCLIENT_RES_NAME); php_info_print_table_end(); } Index: pecl/cvsclient/php_cvsclient.h diff -u pecl/cvsclient/php_cvsclient.h:1.1 pecl/cvsclient/php_cvsclient.h:1.2 --- pecl/cvsclient/php_cvsclient.h:1.1 Sat Nov 29 20:53:38 2003 +++ pecl/cvsclient/php_cvsclient.h Sat Nov 29 23:55:58 2003 @@ -28,6 +28,10 @@ PHP_MSHUTDOWN_FUNCTION(cvsclient); PHP_MINFO_FUNCTION(cvsclient); +PHP_FUNCTION(cvsclient_connect); +PHP_FUNCTION(cvsclient_login); +PHP_FUNCTION(cvsclient_retrieve); + #else #define phpext_cvsclient_ptr NULL