Take 2 at file locking fixes. New args to close_buffer() and

switch_to_prevnext_buffer() to support message passthrough
when trying to lock files using multibuffer.



git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5105 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
master
Chris Allegretta 2015-01-20 06:15:34 +00:00
parent 6948d2e779
commit 5b1fb56a8f
3 changed files with 43 additions and 31 deletions

View File

@ -126,6 +126,7 @@ int write_lockfile(const char *lockfilename, const char *origfilename, bool modi
pid_t mypid; pid_t mypid;
uid_t myuid; uid_t myuid;
struct passwd *mypwuid; struct passwd *mypwuid;
struct stat fileinfo;
char *lockdata = charalloc(1024); char *lockdata = charalloc(1024);
char myhostname[32]; char myhostname[32];
ssize_t lockdatalen = 1024; ssize_t lockdatalen = 1024;
@ -145,6 +146,8 @@ int write_lockfile(const char *lockfilename, const char *origfilename, bool modi
return -1; return -1;
} }
/* Check if the lock exists before we try to delete it...*/
if (stat(lockfilename, &fileinfo) != -1)
if (delete_lockfile(lockfilename) < 0) if (delete_lockfile(lockfilename) < 0)
return -1; return -1;
@ -156,11 +159,13 @@ int write_lockfile(const char *lockfilename, const char *origfilename, bool modi
fd = open(lockfilename, cflags, fd = open(lockfilename, cflags,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
/* Maybe we just don't have write access. Don't stop us from /* Maybe we just don't have write access. Print an error message
* opening the file at all, just don't set the lock_filename and and continue. */
* return success. */ if (fd < 0) {
if (fd < 0 && errno == EACCES) statusbar(_("Error writing lock file %s: %s"), lockfilename,
return 1; strerror(errno));
return 0;
}
/* Now we've got a safe file stream. If the previous open() call /* Now we've got a safe file stream. If the previous open() call
* failed, this will return NULL. */ * failed, this will return NULL. */
@ -314,6 +319,7 @@ int do_lockfile(const char *filename)
* necessary, and then open and read the file, if applicable. */ * necessary, and then open and read the file, if applicable. */
void open_buffer(const char *filename, bool undoable) void open_buffer(const char *filename, bool undoable)
{ {
bool quiet = FALSE;
bool new_buffer = (openfile == NULL bool new_buffer = (openfile == NULL
#ifndef DISABLE_MULTIBUFFER #ifndef DISABLE_MULTIBUFFER
|| ISSET(MULTIBUFFER) || ISSET(MULTIBUFFER)
@ -344,12 +350,14 @@ void open_buffer(const char *filename, bool undoable)
if (ISSET(LOCKING) && filename[0] != '\0') { if (ISSET(LOCKING) && filename[0] != '\0') {
int lockstatus = do_lockfile(filename); int lockstatus = do_lockfile(filename);
if (lockstatus < 0) { if (lockstatus < 0) {
#ifndef DISABLE_MULTIBUFFER
if (openfile->next) { if (openfile->next) {
close_buffer(); close_buffer(TRUE);
statusbar(_("Cancelled"));
return; return;
} else }
filename = ""; #endif
} else if (lockstatus == 0) {
quiet = TRUE;
} }
} }
#endif #endif
@ -359,7 +367,7 @@ void open_buffer(const char *filename, bool undoable)
/* If the filename isn't blank, and we are not in NOREAD_MODE, /* If the filename isn't blank, and we are not in NOREAD_MODE,
* open the file. Otherwise, treat it as a new file. */ * open the file. Otherwise, treat it as a new file. */
rc = (filename[0] != '\0' && !ISSET(NOREAD_MODE)) ? rc = (filename[0] != '\0' && !ISSET(NOREAD_MODE)) ?
open_file(filename, new_buffer, &f) : -2; open_file(filename, new_buffer, quiet, &f) : -2;
/* If we have a file, and we're loading into a new buffer, update /* If we have a file, and we're loading into a new buffer, update
* the filename. */ * the filename. */
@ -411,7 +419,7 @@ void replace_buffer(const char *filename)
/* If the filename isn't blank, open the file. Otherwise, treat it /* If the filename isn't blank, open the file. Otherwise, treat it
* as a new file. */ * as a new file. */
rc = (filename[0] != '\0') ? open_file(filename, TRUE, &f) : -2; rc = (filename[0] != '\0') ? open_file(filename, TRUE, FALSE, &f) : -2;
/* Reinitialize the text of the current buffer. */ /* Reinitialize the text of the current buffer. */
free_filestruct(openfile->fileage); free_filestruct(openfile->fileage);
@ -447,13 +455,14 @@ void display_buffer(void)
#ifndef DISABLE_MULTIBUFFER #ifndef DISABLE_MULTIBUFFER
/* Switch to the next file buffer if next_buf is TRUE. Otherwise, /* Switch to the next file buffer if next_buf is TRUE. Otherwise,
* switch to the previous file buffer. */ * switch to the previous file buffer. */
void switch_to_prevnext_buffer(bool next_buf) void switch_to_prevnext_buffer(bool next_buf, bool quiet)
{ {
assert(openfile != NULL); assert(openfile != NULL);
/* If only one file buffer is open, indicate it on the statusbar and /* If only one file buffer is open, indicate it on the statusbar and
* get out. */ * get out. */
if (openfile == openfile->next) { if (openfile == openfile->next) {
if (quiet == FALSE)
statusbar(_("No more open file buffers")); statusbar(_("No more open file buffers"));
return; return;
} }
@ -470,6 +479,7 @@ void switch_to_prevnext_buffer(bool next_buf)
display_buffer(); display_buffer();
/* Indicate the switch on the statusbar. */ /* Indicate the switch on the statusbar. */
if (quiet == FALSE)
statusbar(_("Switched to %s"), statusbar(_("Switched to %s"),
((openfile->filename[0] == '\0') ? _("New Buffer") : ((openfile->filename[0] == '\0') ? _("New Buffer") :
openfile->filename)); openfile->filename));
@ -483,19 +493,21 @@ void switch_to_prevnext_buffer(bool next_buf)
/* Switch to the previous entry in the openfile filebuffer. */ /* Switch to the previous entry in the openfile filebuffer. */
void switch_to_prev_buffer_void(void) void switch_to_prev_buffer_void(void)
{ {
switch_to_prevnext_buffer(FALSE); switch_to_prevnext_buffer(FALSE, FALSE);
} }
/* Switch to the next entry in the openfile filebuffer. */ /* Switch to the next entry in the openfile filebuffer. */
void switch_to_next_buffer_void(void) void switch_to_next_buffer_void(void)
{ {
switch_to_prevnext_buffer(TRUE); switch_to_prevnext_buffer(TRUE, FALSE);
} }
/* Delete an entry from the openfile filebuffer, and switch to the one /* Delete an entry from the openfile filebuffer, and switch to the one
* after it. Return TRUE on success, or FALSE if there are no more open * after it. Return TRUE on success, or FALSE if there are no more open
* file buffers. */ * file buffers.
bool close_buffer(void) * quiet - should we print messages switching bufers
*/
bool close_buffer(bool quiet)
{ {
assert(openfile != NULL); assert(openfile != NULL);
@ -508,7 +520,7 @@ bool close_buffer(void)
#endif #endif
/* Switch to the next file buffer. */ /* Switch to the next file buffer. */
switch_to_next_buffer_void(); switch_to_prevnext_buffer(TRUE, quiet);
/* Close the file buffer we had open before. */ /* Close the file buffer we had open before. */
unlink_opennode(openfile->prev); unlink_opennode(openfile->prev);
@ -904,10 +916,10 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkw
* Return -2 if we say "New File", -1 if the file isn't opened, and the * Return -2 if we say "New File", -1 if the file isn't opened, and the
* fd opened otherwise. The file might still have an error while reading * fd opened otherwise. The file might still have an error while reading
* with a 0 return value. *f is set to the opened file. */ * with a 0 return value. *f is set to the opened file. */
int open_file(const char *filename, bool newfie, FILE **f) int open_file(const char *filename, bool newfie, bool quiet, FILE **f)
{ {
struct stat fileinfo, fileinfo2; struct stat fileinfo, fileinfo2;
int fd, quiet = 0; int fd;
char *full_filename; char *full_filename;
assert(filename != NULL && f != NULL); assert(filename != NULL && f != NULL);

View File

@ -1152,7 +1152,7 @@ void do_exit(void)
#ifndef DISABLE_MULTIBUFFER #ifndef DISABLE_MULTIBUFFER
/* Exit only if there are no more open file buffers. */ /* Exit only if there are no more open file buffers. */
if (!close_buffer()) if (!close_buffer(FALSE))
#endif #endif
finish(); finish();
/* If the user canceled, we go on. */ /* If the user canceled, we go on. */

View File

@ -283,15 +283,15 @@ void replace_buffer(const char *filename);
#endif #endif
void display_buffer(void); void display_buffer(void);
#ifndef DISABLE_MULTIBUFFER #ifndef DISABLE_MULTIBUFFER
void switch_to_prevnext_buffer(bool next); void switch_to_prevnext_buffer(bool next, bool quiet);
void switch_to_prev_buffer_void(void); void switch_to_prev_buffer_void(void);
void switch_to_next_buffer_void(void); void switch_to_next_buffer_void(void);
bool close_buffer(void); bool close_buffer(bool quiet);
#endif #endif
filestruct *read_line(char *buf, filestruct *prevnode, bool filestruct *read_line(char *buf, filestruct *prevnode, bool
*first_line_ins, size_t buf_len); *first_line_ins, size_t buf_len);
void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkwritable); void read_file(FILE *f, int fd, const char *filename, bool undoable, bool checkwritable);
int open_file(const char *filename, bool newfie, FILE **f); int open_file(const char *filename, bool newfie, bool quiet, FILE **f);
char *get_next_filename(const char *name, const char *suffix); char *get_next_filename(const char *name, const char *suffix);
void do_insertfile( void do_insertfile(
#ifndef NANO_TINY #ifndef NANO_TINY