diff --git a/lib/file_io.c b/lib/file_io.c index 21a05426..17cf3926 100644 --- a/lib/file_io.c +++ b/lib/file_io.c @@ -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) { diff --git a/lib/file_io.h b/lib/file_io.h index 1ecd1a3a..2717c93e 100644 --- a/lib/file_io.h +++ b/lib/file_io.h @@ -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.