is_writable.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "is_writable.h"
  9. #ifdef _WIN32
  10. #include <sys/stat.h>
  11. #ifndef S_IWUSR
  12. # define S_IWUSR S_IWRITE
  13. #endif
  14. IGL_INLINE bool is_writable(const char* filename)
  15. {
  16. // Check if file already exists
  17. struct stat status;
  18. if(stat(filename,&status)!=0)
  19. {
  20. return false;
  21. }
  22. return S_IWUSR & status.st_mode;
  23. }
  24. #else
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. IGL_INLINE bool igl::is_writable(const char* filename)
  28. {
  29. // Check if file already exists
  30. struct stat status;
  31. if(stat(filename,&status)!=0)
  32. {
  33. return false;
  34. }
  35. // Get current users uid and gid
  36. uid_t this_uid = getuid();
  37. gid_t this_gid = getgid();
  38. // Dealing with owner
  39. if( this_uid == status.st_uid )
  40. {
  41. return S_IWUSR & status.st_mode;
  42. }
  43. // Dealing with group member
  44. if( this_gid == status.st_gid )
  45. {
  46. return S_IWGRP & status.st_mode;
  47. }
  48. // Dealing with other
  49. return S_IWOTH & status.st_mode;
  50. }
  51. #endif