From e2e75906da76ab587156b29b5847e57b0e29c0b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Tue, 21 Nov 2023 08:32:21 +0100 Subject: [PATCH] io: add mp_save_file Move from vo_gpu_next.c to io.c and return result. --- osdep/io.c | 29 +++++++++++++++++++++++++++++ osdep/io.h | 1 + video/out/vo_gpu_next.c | 29 ++++------------------------- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/osdep/io.c b/osdep/io.c index 93f97b5ecf433..617764c16a352 100644 --- a/osdep/io.c +++ b/osdep/io.c @@ -918,3 +918,32 @@ int mp_mkostemps(char *template, int suffixlen, int flags) errno = EEXIST; return -1; } + +bool mp_save_to_file(const char *filepath, const void *data, size_t size) +{ + assert(filepath && data && size); + + bool result = false; + char *tmp = talloc_asprintf(NULL, "%sXXXXXX", filepath); + int fd = mkstemp(tmp); + if (fd < 0) + goto done; + FILE *cache = fdopen(fd, "wb"); + if (!cache) { + close(fd); + unlink(tmp); + goto done; + } + size_t written = fwrite(data, size, 1, cache); + int ret = fclose(cache); + if (written > 0 && !ret) { + ret = rename(tmp, filepath); + result = !ret; + } else { + unlink(tmp); + } + +done: + talloc_free(tmp); + return result; +} diff --git a/osdep/io.h b/osdep/io.h index 0a0d01eb0562f..31d17154edfc4 100644 --- a/osdep/io.h +++ b/osdep/io.h @@ -240,5 +240,6 @@ extern char **environ; #endif /* __MINGW32__ */ int mp_mkostemps(char *template, int suffixlen, int flags); +bool mp_save_to_file(const char *filepath, const void *data, size_t size); #endif diff --git a/video/out/vo_gpu_next.c b/video/out/vo_gpu_next.c index f859b0f207310..99e7754a50060 100644 --- a/video/out/vo_gpu_next.c +++ b/video/out/vo_gpu_next.c @@ -1512,30 +1512,6 @@ static char *cache_filepath(void *ta_ctx, char *dir, const char *prefix, uint64_ return mp_path_join_bstr(ta_ctx, bstr0(dir), filename); } -static void cache_save_file(void *ta_ctx, char *filepath, void *data, size_t size) -{ - if (!data || !size) - return; - - char *tmp = talloc_asprintf(ta_ctx, "%sXXXXXX", filepath); - int fd = mkstemp(tmp); - if (fd < 0) - return; - FILE *cache = fdopen(fd, "wb"); - if (!cache) { - close(fd); - unlink(tmp); - return; - } - size_t written = fwrite(data, size, 1, cache); - int ret = fclose(cache); - if (written > 0 && !ret) { - ret = rename(tmp, filepath); - } else { - unlink(tmp); - } -} - static pl_cache_obj cache_load_obj(void *p, uint64_t key) { struct cache *c = p; @@ -1573,6 +1549,9 @@ static pl_cache_obj cache_load_obj(void *p, uint64_t key) static void cache_save_obj(void *p, pl_cache_obj obj) { + if (!obj.data || !obj.size) + return; + const struct cache *c = p; void *ta_ctx = talloc_new(NULL); @@ -1590,7 +1569,7 @@ static void cache_save_obj(void *p, pl_cache_obj obj) } int64_t save_start = mp_time_ns(); - cache_save_file(ta_ctx, filepath, obj.data, obj.size); + mp_save_to_file(filepath, obj.data, obj.size); int64_t save_end = mp_time_ns(); MP_DBG(c, "%s: key(%" PRIx64 "), size(%zu), save time(%.3f ms)\n", __func__, obj.key, obj.size,