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 <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2013-03-01 12:02:25 +04:00
parent c6d6aaedd5
commit dcf78f5382
4 changed files with 39 additions and 8 deletions

View File

@ -41,6 +41,7 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdint.h> /* UINTMAX_MAX */
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h> #include <fcntl.h>
@ -101,6 +102,7 @@ int option_group_undo = 0;
int show_right_margin = 0; int show_right_margin = 0;
char *option_backup_ext = NULL; char *option_backup_ext = NULL;
char *option_filesize_threshold = NULL;
unsigned int edit_stack_iterator = 0; unsigned int edit_stack_iterator = 0;
edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO]; edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
@ -135,6 +137,8 @@ static const struct edit_filters
/* *INDENT-ON* */ /* *INDENT-ON* */
}; };
static const off_t option_filesize_default_threshold = 64 * 1024 * 1024; /* 64 MB */
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/ /*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -247,8 +251,10 @@ edit_insert_stream (WEdit * edit, FILE * f)
static gboolean static gboolean
check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st) check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
{ {
static uintmax_t threshold = UINTMAX_MAX;
int file; int file;
gchar *errmsg = NULL; gchar *errmsg = NULL;
gboolean ret = TRUE;
/* Try opening an existing file */ /* Try opening an existing file */
file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666); 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; 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. * Don't delete non-empty files.
* O_EXCL should prevent it, but let's be on the safe side. * 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) if (st->st_size > 0)
edit->delete_file = 0; edit->delete_file = 0;
/* TODO: if ((uintmax_t) st->st_size > threshold)
* Add ini option of file size alarm threshold. {
* Add here the query dialog "The file is too large. Open it anyway?". 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: cleanup:
(void) mc_close (file);
if (errmsg != NULL) if (errmsg != NULL)
{ {
edit_error_dialog (_("Error"), errmsg); edit_error_dialog (_("Error"), errmsg);
g_free (errmsg); g_free (errmsg);
return FALSE; ret = FALSE;
} }
return TRUE;
if (!ret)
(void) mc_close (file);
return ret;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */

View File

@ -47,6 +47,7 @@ extern int option_save_position;
extern int option_syntax_highlighting; extern int option_syntax_highlighting;
extern int option_group_undo; extern int option_group_undo;
extern char *option_backup_ext; extern char *option_backup_ext;
extern char *option_filesize_threshold;
extern int edit_confirm_save; extern int edit_confirm_save;

View File

@ -373,6 +373,7 @@ static const struct
} str_options[] = { } str_options[] = {
#ifdef USE_INTERNAL_EDIT #ifdef USE_INTERNAL_EDIT
{ "editor_backup_extension", &option_backup_ext, "~" }, { "editor_backup_extension", &option_backup_ext, "~" },
{ "editor_filesize_threshold", &option_filesize_threshold, "64M" },
#endif #endif
{ "mcview_eof", &mcview_show_eof, "" }, { "mcview_eof", &mcview_show_eof, "" },
{ NULL, NULL, NULL } { NULL, NULL, NULL }

View File

@ -155,6 +155,8 @@ my_setup (void)
load_codepages_list (); load_codepages_list ();
#endif /* HAVE_CHARSET */ #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); test_edit = edit_init (NULL, 0, 0, 24, 80, vfs_path_from_str ("test-data.txt"), 1);
editcmd_dialog_completion_show__init (); editcmd_dialog_completion_show__init ();
} }