compat / stat.con commit Merge branch 'ky/branch-d-worktree' into maint (a412714)
   1#define _POSIX_C_SOURCE 200112L
   2#include <sys/stat.h>  /* *stat, S_IS* */
   3#include <sys/types.h> /* mode_t       */
   4
   5static inline mode_t mode_native_to_git(mode_t native_mode)
   6{
   7        mode_t perm_bits = native_mode & 07777;
   8        if (S_ISREG(native_mode))
   9                return 0100000 | perm_bits;
  10        if (S_ISDIR(native_mode))
  11                return 0040000 | perm_bits;
  12        if (S_ISLNK(native_mode))
  13                return 0120000 | perm_bits;
  14        if (S_ISBLK(native_mode))
  15                return 0060000 | perm_bits;
  16        if (S_ISCHR(native_mode))
  17                return 0020000 | perm_bits;
  18        if (S_ISFIFO(native_mode))
  19                return 0010000 | perm_bits;
  20        if (S_ISSOCK(native_mode))
  21                return 0140000 | perm_bits;
  22        /* Non-standard type bits were given. */
  23        return perm_bits;
  24}
  25
  26int git_stat(const char *path, struct stat *buf)
  27{
  28        int rc = stat(path, buf);
  29        if (rc == 0)
  30                buf->st_mode = mode_native_to_git(buf->st_mode);
  31        return rc;
  32}
  33
  34int git_fstat(int fd, struct stat *buf)
  35{
  36        int rc = fstat(fd, buf);
  37        if (rc == 0)
  38                buf->st_mode = mode_native_to_git(buf->st_mode);
  39        return rc;
  40}
  41
  42int git_lstat(const char *path, struct stat *buf)
  43{
  44        int rc = lstat(path, buf);
  45        if (rc == 0)
  46                buf->st_mode = mode_native_to_git(buf->st_mode);
  47        return rc;
  48}