* view.c (do_view_init): Use non-blocking open() followed by

fstat() to avoid race conditions.  Unset O_NONBLOCK flag after
the open().
This commit is contained in:
Pavel Roskin 2002-12-16 03:43:49 +00:00
parent 382930fbf6
commit 6743211748
2 changed files with 22 additions and 7 deletions

View File

@ -1,5 +1,9 @@
2002-12-15 Pavel Roskin <proski@gnu.org> 2002-12-15 Pavel Roskin <proski@gnu.org>
* view.c (do_view_init): Use non-blocking open() followed by
fstat() to avoid race conditions. Unset O_NONBLOCK flag after
the open().
* cmd.c (mkdir_cmd): Don't try to create a directory with empty * cmd.c (mkdir_cmd): Don't try to create a directory with empty
name. name.

View File

@ -644,8 +644,19 @@ do_view_init (WView *view, char *_command, const char *_file,
if (_command && (view->viewer_magic_flag || _file[0] == '\0')) { if (_command && (view->viewer_magic_flag || _file[0] == '\0')) {
error = init_growing_view (view, _command, view->filename); error = init_growing_view (view, _command, view->filename);
} else if (_file[0]) { } else if (_file[0]) {
int cntlflags;
/* Open the file */
if ((fd = mc_open (_file, O_RDONLY | O_NONBLOCK)) == -1) {
g_snprintf (tmp, sizeof (tmp), _(" Cannot open \"%s\"\n %s "),
_file, unix_error_string (errno));
error = set_view_init_error (view, tmp);
goto finish;
}
/* Make sure we are working with a regular file */ /* Make sure we are working with a regular file */
if (mc_stat (view->filename, &view->s) == -1) { if (mc_fstat (fd, &view->s) == -1) {
mc_close (fd);
g_snprintf (tmp, sizeof (tmp), _(" Cannot stat \"%s\"\n %s "), g_snprintf (tmp, sizeof (tmp), _(" Cannot stat \"%s\"\n %s "),
_file, unix_error_string (errno)); _file, unix_error_string (errno));
error = set_view_init_error (view, tmp); error = set_view_init_error (view, tmp);
@ -653,18 +664,18 @@ do_view_init (WView *view, char *_command, const char *_file,
} }
if (!S_ISREG (view->s.st_mode)) { if (!S_ISREG (view->s.st_mode)) {
mc_close (fd);
g_snprintf (tmp, sizeof (tmp), g_snprintf (tmp, sizeof (tmp),
_(" Cannot view: not a regular file ")); _(" Cannot view: not a regular file "));
error = set_view_init_error (view, tmp); error = set_view_init_error (view, tmp);
goto finish; goto finish;
} }
/* Actually open the file */ /* We don't need O_NONBLOCK after opening the file, unset it */
if ((fd = mc_open (_file, O_RDONLY)) == -1) { cntlflags = fcntl (fd, F_GETFL, 0);
g_snprintf (tmp, sizeof (tmp), _(" Cannot open \"%s\"\n %s "), if (cntlflags != -1) {
_file, unix_error_string (errno)); cntlflags &= ~O_NONBLOCK;
error = set_view_init_error (view, tmp); fcntl (fd, F_SETFL, cntlflags);
goto finish;
} }
type = get_compression_type (fd); type = get_compression_type (fd);