comparison src/secure_save.c @ 313:a955b7fd626b

Secure save now unlinks temporary file on error by default. It will prevent left-behind temporary files, but also prevent potential data recovery from partially written files (it should not be a problem here).
author zas_
date Fri, 11 Apr 2008 16:46:21 +0000
parents 8a6650589829
children 4b2d7f9af171
comparison
equal deleted inserted replaced
312:07def8e708e1 313:a955b7fd626b
81 goto end; 81 goto end;
82 } 82 }
83 83
84 ssi->secure_save = TRUE; 84 ssi->secure_save = TRUE;
85 ssi->preserve_perms = TRUE; 85 ssi->preserve_perms = TRUE;
86 ssi->unlink_on_error = TRUE;
86 87
87 ssi->file_name = g_strdup(file_name); 88 ssi->file_name = g_strdup(file_name);
88 if (!ssi->file_name) { 89 if (!ssi->file_name) {
89 secsave_errno = SS_ERR_OUT_OF_MEM; 90 secsave_errno = SS_ERR_OUT_OF_MEM;
90 goto free_f; 91 goto free_f;
104 goto free_file_name; 105 goto free_file_name;
105 } 106 }
106 } else { 107 } else {
107 if (!S_ISREG(st.st_mode)) { 108 if (!S_ISREG(st.st_mode)) {
108 /* Not a regular file, secure_save is disabled. */ 109 /* Not a regular file, secure_save is disabled. */
109 ssi->secure_save = 0; 110 ssi->secure_save = FALSE;
110 } else { 111 } else {
111 #ifdef HAVE_ACCESS 112 #ifdef HAVE_ACCESS
112 /* XXX: access() do not work with setuid programs. */ 113 /* XXX: access() do not work with setuid programs. */
113 if (g_access(ssi->file_name, R_OK | W_OK) < 0) { 114 if (g_access(ssi->file_name, R_OK | W_OK) < 0) {
114 ssi->err = errno; 115 ssi->err = errno;
295 } 296 }
296 297
297 ret = 0; /* Success. */ 298 ret = 0; /* Success. */
298 299
299 free: 300 free:
300 if (ssi->tmp_file_name) g_free(ssi->tmp_file_name); 301 if (ssi->tmp_file_name)
302 {
303 if (ret && ssi->unlink_on_error) unlink(ssi->tmp_file_name);
304 g_free(ssi->tmp_file_name);
305 }
301 if (ssi->file_name) g_free(ssi->file_name); 306 if (ssi->file_name) g_free(ssi->file_name);
302 if (ssi) g_free(ssi); 307 if (ssi) g_free(ssi);
303 308
304 return ret; 309 return ret;
305 } 310 }