netstar pushed a commit to branch master. http://git.enlightenment.org/apps/ecrire.git/commit/?id=50db48daf5f79a96d796943e2d94d9bf1c199b07
commit 50db48daf5f79a96d796943e2d94d9bf1c199b07 Author: Alastair Poole <nets...@gmail.com> Date: Sat Feb 27 11:11:00 2021 +0000 ecire: save/load more denfensive. --- src/bin/file_utils.c | 18 ++++++++++++------ src/bin/main.c | 36 +++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/bin/file_utils.c b/src/bin/file_utils.c index 1c5f6ae..8b97112 100644 --- a/src/bin/file_utils.c +++ b/src/bin/file_utils.c @@ -64,13 +64,18 @@ Eina_Bool file_save(const char *file, const char *text) { FILE *f; + struct stat st; - f = fopen(file, "wb"); - if (!f) + if (stat(file, &st) != -1) { - ERR("could not open '%s' for writing.", file); - return EINA_FALSE; + if (S_ISDIR(st.st_mode)) + return EINA_FALSE; } + + f = fopen(file, "wb"); + if (!f) + return EINA_FALSE; + if (text) { if (fputs(text, f) == EOF) @@ -87,14 +92,15 @@ Eina_Bool file_plain_save(const char *file, const char *text) { char *text2; + Eina_Bool ok; text2 = elm_entry_markup_to_utf8(text); if (!text2) return EINA_FALSE; - file_save(file, text2); + ok = file_save(file, text2); free(text2); - return EINA_TRUE; + return ok; } diff --git a/src/bin/main.c b/src/bin/main.c index 16052e3..9f7ae21 100644 --- a/src/bin/main.c +++ b/src/bin/main.c @@ -335,6 +335,8 @@ _cb_ent_changed(void *data, Evas_Object *obj EINA_UNUSED, void *event_info) static void _load_to_entry(Ecrire_Entry *inst, const char *file) { + Eina_Bool ok = 1; + if (!file) _init_entry(inst); else @@ -346,14 +348,20 @@ _load_to_entry(Ecrire_Entry *inst, const char *file) else buf = file_load(file); - elm_object_text_set(inst->entry, ""); - _init_entry(inst); - elm_entry_entry_append(inst->entry, buf); - elm_object_item_disabled_set(inst->save_item, EINA_TRUE); - free(buf); + if (!buf) + ok = 0; + else + { + elm_object_text_set(inst->entry, ""); + _init_entry(inst); + elm_entry_entry_append(inst->entry, buf); + elm_object_item_disabled_set(inst->save_item, EINA_TRUE); + free(buf); + } } - _update_cur_file(file, inst); + if (ok) + _update_cur_file(file, inst); } static void @@ -375,14 +383,20 @@ _cb_fs_open_done(void *data, Evas_Object *obj EINA_UNUSED, void save_do(const char *file, Ecrire_Entry *inst) { + Eina_Bool ok; + if (plain_utf8) - file_plain_save(file, elm_object_text_get(inst->entry)); + ok = file_plain_save(file, elm_object_text_get(inst->entry)); else - file_save(file, elm_object_text_get(inst->entry)); + ok = file_save(file, elm_object_text_get(inst->entry)); - elm_object_item_disabled_set(inst->save_item, EINA_TRUE); - inst->last_saved_stack_ptr = inst->undo_stack_ptr; - _update_cur_file(file, inst); + if (ok) + { + elm_object_item_disabled_set(inst->save_item, EINA_TRUE); + inst->last_saved_stack_ptr = inst->undo_stack_ptr; + _update_cur_file(file, inst); + } + else ERR("couldn't save %s", file); } static void --