Skip to content

Commit

Permalink
Secure save now unlinks temporary file on error by default.
Browse files Browse the repository at this point in the history
It will prevent left-behind temporary files, but also prevent
potential data recovery from partially written files (it
should not be a problem here).
  • Loading branch information
Laurent Monin committed Apr 11, 2008
1 parent 8f75717 commit e91c942
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/secure_save.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ secure_open_umask(const gchar *file_name)

ssi->secure_save = TRUE;
ssi->preserve_perms = TRUE;
ssi->unlink_on_error = TRUE;

ssi->file_name = g_strdup(file_name);
if (!ssi->file_name) {
Expand All @@ -106,7 +107,7 @@ secure_open_umask(const gchar *file_name)
} else {
if (!S_ISREG(st.st_mode)) {
/* Not a regular file, secure_save is disabled. */
ssi->secure_save = 0;
ssi->secure_save = FALSE;
} else {
#ifdef HAVE_ACCESS
/* XXX: access() do not work with setuid programs. */
Expand Down Expand Up @@ -297,7 +298,11 @@ secure_close(SecureSaveInfo *ssi)
ret = 0; /* Success. */

free:
if (ssi->tmp_file_name) g_free(ssi->tmp_file_name);
if (ssi->tmp_file_name)
{
if (ret && ssi->unlink_on_error) unlink(ssi->tmp_file_name);
g_free(ssi->tmp_file_name);
}
if (ssi->file_name) g_free(ssi->file_name);
if (ssi) g_free(ssi);

Expand Down
7 changes: 4 additions & 3 deletions src/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,10 @@ struct _SecureSaveInfo {
gchar *file_name; /**< final file name */
gchar *tmp_file_name; /**< temporary file name */
gint err; /**< set to non-zero value in case of error */
gint secure_save; /**< use secure save for this file */
gint preserve_perms; /**< whether to preserve perms */
gint preserve_mtime; /**< whether to preserve mtime */
gint secure_save; /**< use secure save for this file, internal use only */
gint preserve_perms; /**< whether to preserve perms, TRUE by default */
gint preserve_mtime; /**< whether to preserve mtime, FALSE by default */
gint unlink_on_error; /**< whether to remove temporary file on save failure, TRUE by default */
};


Expand Down

0 comments on commit e91c942

Please sign in to comment.