Revision: 2151 http://geany.svn.sourceforge.net/geany/?rev=2151&view=rev Author: eht16 Date: 2008-01-06 11:59:01 -0800 (Sun, 06 Jan 2008)
Log Message: ----------- Add utils_is_file_writeable(). Improve checks for write permission of files and directories. Don't overwrite maybe existing project files when trying to create new project and checking for project file's write permission (thanks to Yura Siamashka for reporting and his patch).
Modified Paths: -------------- trunk/ChangeLog trunk/src/project.c trunk/src/utils.c trunk/src/utils.h trunk/src/win32.c
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2008-01-06 18:11:57 UTC (rev 2150) +++ trunk/ChangeLog 2008-01-06 19:59:01 UTC (rev 2151) @@ -4,6 +4,12 @@ po/intl_stats.sh, plugins/*, src/*, tagmanager/rest.c: Update copyright information and change format of email addresses in source files. + * src/project.c, src/utils.c, src/utils.h, src/win32: + Add utils_is_file_writeable(). + Improve checks for write permission of files and directories. + Don't overwrite maybe existing project files when trying to create + new project and checking for project file's write permission + (thanks to Yura Siamashka for reporting and his patch).
2008-01-04 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
Modified: trunk/src/project.c =================================================================== --- trunk/src/project.c 2008-01-06 18:11:57 UTC (rev 2150) +++ trunk/src/project.c 2008-01-06 19:59:01 UTC (rev 2151) @@ -545,6 +545,7 @@ const gchar *name, *file_name, *base_path; gchar *locale_filename; gint name_len; + gint err_code; gboolean new_project = FALSE; GeanyProject *p;
@@ -575,9 +576,9 @@
// finally test whether the given project file can be written locale_filename = utils_get_locale_from_utf8(file_name); - if (utils_write_file(file_name, "") != 0) + if ((err_code = utils_is_file_writeable(locale_filename)) != 0) { - SHOW_ERR(_("Project file could not be written.")); + SHOW_ERR1(_("Project file could not be written (%s)."), g_strerror(err_code)); gtk_widget_grab_focus(e->file_name); return FALSE; }
Modified: trunk/src/utils.c =================================================================== --- trunk/src/utils.c 2008-01-06 18:11:57 UTC (rev 2150) +++ trunk/src/utils.c 2008-01-06 19:59:01 UTC (rev 2151) @@ -716,7 +716,39 @@ }
+/* Checks whether the given file can be written. locale_filename is expected in locale encoding. + * Returns 0 if it can be written, otherwise it returns errno */ +gint utils_is_file_writeable(const gchar *locale_filename) +{ + gchar *file; + gint ret; + + if (! g_file_test(locale_filename, G_FILE_TEST_EXISTS) && + ! g_file_test(locale_filename, G_FILE_TEST_IS_DIR)) + // get the file's directory to check for write permission if it doesn't yet exist + file = g_path_get_dirname(locale_filename); + else + file = g_strdup(locale_filename); + #ifdef G_OS_WIN32 + // use _waccess on Windows, access() doesn't accept special characters + ret = win32_check_write_permission(file); +#else + + // access set also errno to "FILE NOT FOUND" even if locale_filename is writeable, so use + // errno only when access() explicitly returns an error + if (access(file, R_OK | W_OK) != 0) + ret = errno; + else + ret = 0; + + g_free(file); + return ret; +#endif +} + + +#ifdef G_OS_WIN32 # define DIR_SEP "\" // on Windows we need an additional dir separator #else # define DIR_SEP "" @@ -740,15 +772,7 @@
if (saved_errno == 0 && ! g_file_test(conf_file, G_FILE_TEST_EXISTS)) { // check whether geany.conf can be written -#ifdef G_OS_WIN32 - // use _waccess on Windows, access() doesn't accept special characters - saved_errno = win32_check_write_permission(app->configdir); -#else - // access set also errno to "FILE NOT FOUND" even if app->configdir is writeable, so use - // errno only when access() explicitly returns an error - if (access(app->configdir, R_OK | W_OK) != 0) - saved_errno = errno; -#endif + saved_errno = utils_is_file_writeable(app->configdir); }
// make subdir for filetype definitions
Modified: trunk/src/utils.h =================================================================== --- trunk/src/utils.h 2008-01-06 18:11:57 UTC (rev 2150) +++ trunk/src/utils.h 2008-01-06 19:59:01 UTC (rev 2151) @@ -172,4 +172,6 @@
gboolean utils_str_has_upper(const gchar *str);
+gint utils_is_file_writeable(const gchar *locale_filename); + #endif
Modified: trunk/src/win32.c =================================================================== --- trunk/src/win32.c 2008-01-06 18:11:57 UTC (rev 2150) +++ trunk/src/win32.c 2008-01-06 19:59:01 UTC (rev 2151) @@ -560,10 +560,11 @@ gint win32_check_write_permission(const gchar *dir) { static wchar_t w_dir[512]; - errno = 0; // to get sure it is clean MultiByteToWideChar(CP_UTF8, 0, dir, -1, w_dir, sizeof w_dir); - _waccess(w_dir, R_OK | W_OK); - return errno; + if (_waccess_s(w_dir, R_OK | W_OK) != 0) + return errno; + else + return 0; }
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.