summaryrefslogtreecommitdiff
path: root/gio/glocalfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'gio/glocalfile.c')
-rw-r--r--gio/glocalfile.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/gio/glocalfile.c b/gio/glocalfile.c
index 67d4b99fb..dbb56902d 100644
--- a/gio/glocalfile.c
+++ b/gio/glocalfile.c
@@ -86,6 +86,9 @@
#define FILE_READ_ONLY_VOLUME 0x00080000
#endif
+#ifndef S_ISREG
+#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
+#endif
#ifndef S_ISDIR
#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
#endif
@@ -2777,6 +2780,39 @@ g_local_file_measure_size_of_contents (gint fd,
MeasureState *state,
GError **error);
+/*
+ * _g_stat_is_size_usable:
+ * @buf: a #GLocalFileStat.
+ *
+ * Checks if the file type is such that the `st_size` field of `struct stat` is
+ * well-defined by POSIX.
+ * (see https://pubs.opengroup.org/onlinepubs/009696799/basedefs/sys/stat.h.html)
+ *
+ * This behaviour is aligned with `du` from GNU Coreutils 9.2+
+ * (see https://lists.gnu.org/archive/html/bug-coreutils/2023-03/msg00007.html)
+ * and makes apparent size sums well-defined; formerly, they depended on the
+ * implementation, and could differ across filesystems.
+ *
+ * Returns: %TRUE if the size field is well-defined, %FALSE otherwise.
+ **/
+inline static gboolean
+_g_stat_is_size_usable (const GLocalFileStat *buf)
+{
+#ifndef HAVE_STATX
+ /* Memory objects are defined by POSIX, but are not supported by statx nor Windows */
+#ifdef S_TYPEISSHM
+ if (S_TYPEISSHM (buf))
+ return TRUE;
+#endif
+#ifdef S_TYPEISTMO
+ if (S_TYPEISTMO (buf))
+ return TRUE;
+#endif
+#endif
+
+ return S_ISREG (_g_stat_mode (buf)) || S_ISLNK (_g_stat_mode (buf));
+}
+
static gboolean
g_local_file_measure_size_of_file (gint parent_fd,
GSList *name,
@@ -2836,6 +2872,7 @@ g_local_file_measure_size_of_file (gint parent_fd,
state->disk_usage += _g_stat_blocks (&buf) * G_GUINT64_CONSTANT (512);
else
#endif
+ if (_g_stat_is_size_usable (&buf))
state->disk_usage += _g_stat_size (&buf);
if (S_ISDIR (_g_stat_mode (&buf)))