diff --git a/gtk/Makefile.am b/gtk/Makefile.am index 21d9d4a50..425e4d1d8 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -390,6 +390,7 @@ gtk_private_h_sources = \ gtkfilesystem.h \ gtkfilesystemmodel.h \ gtkfilethumbnail.h \ + gtktimeprivate.h \ gtkiconcache.h \ gtkimcontextsimpleseqs.h \ gtkintl.h \ diff --git a/gtk/gtkiconcache.c b/gtk/gtkiconcache.c index 7a363f6ac..3df44fbff 100644 --- a/gtk/gtkiconcache.c +++ b/gtk/gtkiconcache.c @@ -22,6 +22,7 @@ #include "gtkdebug.h" #include "gtkiconcache.h" #include "gtkiconcachevalidator.h" +#include "gtktimeprivate.h" #include "gtkalias.h" #include @@ -101,9 +102,8 @@ _gtk_icon_cache_new_for_path (const gchar *path) GMappedFile *map; gchar *cache_filename; - gint fd = -1; - GStatBuf st; - GStatBuf path_st; + GtkStatBuf st; + GtkStatBuf path_st; /* Check if we have a cache file */ cache_filename = g_build_filename (path, "icon-theme.cache", NULL); @@ -111,44 +111,16 @@ _gtk_icon_cache_new_for_path (const gchar *path) GTK_NOTE (ICONTHEME, g_print ("look for cache in %s\n", path)); - if (g_stat (path, &path_st) < 0) + if (_gtk_stat (path, TRUE, &path_st) < 0) goto done; - /* Open the file and map it into memory */ - fd = g_open (cache_filename, O_RDONLY|_O_BINARY, 0); - - if (fd < 0) + /* The times as 64-bit seconds, where the stat of 32-bit Windows and + * of a 32-bit time_t ends in 2038 */ + if (_gtk_stat (cache_filename, TRUE, &st) < 0 || st.size < 4) goto done; - /* We need to know the correct function to call for the GStatBuf st we are using here, - * because GStatBuf may not be stat on Windows - */ -#ifdef G_OS_WIN32 - if (GLIB_CHECK_VERSION (2, 57, 3)) - { -# ifdef __MINGW64_VERSION_MAJOR -# ifdef _WIN64 -# define gtk_fstat _fstat64 -# else -# define gtk_fstat _fstat32 -# endif -# elif defined (_MSC_VER) && !defined (_WIN64) -# define gtk_fstat _fstat32 -# endif - } -#endif - -#ifndef gtk_fstat -#define gtk_fstat fstat -#endif - - if (gtk_fstat (fd, &st) < 0 || st.st_size < 4) - goto done; - -#undef gtk_fstat - /* Verify cache is uptodate */ - if ((gint64) st.st_mtime < (gint64) path_st.st_mtime) + if (st.mtime < path_st.mtime) { GTK_NOTE (ICONTHEME, g_print ("cache outdated\n")); @@ -189,8 +161,6 @@ _gtk_icon_cache_new_for_path (const gchar *path) done: g_free (cache_filename); - if (fd >= 0) - close (fd); return cache; } diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c index 11308083c..3de9d7e6c 100644 --- a/gtk/gtkicontheme.c +++ b/gtk/gtkicontheme.c @@ -46,6 +46,7 @@ #include "gtkmain.h" #include "gtksettings.h" #include "gtkprivate.h" +#include "gtktimeprivate.h" #include "gtkalias.h" #define DEFAULT_THEME_NAME "hicolor" @@ -928,7 +929,7 @@ insert_theme (GtkIconTheme *icon_theme, const char *theme_name) GKeyFile *theme_file; GError *error = NULL; IconThemeDirMtime *dir_mtime; - GStatBuf stat_buf; + GtkStatBuf stat_buf; priv = icon_theme->priv; @@ -948,9 +949,9 @@ insert_theme (GtkIconTheme *icon_theme, const char *theme_name) dir_mtime->cache = NULL; dir_mtime->cache_tried = FALSE; dir_mtime->dir = path; - if (g_stat (path, &stat_buf) == 0 && S_ISDIR (stat_buf.st_mode)) + if (_gtk_stat (path, TRUE, &stat_buf) == 0 && S_ISDIR (stat_buf.mode)) { - dir_mtime->mtime = (gint64) stat_buf.st_mtime; + dir_mtime->mtime = stat_buf.mtime; dir_mtime->exists = TRUE; } else @@ -1078,7 +1079,7 @@ load_themes (GtkIconTheme *icon_theme) UnthemedIcon *unthemed_icon; IconSuffix old_suffix, new_suffix; IconThemeDirMtime *dir_mtime; - GStatBuf stat_buf; + GtkStatBuf stat_buf; priv = icon_theme->priv; @@ -1110,9 +1111,9 @@ load_themes (GtkIconTheme *icon_theme) dir_mtime->cache = NULL; dir_mtime->cache_tried = TRUE; - if (g_stat (dir, &stat_buf) != 0 || !S_ISDIR (stat_buf.st_mode)) + if (_gtk_stat (dir, TRUE, &stat_buf) != 0 || !S_ISDIR (stat_buf.mode)) continue; - dir_mtime->mtime = (gint64) stat_buf.st_mtime; + dir_mtime->mtime = stat_buf.mtime; dir_mtime->exists = TRUE; dir_mtime->cache = _gtk_icon_cache_new_for_path (dir); @@ -1973,7 +1974,7 @@ rescan_themes (GtkIconTheme *icon_theme) IconThemeDirMtime *dir_mtime; GList *d; int stat_res; - GStatBuf stat_buf; + GtkStatBuf stat_buf; priv = icon_theme->priv; @@ -1981,16 +1982,16 @@ rescan_themes (GtkIconTheme *icon_theme) { dir_mtime = d->data; - stat_res = g_stat (dir_mtime->dir, &stat_buf); + stat_res = _gtk_stat (dir_mtime->dir, TRUE, &stat_buf); /* dir mtime didn't change */ if (stat_res == 0 && dir_mtime->exists && - S_ISDIR (stat_buf.st_mode) && - dir_mtime->mtime == (gint64) stat_buf.st_mtime) + S_ISDIR (stat_buf.mode) && + dir_mtime->mtime == stat_buf.mtime) continue; /* didn't exist before, and still doesn't */ if (!dir_mtime->exists && - (stat_res != 0 || !S_ISDIR (stat_buf.st_mode))) + (stat_res != 0 || !S_ISDIR (stat_buf.mode))) continue; return TRUE; diff --git a/gtk/gtkrc.c b/gtk/gtkrc.c index 11a97b141..94e71aee5 100644 --- a/gtk/gtkrc.c +++ b/gtk/gtkrc.c @@ -55,6 +55,7 @@ #include "gtkprivate.h" #include "gtksettings.h" #include "gtkwindow.h" +#include "gtktimeprivate.h" #include "gtkalias.h" @@ -1009,7 +1010,7 @@ gtk_rc_context_parse_one_file (GtkRcContext *context, gboolean reload) { GtkRcFile *rc_file; - GStatBuf statbuf; + GtkStatBuf statbuf; gint saved_priority; g_return_if_fail (filename != NULL); @@ -1042,11 +1043,11 @@ gtk_rc_context_parse_one_file (GtkRcContext *context, if (g_slist_find (current_files_stack, rc_file)) return; - if (!g_lstat (rc_file->canonical_name, &statbuf)) + if (!_gtk_stat (rc_file->canonical_name, FALSE, &statbuf)) { gint fd; - rc_file->mtime = (gint64) statbuf.st_mtime; + rc_file->mtime = statbuf.mtime; fd = g_open (rc_file->canonical_name, O_RDONLY, 0); if (fd < 0) @@ -1798,7 +1799,7 @@ gtk_rc_reparse_all_for_settings (GtkSettings *settings, GtkRcFile *rc_file; GSList *tmp_list; GtkRcContext *context; - GStatBuf statbuf; + GtkStatBuf statbuf; g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE); @@ -1819,8 +1820,8 @@ gtk_rc_reparse_all_for_settings (GtkSettings *settings, if (!rc_file->is_string) { - if (!g_lstat (rc_file->name, &statbuf) && - ((gint64) statbuf.st_mtime != rc_file->mtime)) + if (!_gtk_stat (rc_file->name, FALSE, &statbuf) && + statbuf.mtime != rc_file->mtime) { mtime_modified = TRUE; break; diff --git a/gtk/gtktimeprivate.h b/gtk/gtktimeprivate.h new file mode 100644 index 000000000..f1f7c3e12 --- /dev/null +++ b/gtk/gtktimeprivate.h @@ -0,0 +1,296 @@ +/* GTK - The GIMP Toolkit + * Copyright (C) 2026 the gtk2-ng authors + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* The time of now and the times of files as 64-bit seconds, also where + * time_t has 32 bits and the C library ends on 2038-01-19: + * + * - Linux: the kernel has had 64-bit calls for 32-bit programs since + * 4.11 (statx) and 5.1 (clock_gettime64, utimensat_time64); they are + * called directly where time_t is 32 bits, as Rust's standard + * library does, so a GTK+ built for an old 32-bit system still + * knows the time after 2038 when it runs on a newer kernel + * - Windows: GetFileAttributesEx and SetFileTime take the 64-bit + * FILETIME (since Windows 95/NT 4), where the stat of a 32-bit + * program has a 32-bit time + * - elsewhere, where time_t has 32 bits (Mac OS X on PowerPC and + * i386, FreeBSD on i386), a time past 2038 comes back negative; + * no clock and no file of a desktop goes back to 1901, so it is + * read as the unsigned number it was, which lasts until 2106 (the + * way NTP reads its 32-bit seconds) + * + * Where time_t has 64 bits, all of this is the plain GLib call. + * Header only, for libgtk, gtk-update-icon-cache and the modules. */ + +#ifndef __GTK_TIME_PRIVATE_H__ +#define __GTK_TIME_PRIVATE_H__ + +#include +#include +#include +#include +#include +#include + +#ifdef G_OS_WIN32 +#include +#else +#include +#include +#include +#endif + +#include + +#ifdef __linux__ +#include +#include +/* declared by unistd.h only with _DEFAULT_SOURCE or _GNU_SOURCE */ +extern long syscall (long number, ...); +#endif + +G_BEGIN_DECLS + +typedef struct +{ + guint mode; /* as st_mode */ + guint64 dev; /* as st_dev, 0 on Windows */ + guint64 ino; /* as st_ino, 0 on Windows */ + gint64 size; + gint64 mtime; /* seconds since 1970 */ +} GtkStatBuf; + +/* A time from a 32-bit time_t: see above */ +static inline gint64 +_gtk_time_from_time_t (gint64 t) +{ + if (sizeof (time_t) < 8 && t < 0) + t += G_GINT64_CONSTANT (1) << 32; + return t; +} + +/* g_get_real_time(), past 2038 also where time_t has 32 bits */ +static inline gint64 +_gtk_get_real_time (void) +{ + gint64 t; + +#if defined (__linux__) && defined (SYS_clock_gettime64) + if (sizeof (time_t) < 8) + { + struct { gint64 tv_sec; gint64 tv_nsec; } ts; /* struct __kernel_timespec */ + + if (syscall (SYS_clock_gettime64, CLOCK_REALTIME, &ts) == 0) + return ts.tv_sec * G_USEC_PER_SEC + ts.tv_nsec / 1000; + } +#endif + + t = g_get_real_time (); + if (sizeof (time_t) < 8 && t < 0) + t += (G_GINT64_CONSTANT (1) << 32) * G_USEC_PER_SEC; + return t; +} + +/* g_date_time_new_now_local(), past 2038 also where time_t has 32 bits */ +static inline GDateTime * +_gtk_date_time_new_now_local (void) +{ + return g_date_time_new_from_unix_local (_gtk_get_real_time () / G_USEC_PER_SEC); +} + +#ifdef G_OS_WIN32 +static inline gint64 +_gtk_time_from_filetime (const FILETIME *ft) +{ + guint64 t = ((guint64) ft->dwHighDateTime << 32) | ft->dwLowDateTime; + + /* 100 ns since 1601 */ + return (gint64) (t / 10000000) - G_GINT64_CONSTANT (11644473600); +} + +static inline void +_gtk_set_errno_from_win32 (DWORD error) +{ + switch (error) + { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + case ERROR_INVALID_NAME: + case ERROR_INVALID_DRIVE: + errno = ENOENT; + break; + case ERROR_ACCESS_DENIED: + case ERROR_SHARING_VIOLATION: + errno = EACCES; + break; + default: + errno = EIO; + break; + } +} +#endif + +/* g_stat() or g_lstat() (@follow_links FALSE) for the fields in + * GtkStatBuf: 0 on success, -1 with errno set on failure */ +static inline int +_gtk_stat (const gchar *filename, + gboolean follow_links, + GtkStatBuf *buf) +{ +#ifdef G_OS_WIN32 + WIN32_FILE_ATTRIBUTE_DATA data; + wchar_t *wfilename; + BOOL ok; + + memset (buf, 0, sizeof *buf); + wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); + if (wfilename == NULL) + { + errno = EINVAL; + return -1; + } + ok = GetFileAttributesExW (wfilename, GetFileExInfoStandard, &data); + g_free (wfilename); + if (!ok) + { + _gtk_set_errno_from_win32 (GetLastError ()); + return -1; + } + buf->mode = (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? S_IFDIR | 0755 : S_IFREG | 0644; + buf->dev = 0; + buf->ino = 0; + buf->size = ((gint64) data.nFileSizeHigh << 32) | data.nFileSizeLow; + buf->mtime = _gtk_time_from_filetime (&data.ftLastWriteTime); + return 0; +#else + GStatBuf st; + int res; + + memset (buf, 0, sizeof *buf); + +#if defined (__linux__) && defined (SYS_statx) + static gboolean no_statx = FALSE; + + if (sizeof (time_t) < 8 && !no_statx) + { + guint64 stx[32]; /* struct statx, 256 bytes */ + guint16 mode; + guint32 major, minor; + + memset (stx, 0, sizeof stx); + /* AT_FDCWD, AT_SYMLINK_NOFOLLOW, STATX_BASIC_STATS */ + if (syscall (SYS_statx, -100, filename, follow_links ? 0 : 0x100, 0x7ffU, stx) == 0) + { + guchar *p = (guchar *) stx; + + memcpy (&mode, p + 28, 2); + memcpy (&buf->ino, p + 32, 8); + memcpy (&buf->size, p + 40, 8); + memcpy (&buf->mtime, p + 112, 8); + memcpy (&major, p + 136, 4); + memcpy (&minor, p + 140, 4); + buf->mode = mode; + buf->dev = makedev (major, minor); + return 0; + } + /* a kernel before 4.11, or a sandbox that does not let statx through */ + if (errno != ENOSYS && errno != EPERM) + return -1; + no_statx = TRUE; + } +#endif + + res = follow_links ? g_stat (filename, &st) : g_lstat (filename, &st); + if (res != 0) + return res; + buf->mode = st.st_mode; + buf->dev = st.st_dev; + buf->ino = st.st_ino; + buf->size = st.st_size; + buf->mtime = _gtk_time_from_time_t (st.st_mtime); + return 0; +#endif +} + +/* Sets the modification time of @filename and leaves its access time: + * 0 on success, -1 with errno set on failure */ +static inline int +_gtk_set_mtime (const gchar *filename, + gint64 mtime) +{ +#ifdef G_OS_WIN32 + wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); + HANDLE h; + FILETIME ft; + guint64 t; + BOOL ok; + + if (wfilename == NULL) + { + errno = EINVAL; + return -1; + } + /* FILE_FLAG_BACKUP_SEMANTICS: folders too */ + h = CreateFileW (wfilename, FILE_WRITE_ATTRIBUTES, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + g_free (wfilename); + if (h == INVALID_HANDLE_VALUE) + { + _gtk_set_errno_from_win32 (GetLastError ()); + return -1; + } + t = (guint64) (mtime + G_GINT64_CONSTANT (11644473600)) * 10000000; + ft.dwLowDateTime = (DWORD) t; + ft.dwHighDateTime = (DWORD) (t >> 32); + ok = SetFileTime (h, NULL, NULL, &ft); + if (!ok) + _gtk_set_errno_from_win32 (GetLastError ()); + CloseHandle (h); + return ok ? 0 : -1; +#else + struct stat st; + struct utimbuf times; + +#if defined (__linux__) && defined (SYS_utimensat_time64) + if (sizeof (time_t) < 8) + { + /* struct __kernel_timespec; the access time is left alone */ + struct { gint64 tv_sec; gint64 tv_nsec; } ts[2] = { + { 0, (1L << 30) - 2L }, /* UTIME_OMIT */ + { mtime, 0 } + }; + + if (syscall (SYS_utimensat_time64, -100, filename, ts, 0) == 0) + return 0; + if (errno != ENOSYS && errno != EPERM) + return -1; + } +#endif + + if (stat (filename, &st) != 0) + return -1; + times.actime = st.st_atime; + times.modtime = (time_t) mtime; + return utime (filename, ×); +#endif +} + +G_END_DECLS + +#endif /* __GTK_TIME_PRIVATE_H__ */ diff --git a/gtk/updateiconcache.c b/gtk/updateiconcache.c index 70115e6b6..20a88a14d 100644 --- a/gtk/updateiconcache.c +++ b/gtk/updateiconcache.c @@ -49,6 +49,7 @@ #include "gtkpixdatautils.h" #include #include "gtkiconcachevalidator.h" +#include "gtktimeprivate.h" static gboolean force_update = FALSE; static gboolean ignore_theme_index = FALSE; @@ -76,19 +77,36 @@ static gchar *var_name = "-"; #define ALIGN_VALUE(this, boundary) \ (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1))) +/* The times are 64-bit seconds (gtktimeprivate.h): with the stat of a + * 32-bit time_t or of 32-bit Windows, a theme changed after 2038 could + * not be checked or got no cache at all */ #ifdef HAVE_FTW_H #include -static struct stat cache_stat; +static gint64 cache_mtime; static gboolean cache_up_to_date; static int check_dir_mtime (const char *dir, const struct stat *sb, int tf) { - if (tf != FTW_NS && - (gint64) sb->st_mtime > (gint64) cache_stat.st_mtime) + gint64 mtime; + + if (tf == FTW_NS) + { + /* the stat of ftw() failed, as it does for a time it cannot give + * in a 32-bit time_t */ + GtkStatBuf buf; + + if (_gtk_stat (dir, TRUE, &buf) != 0) + return 0; + mtime = buf.mtime; + } + else + mtime = _gtk_time_from_time_t (sb->st_mtime); + + if (mtime > cache_mtime) { cache_up_to_date = FALSE; /* stop tree walk */ @@ -103,9 +121,10 @@ static int check_dir_mtime (const char *dir, { gchar *cache_path; gint retval; + GtkStatBuf cache_stat; cache_path = g_build_filename (path, CACHE_NAME, NULL); - retval = g_stat (cache_path, &cache_stat); + retval = _gtk_stat (cache_path, TRUE, &cache_stat); g_free (cache_path); if (retval < 0) @@ -114,6 +133,7 @@ static int check_dir_mtime (const char *dir, return FALSE; } + cache_mtime = cache_stat.mtime; cache_up_to_date = TRUE; ftw (path, check_dir_mtime, 20); @@ -126,11 +146,11 @@ static int check_dir_mtime (const char *dir, gboolean is_cache_up_to_date (const gchar *path) { - GStatBuf path_stat, cache_stat; + GtkStatBuf path_stat, cache_stat; gchar *cache_path; int retval; - retval = g_stat (path, &path_stat); + retval = _gtk_stat (path, TRUE, &path_stat); if (retval < 0) { @@ -140,7 +160,7 @@ is_cache_up_to_date (const gchar *path) } cache_path = g_build_filename (path, CACHE_NAME, NULL); - retval = g_stat (cache_path, &cache_stat); + retval = _gtk_stat (cache_path, TRUE, &cache_stat); g_free (cache_path); if (retval < 0) @@ -150,7 +170,7 @@ is_cache_up_to_date (const gchar *path) } /* Check mtime */ - return (gint64) cache_stat.st_mtime >= (gint64) path_stat.st_mtime; + return cache_stat.mtime >= path_stat.mtime; } #endif /* !HAVE_FTW_H */ @@ -660,7 +680,7 @@ scan_directory (const gchar *base_path, for (i = 0; i < names->len; i++) { gchar *path; - GStatBuf st; + GtkStatBuf st; int flags = 0; Image *image; gchar *basename, *dot; @@ -668,13 +688,15 @@ scan_directory (const gchar *base_path, name = g_ptr_array_index (names, i); path = g_build_filename (dir_path, name, NULL); - if (g_stat (path, &st) != 0) + /* not g_stat(): with a 32-bit time_t it fails for files changed + * after 2038, which then were left out of the cache */ + if (_gtk_stat (path, TRUE, &st) != 0) { g_free (path); continue; } - if (S_ISDIR (st.st_mode)) + if (S_ISDIR (st.mode)) { gchar *subsubdir; @@ -687,7 +709,7 @@ scan_directory (const gchar *base_path, g_free (subsubdir); } /* ignore images in the toplevel directory */ - else if (subdir != NULL && S_ISREG (st.st_mode)) + else if (subdir != NULL && S_ISREG (st.mode)) { if (g_str_has_suffix (name, ".png")) flags |= HAS_SUFFIX_PNG; @@ -1510,8 +1532,7 @@ build_cache (const gchar *path) #endif GHashTable *files; FILE *cache; - GStatBuf path_stat, cache_stat; - struct utimbuf utime_buf; + GtkStatBuf cache_stat; GList *directories = NULL; int fd; int retry_count = 0; @@ -1635,15 +1656,13 @@ opentmp: g_unlink (bak_cache_path); #endif - /* Update time */ + /* Update time: the folder gets the time of the cache, which renaming + * the cache into it made older */ /* FIXME: What do do if an error occurs here? */ - if (g_stat (path, &path_stat) < 0 || - g_stat (cache_path, &cache_stat)) + if (_gtk_stat (cache_path, TRUE, &cache_stat) != 0) exit (1); - utime_buf.actime = path_stat.st_atime; - utime_buf.modtime = cache_stat.st_mtime; - g_utime (path, &utime_buf); + _gtk_set_mtime (path, cache_stat.mtime); if (!quiet) g_printerr (_("Cache file created successfully.\n"));