FFmpeg
Loading...
Searching...
No Matches
rename.c
Go to the documentation of this file.
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
30
32
33static int create_file(const char *path)
34{
35 FILE *f = fopen(path, "wb");
36 if (!f)
37 return -1;
38 fputs("ffmpeg rename test\n", f);
39 fclose(f);
40 return 0;
41}
42
43static int file_exists(const char *path)
44{
45 FILE *f = fopen(path, "rb");
46 if (!f)
47 return 0;
48 fclose(f);
49 return 1;
50}
51
52int main(void)
53{
54 char src[64];
55 char dst[64];
56 unsigned seed = av_get_random_seed();
57 int ret = 0;
58
59 snprintf(src, sizeof(src), "ff-rename-test-%08x.src", seed);
60 snprintf(dst, sizeof(dst), "ff-rename-test-%08x.dst", seed);
61
62 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 if (rename(src, dst) != 0) {
69 perror("rename");
70 ret = 1;
71 goto cleanup;
72 }
73
74 if (file_exists(src)) {
75 fprintf(stderr, "source still exists after rename\n");
76 ret = 1;
77 goto cleanup;
78 }
79
80 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 if (rename(src, dst) != -1) {
88 fprintf(stderr, "rename of nonexistent source unexpectedly succeeded\n");
89 ret = 1;
90 goto cleanup;
91 }
92
94 unlink(src);
95 unlink(dst);
96 return ret;
97}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
#define f(width, name)
Definition cbs_vp8.c:236
static av_cold void cleanup(FlashSV2Context *s)
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
miscellaneous OS support macros and functions.
static int create_file(const char *path)
Definition rename.c:33
static int file_exists(const char *path)
Definition rename.c:43
int main(void)
Definition rename.c:52
#define snprintf
Definition snprintf.h:34
#define src
Definition vp8dsp.c:248
static unsigned int seed
Definition videogen.c:78