apfs_util.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // apfs_util.c
  3. // electra
  4. //
  5. // Created by CoolStar on 2/26/18.
  6. // Copyright © 2018 Electra Team. All rights reserved.
  7. //
  8. #include "apfs_util.h"
  9. #include <fcntl.h>
  10. #include <sys/syscall.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <errno.h>
  16. #include <sys/attr.h>
  17. #include <sys/snapshot.h>
  18. int list_snapshots(const char *vol)
  19. {
  20. int dirfd = open(vol, O_RDONLY, 0);
  21. if (dirfd < 0) {
  22. perror("open");
  23. return -1;
  24. }
  25. struct attrlist alist = { 0 };
  26. char abuf[2048];
  27. alist.commonattr = ATTR_BULK_REQUIRED;
  28. int count = fs_snapshot_list(dirfd, &alist, &abuf[0], sizeof (abuf), 0);
  29. if (count < 0) {
  30. perror("fs_snapshot_list");
  31. return -1;
  32. }
  33. char *p = &abuf[0];
  34. for (int i = 0; i < count; i++) {
  35. char *field = p;
  36. uint32_t len = *(uint32_t *)field;
  37. field += sizeof (uint32_t);
  38. attribute_set_t attrs = *(attribute_set_t *)field;
  39. field += sizeof (attribute_set_t);
  40. if (attrs.commonattr & ATTR_CMN_NAME) {
  41. attrreference_t ar = *(attrreference_t *)field;
  42. char *name = field + ar.attr_dataoffset;
  43. field += sizeof (attrreference_t);
  44. (void) printf("%s\n", name);
  45. }
  46. p += len;
  47. }
  48. return (0);
  49. }
  50. int check_snapshot(const char *vol, const char *snap)
  51. {
  52. int dirfd = open(vol, O_RDONLY, 0);
  53. if (dirfd < 0) {
  54. perror("open");
  55. return -1;
  56. }
  57. struct attrlist alist = { 0 };
  58. char abuf[2048];
  59. alist.commonattr = ATTR_BULK_REQUIRED;
  60. int count = fs_snapshot_list(dirfd, &alist, &abuf[0], sizeof (abuf), 0);
  61. if (count < 0) {
  62. perror("fs_snapshot_list");
  63. return -1;
  64. }
  65. char *p = &abuf[0];
  66. for (int i = 0; i < count; i++) {
  67. char *field = p;
  68. uint32_t len = *(uint32_t *)field;
  69. field += sizeof (uint32_t);
  70. attribute_set_t attrs = *(attribute_set_t *)field;
  71. field += sizeof (attribute_set_t);
  72. if (attrs.commonattr & ATTR_CMN_NAME) {
  73. attrreference_t ar = *(attrreference_t *)field;
  74. const char *name = field + ar.attr_dataoffset;
  75. field += sizeof (attrreference_t);
  76. if (strcmp(name, snap) == 0){
  77. return 1;
  78. }
  79. }
  80. p += len;
  81. }
  82. return (0);
  83. }