access.c 669 B

1234567891011121314151617181920212223242526272829303132
  1. #define COMPAT_CODE_ACCESS
  2. #include "../git-compat-util.h"
  3. /* Do the same thing access(2) does, but use the effective uid,
  4. * and don't make the mistake of telling root that any file is
  5. * executable. This version uses stat(2).
  6. */
  7. int git_access(const char *path, int mode)
  8. {
  9. struct stat st;
  10. /* do not interfere a normal user */
  11. if (geteuid())
  12. return access(path, mode);
  13. if (stat(path, &st) < 0)
  14. return -1;
  15. /* Root can read or write any file. */
  16. if (!(mode & X_OK))
  17. return 0;
  18. /* Root can execute any file that has any one of the execute
  19. * bits set.
  20. */
  21. if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
  22. return 0;
  23. errno = EACCES;
  24. return -1;
  25. }