Subject: bug#34239: build failure on Android, due to S_MAGIC_* symbols

From: Pádraig Brady <[email-masked]>
Date: Tue, 29 Jan 2019 21:00:35 -0800

On 28/01/19 19:19, Bruno Haible wrote:
> Hi,
> 
> Compiling coreutils on Android produces this error:
> 
>   CC       src/tail.o
> In file included from ../src/tail.c:63:
> ../src/fs-is-local.h: In function 'is_local_fs_type':
> ../src/fs-is-local.h:9: error: 'S_MAGIC_AAFS' undeclared (first use in this function)
> ...
> make[2]: *** [src/tail.o] Error 1
> 
> The Android libc, Bionic, does not define any of these S_MAGIC_* symbols or
> macros, even in the newest version [1].
> 
> Can some #ifdef be used to avoid this build failure?
> 'defined __ANDROID__' tests for Android.
> 'defined __linux__' tests for Linux excluding Android.
> 
> Bruno
> 
> [1] https://android.googlesource.com/platform/bionic/

Interesting. So inotify is supported on that android system.
Our ifdefs were wrong anyway as we check for remoteness even if one disables inotify.
I.E. our build would have failed on standard linux if one explicitly disabled inotify.
I've fixed that up and added support for the android specific "sdcardfs" which I found in:
https://android.googlesource.com/kernel/common/+/android-mainline-tracking/include/uapi/linux/magic.h

Proposed patch attached.

thanks!
Pádraig
>From 500e567165d210a8d8a96e4084a5fdc6007b2ca8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email-masked]>
Date: Tue, 29 Jan 2019 20:32:53 -0800
Subject: [PATCH] stat,tail: fix android build and support inotify

* src/extract-magic: Treat android like linux,
which fixes the build by ensuring the constants are defined.
* src/stat.c: Support all constants on android, including
the android specific "sdcardfs".
* src/tail.c: Fix inclusion of statfs headers to be independent
of inotify availability, as fremote() is used on linux even
if inotify has been disabled.  Also enable fremote() on android.
* NEWS: Mention the improvment.
Fixes https://bugs.gnu.org/34239
---
 NEWS              | 5 +++++
 src/extract-magic | 2 +-
 src/stat.c        | 4 +++-
 src/tail.c        | 7 +++++--
 4 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/NEWS b/NEWS
index 4b6b8bf..e6ccb19 100644
--- a/NEWS
+++ b/NEWS
@@ -69,6 +69,11 @@ GNU coreutils NEWS                                    -*- outline -*-
   and encodes and decodes printable text using various common encodings:
   base64,base64url,base32,base32hex,base16,base2,z85.
 
+** Improvements
+
+  stat and tail now know about the "sdcardfs" file system on Android.
+  stat -f -c%T now reports the file system type, and tail -f uses inotify.
+
 
 * Noteworthy changes in release 8.30 (2018-07-01) [stable]
 
diff --git a/src/extract-magic b/src/extract-magic
index 48c38df..892c1db 100644
--- a/src/extract-magic
+++ b/src/extract-magic
@@ -125,7 +125,7 @@ EOF
   print $emit_magic ? $magic_comment : $map_comment;
 
   $emit_magic
-    and print "\n#if defined __linux__\n";
+    and print "\n#if defined __linux__ || defined __ANDROID__\n";
   $emit_magic
     or print "static inline int\n"
       . "is_local_fs_type (unsigned long int magic)\n"
diff --git a/src/stat.c b/src/stat.c
index f17246d..c8f1809 100644
--- a/src/stat.c
+++ b/src/stat.c
@@ -235,7 +235,7 @@ human_fstype (STRUCT_STATVFS const *statfsbuf)
 #else
   switch (statfsbuf->f_type)
     {
-# if defined __linux__
+# if defined __linux__ || defined __ANDROID__
 
       /* Compare with what's in libc:
          f=/a/libc/sysdeps/unix/sysv/linux/linux_fsinfo.h
@@ -450,6 +450,8 @@ human_fstype (STRUCT_STATVFS const *statfsbuf)
       return "romfs";
     case S_MAGIC_RPC_PIPEFS: /* 0x67596969 local */
       return "rpc_pipefs";
+    case S_MAGIC_SDCARDFS: /* 0x5DCA2DF5 local */
+      return "sdcardfs";
     case S_MAGIC_SECURITYFS: /* 0x73636673 local */
       return "securityfs";
     case S_MAGIC_SELINUX: /* 0xF97CFF8C local */
diff --git a/src/tail.c b/src/tail.c
index dee827b..8fab049 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -57,8 +57,10 @@
 # include <sys/inotify.h>
 /* 'select' is used by tail_forever_inotify.  */
 # include <sys/select.h>
+#endif
 
-/* inotify needs to know if a file is local.  */
+/* Linux can optimize the handling of local files.  */
+#if defined __linux__ || defined __ANDROID__
 # include "fs.h"
 # include "fs-is-local.h"
 # if HAVE_SYS_STATFS_H
@@ -938,7 +940,8 @@ fremote (int fd, const char *name)
 {
   bool remote = true;           /* be conservative (poll by default).  */
 
-#if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE && defined __linux__
+#if HAVE_FSTATFS && HAVE_STRUCT_STATFS_F_TYPE \
+ && (defined __linux__ || defined __ANDROID__)
   struct statfs buf;
   int err = fstatfs (fd, &buf);
   if (err != 0)
-- 
2.9.3