diff options
author | Remi Collet <remi@remirepo.net> | 2023-10-12 15:49:40 +0200 |
---|---|---|
committer | Remi Collet <remi@php.net> | 2023-10-12 15:49:40 +0200 |
commit | cecd15012c843bc60ed0e667eef7447632e0ece6 (patch) | |
tree | e3b136725eda0fe6ddd4c1815538e10d2e1a7ed7 | |
parent | 8bf3b94ac38682b4e5f1553ce0a501873dbcb855 (diff) |
implement stat
-rw-r--r-- | rpminfo.c | 68 | ||||
-rw-r--r-- | tests/014-stream.phpt | 20 |
2 files changed, 72 insertions, 16 deletions
@@ -590,10 +590,8 @@ static ssize_t php_rpm_ops_read(php_stream *stream, char *buf, size_t count) return n; } -static int php_rpm_ops_close(php_stream *stream, int close_handle) +static void php_rpm_ops_free(struct php_rpm_stream_data_t *self, int close_handle) { - STREAM_DATA_FROM_STREAM(); - if (self) { if (close_handle) { Fclose(self->gzdi); @@ -603,6 +601,13 @@ static int php_rpm_ops_close(php_stream *stream, int close_handle) } efree(self); } +} + +static int php_rpm_ops_close(php_stream *stream, int close_handle) +{ + STREAM_DATA_FROM_STREAM(); + + php_rpm_ops_free(self, close_handle); stream->abstract = NULL; return EOF; @@ -630,20 +635,14 @@ const php_stream_ops php_stream_rpmio_ops = { NULL /* set_option */ }; -php_stream *php_stream_rpm_opener(php_stream_wrapper *wrapper, - const char *path, - const char *mode, - int options, - zend_string **opened_path, - php_stream_context *context STREAMS_DC) +static struct php_rpm_stream_data_t *php_stream_rpm_finder(const char *path, const char *mode) { size_t path_len; zend_string *file_basename; char file_dirname[MAXPATHLEN]; char *fragment; size_t fragment_len; - php_stream *stream = NULL; - struct php_rpm_stream_data_t *self; + struct php_rpm_stream_data_t *self = NULL; FD_t fdi; FD_t gzdi; int rc; @@ -710,7 +709,7 @@ php_stream *php_stream_rpm_opener(php_stream_wrapper *wrapper, break; } } - if (rc == RPMERR_ITER_END || !S_ISREG(rpmfiFMode(fi)) || !rpmfiArchiveHasContent(fi)) { + if (rc == RPMERR_ITER_END) { Fclose(gzdi); rpmfilesFree(files); rpmfiFree(fi); @@ -721,19 +720,56 @@ php_stream *php_stream_rpm_opener(php_stream_wrapper *wrapper, self->files = files; self->fi = fi; self->h = h; - - stream = php_stream_alloc(&php_stream_rpmio_ops, self, NULL, mode); } zend_string_release_ex(file_basename, 0); - return stream; + return self; +} + +php_stream *php_stream_rpm_opener(php_stream_wrapper *wrapper, + const char *path, + const char *mode, + int options, + zend_string **opened_path, + php_stream_context *context STREAMS_DC) +{ + struct php_rpm_stream_data_t *self; + + self = php_stream_rpm_finder(path, mode); + if (self) { + if (opened_path) { + *opened_path = zend_string_init(path, strlen(path), 0); + } + if (!S_ISREG(rpmfiFMode(self->fi)) || !rpmfiArchiveHasContent(self->fi)) { + php_rpm_ops_free(self, 1); + } else { + return php_stream_alloc(&php_stream_rpmio_ops, self, NULL, mode); + } + } + + return NULL; +} + +static int php_stream_rpm_stat(php_stream_wrapper *wrapper, const char *url, int flags, + php_stream_statbuf *ssb, php_stream_context *context) +{ + struct php_rpm_stream_data_t *self; + int rc = -1; + + self = php_stream_rpm_finder(url, "r"); + if (self) { + rc = rpmfiStat(self->fi, 0, &ssb->sb); + php_rpm_ops_free(self, 1); + } + + return rc; } static const php_stream_wrapper_ops rpm_stream_wops = { php_stream_rpm_opener, NULL, /* close */ NULL, /* fstat */ - NULL, /* stat */ + php_stream_rpm_stat, NULL, /* opendir */ "RPM wrapper", NULL, /* unlink */ diff --git a/tests/014-stream.phpt b/tests/014-stream.phpt index 90192bf..6651a8c 100644 --- a/tests/014-stream.phpt +++ b/tests/014-stream.phpt @@ -7,10 +7,19 @@ if (version_compare(RPMVERSION, '4.13', 'lt')) print("skip librpm is older than ?> --FILE-- <?php +$d = "rpm://" . __DIR__ . "/bidon.rpm#/usr/share/doc/bidon"; $n = "rpm://" . __DIR__ . "/bidon.rpm#/usr/share/doc/bidon/README"; +echo "+ wrapper\n"; var_dump(in_array('rpm', stream_get_wrappers())); +echo "+ stat\n"; +$s = stat($d); // S_ISDIR +var_dump($s['size'], $s['mode'] , $s['mode'] & 0040000 ? "OK" : "KO"); +$s = stat($n); // S_ISREG +var_dump($s['size'], $s['mode'] , $s['mode'] & 0100000 ? "OK" : "KO"); + +echo "+ file\n"; var_dump($f = fopen($n, "r")); $s = fstat($f); var_dump($s['size'], $s['mode']); @@ -20,13 +29,23 @@ var_dump(trim(fread($f, 100))); var_dump(feof($f)); fclose($f); +echo "+ stream\n"; var_dump(trim(file_get_contents($n))); var_dump(file_get_contents(str_replace('README', 'TODO', $n))); ?> Done --EXPECTF-- ++ wrapper bool(true) ++ stat +int(0) +int(16877) +string(2) "OK" +int(29) +int(33188) +string(2) "OK" ++ file resource(%d) of type (stream) int(29) int(33188) @@ -34,6 +53,7 @@ string(10) "Mon Feb 12" bool(false) string(17) "13:27:47 CET 2018" bool(true) ++ stream string(28) "Mon Feb 12 13:27:47 CET 2018" Warning: file_get_contents(%s/bidon.rpm#/usr/share/doc/bidon/TODO): Failed to open stream: operation failed in %s on line %d |