Add checked UTF-8 opening of existing block files for update

This commit is contained in:
evgeny committed 2026-10-22 19:03:16 +02:00
1 parent 55d2f437a0
commit a3a8b5b1be
2 files changed
+18

No files matched your search

+16
View File
@@ -83,6 +83,22 @@ FILE *file_io_open_read(const char *path) {
return file;
}
FILE *file_io_open_update(const char *path) {
struct file_io_info info;
if (file_io_inspect(path, 0, &info) || info.type != FILE_IO_REGULAR) {
DEBUG_ERROR(DEBUG_CATEGORY_SYS, "file update requires regular file path=%s", path ? path : "(null)"); return NULL;
}
#ifdef _WIN32
wchar_t wide[FILE_IO_PATH_MAX];
if (file_io_wide(path, wide)) return NULL;
FILE *file = _wfopen(wide, L"r+b");
#else
FILE *file = fopen(path, "r+b");
#endif
if (!file) DEBUG_ERROR(DEBUG_CATEGORY_SYS, "file update open failed path=%s errno=%d", path, errno);
return file;
}
int file_io_paths(const char *source, const char *destination) {
struct file_io_info input, output;
if (file_io_inspect(source, 1, &input) || input.type != FILE_IO_REGULAR) {
+2
View File
@@ -29,6 +29,8 @@ int file_io_wide(const char *path, wchar_t wide[FILE_IO_PATH_MAX]);
#endif
// Открыть обычный файл для чтения; путь UTF-8. NULL при ошибке, fclose принадлежит вызывающему.
FILE *file_io_open_read(const char *path);
// Открыть существующий regular-файл для чтения/записи без усечения; конечные ссылки запрещены. UTF-8, NULL при ошибке.
FILE *file_io_open_update(const char *path);
// Источник должен быть regular; назначение отсутствует или regular без конечной ссылки. 1 — один файл, 0 — разные, -1 — ошибка.
int file_io_paths(const char *source, const char *destination);
// Создать эксклюзивный temp рядом с destination; структура заранее обнулена, даже после ошибки вызвать discard.