fixed acessing members of freed pfile struct
[claws.git] / src / common / prefs.c
index 29483a2199f5b09d2039a38c8260f9fe89f8e885..844a38b6e59654c05a3c01cbedf5756427b31ade 100644 (file)
 #include "prefs.h"
 #include "utils.h"
 
+/*!
+ *\brief       Open preferences file for reading
+ *
+ *\param       path Filename with path of preferences file to read
+ *
+ *\return      PrefFile * preferences file struct
+ */
 PrefFile *prefs_read_open(const gchar *path)
 {
        PrefFile *pfile;
@@ -50,6 +57,15 @@ PrefFile *prefs_read_open(const gchar *path)
        return pfile;
 }
 
+/*!
+ *\brief       Open preferences file for writing
+ *             Prefs are written to a temp file: Call prefs_write_close()
+ *             to rename this to the final filename
+ *
+ *\param       path Filename with path of preferences file to write
+ *
+ *\return      PrefFile * preferences file struct
+ */
 PrefFile *prefs_write_open(const gchar *path)
 {
        PrefFile *pfile;
@@ -84,6 +100,21 @@ PrefFile *prefs_write_open(const gchar *path)
        return pfile;
 }
 
+/*!
+ *\brief       Close and free preferences file
+ *             Creates final file from temp, creates backup
+ *
+ *\param       pfile Preferences file struct
+ *
+ *\return      0 on success, -1 on failure
+ */
+#define PREFS_FILE_FREE() \
+{ \
+  if (path)    g_free(path); \
+  if (tmppath) g_free(tmppath); \
+  if (bakpath) g_free(bakpath); \
+  if (pfile)   g_free(pfile); \
+}
 gint prefs_file_close(PrefFile *pfile)
 {
        FILE *fp, *orig_fp;
@@ -97,10 +128,11 @@ gint prefs_file_close(PrefFile *pfile)
        fp = pfile->fp;
        orig_fp = pfile->orig_fp;
        path = pfile->path;
-       g_free(pfile);
 
        if (!pfile->writing) {
                fclose(fp);
+               g_free(pfile);
+               g_free(path);
                return 0;
        }
 
@@ -159,12 +191,19 @@ gint prefs_file_close(PrefFile *pfile)
                return -1;
        }
 
+       g_free(pfile);
        g_free(path);
        g_free(tmppath);
        g_free(bakpath);
        return 0;
 }
+#undef PREFS_FILE_FREE
 
+/*!
+ *\brief       Close and free preferences file, delete temp file
+ *
+ *\param       pfile Preferences file struct
+ */
 gint prefs_file_close_revert(PrefFile *pfile)
 {
        gchar *tmppath;
@@ -186,6 +225,9 @@ gint prefs_file_close_revert(PrefFile *pfile)
        return 0;
 }
 
+/*!
+ *\brief       Check if "path" is a file and readonly
+ */
 gboolean prefs_is_readonly(const gchar * path)
 {
        if (path == NULL)
@@ -194,6 +236,9 @@ gboolean prefs_is_readonly(const gchar * path)
        return (access(path, W_OK) != 0 && access(path, F_OK) == 0);
 }
 
+/*!
+ *\brief       Check if "rcfile" is in rcdir, a file and readonly
+ */
 gboolean prefs_rc_is_readonly(const gchar * rcfile)
 {
        gboolean result;
@@ -209,6 +254,14 @@ gboolean prefs_rc_is_readonly(const gchar * rcfile)
        return result;
 }
 
+/*!
+ *\brief       Selects current section in preferences file
+ *             Creates section if file is written
+ *
+ *\param       pfile Preferences file struct
+ *
+ *\return      0 on success, -1 on failure
+ */
 gint prefs_set_block_label(PrefFile *pfile, const gchar *label)
 {
        gchar *block_label;