| 7127 | } |
| 7128 | |
| 7129 | static void slaveMode(void) { |
| 7130 | static char eofmark[RDB_EOF_MARK_SIZE]; |
| 7131 | static char lastbytes[RDB_EOF_MARK_SIZE]; |
| 7132 | static int usemark = 0; |
| 7133 | unsigned long long payload = sendSync(context,eofmark); |
| 7134 | char buf[1024]; |
| 7135 | int original_output = config.output; |
| 7136 | |
| 7137 | if (payload == 0) { |
| 7138 | payload = ULLONG_MAX; |
| 7139 | memset(lastbytes,0,RDB_EOF_MARK_SIZE); |
| 7140 | usemark = 1; |
| 7141 | fprintf(stderr,"SYNC with master, discarding " |
| 7142 | "bytes of bulk transfer until EOF marker...\n"); |
| 7143 | } else { |
| 7144 | fprintf(stderr,"SYNC with master, discarding %llu " |
| 7145 | "bytes of bulk transfer...\n", payload); |
| 7146 | } |
| 7147 | |
| 7148 | |
| 7149 | /* Discard the payload. */ |
| 7150 | while(payload) { |
| 7151 | ssize_t nread; |
| 7152 | |
| 7153 | nread = readConn(context,buf,(payload > sizeof(buf)) ? sizeof(buf) : payload); |
| 7154 | if (nread <= 0) { |
| 7155 | fprintf(stderr,"Error reading RDB payload while SYNCing\n"); |
| 7156 | exit(1); |
| 7157 | } |
| 7158 | payload -= nread; |
| 7159 | |
| 7160 | if (usemark) { |
| 7161 | /* Update the last bytes array, and check if it matches our delimiter.*/ |
| 7162 | if (nread >= RDB_EOF_MARK_SIZE) { |
| 7163 | memcpy(lastbytes,buf+nread-RDB_EOF_MARK_SIZE,RDB_EOF_MARK_SIZE); |
| 7164 | } else { |
| 7165 | int rem = RDB_EOF_MARK_SIZE-nread; |
| 7166 | memmove(lastbytes,lastbytes+nread,rem); |
| 7167 | memcpy(lastbytes+rem,buf,nread); |
| 7168 | } |
| 7169 | if (memcmp(lastbytes,eofmark,RDB_EOF_MARK_SIZE) == 0) |
| 7170 | break; |
| 7171 | } |
| 7172 | } |
| 7173 | |
| 7174 | if (usemark) { |
| 7175 | unsigned long long offset = ULLONG_MAX - payload; |
| 7176 | fprintf(stderr,"SYNC done after %llu bytes. Logging commands from master.\n", offset); |
| 7177 | /* put the slave online */ |
| 7178 | sleep(1); |
| 7179 | sendReplconf("ACK", "0"); |
| 7180 | } else |
| 7181 | fprintf(stderr,"SYNC done. Logging commands from master.\n"); |
| 7182 | |
| 7183 | /* Now we can use hiredis to read the incoming protocol. */ |
| 7184 | config.output = OUTPUT_CSV; |
| 7185 | while (cliReadReply(0) == REDIS_OK); |
| 7186 | config.output = original_output; |
no test coverage detected