archive_entry_xattr.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. if ((xp = (struct ae_xattr *)malloc(sizeof(struct ae_xattr))) == NULL)
  86. __archive_errx(1, "Out of memory");
  87. if ((xp->name = strdup(name)) == NULL)
  88. __archive_errx(1, "Out of memory");
  89. if ((xp->value = malloc(size)) != NULL) {
  90. memcpy(xp->value, value, size);
  91. xp->size = size;
  92. } else
  93. xp->size = 0;
  94. xp->next = entry->xattr_head;
  95. entry->xattr_head = xp;
  96. }
  97. /*
  98. * returns number of the extended attribute entries
  99. */
  100. int
  101. archive_entry_xattr_count(struct archive_entry *entry)
  102. {
  103. struct ae_xattr *xp;
  104. int count = 0;
  105. for (xp = entry->xattr_head; xp != NULL; xp = xp->next)
  106. count++;
  107. return count;
  108. }
  109. int
  110. archive_entry_xattr_reset(struct archive_entry * entry)
  111. {
  112. entry->xattr_p = entry->xattr_head;
  113. return archive_entry_xattr_count(entry);
  114. }
  115. int
  116. archive_entry_xattr_next(struct archive_entry * entry,
  117. const char **name, const void **value, size_t *size)
  118. {
  119. if (entry->xattr_p) {
  120. *name = entry->xattr_p->name;
  121. *value = entry->xattr_p->value;
  122. *size = entry->xattr_p->size;
  123. entry->xattr_p = entry->xattr_p->next;
  124. return (ARCHIVE_OK);
  125. } else {
  126. *name = NULL;
  127. *value = NULL;
  128. *size = (size_t)0;
  129. return (ARCHIVE_WARN);
  130. }
  131. }
  132. /*
  133. * end of xattr handling
  134. */