is_writable.cpp 914 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "is_writable.h"
  2. #ifdef _WIN32
  3. #include <sys/stat.h>
  4. #ifndef S_IWUSR
  5. # define S_IWUSR S_IWRITE
  6. #endif
  7. IGL_INLINE bool is_writable(const char* filename)
  8. {
  9. // Check if file already exists
  10. struct stat status;
  11. if(stat(filename,&status)!=0)
  12. {
  13. return false;
  14. }
  15. return S_IWUSR & status.st_mode;
  16. }
  17. #else
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. IGL_INLINE bool igl::is_writable(const char* filename)
  21. {
  22. // Check if file already exists
  23. struct stat status;
  24. if(stat(filename,&status)!=0)
  25. {
  26. return false;
  27. }
  28. // Get current users uid and gid
  29. uid_t this_uid = getuid();
  30. gid_t this_gid = getgid();
  31. // Dealing with owner
  32. if( this_uid == status.st_uid )
  33. {
  34. return S_IWUSR & status.st_mode;
  35. }
  36. // Dealing with group member
  37. if( this_gid == status.st_gid )
  38. {
  39. return S_IWGRP & status.st_mode;
  40. }
  41. // Dealing with other
  42. return S_IWOTH & status.st_mode;
  43. }
  44. #endif