1 /*
2  * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
3  * Copyright (c) 2009-2019, Salvatore Sanfilippo <antirez at gmail dot com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   * Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *   * Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *   * Neither the name of Redis nor the names of its contributors may be used
15  *     to endorse or promote products derived from this software without
16  *     specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 #ifndef __REDIS_RIO_H
33 #define __REDIS_RIO_H
34 
35 #include <stdio.h>
36 #include <stdint.h>
37 #include "sds.h"
38 #include "connection.h"
39 
40 #define RIO_FLAG_READ_ERROR (1<<0)
41 #define RIO_FLAG_WRITE_ERROR (1<<1)
42 
43 struct _rio {
44     /* Backend functions.
45      * Since this functions do not tolerate short writes or reads the return
46      * value is simplified to: zero on error, non zero on complete success. */
47     size_t (*read)(struct _rio *, void *buf, size_t len);
48     size_t (*write)(struct _rio *, const void *buf, size_t len);
49     off_t (*tell)(struct _rio *);
50     int (*flush)(struct _rio *);
51     /* The update_cksum method if not NULL is used to compute the checksum of
52      * all the data that was read or written so far. The method should be
53      * designed so that can be called with the current checksum, and the buf
54      * and len fields pointing to the new block of data to add to the checksum
55      * computation. */
56     void (*update_cksum)(struct _rio *, const void *buf, size_t len);
57 
58     /* The current checksum and flags (see RIO_FLAG_*) */
59     uint64_t cksum, flags;
60 
61     /* number of bytes read or written */
62     size_t processed_bytes;
63 
64     /* maximum single read or write chunk size */
65     size_t max_processing_chunk;
66 
67     /* Backend-specific vars. */
68     union {
69         /* In-memory buffer target. */
70         struct {
71             sds ptr;
72             off_t pos;
73         } buffer;
74         /* Stdio file pointer target. */
75         struct {
76             FILE *fp;
77             off_t buffered; /* Bytes written since last fsync. */
78             off_t autosync; /* fsync after 'autosync' bytes written. */
79         } file;
80         /* Connection object (used to read from socket) */
81         struct {
82             connection *conn;   /* Connection */
83             off_t pos;    /* pos in buf that was returned */
84             sds buf;      /* buffered data */
85             size_t read_limit;  /* don't allow to buffer/read more than that */
86             size_t read_so_far; /* amount of data read from the rio (not buffered) */
87         } conn;
88         /* FD target (used to write to pipe). */
89         struct {
90             int fd;       /* File descriptor. */
91             off_t pos;
92             sds buf;
93         } fd;
94     } io;
95 };
96 
97 typedef struct _rio rio;
98 
99 /* The following functions are our interface with the stream. They'll call the
100  * actual implementation of read / write / tell, and will update the checksum
101  * if needed. */
102 
rioWrite(rio * r,const void * buf,size_t len)103 static inline size_t rioWrite(rio *r, const void *buf, size_t len) {
104     if (r->flags & RIO_FLAG_WRITE_ERROR) return 0;
105     while (len) {
106         size_t bytes_to_write = (r->max_processing_chunk && r->max_processing_chunk < len) ? r->max_processing_chunk : len;
107         if (r->update_cksum) r->update_cksum(r,buf,bytes_to_write);
108         if (r->write(r,buf,bytes_to_write) == 0) {
109             r->flags |= RIO_FLAG_WRITE_ERROR;
110             return 0;
111         }
112         buf = (char*)buf + bytes_to_write;
113         len -= bytes_to_write;
114         r->processed_bytes += bytes_to_write;
115     }
116     return 1;
117 }
118 
rioRead(rio * r,void * buf,size_t len)119 static inline size_t rioRead(rio *r, void *buf, size_t len) {
120     if (r->flags & RIO_FLAG_READ_ERROR) return 0;
121     while (len) {
122         size_t bytes_to_read = (r->max_processing_chunk && r->max_processing_chunk < len) ? r->max_processing_chunk : len;
123         if (r->read(r,buf,bytes_to_read) == 0) {
124             r->flags |= RIO_FLAG_READ_ERROR;
125             return 0;
126         }
127         if (r->update_cksum) r->update_cksum(r,buf,bytes_to_read);
128         buf = (char*)buf + bytes_to_read;
129         len -= bytes_to_read;
130         r->processed_bytes += bytes_to_read;
131     }
132     return 1;
133 }
134 
rioTell(rio * r)135 static inline off_t rioTell(rio *r) {
136     return r->tell(r);
137 }
138 
rioFlush(rio * r)139 static inline int rioFlush(rio *r) {
140     return r->flush(r);
141 }
142 
143 /* This function allows to know if there was a read error in any past
144  * operation, since the rio stream was created or since the last call
145  * to rioClearError(). */
rioGetReadError(rio * r)146 static inline int rioGetReadError(rio *r) {
147     return (r->flags & RIO_FLAG_READ_ERROR) != 0;
148 }
149 
150 /* Like rioGetReadError() but for write errors. */
rioGetWriteError(rio * r)151 static inline int rioGetWriteError(rio *r) {
152     return (r->flags & RIO_FLAG_WRITE_ERROR) != 0;
153 }
154 
rioClearErrors(rio * r)155 static inline void rioClearErrors(rio *r) {
156     r->flags &= ~(RIO_FLAG_READ_ERROR|RIO_FLAG_WRITE_ERROR);
157 }
158 
159 void rioInitWithFile(rio *r, FILE *fp);
160 void rioInitWithBuffer(rio *r, sds s);
161 void rioInitWithConn(rio *r, connection *conn, size_t read_limit);
162 void rioInitWithFd(rio *r, int fd);
163 
164 void rioFreeFd(rio *r);
165 void rioFreeConn(rio *r, sds* out_remainingBufferedData);
166 
167 size_t rioWriteBulkCount(rio *r, char prefix, long count);
168 size_t rioWriteBulkString(rio *r, const char *buf, size_t len);
169 size_t rioWriteBulkLongLong(rio *r, long long l);
170 size_t rioWriteBulkDouble(rio *r, double d);
171 
172 struct redisObject;
173 int rioWriteBulkObject(rio *r, struct redisObject *obj);
174 
175 void rioGenericUpdateChecksum(rio *r, const void *buf, size_t len);
176 void rioSetAutoSync(rio *r, off_t bytes);
177 
178 #endif
179