| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2026 Christopher Decker | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "config.h" | ||
| 22 | |||
| 23 | #include <stdio.h> | ||
| 24 | |||
| 25 | #if HAVE_UNISTD_H | ||
| 26 | #include <unistd.h> | ||
| 27 | #endif | ||
| 28 | |||
| 29 | #include "libavutil/random_seed.h" | ||
| 30 | |||
| 31 | #include "libavformat/os_support.h" | ||
| 32 | |||
| 33 | 1 | static int create_file(const char *path) | |
| 34 | { | ||
| 35 | 1 | FILE *f = fopen(path, "wb"); | |
| 36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!f) |
| 37 | ✗ | return -1; | |
| 38 | 1 | fputs("ffmpeg rename test\n", f); | |
| 39 | 1 | fclose(f); | |
| 40 | 1 | return 0; | |
| 41 | } | ||
| 42 | |||
| 43 | 2 | static int file_exists(const char *path) | |
| 44 | { | ||
| 45 | 2 | FILE *f = fopen(path, "rb"); | |
| 46 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (!f) |
| 47 | 1 | return 0; | |
| 48 | 1 | fclose(f); | |
| 49 | 1 | return 1; | |
| 50 | } | ||
| 51 | |||
| 52 | 1 | int main(void) | |
| 53 | { | ||
| 54 | char src[64]; | ||
| 55 | char dst[64]; | ||
| 56 | 1 | unsigned seed = av_get_random_seed(); | |
| 57 | 1 | int ret = 0; | |
| 58 | |||
| 59 | 1 | snprintf(src, sizeof(src), "ff-rename-test-%08x.src", seed); | |
| 60 | 1 | snprintf(dst, sizeof(dst), "ff-rename-test-%08x.dst", seed); | |
| 61 | |||
| 62 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (create_file(src) < 0) { |
| 63 | ✗ | perror("create src"); | |
| 64 | ✗ | return 1; | |
| 65 | } | ||
| 66 | |||
| 67 | /* rename() must follow POSIX semantics and return 0 on success. */ | ||
| 68 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (rename(src, dst) != 0) { |
| 69 | ✗ | perror("rename"); | |
| 70 | ✗ | ret = 1; | |
| 71 | ✗ | goto cleanup; | |
| 72 | } | ||
| 73 | |||
| 74 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (file_exists(src)) { |
| 75 | ✗ | fprintf(stderr, "source still exists after rename\n"); | |
| 76 | ✗ | ret = 1; | |
| 77 | ✗ | goto cleanup; | |
| 78 | } | ||
| 79 | |||
| 80 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (!file_exists(dst)) { |
| 81 | ✗ | fprintf(stderr, "destination missing after rename\n"); | |
| 82 | ✗ | ret = 1; | |
| 83 | ✗ | goto cleanup; | |
| 84 | } | ||
| 85 | |||
| 86 | /* Renaming a nonexistent source must fail with a -1 return. */ | ||
| 87 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (rename(src, dst) != -1) { |
| 88 | ✗ | fprintf(stderr, "rename of nonexistent source unexpectedly succeeded\n"); | |
| 89 | ✗ | ret = 1; | |
| 90 | ✗ | goto cleanup; | |
| 91 | } | ||
| 92 | |||
| 93 | 1 | cleanup: | |
| 94 | 1 | unlink(src); | |
| 95 | 1 | unlink(dst); | |
| 96 | 1 | return ret; | |
| 97 | } | ||
| 98 |