All pastes #25244 Raw Edit

Unnamed

public text v1 · immutable
#25244 ·published 2005-10-11 17:05 UTC
rendered paste body
Index: gedit-document-saver.c
===================================================================
RCS file: /cvs/gnome/gedit/gedit/Attic/gedit-document-saver.c,v
retrieving revision 1.1.2.23
diff -u -p -r1.1.2.23 gedit-document-saver.c
--- gedit-document-saver.c	11 Oct 2005 12:57:04 -0000	1.1.2.23
+++ gedit-document-saver.c	11 Oct 2005 17:02:05 -0000
@@ -359,6 +359,7 @@ copy_file_data (gint     sfd,
 		GError **error)
 {
 	gboolean ret = TRUE;
+	GError *err = NULL;
 	gpointer buffer;
 	const gchar *write_buffer;
 	ssize_t bytes_read;
@@ -374,7 +375,7 @@ copy_file_data (gint     sfd,
 		{
 			GnomeVFSResult result = gnome_vfs_result_from_errno ();
 
-			g_set_error (error,
+			g_set_error (&err,
 				     GEDIT_DOCUMENT_ERROR,
 				     result,
 				     gnome_vfs_result_to_string (result));
@@ -394,7 +395,7 @@ copy_file_data (gint     sfd,
 			{
 				GnomeVFSResult result = gnome_vfs_result_from_errno ();
 
-				g_set_error (error,
+				g_set_error (&err,
 					     GEDIT_DOCUMENT_ERROR,
 					     result,
 					     gnome_vfs_result_to_string (result));
@@ -411,6 +412,9 @@ copy_file_data (gint     sfd,
 
 	} while ((bytes_read != 0) && (ret == TRUE));
 
+	if (error)
+		*error = err;
+
 	return ret;
 }
 
@@ -513,6 +517,16 @@ save_existing_local_file (GeditDocumentS
 
 	/* prepare the backup name */
 	backup_filename = get_backup_filename (saver);
+	if (backup_filename == NULL)
+	{
+		/* bad bad luck... */
+		g_set_error (&saver->priv->error,
+			     GEDIT_DOCUMENT_ERROR,
+			     GNOME_VFS_ERROR_GENERIC,
+			     gnome_vfs_result_to_string (GNOME_VFS_ERROR_GENERIC));
+
+		goto out;
+	}
 
 	/* We now use two backup strategies.
 	 * The first one (which is faster) consist in saving to a
@@ -636,58 +650,72 @@ save_existing_local_file (GeditDocumentS
 
  fallback_strategy:
 
-	/* move away old backups */
-	if (!remove_file (backup_filename))
+	/* try to copy the old contents in a backup for safety
+	 * unless we are explicetely told not to.
+	 */
+	if ((saver->priv->flags & GEDIT_DOCUMENT_SAVE_IGNORE_BACKUP) == 0)
 	{
-		GnomeVFSResult result = gnome_vfs_result_from_errno ();
+		/* move away old backups */
+		if (!remove_file (backup_filename))
+		{
+			/* we don't care about which was the problem, just
+			 * that a backup was not possible.
+			 */
+			g_set_error (&saver->priv->error,
+				     GEDIT_DOCUMENT_ERROR,
+				     GEDIT_DOCUMENT_ERROR_CANT_CREATE_BACKUP,
+				     "No backup created");
 
-		g_set_error (&saver->priv->error,
-			     GEDIT_DOCUMENT_ERROR,
-			     result,
-			     gnome_vfs_result_to_string (result));
+			goto out;
+		}
 
-		goto out;
-	}
+		bfd = open (backup_filename,
+			    O_WRONLY | O_CREAT | O_EXCL,
+			    statbuf.st_mode & 0777);
 
-	bfd = open (backup_filename,
-		    O_WRONLY | O_CREAT | O_EXCL,
-		    statbuf.st_mode & 0777);
+		if (bfd == -1)
+		{
+			g_set_error (&saver->priv->error,
+				     GEDIT_DOCUMENT_ERROR,
+				     GEDIT_DOCUMENT_ERROR_CANT_CREATE_BACKUP,
+				     "No backup created");
 
-	if (bfd == -1)
-	{
-		GnomeVFSResult result = gnome_vfs_result_from_errno ();
+			goto out;
+		}
 
-		g_set_error (&saver->priv->error,
-			     GEDIT_DOCUMENT_ERROR,
-			     result,
-			     gnome_vfs_result_to_string (result));
+		/* Try to set the group of the backup same as the
+		 * original file. If this fails, set the protection
+		 * bits for the group same as the protection bits for
+		 * others. */
+		if (fchown (bfd, (uid_t) -1, statbuf.st_gid) != 0)
+		{
+			if (fchmod (bfd,
+			            (statbuf.st_mode& 0707) |
+			            ((statbuf.st_mode & 07) << 3)) != 0)
+			{
+				g_set_error (&saver->priv->error,
+					     GEDIT_DOCUMENT_ERROR,
+					     GEDIT_DOCUMENT_ERROR_CANT_CREATE_BACKUP,
+					     "No backup created");
 
-		goto out;
-	}
+				unlink (backup_filename);
 
-	/* Try to set the group of the backup same as the
-	 * original file. If this fails, set the protection
-	 * bits for the group same as the protection bits for
-	 * others. */
-	if (fchown (bfd, (uid_t) -1, statbuf.st_gid) != 0)
-	{
-		if (fchmod (bfd,
-		            (statbuf.st_mode& 0707) |
-		            ((statbuf.st_mode & 07) << 3)) != 0)
+				goto out;
+			}
+		}
+
+		if (!copy_file_data (saver->priv->fd, bfd, NULL))
 		{
-			GnomeVFSResult result = gnome_vfs_result_from_errno ();
+				g_set_error (&saver->priv->error,
+					     GEDIT_DOCUMENT_ERROR,
+					     GEDIT_DOCUMENT_ERROR_CANT_CREATE_BACKUP,
+					     "No backup created");
 
-			g_set_error (&saver->priv->error,
-				     GEDIT_DOCUMENT_ERROR,
-				     result,
-				     gnome_vfs_result_to_string (result));
+				unlink (backup_filename);
 
-			goto out;
+				goto out;
 		}
 	}
-
-	if (!copy_file_data (saver->priv->fd, bfd, &saver->priv->error))
-		goto out;
 
 	/* finally overwrite the original */
 	if (!write_document_contents (saver->priv->fd,
Index: gedit-document.h
===================================================================
RCS file: /cvs/gnome/gedit/gedit/gedit-document.h,v
retrieving revision 1.41.2.21
diff -u -p -r1.41.2.21 gedit-document.h
--- gedit-document.h	9 Oct 2005 14:39:15 -0000	1.41.2.21
+++ gedit-document.h	11 Oct 2005 17:02:05 -0000
@@ -109,8 +109,11 @@ struct _GeditDocumentClass
 
 typedef enum
 {
-	GEDIT_DOCUMENT_SAVE_IGNORE_MTIME	 = 1 << 0,
-	GEDIT_DOCUMENT_SAVE_IGNORE_NOBACKUP	 = 1 << 1
+	/* save file despite external modifications */
+	GEDIT_DOCUMENT_SAVE_IGNORE_MTIME 	= 1 << 0,
+
+	/* try to save anyway even if a prior backup was not possible */
+	GEDIT_DOCUMENT_SAVE_IGNORE_BACKUP	= 1 << 1
 } GeditDocumentSaveFlags;
 
 
@@ -122,6 +125,7 @@ enum
 	 * for the error codes */ 
 	GEDIT_DOCUMENT_ERROR_EXTERNALLY_MODIFIED = GNOME_VFS_NUM_ERRORS,
 	GEDIT_DOCUMENT_ERROR_NOT_REGULAR_FILE,
+	GEDIT_DOCUMENT_ERROR_CANT_CREATE_BACKUP,
 	GEDIT_DOCUMENT_NUM_ERRORS 
 };