Re: [PATCH v2] compat: convert modes to use portable file type values

2014-12-04 Thread David Michael
On Wed, Dec 3, 2014 at 9:24 PM, David Michael fedora@gmail.com wrote:
 --- /dev/null
 +++ b/compat/stat.c
 @@ -0,0 +1,49 @@
 +#define _POSIX_C_SOURCE 200112L
 +#include stddef.h/* NULL */
 +#include sys/stat.h  /* *stat, S_IS* */
 +#include sys/types.h /* mode_t   */

Oops, the stddef.h line can be removed now that this is no longer
testing for NULL.  Let me know if this warrants a v3 if there is no
other feedback.

Thanks.

David
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v2] compat: convert modes to use portable file type values

2014-12-04 Thread Junio C Hamano
David Michael fedora@gmail.com writes:

 On Wed, Dec 3, 2014 at 9:24 PM, David Michael fedora@gmail.com wrote:
 --- /dev/null
 +++ b/compat/stat.c
 @@ -0,0 +1,49 @@
 +#define _POSIX_C_SOURCE 200112L
 +#include stddef.h/* NULL */
 +#include sys/stat.h  /* *stat, S_IS* */
 +#include sys/types.h /* mode_t   */

 Oops, the stddef.h line can be removed now that this is no longer
 testing for NULL.  Let me know if this warrants a v3 if there is no
 other feedback.

Let me queue with this squashed in for now.

 compat/stat.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/compat/stat.c b/compat/stat.c
index c2d4711..a2d3931 100644
--- a/compat/stat.c
+++ b/compat/stat.c
@@ -1,5 +1,4 @@
 #define _POSIX_C_SOURCE 200112L
-#include stddef.h/* NULL */
 #include sys/stat.h  /* *stat, S_IS* */
 #include sys/types.h /* mode_t   */
 
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2] compat: convert modes to use portable file type values

2014-12-03 Thread David Michael
This adds simple wrapper functions around calls to stat(), fstat(),
and lstat() that translate the operating system's native file type
bits to those used by most operating systems.  It also rewrites the
S_IF* macros to the common values, so all file type processing is
performed using the translated modes.  This makes projects portable
across operating systems that use different file type definitions.

Only the file type bits may be affected by these compatibility
functions; the file permission bits are assumed to be 0 and are
passed through unchanged.

Signed-off-by: David Michael fedora@gmail.com
---

Changes in v2:

Remove elses and use a perm_bits variable as suggested by Torsten
Bögershausen.

Only translate the mode on stat() success as suggested by Duy Nguyen.

Add S_IFSOCK translation to support a git.c test.  (I left the device
types defined just to support all the POSIX file types.)

Replace _POSIX_SOURCE=1 with _POSIX_C_SOURCE=200112L to properly get
IEEE 1003.1a definitions for lstat(), as suggested for glibc.

 Makefile  |  8 
 cache.h   |  7 ---
 compat/stat.c | 49 +
 configure.ac  | 23 +++
 git-compat-util.h | 34 ++
 5 files changed, 114 insertions(+), 7 deletions(-)
 create mode 100644 compat/stat.c

diff --git a/Makefile b/Makefile
index 827006b..cba3be1 100644
--- a/Makefile
+++ b/Makefile
@@ -191,6 +191,10 @@ all::
 # Define NO_TRUSTABLE_FILEMODE if your filesystem may claim to support
 # the executable mode bit, but doesn't really do so.
 #
+# Define NEEDS_MODE_TRANSLATION if your OS strays from the typical file type
+# bits in mode values (e.g. z/OS defines I_SFMT to 0xFF00 as opposed to the
+# usual 0xF000).
+#
 # Define NO_IPV6 if you lack IPv6 support and getaddrinfo().
 #
 # Define NO_UNIX_SOCKETS if your system does not offer unix sockets.
@@ -1230,6 +1234,10 @@ endif
 ifdef NO_TRUSTABLE_FILEMODE
BASIC_CFLAGS += -DNO_TRUSTABLE_FILEMODE
 endif
+ifdef NEEDS_MODE_TRANSLATION
+   COMPAT_CFLAGS += -DNEEDS_MODE_TRANSLATION
+   COMPAT_OBJS += compat/stat.o
+endif
 ifdef NO_IPV6
BASIC_CFLAGS += -DNO_IPV6
 endif
diff --git a/cache.h b/cache.h
index 99ed096..f8174fe 100644
--- a/cache.h
+++ b/cache.h
@@ -65,13 +65,6 @@ unsigned long git_deflate_bound(git_zstream *, unsigned 
long);
  *
  * The value 016 is not normally a valid mode, and
  * also just happens to be S_IFDIR + S_IFLNK
- *
- * NOTE! We *really* shouldn't depend on the S_IFxxx macros
- * always having the same values everywhere. We should use
- * our internal git values for these things, and then we can
- * translate that to the OS-specific value. It just so
- * happens that everybody shares the same bit representation
- * in the UNIX world (and apparently wider too..)
  */
 #define S_IFGITLINK016
 #define S_ISGITLINK(m) (((m)  S_IFMT) == S_IFGITLINK)
diff --git a/compat/stat.c b/compat/stat.c
new file mode 100644
index 000..c2d4711
--- /dev/null
+++ b/compat/stat.c
@@ -0,0 +1,49 @@
+#define _POSIX_C_SOURCE 200112L
+#include stddef.h/* NULL */
+#include sys/stat.h  /* *stat, S_IS* */
+#include sys/types.h /* mode_t   */
+
+static inline mode_t mode_native_to_git(mode_t native_mode)
+{
+   mode_t perm_bits = native_mode  0;
+   if (S_ISREG(native_mode))
+   return 010 | perm_bits;
+   if (S_ISDIR(native_mode))
+   return 004 | perm_bits;
+   if (S_ISLNK(native_mode))
+   return 012 | perm_bits;
+   if (S_ISBLK(native_mode))
+   return 006 | perm_bits;
+   if (S_ISCHR(native_mode))
+   return 002 | perm_bits;
+   if (S_ISFIFO(native_mode))
+   return 001 | perm_bits;
+   if (S_ISSOCK(native_mode))
+   return 014 | perm_bits;
+   /* Non-standard type bits were given. */
+   return perm_bits;
+}
+
+int git_stat(const char *path, struct stat *buf)
+{
+   int rc = stat(path, buf);
+   if (rc == 0)
+   buf-st_mode = mode_native_to_git(buf-st_mode);
+   return rc;
+}
+
+int git_fstat(int fd, struct stat *buf)
+{
+   int rc = fstat(fd, buf);
+   if (rc == 0)
+   buf-st_mode = mode_native_to_git(buf-st_mode);
+   return rc;
+}
+
+int git_lstat(const char *path, struct stat *buf)
+{
+   int rc = lstat(path, buf);
+   if (rc == 0)
+   buf-st_mode = mode_native_to_git(buf-st_mode);
+   return rc;
+}
diff --git a/configure.ac b/configure.ac
index 6af9647..5c1312f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -873,6 +873,29 @@ else
SNPRINTF_RETURNS_BOGUS=
 fi
 GIT_CONF_SUBST([SNPRINTF_RETURNS_BOGUS])
+#
+# Define NEEDS_MODE_TRANSLATION if your OS strays from the typical file type
+# bits in mode values.
+AC_CACHE_CHECK([whether the platform uses typical file type bits],
+ [ac_cv_sane_mode_bits], [