| 6217 | } |
| 6218 | |
| 6219 | static void slaveMode(void) { |
| 6220 | static char eofmark[RDB_EOF_MARK_SIZE]; |
| 6221 | static char lastbytes[RDB_EOF_MARK_SIZE]; |
| 6222 | static int usemark = 0; |
| 6223 | unsigned long long payload = sendSync(context,eofmark); |
| 6224 | char buf[1024]; |
| 6225 | int original_output = config.output; |
| 6226 | |
| 6227 | if (payload == 0) { |
| 6228 | payload = ULLONG_MAX; |
| 6229 | memset(lastbytes,0,RDB_EOF_MARK_SIZE); |
| 6230 | usemark = 1; |
| 6231 | fprintf(stderr,"SYNC with master, discarding " |
| 6232 | "bytes of bulk transfer until EOF marker...\n"); |
| 6233 | } else { |
| 6234 | fprintf(stderr,"SYNC with master, discarding %llu " |
| 6235 | "bytes of bulk transfer...\n", payload); |
| 6236 | } |
| 6237 | |
| 6238 | |
| 6239 | /* Discard the payload. */ |
| 6240 | while(payload) { |
| 6241 | ssize_t nread; |
| 6242 | |
| 6243 | nread = readConn(context,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload); |
| 6244 | if (nread <= 0) { |
| 6245 | fprintf(stderr,"Error reading RDB payload while SYNCing\n"); |
| 6246 | exit(1); |
| 6247 | } |
| 6248 | payload -= nread; |
| 6249 | |
| 6250 | if (usemark) { |
| 6251 | /* Update the last bytes array, and check if it matches our delimiter.*/ |
| 6252 | if (nread >= RDB_EOF_MARK_SIZE) { |
| 6253 | memcpy(lastbytes,buf+nread-RDB_EOF_MARK_SIZE,RDB_EOF_MARK_SIZE); |
| 6254 | } else { |
| 6255 | int rem = RDB_EOF_MARK_SIZE-nread; |
| 6256 | memmove(lastbytes,lastbytes+nread,rem); |
| 6257 | memcpy(lastbytes+rem,buf,nread); |
| 6258 | } |
| 6259 | if (memcmp(lastbytes,eofmark,RDB_EOF_MARK_SIZE) == 0) |
| 6260 | break; |
| 6261 | } |
| 6262 | } |
| 6263 | |
| 6264 | if (usemark) { |
| 6265 | unsigned long long offset = ULLONG_MAX - payload; |
| 6266 | fprintf(stderr,"SYNC done after %llu bytes. Logging commands from master.\n", offset); |
| 6267 | /* put the slave online */ |
| 6268 | sleep(1); |
| 6269 | sendReplconf("ACK", "0"); |
| 6270 | } else |
| 6271 | fprintf(stderr,"SYNC done. Logging commands from master.\n"); |
| 6272 | |
| 6273 | /* Now we can use hiredis to read the incoming protocol. */ |
| 6274 | config.output = OUTPUT_CSV; |
| 6275 | while (cliReadReply(0) == REDIS_OK); |
| 6276 | config.output = original_output; |
no test coverage detected