fgetsetflags.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * fgetflags.c - Get a file flags on an ext2 file system
  3. * fsetflags.c - Set a file flags on an ext2 file system
  4. *
  5. * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
  6. * Laboratoire MASI, Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * This file can be redistributed under the terms of the GNU Library General
  10. * Public License
  11. */
  12. /*
  13. * History:
  14. * 93/10/30 - Creation
  15. */
  16. #if HAVE_ERRNO_H
  17. #include <errno.h>
  18. #endif
  19. #if HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #if HAVE_EXT2_IOCTLS
  25. #include <fcntl.h>
  26. #include <sys/ioctl.h>
  27. #endif
  28. #include "e2p.h"
  29. #ifdef O_LARGEFILE
  30. #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK|O_LARGEFILE)
  31. #else
  32. #define OPEN_FLAGS (O_RDONLY|O_NONBLOCK)
  33. #endif
  34. int fgetsetflags (const char * name, unsigned long * get_flags, unsigned long set_flags)
  35. {
  36. #if HAVE_EXT2_IOCTLS
  37. struct stat buf;
  38. int fd, r, f, save_errno = 0;
  39. if (!stat(name, &buf) &&
  40. !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)) {
  41. goto notsupp;
  42. }
  43. fd = open (name, OPEN_FLAGS);
  44. if (fd == -1)
  45. return -1;
  46. if (!get_flags) {
  47. f = (int) set_flags;
  48. r = ioctl (fd, EXT2_IOC_SETFLAGS, &f);
  49. } else {
  50. r = ioctl (fd, EXT2_IOC_GETFLAGS, &f);
  51. *get_flags = f;
  52. }
  53. if (r == -1)
  54. save_errno = errno;
  55. close (fd);
  56. if (save_errno)
  57. errno = save_errno;
  58. return r;
  59. #endif /* HAVE_EXT2_IOCTLS */
  60. notsupp:
  61. errno = EOPNOTSUPP;
  62. return -1;
  63. }