Repository: valkey-io/valkey
File: src/aof.c (C)

Code before:
        addReplyError(c, "Background append only file rewriting already in progress");
    } else if (hasActiveChildProcess() || server.in_exec) {
        server.aof_rewrite_scheduled = 1;
        /* When manually triggering AOFRW we reset the count
         * so that it can be executed immediately. */
        server.stat_aofrw_consecutive_failures = 0;
        serverLog(LL_NOTICE, "Background append only file rewriting scheduled.");
        addReplyStatus(c, "Background append only file rewriting scheduled");
    } else if (rewriteAppendOnlyFileBackground() == C_OK) {
        addReplyStatus(c, "Background append only file rewriting started");
    } else {
        addReplyError(c, "Can't execute an AOF background rewriting. "
                         "Please check the server logs for more information.");
    }
}

/* Note that we may call this function in signal handle 'sigShutdownHandler',
 * so we need guarantee all functions we call are async-signal-safe.
 * If we call this function from signal handle, we won't call bg_unlink that
 * is not async-signal-safe. */
void aofRemoveTempFile(pid_t childpid, int from_signal) {
    char tmpfile[256];
    char tmpfile2[256];
    char pid[32];

    /* Generate temp aof file name using aync-signal safe functions. */
    ll2string(pid, sizeof(pid), childpid);
    valkey_strlcpy(tmpfile, "temp-rewriteaof-bg-", sizeof(tmpfile));
    valkey_strlcat(tmpfile, pid, sizeof(tmpfile));
    valkey_strlcat(tmpfile, ".aof", sizeof(tmpfile));
    valkey_strlcpy(tmpfile2, "temp-rewriteaof-", sizeof(tmpfile2));
    valkey_strlcat(tmpfile2, pid, sizeof(tmpfile2));
    valkey_strlcat(tmpfile2, ".aof", sizeof(tmpfile2));

    if (from_signal) {
        /* bg_unlink is not async-signal-safe, but in this case we don't really
         * need to close the fd, it'll be released when the process exists. */
        int fd = open(tmpfile, O_RDONLY | O_NONBLOCK);
        int fd2 = open(tmpfile2, O_RDONLY | O_NONBLOCK);
        UNUSED(fd);
        UNUSED(fd2);
        unlink(tmpfile);
        unlink(tmpfile2);
    } else {
        bg_unlink(tmpfile);
        bg_unlink(tmpfile2);
    }
}

/* Get size of an AOF file.
 * The status argument is an optional output argument to be filled with

Review comment (suggestion):
```suggestion
    /* Generate temp aof file name using async-signal safe functions. */
```

Code after:
        addReplyError(c, "Background append only file rewriting already in progress");
    } else if (hasActiveChildProcess() || server.in_exec) {
        server.aof_rewrite_scheduled = 1;
        /* When manually triggering AOFRW we reset the count
         * so that it can be executed immediately. */
        server.stat_aofrw_consecutive_failures = 0;
        serverLog(LL_NOTICE, "Background append only file rewriting scheduled.");
        addReplyStatus(c, "Background append only file rewriting scheduled");
    } else if (rewriteAppendOnlyFileBackground() == C_OK) {
        addReplyStatus(c, "Background append only file rewriting started");
    } else {
        addReplyError(c, "Can't execute an AOF background rewriting. "
                         "Please check the server logs for more information.");
    }
}

/* Note that we may call this function in signal handler 'sigShutdownHandler',
 * so we need guarantee all functions we call are async-signal-safe.
 * If we call this function from signal handle, we won't call bg_unlink that
 * is not async-signal-safe. */
void aofRemoveTempFile(pid_t childpid, int from_signal) {
    char tmpfile[256];
    char tmpfile2[256];
    char pid[32];

    /* Generate temp aof file name using async-signal safe functions. */
    ll2string(pid, sizeof(pid), childpid);
    valkey_strlcpy(tmpfile, "temp-rewriteaof-bg-", sizeof(tmpfile));
    valkey_strlcat(tmpfile, pid, sizeof(tmpfile));
    valkey_strlcat(tmpfile, ".aof", sizeof(tmpfile));
    valkey_strlcpy(tmpfile2, "temp-rewriteaof-", sizeof(tmpfile2));
    valkey_strlcat(tmpfile2, pid, sizeof(tmpfile2));
    valkey_strlcat(tmpfile2, ".aof", sizeof(tmpfile2));

    if (from_signal) {
        /* bg_unlink is not async-signal-safe, but in this case we don't really
         * need to close the fd, it'll be released when the process exists. */
        int fd = open(tmpfile, O_RDONLY | O_NONBLOCK);
        int fd2 = open(tmpfile2, O_RDONLY | O_NONBLOCK);
        UNUSED(fd);
        UNUSED(fd2);
        unlink(tmpfile);
        unlink(tmpfile2);
    } else {
        bg_unlink(tmpfile);
        bg_unlink(tmpfile2);
    }
}

/* Get size of an AOF file.
 * The status argument is an optional output argument to be filled with