php_stream *pdo_pgsql_create_lob_stream(zval *dbh, int lfd, Oid oid) { php_stream *stm; struct pdo_pgsql_lob_self *self = ecalloc(1, sizeof(*self)); pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)(Z_PDO_DBH_P(dbh))->driver_data; ZVAL_COPY_VALUE(&self->dbh, dbh); self->lfd = lfd; self->oid = oid; self->conn = H->server; stm = php_stream_alloc(&pdo_pgsql_lob_stream_ops, self, 0, "r+b"); if (stm) { Z_ADDREF_P(dbh); return stm; } efree(self); return NULL; }
static PHP_METHOD(swoole_mmap, open) { char *filename; size_t l_filename; long offset = 0; long size = -1; if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|ll", &filename, &l_filename, &size, &offset) == FAILURE) { RETURN_FALSE; } if (l_filename <= 0) { swoole_php_fatal_error(E_WARNING, "file name is required."); RETURN_FALSE; } int fd; if ((fd = open(filename, O_RDWR)) < 0) { swoole_php_sys_error(E_WARNING, "open(%s, O_RDWR) failed.", filename); RETURN_FALSE; } if (size <= 0) { struct stat _stat; if (fstat(fd, &_stat) < 0) { swoole_php_sys_error(E_WARNING, "fstat(%s) failed.", filename); close(fd); RETURN_FALSE; } if (_stat.st_size == 0) { swoole_php_sys_error(E_WARNING, "file[%s] is empty.", filename); close(fd); RETURN_FALSE; } if (offset > 0) { size = _stat.st_size - offset; } else { size = _stat.st_size; } } void *addr = mmap(NULL, size, PROT_WRITE | PROT_READ, MAP_SHARED, fd, offset); if (addr == MAP_FAILED) { swoole_php_sys_error(E_WARNING, "mmap(%ld) failed.", size); close(fd); RETURN_FALSE; } swMmapFile *res = emalloc(sizeof(swMmapFile)); res->filename = filename; res->size = size; res->offset = offset; res->memory = addr; res->ptr = addr; close(fd); php_stream *stream = php_stream_alloc(&mmap_ops, res, NULL, "r+"); php_stream_to_zval(stream, return_value); }
self->id = *id; self->chunkId = -1; self->totalChunks = ceil(self->size/self->chunkSize); zval_add_ref(&self->fileObj); zval_add_ref(&self->chunkObj); zval_add_ref(&self->id); /* create base query object */ MAKE_STD_ZVAL(self->query); array_init(self->query); add_assoc_zval(self->query, "files_id", self->id); zval_add_ref(&self->id); stream = php_stream_alloc(&gridfs_stream_ops, self, 0, "rb"); return stream; } /* }}} */ /* {{{ array fstat($fp) */ static int gridfs_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC) { gridfs_stream_data *self = (gridfs_stream_data *) stream->abstract; ssb->sb.st_size = self->size; return SUCCESS; } /* }}} */
/** * Create a opendir() directory stream handle by iterating over each of the * files in a phar and retrieving its relative path. From this, construct * a list of files/directories that are "in" the directory represented by dir */ static php_stream *phar_make_dirstream(char *dir, HashTable *manifest) /* {{{ */ { HashTable *data; size_t dirlen = strlen(dir); char *entry, *found, *save; zend_string *str_key; uint32_t keylen; zend_ulong unused; ALLOC_HASHTABLE(data); zend_hash_init(data, 64, NULL, NULL, 0); if ((*dir == '/' && dirlen == 1 && (manifest->nNumOfElements == 0)) || (dirlen >= sizeof(".phar")-1 && !memcmp(dir, ".phar", sizeof(".phar")-1))) { /* make empty root directory for empty phar */ /* make empty directory for .phar magic directory */ efree(dir); return php_stream_alloc(&phar_dir_ops, data, NULL, "r"); } zend_hash_internal_pointer_reset(manifest); while (FAILURE != zend_hash_has_more_elements(manifest)) { if (HASH_KEY_NON_EXISTENT == zend_hash_get_current_key(manifest, &str_key, &unused)) { break; } keylen = ZSTR_LEN(str_key); if (keylen <= (uint32_t)dirlen) { if (keylen == 0 || keylen < (uint32_t)dirlen || !strncmp(ZSTR_VAL(str_key), dir, dirlen)) { if (SUCCESS != zend_hash_move_forward(manifest)) { break; } continue; } } if (*dir == '/') { /* root directory */ if (keylen >= sizeof(".phar")-1 && !memcmp(ZSTR_VAL(str_key), ".phar", sizeof(".phar")-1)) { /* do not add any magic entries to this directory */ if (SUCCESS != zend_hash_move_forward(manifest)) { break; } continue; } if (NULL != (found = (char *) memchr(ZSTR_VAL(str_key), '/', keylen))) { /* the entry has a path separator and is a subdirectory */ entry = (char *) safe_emalloc(found - ZSTR_VAL(str_key), 1, 1); memcpy(entry, ZSTR_VAL(str_key), found - ZSTR_VAL(str_key)); keylen = found - ZSTR_VAL(str_key); entry[keylen] = '\0'; } else { entry = (char *) safe_emalloc(keylen, 1, 1); memcpy(entry, ZSTR_VAL(str_key), keylen); entry[keylen] = '\0'; } goto PHAR_ADD_ENTRY; } else { if (0 != memcmp(ZSTR_VAL(str_key), dir, dirlen)) { /* entry in directory not found */ if (SUCCESS != zend_hash_move_forward(manifest)) { break; } continue; } else { if (ZSTR_VAL(str_key)[dirlen] != '/') { if (SUCCESS != zend_hash_move_forward(manifest)) { break; } continue; } } } save = ZSTR_VAL(str_key); save += dirlen + 1; /* seek to just past the path separator */ if (NULL != (found = (char *) memchr(save, '/', keylen - dirlen - 1))) { /* is subdirectory */ save -= dirlen + 1; entry = (char *) safe_emalloc(found - save + dirlen, 1, 1); memcpy(entry, save + dirlen + 1, found - save - dirlen - 1); keylen = found - save - dirlen - 1; entry[keylen] = '\0'; } else { /* is file */ save -= dirlen + 1; entry = (char *) safe_emalloc(keylen - dirlen, 1, 1); memcpy(entry, save + dirlen + 1, keylen - dirlen - 1); entry[keylen - dirlen - 1] = '\0'; keylen = keylen - dirlen - 1; } PHAR_ADD_ENTRY: if (keylen) { phar_add_empty(data, entry, keylen); } efree(entry); if (SUCCESS != zend_hash_move_forward(manifest)) { break; } } if (FAILURE != zend_hash_has_more_elements(data)) { efree(dir); if (zend_hash_sort(data, phar_compare_dir_name, 0) == FAILURE) { FREE_HASHTABLE(data); return NULL; } return php_stream_alloc(&phar_dir_ops, data, NULL, "r"); } else { efree(dir); return php_stream_alloc(&phar_dir_ops, data, NULL, "r"); } }