redis-cli --rdb: fix broken fsync/ftruncate for stdout (#9135)

A change in redis 6.2 caused redis-cli --rdb that's directed to stdout to fail because fsync fails.
This commit avoids doing ftruncate (fails with a warning) and fsync (fails with an error) when the
output file is `-`, and adds the missing documentation that `-` means stdout.

Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Wang Yuan <wangyuancode@163.com>
This commit is contained in:
Mikhail Fesenko 2021-06-30 16:49:54 +03:00 committed by GitHub
parent eaa52719a3
commit 74fe15b360
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1930,6 +1930,7 @@ static void usage(int err) {
" --lru-test <keys> Simulate a cache workload with an 80-20 distribution.\n"
" --replica Simulate a replica showing commands received from the master.\n"
" --rdb <filename> Transfer an RDB dump from remote server to local file.\n"
" Use filename of \"-\" to write to stdout.\n"
" --pipe Transfer raw Redis protocol from stdin to server.\n"
" --pipe-timeout <n> In --pipe mode, abort with error if after sending all data.\n"
" no reply is received within <n> seconds.\n"
@ -7227,8 +7228,9 @@ static void getRDB(clusterManagerNode *node) {
payload, filename);
}
int write_to_stdout = !strcmp(filename,"-");
/* Write to file. */
if (!strcmp(filename,"-")) {
if (write_to_stdout) {
fd = STDOUT_FILENO;
} else {
fd = open(filename, O_CREAT|O_WRONLY, 0644);
@ -7270,7 +7272,7 @@ static void getRDB(clusterManagerNode *node) {
}
if (usemark) {
payload = ULLONG_MAX - payload - RDB_EOF_MARK_SIZE;
if (ftruncate(fd, payload) == -1)
if (!write_to_stdout && ftruncate(fd, payload) == -1)
fprintf(stderr,"ftruncate failed: %s.\n", strerror(errno));
fprintf(stderr,"Transfer finished with success after %llu bytes\n", payload);
} else {
@ -7279,7 +7281,7 @@ static void getRDB(clusterManagerNode *node) {
redisFree(s); /* Close the connection ASAP as fsync() may take time. */
if (node)
node->context = NULL;
if (fsync(fd) == -1) {
if (!write_to_stdout && fsync(fd) == -1) {
fprintf(stderr,"Fail to fsync '%s': %s\n", filename, strerror(errno));
exit(1);
}