archive_entry_xattr.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: head/lib/libarchive/archive_entry_xattr.c 201096 2009-12-28 02:41:27Z kientzle $");
  27. #ifdef HAVE_SYS_STAT_H
  28. #include <sys/stat.h>
  29. #endif
  30. #ifdef HAVE_SYS_TYPES_H
  31. #include <sys/types.h>
  32. #endif
  33. #ifdef HAVE_LIMITS_H
  34. #include <limits.h>
  35. #endif
  36. #ifdef HAVE_LINUX_FS_H
  37. #include <linux/fs.h> /* for Linux file flags */
  38. #endif
  39. /*
  40. * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
  41. * As the include guards don't agree, the order of include is important.
  42. */
  43. #ifdef HAVE_LINUX_EXT2_FS_H
  44. #include <linux/ext2_fs.h> /* for Linux file flags */
  45. #endif
  46. #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
  47. #include <ext2fs/ext2_fs.h> /* for Linux file flags */
  48. #endif
  49. #include <stddef.h>
  50. #include <stdio.h>
  51. #ifdef HAVE_STDLIB_H
  52. #include <stdlib.h>
  53. #endif
  54. #ifdef HAVE_STRING_H
  55. #include <string.h>
  56. #endif
  57. #ifdef HAVE_WCHAR_H
  58. #include <wchar.h>
  59. #endif
  60. #include "archive.h"
  61. #include "archive_entry.h"
  62. #include "archive_private.h"
  63. #include "archive_entry_private.h"
  64. /*
  65. * extended attribute handling
  66. */
  67. void
  68. archive_entry_xattr_clear(struct archive_entry *entry)
  69. {
  70. struct ae_xattr *xp;
  71. while (entry->xattr_head != NULL) {
  72. xp = entry->xattr_head->next;
  73. free(entry->xattr_head->name);
  74. free(entry->xattr_head->value);
  75. free(entry->xattr_head);
  76. entry->xattr_head = xp;
  77. }
  78. entry->xattr_head = NULL;
  79. }
  80. void
  81. archive_entry_xattr_add_entry(struct archive_entry *entry,
  82. const char *name, const void *value, size_t size)
  83. {
  84. struct ae_xattr *xp;
  85. for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
  86. ;
  87. if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
  88. /* XXX Error XXX */
  89. return;
  90. xp->name = strdup(name);
  91. if ((xp->value = malloc(size)) != NULL) {
  92. memcpy(xp->value, value, size);
  93. xp->size = size;
  94. } else
  95. xp->size = 0;
  96. xp->next = entry->xattr_head;
  97. entry->xattr_head = xp;
  98. }
  99. /*
  100. * returns number of the extended attribute entries
  101. */
  102. int
  103. archive_entry_xattr_count(struct archive_entry *entry)
  104. {
  105. struct ae_xattr *xp;
  106. int count = 0;
  107. for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
  108. count++;
  109. return count;
  110. }
  111. int
  112. archive_entry_xattr_reset(struct archive_entry * entry)
  113. {
  114. entry->xattr_p = entry->xattr_head;
  115. return archive_entry_xattr_count(entry);
  116. }
  117. int
  118. archive_entry_xattr_next(struct archive_entry * entry,
  119. const char **name, const void **value, size_t *size)
  120. {
  121. if (entry->xattr_p) {
  122. *name = entry->xattr_p->name;
  123. *value = entry->xattr_p->value;
  124. *size = entry->xattr_p->size;
  125. entry->xattr_p = entry->xattr_p->next;
  126. return (ARCHIVE_OK);
  127. } else {
  128. *name = NULL;
  129. *value = NULL;
  130. *size = (size_t)0;
  131. return (ARCHIVE_WARN);
  132. }
  133. }
  134. /*
  135. * end of xattr handling
  136. */