summarylogtreecommitdiffstats
path: root/0002-glocalfile-Sum-apparent-size-only-for-files-and-syml.patch
blob: 8e96aaf3008a336166b545b102a1e2bcddf75d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Joan Bruguera <joanbrugueram@gmail.com>
Date: Thu, 23 Mar 2023 02:24:30 +0000
Subject: [PATCH] glocalfile: Sum apparent size only for files and symlinks

Since GNU Coreutils 9.2 (commit 110bcd28386b1f47a4cd876098acb708fdcbbb25),
`du --apparent-size` (including `du --bytes`) no longer counts all kinds of
files (directories, FIFOs, etc.), but only those for which `st_size` in
`struct stat` is defined by POSIX, namely regular files and symlinks
(and also rarely supported memory objects).

This aligns the behaviour of GLib's `G_FILE_MEASURE_APPARENT_SIZE` flag
with the new GNU Coreutils `du` and correct POSIX use.

Note that this may be a breaking change for some uses.

Link: https://lists.gnu.org/archive/html/bug-coreutils/2023-03/msg00007.html
Fixes: https://gitlab.gnome.org/GNOME/glib/-/issues/2965
---
 gio/glocalfile.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/gio/glocalfile.c b/gio/glocalfile.c
index 67d4b99fb741..e53216962faf 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,23 @@ g_local_file_measure_size_of_contents (gint           fd,
                                        MeasureState  *state,
                                        GError       **error);
 
+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 +2856,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)))