summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornanahi <130121847+na-na-hi@users.noreply.github.com>2024-02-19 03:41:45 -0500
committersfan5 <sfan5@live.de>2024-02-23 21:35:57 +0100
commit84015959cc5d36d8973b2f07ece066902bbbdbe7 (patch)
treefca1aec3c77de0218a4f5c3b7d89713c36f68482
parentc36e051470dceb24c75d36316490e063a77dacba (diff)
downloadmpv-84015959cc5d36d8973b2f07ece066902bbbdbe7.tar.bz2
mpv-84015959cc5d36d8973b2f07ece066902bbbdbe7.tar.xz
osdep/io: move I/O utility functions to misc/io_utils
The purpose of osdep/io is to provide a wrapper for POSIX I/O functions on Windows. The last 2 functions are utility functions which don't serve this purpose. Move them to a separate file.
-rw-r--r--demux/cache.c1
-rw-r--r--meson.build1
-rw-r--r--misc/io_utils.c87
-rw-r--r--misc/io_utils.h25
-rw-r--r--osdep/io.c55
-rw-r--r--osdep/io.h3
-rw-r--r--video/out/vo_gpu_next.c2
7 files changed, 115 insertions, 59 deletions
diff --git a/demux/cache.c b/demux/cache.c
index 562eab087b..dc0e91b74a 100644
--- a/demux/cache.c
+++ b/demux/cache.c
@@ -26,6 +26,7 @@
#include "common/msg.h"
#include "common/av_common.h"
#include "demux.h"
+#include "misc/io_utils.h"
#include "options/path.h"
#include "options/m_config.h"
#include "options/m_option.h"
diff --git a/meson.build b/meson.build
index b564459e09..4ce2b84125 100644
--- a/meson.build
+++ b/meson.build
@@ -134,6 +134,7 @@ sources = files(
'misc/bstr.c',
'misc/charset_conv.c',
'misc/dispatch.c',
+ 'misc/io_utils.c',
'misc/json.c',
'misc/language.c',
'misc/natural_sort.c',
diff --git a/misc/io_utils.c b/misc/io_utils.c
new file mode 100644
index 0000000000..c973cee0e4
--- /dev/null
+++ b/misc/io_utils.c
@@ -0,0 +1,87 @@
+/*
+ * I/O utility functions
+ *
+ * This file is part of mpv.
+ *
+ * mpv is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * mpv is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <limits.h>
+#include <unistd.h>
+
+#include "mpv_talloc.h"
+#include "config.h"
+#include "misc/random.h"
+#include "misc/io_utils.h"
+#include "osdep/io.h"
+
+int mp_mkostemps(char *template, int suffixlen, int flags)
+{
+ size_t len = strlen(template);
+ char *t = len >= 6 + suffixlen ? &template[len - (6 + suffixlen)] : NULL;
+ if (!t || strncmp(t, "XXXXXX", 6) != 0) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ for (size_t fuckshit = 0; fuckshit < UINT32_MAX; fuckshit++) {
+ // Using a random value may make it require fewer iterations (even if
+ // not truly random; just a counter would be sufficient).
+ size_t fuckmess = mp_rand_next();
+ char crap[7] = "";
+ snprintf(crap, sizeof(crap), "%06zx", fuckmess);
+ memcpy(t, crap, 6);
+
+ int res = open(template, O_RDWR | O_CREAT | O_EXCL | flags, 0600);
+ if (res >= 0 || errno != EEXIST)
+ return res;
+ }
+
+ 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/misc/io_utils.h b/misc/io_utils.h
new file mode 100644
index 0000000000..012f2425f6
--- /dev/null
+++ b/misc/io_utils.h
@@ -0,0 +1,25 @@
+/*
+ * I/O utility functions
+ *
+ * This file is part of mpv.
+ *
+ * mpv is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * mpv is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <stddef.h>
+
+int mp_mkostemps(char *template, int suffixlen, int flags);
+bool mp_save_to_file(const char *filepath, const void *data, size_t size);
diff --git a/osdep/io.c b/osdep/io.c
index bfcd44f096..18c2a83f94 100644
--- a/osdep/io.c
+++ b/osdep/io.c
@@ -891,58 +891,3 @@ void freelocale(locale_t locobj)
}
#endif // __MINGW32__
-
-int mp_mkostemps(char *template, int suffixlen, int flags)
-{
- size_t len = strlen(template);
- char *t = len >= 6 + suffixlen ? &template[len - (6 + suffixlen)] : NULL;
- if (!t || strncmp(t, "XXXXXX", 6) != 0) {
- errno = EINVAL;
- return -1;
- }
-
- for (size_t fuckshit = 0; fuckshit < UINT32_MAX; fuckshit++) {
- // Using a random value may make it require fewer iterations (even if
- // not truly random; just a counter would be sufficient).
- size_t fuckmess = mp_rand_next();
- char crap[7] = "";
- snprintf(crap, sizeof(crap), "%06zx", fuckmess);
- memcpy(t, crap, 6);
-
- int res = open(template, O_RDWR | O_CREAT | O_EXCL | flags, 0600);
- if (res >= 0 || errno != EEXIST)
- return res;
- }
-
- 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 31d17154ed..21555e8799 100644
--- a/osdep/io.h
+++ b/osdep/io.h
@@ -239,7 +239,4 @@ 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 2e6d08dcb1..c68e67e685 100644
--- a/video/out/vo_gpu_next.c
+++ b/video/out/vo_gpu_next.c
@@ -32,10 +32,10 @@
#include "config.h"
#include "common/common.h"
+#include "misc/io_utils.h"
#include "options/m_config.h"
#include "options/options.h"
#include "options/path.h"
-#include "osdep/io.h"
#include "osdep/threads.h"
#include "stream/stream.h"
#include "video/fmt-conversion.h"