From dcf78f5382f63663412c81c4bbf5c0c883c3260f Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Fri, 1 Mar 2013 12:02:25 +0400 Subject: [PATCH] Add threshold for file size. Add "editor_filesize_threshold" ini option to ask open file if it size is larger than specified threshold. Supported string value formats are: "640000000", "64000K", "64M". Default value is 64M. Signed-off-by: Andrew Borodin --- src/editor/edit.c | 43 +++++++++++++++---- src/editor/edit.h | 1 + src/setup.c | 1 + .../editor/editcmd__edit_complete_word_cmd.c | 2 + 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/editor/edit.c b/src/editor/edit.c index f873e8d45..7354197fb 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -41,6 +41,7 @@ #include #include #include +#include /* UINTMAX_MAX */ #include #include @@ -101,6 +102,7 @@ int option_group_undo = 0; int show_right_margin = 0; char *option_backup_ext = NULL; +char *option_filesize_threshold = NULL; unsigned int edit_stack_iterator = 0; edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO]; @@ -135,6 +137,8 @@ static const struct edit_filters /* *INDENT-ON* */ }; +static const off_t option_filesize_default_threshold = 64 * 1024 * 1024; /* 64 MB */ + /* --------------------------------------------------------------------------------------------- */ /*** file scope functions ************************************************************************/ /* --------------------------------------------------------------------------------------------- */ @@ -247,8 +251,10 @@ edit_insert_stream (WEdit * edit, FILE * f) static gboolean check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st) { + static uintmax_t threshold = UINTMAX_MAX; int file; gchar *errmsg = NULL; + gboolean ret = TRUE; /* Try opening an existing file */ file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666); @@ -287,6 +293,16 @@ check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat goto cleanup; } + /* get file size threshold for alarm */ + if (threshold == UINTMAX_MAX) + { + gboolean err = FALSE; + + threshold = parse_integer (option_filesize_threshold, &err); + if (err) + threshold = option_filesize_default_threshold; + } + /* * Don't delete non-empty files. * O_EXCL should prevent it, but let's be on the safe side. @@ -294,21 +310,32 @@ check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat if (st->st_size > 0) edit->delete_file = 0; - /* TODO: - * Add ini option of file size alarm threshold. - * Add here the query dialog "The file is too large. Open it anyway?". - */ + if ((uintmax_t) st->st_size > threshold) + { + int act; + + errmsg = g_strdup_printf (_("File \"%s\" is too large.\nOpen it anyway?"), + vfs_path_as_str (filename_vpath)); + act = edit_query_dialog2 (_("Warning"), errmsg, _("&Yes"), _("&No")); + g_free (errmsg); + errmsg = NULL; + + if (act == 1) + ret = FALSE; + } cleanup: - (void) mc_close (file); - if (errmsg != NULL) { edit_error_dialog (_("Error"), errmsg); g_free (errmsg); - return FALSE; + ret = FALSE; } - return TRUE; + + if (!ret) + (void) mc_close (file); + + return ret; } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/editor/edit.h b/src/editor/edit.h index 3ff18607c..2382d732f 100644 --- a/src/editor/edit.h +++ b/src/editor/edit.h @@ -47,6 +47,7 @@ extern int option_save_position; extern int option_syntax_highlighting; extern int option_group_undo; extern char *option_backup_ext; +extern char *option_filesize_threshold; extern int edit_confirm_save; diff --git a/src/setup.c b/src/setup.c index acb0cc58b..bdd49d964 100644 --- a/src/setup.c +++ b/src/setup.c @@ -373,6 +373,7 @@ static const struct } str_options[] = { #ifdef USE_INTERNAL_EDIT { "editor_backup_extension", &option_backup_ext, "~" }, + { "editor_filesize_threshold", &option_filesize_threshold, "64M" }, #endif { "mcview_eof", &mcview_show_eof, "" }, { NULL, NULL, NULL } diff --git a/tests/src/editor/editcmd__edit_complete_word_cmd.c b/tests/src/editor/editcmd__edit_complete_word_cmd.c index 57dd64cb8..a938e4588 100644 --- a/tests/src/editor/editcmd__edit_complete_word_cmd.c +++ b/tests/src/editor/editcmd__edit_complete_word_cmd.c @@ -155,6 +155,8 @@ my_setup (void) load_codepages_list (); #endif /* HAVE_CHARSET */ + option_filesize_threshold = (char *) "64M"; + test_edit = edit_init (NULL, 0, 0, 24, 80, vfs_path_from_str ("test-data.txt"), 1); editcmd_dialog_completion_show__init (); }