Revision: 4976 http://geany.svn.sourceforge.net/geany/?rev=4976&view=rev Author: statc Date: 2010-05-31 09:26:31 +0000 (Mon, 31 May 2010)
Log Message: ----------- Lock configuration file before writing, using open(O_EXCL) on temporary file. Original patch by Lex Trotman, thanks.
Modified Paths: -------------- branches/sm/ChangeLog.sm branches/sm/src/keyfile.c branches/sm/src/project.c branches/sm/src/utils.c branches/sm/src/utils.h
Modified: branches/sm/ChangeLog.sm =================================================================== --- branches/sm/ChangeLog.sm 2010-05-31 09:26:07 UTC (rev 4975) +++ branches/sm/ChangeLog.sm 2010-05-31 09:26:31 UTC (rev 4976) @@ -7,6 +7,9 @@ * src/document.c, src/keybindings.c, src/main.c, src/ui_utils.c, src/ui_utils.h: Use a separate queue to store recently closed files + * src/keyfile.c, src/project.c, src/utils.c, src/utils.h: + Lock configuration file before writing, using open(O_EXCL) on + temporary file. Original patch by Lex Trotman, thanks.
2010-05-30 Eugene Arshinov <earshinov(at)gmail(dot)com>
Modified: branches/sm/src/keyfile.c =================================================================== --- branches/sm/src/keyfile.c 2010-05-31 09:26:07 UTC (rev 4975) +++ branches/sm/src/keyfile.c 2010-05-31 09:26:31 UTC (rev 4976) @@ -237,7 +237,11 @@ static void write_config(GKeyFile * config, gchar * configfile) { gchar *data = g_key_file_to_data(config, NULL, NULL); - utils_write_file(configfile, data); + if (utils_lock(configfile) >= 0) + { + utils_write_file(configfile, data); + utils_unlock(configfile); + } g_free(data); }
Modified: branches/sm/src/project.c =================================================================== --- branches/sm/src/project.c 2010-05-31 09:26:07 UTC (rev 4975) +++ branches/sm/src/project.c 2010-05-31 09:26:31 UTC (rev 4976) @@ -1077,7 +1077,11 @@ } /* write the file */ data = g_key_file_to_data(config, NULL, NULL); - ret = (utils_write_file(filename, data) == 0); + if (utils_lock(filename) >= 0) + { + ret = (utils_write_file(filename, data) == 0); + utils_unlock(filename); + }
g_free(data); g_free(filename);
Modified: branches/sm/src/utils.c =================================================================== --- branches/sm/src/utils.c 2010-05-31 09:26:07 UTC (rev 4975) +++ branches/sm/src/utils.c 2010-05-31 09:26:31 UTC (rev 4976) @@ -35,6 +35,9 @@ #include <errno.h> #include <stdarg.h>
+#ifdef HAVE_FCNTL_H +# include <fcntl.h> +#endif #ifdef HAVE_SYS_STAT_H # include <sys/stat.h> #endif @@ -2103,3 +2106,22 @@ return ret; } } + + +/* NB int not gint see g_open */ +int utils_lock(const gchar *file) +{ + gchar *lock = g_strconcat(file, "_lock", NULL); + int ret = g_open(lock, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if (ret >= 0) close(ret); + g_free(lock); + return ret; +}; + + +void utils_unlock(const gchar *file) +{ + gchar *lock = g_strconcat(file, "_lock", NULL); + g_unlink(lock); + g_free(lock); +};
Modified: branches/sm/src/utils.h =================================================================== --- branches/sm/src/utils.h 2010-05-31 09:26:07 UTC (rev 4975) +++ branches/sm/src/utils.h 2010-05-31 09:26:31 UTC (rev 4976) @@ -107,7 +107,11 @@ #define foreach_str(char_ptr, string) \ for (char_ptr = string; *char_ptr; char_ptr++)
+/* NB int not gint see g_open */ +int utils_lock(const gchar *file);
+void utils_unlock(const gchar *file); + void utils_open_browser(const gchar *uri);
gint utils_get_line_endings(const gchar* buffer, glong size);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.