diff options
author | Marco Trevisan <mail@3v1n0.net> | 2022-10-11 13:11:07 +0000 |
---|---|---|
committer | Marco Trevisan <mail@3v1n0.net> | 2022-10-11 13:11:07 +0000 |
commit | 10e3715bc1887c919a5ae8ab07012c101b83bf6b (patch) | |
tree | 03f4a88d0508bcc1f43bea01c2c7f08f35598715 | |
parent | 4231426abe903d3c7c4f252d64c3d9041abf9141 (diff) | |
parent | 60587d5965ee7e303b4bed773689fb488e0afa29 (diff) | |
download | glib-10e3715bc1887c919a5ae8ab07012c101b83bf6b.tar.gz |
Merge branch 'utimensat2' into 'main'
glocalfileinfo: In set_mtime_atime(), don't use utimensat() if it's not available
See merge request GNOME/glib!2911
-rw-r--r-- | gio/glocalfileinfo.c | 127 | ||||
-rw-r--r-- | gio/tests/file.c | 2 |
2 files changed, 75 insertions, 54 deletions
diff --git a/gio/glocalfileinfo.c b/gio/glocalfileinfo.c index c9e5c8b64..ecbc629c6 100644 --- a/gio/glocalfileinfo.c +++ b/gio/glocalfileinfo.c @@ -2678,7 +2678,6 @@ lazy_stat (char *filename, return res; } - static gboolean set_mtime_atime (char *filename, const GFileAttributeValue *mtime_value, @@ -2691,57 +2690,37 @@ set_mtime_atime (char *filename, { int res; guint64 val = 0; - guint32 val_usec = 0; - guint32 val_nsec = 0; struct stat statbuf; gboolean got_stat = FALSE; - struct timeval times[2] = { {0, 0}, {0, 0} }; #ifdef HAVE_UTIMENSAT struct timespec times_n[2] = { {0, 0}, {0, 0} }; -#endif /* ATIME */ if (atime_value) { if (!get_uint64 (atime_value, &val, error)) return FALSE; - times[0].tv_sec = val; -#if defined (HAVE_UTIMENSAT) times_n[0].tv_sec = val; -#endif } else { if (lazy_stat (filename, &statbuf, &got_stat) == 0) { - times[0].tv_sec = statbuf.st_atime; + times_n[0].tv_sec = statbuf.st_atime; #if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC) - times[0].tv_usec = statbuf.st_atimensec / 1000; -#if defined (HAVE_UTIMENSAT) times_n[0].tv_nsec = statbuf.st_atimensec; -#endif /* HAVE_UTIMENSAT */ #elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC) - times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000; -#if defined (HAVE_UTIMENSAT) times_n[0].tv_nsec = statbuf.st_atim.tv_nsec; -#endif /* HAVE_UTIMENSAT */ #endif } } - - if (atime_usec_value) - { - if (!get_uint32 (atime_usec_value, &val_usec, error)) - return FALSE; - times[0].tv_usec = val_usec; - } if (atime_nsec_value) { + guint32 val_nsec = 0; + if (!get_uint32 (atime_nsec_value, &val_nsec, error)) return FALSE; -#if defined (HAVE_UTIMENSAT) times_n[0].tv_nsec = val_nsec; -#endif } /* MTIME */ @@ -2749,59 +2728,100 @@ set_mtime_atime (char *filename, { if (!get_uint64 (mtime_value, &val, error)) return FALSE; - times[1].tv_sec = val; -#if defined (HAVE_UTIMENSAT) times_n[1].tv_sec = val; -#endif } else { if (lazy_stat (filename, &statbuf, &got_stat) == 0) { - times[1].tv_sec = statbuf.st_mtime; #if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC) - times[1].tv_usec = statbuf.st_mtimensec / 1000; -#if defined (HAVE_UTIMENSAT) times_n[1].tv_nsec = statbuf.st_mtimensec; -#endif /* HAVE_UTIMENSAT */ #elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) - times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000; -#if defined (HAVE_UTIMENSAT) times_n[1].tv_nsec = statbuf.st_mtim.tv_nsec; -#endif /* HAVE_UTIMENSAT */ #endif } } - - if (mtime_usec_value) - { - if (!get_uint32 (mtime_usec_value, &val_usec, error)) - return FALSE; - times[1].tv_usec = val_usec; - } + if (mtime_nsec_value) { + guint32 val_nsec = 0; + if (!get_uint32 (mtime_nsec_value, &val_nsec, error)) return FALSE; -#if defined (HAVE_UTIMENSAT) times_n[1].tv_nsec = val_nsec; + } + + res = utimensat (AT_FDCWD, filename, times_n, 0); + +#else /* HAVE_UTIMES */ + + struct timeval times[2] = { {0, 0}, {0, 0} }; + + /* ATIME */ + if (atime_value) + { + if (!get_uint64 (atime_value, &val, error)) + return FALSE; + + times[0].tv_sec = val; + } + else + { + if (lazy_stat (filename, &statbuf, &got_stat) == 0) + { + times[0].tv_sec = statbuf.st_atime; +#if defined (HAVE_STRUCT_STAT_ST_ATIMENSEC) + times[0].tv_usec = statbuf.st_atimensec / 1000; +#elif defined (HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC) + times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000; #endif + } } - - res = utimes (filename, times); - if (res == -1) + + if (atime_usec_value) { - int errsv = errno; + guint32 val_usec = 0; - g_set_error (error, G_IO_ERROR, - g_io_error_from_errno (errsv), - _("Error setting modification or access time: %s"), - g_strerror (errsv)); - return FALSE; + if (!get_uint32 (atime_usec_value, &val_usec, error)) + return FALSE; + + times[0].tv_usec = val_usec; } -#if defined (HAVE_UTIMENSAT) - res = utimensat (AT_FDCWD, filename, times_n, 0); + /* MTIME */ + if (mtime_value) + { + if (!get_uint64 (mtime_value, &val, error)) + return FALSE; + + times[1].tv_sec = val; + } + else + { + if (lazy_stat (filename, &statbuf, &got_stat) == 0) + { + times[1].tv_sec = statbuf.st_mtime; +#if defined (HAVE_STRUCT_STAT_ST_MTIMENSEC) + times[1].tv_usec = statbuf.st_mtimensec / 1000; +#elif defined (HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) + times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000; +#endif + } + } + + if (mtime_usec_value) + { + guint32 val_usec = 0; + + if (!get_uint32 (mtime_usec_value, &val_usec, error)) + return FALSE; + + times[1].tv_usec = val_usec; + } + + res = utimes (filename, times); +#endif + if (res == -1) { int errsv = errno; @@ -2812,7 +2832,6 @@ set_mtime_atime (char *filename, g_strerror (errsv)); return FALSE; } -#endif return TRUE; } #endif diff --git a/gio/tests/file.c b/gio/tests/file.c index 9a15f9726..9d98ff205 100644 --- a/gio/tests/file.c +++ b/gio/tests/file.c @@ -1,3 +1,5 @@ +#include "config.h" + #include <locale.h> #include <string.h> #include <stdio.h> |