diff options
Diffstat (limited to 'libgfortran/io/unix.c')
-rw-r--r-- | libgfortran/io/unix.c | 89 |
1 files changed, 88 insertions, 1 deletions
diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c index 1e84c42..5301b84 100644 --- a/libgfortran/io/unix.c +++ b/libgfortran/io/unix.c @@ -1425,6 +1425,56 @@ regular_file2 (const char *path, st_parameter_open *opp, unit_flags *flags) } +/* Lock the file, if necessary, based on SHARE flags. */ + +#if defined(HAVE_FCNTL) && defined(F_SETLK) && defined(F_UNLCK) +static int +open_share (st_parameter_open *opp, int fd, unit_flags *flags) +{ + int r = 0; + struct flock f; + if (fd == STDOUT_FILENO || fd == STDERR_FILENO || fd == STDIN_FILENO) + return 0; + + f.l_start = 0; + f.l_len = 0; + f.l_whence = SEEK_SET; + + switch (flags->share) + { + case SHARE_DENYNONE: + f.l_type = F_RDLCK; + r = fcntl (fd, F_SETLK, &f); + break; + case SHARE_DENYRW: + /* Must be writable to hold write lock. */ + if (flags->action == ACTION_READ) + { + generate_error (&opp->common, LIBERROR_BAD_ACTION, + "Cannot set write lock on file opened for READ"); + return -1; + } + f.l_type = F_WRLCK; + r = fcntl (fd, F_SETLK, &f); + break; + case SHARE_UNSPECIFIED: + default: + break; + } + + return r; +} +#else +static int +open_share (st_parameter_open *opp __attribute__ ((unused)), + int fd __attribute__ ((unused)), + unit_flags *flags __attribute__ ((unused))) +{ + return 0; +} +#endif /* defined(HAVE_FCNTL) ... */ + + /* Wrapper around regular_file2, to make sure we free the path after we're done. */ @@ -1450,7 +1500,7 @@ open_external (st_parameter_open *opp, unit_flags *flags) { fd = tempfile (opp); if (flags->action == ACTION_UNSPECIFIED) - flags->action = ACTION_READWRITE; + flags->action = flags->readonly ? ACTION_READ : ACTION_READWRITE; #if HAVE_UNLINK_OPEN_FILE /* We can unlink scratch files now and it will go away when closed. */ @@ -1472,6 +1522,9 @@ open_external (st_parameter_open *opp, unit_flags *flags) return NULL; fd = fix_fd (fd); + if (open_share (opp, fd, flags) < 0) + return NULL; + return fd_to_stream (fd, flags->form == FORM_UNFORMATTED); } @@ -1752,6 +1805,40 @@ flush_all_units (void) } +/* Unlock the unit if necessary, based on SHARE flags. */ + +int +close_share (gfc_unit *u __attribute__ ((unused))) +{ + int r = 0; +#if defined(HAVE_FCNTL) && defined(F_SETLK) && defined(F_UNLCK) + unix_stream *s = (unix_stream *) u->s; + int fd = s->fd; + struct flock f; + + switch (u->flags.share) + { + case SHARE_DENYRW: + case SHARE_DENYNONE: + if (fd != STDOUT_FILENO && fd != STDERR_FILENO && fd != STDIN_FILENO) + { + f.l_start = 0; + f.l_len = 0; + f.l_whence = SEEK_SET; + f.l_type = F_UNLCK; + r = fcntl (fd, F_SETLK, &f); + } + break; + case SHARE_UNSPECIFIED: + default: + break; + } + +#endif + return r; +} + + /* file_exists()-- Returns nonzero if the current filename exists on * the system */ |