Write a raw buffer through a redisContext. If we already have something * in the buffer (leftovers from hiredis operations) it will be written * as well. */
| 130 | * as well. |
| 131 | */ |
| 132 | ssize_t cliWriteConn(redisContext *c, const char *buf, size_t buf_len) |
| 133 | { |
| 134 | int done = 0; |
| 135 | |
| 136 | /* Append data to buffer which is *usually* expected to be empty |
| 137 | * but we don't assume that, and write. |
| 138 | */ |
| 139 | c->obuf = sdscatlen(c->obuf, buf, buf_len); |
| 140 | if (redisBufferWrite(c, &done) == REDIS_ERR) { |
| 141 | if (!(c->flags & REDIS_BLOCK)) |
| 142 | errno = EAGAIN; |
| 143 | |
| 144 | /* On error, we assume nothing was written and we roll back the |
| 145 | * buffer to its original state. |
| 146 | */ |
| 147 | if (sdslen(c->obuf) > buf_len) |
| 148 | sdsrange(c->obuf, 0, -(buf_len+1)); |
| 149 | else |
| 150 | sdsclear(c->obuf); |
| 151 | |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | /* If we're done, free up everything. We may have written more than |
| 156 | * buf_len (if c->obuf was not initially empty) but we don't have to |
| 157 | * tell. |
| 158 | */ |
| 159 | if (done) { |
| 160 | sdsclear(c->obuf); |
| 161 | return buf_len; |
| 162 | } |
| 163 | |
| 164 | /* Write was successful but we have some leftovers which we should |
| 165 | * remove from the buffer. |
| 166 | * |
| 167 | * Do we still have data that was there prior to our buf? If so, |
| 168 | * restore buffer to it's original state and report no new data was |
| 169 | * writen. |
| 170 | */ |
| 171 | if (sdslen(c->obuf) > buf_len) { |
| 172 | sdsrange(c->obuf, 0, -(buf_len+1)); |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /* At this point we're sure no prior data is left. We flush the buffer |
| 177 | * and report how much we've written. |
| 178 | */ |
| 179 | size_t left = sdslen(c->obuf); |
| 180 | sdsclear(c->obuf); |
| 181 | return buf_len - left; |
| 182 | } |
| 183 | |
| 184 | /* Wrapper around OpenSSL (libssl and libcrypto) initialisation |
| 185 | */ |
no test coverage detected