Consistently-use-g_stat-and-GStatBuf.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. From: Eduard Braun <eduard.braun2@gmx.de>
  2. Date: Sat, 13 Jan 2018 23:09:51 +0100
  3. Subject: Consistently use g_stat and GStatBuf
  4. Replace "stat struct" with "GStatBuf" and "stat" with "g_stat" where
  5. appropriate to fix cross-platform issues, specifically on Windows.
  6. Code should be identical on *nix but fixes some serious issues
  7. on Windows:
  8. - Field widths of "struct stat" are not constant on Windows.
  9. If the stat function does not match the stat struct used
  10. it will cause overwrites and undefined behavior
  11. - The Windows stat function needs a properly encoded filename.
  12. In many places we pass an UTF-8 encoded value which breaks as soon
  13. as non-ASCII characters are involved.
  14. Bug: https://bugzilla.gnome.org/show_bug.cgi?id=787772
  15. Origin: upstream, 2.24.33, commit:eec3ce0b4eb25cf290ff649d3d87f7f0b25c02e4
  16. ---
  17. demos/pixbuf-init.c | 8 +++-----
  18. gtk/gtkfilesel.c | 30 +++++++++++++++---------------
  19. gtk/gtkiconcache.c | 4 ++--
  20. gtk/gtkicontheme.c | 6 +++---
  21. gtk/gtkrc.c | 4 ++--
  22. gtk/gtkrecentmanager.c | 4 ++--
  23. gtk/tests/pixbuf-init.c | 8 +++-----
  24. gtk/updateiconcache.c | 4 ++--
  25. tests/testfilechooser.c | 10 +++++-----
  26. tests/testgtk.c | 6 +++---
  27. tests/testtext.c | 6 +++---
  28. 11 files changed, 43 insertions(+), 47 deletions(-)
  29. diff --git a/demos/pixbuf-init.c b/demos/pixbuf-init.c
  30. index 898de45..20e35e5 100644
  31. --- a/demos/pixbuf-init.c
  32. +++ b/demos/pixbuf-init.c
  33. @@ -1,15 +1,13 @@
  34. #include "config.h"
  35. #include <glib.h>
  36. -
  37. -#include <sys/stat.h>
  38. -#include <stdlib.h>
  39. +#include <glib/gstdio.h>
  40. static gboolean
  41. file_exists (const char *filename)
  42. {
  43. - struct stat statbuf;
  44. + GStatBuf statbuf;
  45. - return stat (filename, &statbuf) == 0;
  46. + return g_stat (filename, &statbuf) == 0;
  47. }
  48. void
  49. diff --git a/gtk/gtkfilesel.c b/gtk/gtkfilesel.c
  50. index d18c307..245bdf1 100644
  51. --- a/gtk/gtkfilesel.c
  52. +++ b/gtk/gtkfilesel.c
  53. @@ -325,7 +325,7 @@ static CompletionDir* open_ref_dir (gchar* text_to_complete,
  54. CompletionState* cmpl_state);
  55. #ifndef G_PLATFORM_WIN32
  56. static gboolean check_dir (gchar *dir_name,
  57. - struct stat *result,
  58. + GStatBuf *result,
  59. gboolean *stat_subdirs);
  60. #endif
  61. static CompletionDir* open_dir (gchar* dir_name,
  62. @@ -337,11 +337,11 @@ static CompletionDir* open_user_dir (const gchar* text_to_complete,
  63. static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
  64. CompletionState *cmpl_state);
  65. static CompletionDirSent* open_new_dir (gchar* dir_name,
  66. - struct stat* sbuf,
  67. + GStatBuf *sbuf,
  68. gboolean stat_subdirs);
  69. static gint correct_dir_fullname (CompletionDir* cmpl_dir);
  70. static gint correct_parent (CompletionDir* cmpl_dir,
  71. - struct stat *sbuf);
  72. + GStatBuf *sbuf);
  73. #ifndef G_PLATFORM_WIN32
  74. static gchar* find_parent_dir_fullname (gchar* dirname);
  75. #endif
  76. @@ -2984,9 +2984,9 @@ open_relative_dir (gchar *dir_name,
  77. /* after the cache lookup fails, really open a new directory */
  78. static CompletionDirSent*
  79. -open_new_dir (gchar *dir_name,
  80. - struct stat *sbuf,
  81. - gboolean stat_subdirs)
  82. +open_new_dir (gchar *dir_name,
  83. + GStatBuf *sbuf,
  84. + gboolean stat_subdirs)
  85. {
  86. CompletionDirSent *sent;
  87. GDir *directory;
  88. @@ -2995,7 +2995,7 @@ open_new_dir (gchar *dir_name,
  89. gint entry_count = 0;
  90. gint n_entries = 0;
  91. gint i;
  92. - struct stat ent_sbuf;
  93. + GStatBuf ent_sbuf;
  94. GString *path;
  95. gchar *sys_dir_name;
  96. @@ -3101,9 +3101,9 @@ open_new_dir (gchar *dir_name,
  97. #ifndef G_PLATFORM_WIN32
  98. static gboolean
  99. -check_dir (gchar *dir_name,
  100. - struct stat *result,
  101. - gboolean *stat_subdirs)
  102. +check_dir (gchar *dir_name,
  103. + GStatBuf *result,
  104. + gboolean *stat_subdirs)
  105. {
  106. /* A list of directories that we know only contain other directories.
  107. * Trying to stat every file in these directories would be very
  108. @@ -3113,7 +3113,7 @@ check_dir (gchar *dir_name,
  109. static struct {
  110. const gchar name[5];
  111. gboolean present;
  112. - struct stat statbuf;
  113. + GStatBuf statbuf;
  114. } no_stat_dirs[] = {
  115. { "/afs", FALSE, { 0 } },
  116. { "/net", FALSE, { 0 } }
  117. @@ -3172,7 +3172,7 @@ open_dir (gchar *dir_name,
  118. CompletionState *cmpl_state)
  119. {
  120. #ifndef G_PLATFORM_WIN32
  121. - struct stat sbuf;
  122. + GStatBuf sbuf;
  123. gboolean stat_subdirs;
  124. GList* cdsl;
  125. #endif
  126. @@ -3238,7 +3238,7 @@ correct_dir_fullname (CompletionDir* cmpl_dir)
  127. gint length = strlen (cmpl_dir->fullname);
  128. gchar *first_slash = strchr (cmpl_dir->fullname, G_DIR_SEPARATOR);
  129. gchar *sys_filename;
  130. - struct stat sbuf;
  131. + GStatBuf sbuf;
  132. /* Does it end with /. (\.) ? */
  133. if (length >= 2 &&
  134. @@ -3338,9 +3338,9 @@ correct_dir_fullname (CompletionDir* cmpl_dir)
  135. static gint
  136. correct_parent (CompletionDir *cmpl_dir,
  137. - struct stat *sbuf)
  138. + GStatBuf *sbuf)
  139. {
  140. - struct stat parbuf;
  141. + GStatBuf parbuf;
  142. gchar *last_slash;
  143. gchar *first_slash;
  144. #ifndef G_PLATFORM_WIN32
  145. diff --git a/gtk/gtkiconcache.c b/gtk/gtkiconcache.c
  146. index 576dd94..143f8f1 100644
  147. --- a/gtk/gtkiconcache.c
  148. +++ b/gtk/gtkiconcache.c
  149. @@ -92,8 +92,8 @@ _gtk_icon_cache_new_for_path (const gchar *path)
  150. gchar *cache_filename;
  151. gint fd = -1;
  152. - struct stat st;
  153. - struct stat path_st;
  154. + GStatBuf st;
  155. + GStatBuf path_st;
  156. CacheInfo info;
  157. /* Check if we have a cache file */
  158. diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c
  159. index 470158c..60661c8 100644
  160. --- a/gtk/gtkicontheme.c
  161. +++ b/gtk/gtkicontheme.c
  162. @@ -902,7 +902,7 @@ insert_theme (GtkIconTheme *icon_theme, const char *theme_name)
  163. GKeyFile *theme_file;
  164. GError *error = NULL;
  165. IconThemeDirMtime *dir_mtime;
  166. - struct stat stat_buf;
  167. + GStatBuf stat_buf;
  168. priv = icon_theme->priv;
  169. @@ -1052,7 +1052,7 @@ load_themes (GtkIconTheme *icon_theme)
  170. IconSuffix old_suffix, new_suffix;
  171. GTimeVal tv;
  172. IconThemeDirMtime *dir_mtime;
  173. - struct stat stat_buf;
  174. + GStatBuf stat_buf;
  175. priv = icon_theme->priv;
  176. @@ -1898,7 +1898,7 @@ rescan_themes (GtkIconTheme *icon_theme)
  177. IconThemeDirMtime *dir_mtime;
  178. GList *d;
  179. int stat_res;
  180. - struct stat stat_buf;
  181. + GStatBuf stat_buf;
  182. GTimeVal tv;
  183. priv = icon_theme->priv;
  184. diff --git a/gtk/gtkrc.c b/gtk/gtkrc.c
  185. index 50b2563..952010e 100644
  186. --- a/gtk/gtkrc.c
  187. +++ b/gtk/gtkrc.c
  188. @@ -983,7 +983,7 @@ gtk_rc_context_parse_one_file (GtkRcContext *context,
  189. gboolean reload)
  190. {
  191. GtkRcFile *rc_file;
  192. - struct stat statbuf;
  193. + GStatBuf statbuf;
  194. gint saved_priority;
  195. g_return_if_fail (filename != NULL);
  196. @@ -1764,7 +1764,7 @@ gtk_rc_reparse_all_for_settings (GtkSettings *settings,
  197. GtkRcFile *rc_file;
  198. GSList *tmp_list;
  199. GtkRcContext *context;
  200. - struct stat statbuf;
  201. + GStatBuf statbuf;
  202. g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
  203. diff --git a/gtk/gtkrecentmanager.c b/gtk/gtkrecentmanager.c
  204. index 609ee1f..1f67c13 100644
  205. --- a/gtk/gtkrecentmanager.c
  206. +++ b/gtk/gtkrecentmanager.c
  207. @@ -2221,7 +2221,7 @@ gboolean
  208. gtk_recent_info_exists (GtkRecentInfo *info)
  209. {
  210. gchar *filename;
  211. - struct stat stat_buf;
  212. + GStatBuf stat_buf;
  213. gboolean retval = FALSE;
  214. g_return_val_if_fail (info != NULL, FALSE);
  215. @@ -2233,7 +2233,7 @@ gtk_recent_info_exists (GtkRecentInfo *info)
  216. filename = g_filename_from_uri (info->uri, NULL, NULL);
  217. if (filename)
  218. {
  219. - if (stat (filename, &stat_buf) == 0)
  220. + if (g_stat (filename, &stat_buf) == 0)
  221. retval = TRUE;
  222. g_free (filename);
  223. diff --git a/gtk/tests/pixbuf-init.c b/gtk/tests/pixbuf-init.c
  224. index 47573ab..5184a38 100644
  225. --- a/gtk/tests/pixbuf-init.c
  226. +++ b/gtk/tests/pixbuf-init.c
  227. @@ -1,15 +1,13 @@
  228. #include "config.h"
  229. #include <glib.h>
  230. -
  231. -#include <sys/stat.h>
  232. -#include <stdlib.h>
  233. +#include <glib/gstdio.h>
  234. static gboolean
  235. file_exists (const char *filename)
  236. {
  237. - struct stat statbuf;
  238. + GStatBuf statbuf;
  239. - return stat (filename, &statbuf) == 0;
  240. + return g_stat (filename, &statbuf) == 0;
  241. }
  242. void
  243. diff --git a/gtk/updateiconcache.c b/gtk/updateiconcache.c
  244. index 32c8f41..39e1345 100644
  245. --- a/gtk/updateiconcache.c
  246. +++ b/gtk/updateiconcache.c
  247. @@ -119,7 +119,7 @@ static int check_dir_mtime (const char *dir,
  248. gboolean
  249. is_cache_up_to_date (const gchar *path)
  250. {
  251. - struct stat path_stat, cache_stat;
  252. + GStatBuf path_stat, cache_stat;
  253. gchar *cache_path;
  254. int retval;
  255. @@ -1478,7 +1478,7 @@ build_cache (const gchar *path)
  256. #endif
  257. GHashTable *files;
  258. FILE *cache;
  259. - struct stat path_stat, cache_stat;
  260. + GStatBuf path_stat, cache_stat;
  261. struct utimbuf utime_buf;
  262. GList *directories = NULL;
  263. int fd;
  264. diff --git a/tests/testfilechooser.c b/tests/testfilechooser.c
  265. index 443f5c3..86cb14d 100644
  266. --- a/tests/testfilechooser.c
  267. +++ b/tests/testfilechooser.c
  268. @@ -21,13 +21,13 @@
  269. #include <string.h>
  270. #include <sys/types.h>
  271. -#include <sys/stat.h>
  272. #include <stdlib.h>
  273. #include <time.h>
  274. #ifdef HAVE_UNISTD_H
  275. #include <unistd.h>
  276. #endif
  277. #include <gtk/gtk.h>
  278. +#include <glib/gstdio.h>
  279. #ifdef G_OS_WIN32
  280. # include <io.h>
  281. @@ -189,7 +189,7 @@ my_new_from_file_at_size (const char *filename,
  282. GdkPixbufLoader *loader;
  283. GdkPixbuf *pixbuf;
  284. int info[2];
  285. - struct stat st;
  286. + GStatBuf st;
  287. guchar buffer [4096];
  288. int length;
  289. @@ -198,7 +198,7 @@ my_new_from_file_at_size (const char *filename,
  290. g_return_val_if_fail (filename != NULL, NULL);
  291. g_return_val_if_fail (width > 0 && height > 0, NULL);
  292. - if (stat (filename, &st) != 0) {
  293. + if (g_stat (filename, &st) != 0) {
  294. int errsv = errno;
  295. g_set_error (error,
  296. @@ -298,8 +298,8 @@ update_preview_cb (GtkFileChooser *chooser)
  297. }
  298. else
  299. {
  300. - struct stat buf;
  301. - if (stat (filename, &buf) == 0)
  302. + GStatBuf buf;
  303. + if (g_stat (filename, &buf) == 0)
  304. {
  305. gchar *preview_text;
  306. gchar *size_str;
  307. diff --git a/tests/testgtk.c b/tests/testgtk.c
  308. index ec2c57c..b7ea442 100644
  309. --- a/tests/testgtk.c
  310. +++ b/tests/testgtk.c
  311. @@ -35,7 +35,6 @@
  312. #include <stdio.h>
  313. #include <stdlib.h>
  314. #include <string.h>
  315. -#include <sys/stat.h>
  316. #include <math.h>
  317. #include <time.h>
  318. #ifdef HAVE_UNISTD_H
  319. @@ -46,6 +45,7 @@
  320. #include "gtk/gtk.h"
  321. #include "gdk/gdk.h"
  322. #include "gdk/gdkkeysyms.h"
  323. +#include "glib/gstdio.h"
  324. #ifdef G_OS_WIN32
  325. #define sleep(n) _sleep(n)
  326. @@ -59,9 +59,9 @@
  327. gboolean
  328. file_exists (const char *filename)
  329. {
  330. - struct stat statbuf;
  331. + GStatBuf statbuf;
  332. - return stat (filename, &statbuf) == 0;
  333. + return g_stat (filename, &statbuf) == 0;
  334. }
  335. GtkWidget *
  336. diff --git a/tests/testtext.c b/tests/testtext.c
  337. index 880eaa7..b9678bf 100644
  338. --- a/tests/testtext.c
  339. +++ b/tests/testtext.c
  340. @@ -20,7 +20,6 @@
  341. #include "config.h"
  342. #include <stdio.h>
  343. -#include <sys/stat.h>
  344. #include <errno.h>
  345. #include <stdlib.h>
  346. #include <string.h>
  347. @@ -30,6 +29,7 @@
  348. #include <gtk/gtk.h>
  349. #include <gdk/gdkkeysyms.h>
  350. +#include <glib/gstdio.h>
  351. #include "prop-editor.h"
  352. @@ -2096,9 +2096,9 @@ save_as_ok_func (const char *filename, gpointer data)
  353. if (!buffer->filename || strcmp (filename, buffer->filename) != 0)
  354. {
  355. - struct stat statbuf;
  356. + GStatBuf statbuf;
  357. - if (stat (filename, &statbuf) == 0)
  358. + if (g_stat (filename, &statbuf) == 0)
  359. {
  360. gchar *err = g_strdup_printf ("Ovewrite existing file '%s'?", filename);
  361. gint result = msgbox_run (NULL, err, "Yes", "No", NULL, 1);