Merge branch 'jk/http-leakfixes' into ps/leakfixes-part-8
[git/gitster.git] / compat / stat.c
bloba2d3931cb73c35eb6144d4eb614ca5d63af84244
1 #define _POSIX_C_SOURCE 200112L
2 #include <sys/stat.h> /* *stat, S_IS* */
3 #include <sys/types.h> /* mode_t */
5 static inline mode_t mode_native_to_git(mode_t native_mode)
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;
26 int git_stat(const char *path, struct stat *buf)
28 int rc = stat(path, buf);
29 if (rc == 0)
30 buf->st_mode = mode_native_to_git(buf->st_mode);
31 return rc;
34 int git_fstat(int fd, struct stat *buf)
36 int rc = fstat(fd, buf);
37 if (rc == 0)
38 buf->st_mode = mode_native_to_git(buf->st_mode);
39 return rc;
42 int git_lstat(const char *path, struct stat *buf)
44 int rc = lstat(path, buf);
45 if (rc == 0)
46 buf->st_mode = mode_native_to_git(buf->st_mode);
47 return rc;