From 93aba10379bf787d4b5de271895c40bae88e8d95 Mon Sep 17 00:00:00 2001 From: All Asmaul Husnain <2263081025@uttarauniversity.edu.bd> Date: Wed, 4 Nov 2026 03:20:55 +0600 Subject: [PATCH] fix: avoid truncating existing database files on open failure --- src/pager.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/pager.c b/src/pager.c index 8e8d451..67c5d43 100644 --- a/src/pager.c +++ b/src/pager.c @@ -1,5 +1,6 @@ #include "pager.h" +#include #include static size_t page_count_for_length(size_t file_length) @@ -16,8 +17,11 @@ Pager *pager_open(const char *filename, PagerResult *result) *result = PAGER_OK; FILE *file = fopen(filename, "r+b"); - if (file == NULL) { - file = fopen(filename, "w+b"); + if (file == NULL && errno == ENOENT) { + file = fopen(filename, "w+bx"); + if (file == NULL && errno == EEXIST) { + file = fopen(filename, "r+b"); + } } if (file == NULL) {