archive_read_disk.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*-
  2. * Copyright (c) 2003-2009 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. * in this position and unchanged.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. __FBSDID("$FreeBSD$");
  28. #include "archive.h"
  29. #include "archive_string.h"
  30. #include "archive_entry.h"
  31. #include "archive_private.h"
  32. #include "archive_read_disk_private.h"
  33. static int _archive_read_finish(struct archive *);
  34. static int _archive_read_close(struct archive *);
  35. static const char *trivial_lookup_gname(void *, gid_t gid);
  36. static const char *trivial_lookup_uname(void *, uid_t uid);
  37. static struct archive_vtable *
  38. archive_read_disk_vtable(void)
  39. {
  40. static struct archive_vtable av;
  41. static int inited = 0;
  42. if (!inited) {
  43. av.archive_finish = _archive_read_finish;
  44. av.archive_close = _archive_read_close;
  45. }
  46. return (&av);
  47. }
  48. const char *
  49. archive_read_disk_gname(struct archive *_a, gid_t gid)
  50. {
  51. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  52. if (a->lookup_gname != NULL)
  53. return ((*a->lookup_gname)(a->lookup_gname_data, gid));
  54. return (NULL);
  55. }
  56. const char *
  57. archive_read_disk_uname(struct archive *_a, uid_t uid)
  58. {
  59. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  60. if (a->lookup_uname != NULL)
  61. return ((*a->lookup_uname)(a->lookup_uname_data, uid));
  62. return (NULL);
  63. }
  64. int
  65. archive_read_disk_set_gname_lookup(struct archive *_a,
  66. void *private_data,
  67. const char * (*lookup_gname)(void *private, gid_t gid),
  68. void (*cleanup_gname)(void *private))
  69. {
  70. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  71. __archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
  72. ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
  73. if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
  74. (a->cleanup_gname)(a->lookup_gname_data);
  75. a->lookup_gname = lookup_gname;
  76. a->cleanup_gname = cleanup_gname;
  77. a->lookup_gname_data = private_data;
  78. return (ARCHIVE_OK);
  79. }
  80. int
  81. archive_read_disk_set_uname_lookup(struct archive *_a,
  82. void *private_data,
  83. const char * (*lookup_uname)(void *private, uid_t uid),
  84. void (*cleanup_uname)(void *private))
  85. {
  86. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  87. __archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
  88. ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
  89. if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
  90. (a->cleanup_uname)(a->lookup_uname_data);
  91. a->lookup_uname = lookup_uname;
  92. a->cleanup_uname = cleanup_uname;
  93. a->lookup_uname_data = private_data;
  94. return (ARCHIVE_OK);
  95. }
  96. /*
  97. * Create a new archive_read_disk object and initialize it with global state.
  98. */
  99. struct archive *
  100. archive_read_disk_new(void)
  101. {
  102. struct archive_read_disk *a;
  103. a = (struct archive_read_disk *)malloc(sizeof(*a));
  104. if (a == NULL)
  105. return (NULL);
  106. memset(a, 0, sizeof(*a));
  107. a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
  108. /* We're ready to write a header immediately. */
  109. a->archive.state = ARCHIVE_STATE_HEADER;
  110. a->archive.vtable = archive_read_disk_vtable();
  111. a->lookup_uname = trivial_lookup_uname;
  112. a->lookup_gname = trivial_lookup_gname;
  113. return (&a->archive);
  114. }
  115. static int
  116. _archive_read_finish(struct archive *_a)
  117. {
  118. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  119. if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
  120. (a->cleanup_gname)(a->lookup_gname_data);
  121. if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
  122. (a->cleanup_uname)(a->lookup_uname_data);
  123. archive_string_free(&a->archive.error_string);
  124. free(a);
  125. return (ARCHIVE_OK);
  126. }
  127. static int
  128. _archive_read_close(struct archive *_a)
  129. {
  130. (void)_a; /* UNUSED */
  131. return (ARCHIVE_OK);
  132. }
  133. int
  134. archive_read_disk_set_symlink_logical(struct archive *_a)
  135. {
  136. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  137. a->symlink_mode = 'L';
  138. a->follow_symlinks = 1;
  139. return (ARCHIVE_OK);
  140. }
  141. int
  142. archive_read_disk_set_symlink_physical(struct archive *_a)
  143. {
  144. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  145. a->symlink_mode = 'P';
  146. a->follow_symlinks = 0;
  147. return (ARCHIVE_OK);
  148. }
  149. int
  150. archive_read_disk_set_symlink_hybrid(struct archive *_a)
  151. {
  152. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  153. a->symlink_mode = 'H';
  154. a->follow_symlinks = 1; /* Follow symlinks initially. */
  155. return (ARCHIVE_OK);
  156. }
  157. /*
  158. * Trivial implementations of gname/uname lookup functions.
  159. * These are normally overridden by the client, but these stub
  160. * versions ensure that we always have something that works.
  161. */
  162. static const char *
  163. trivial_lookup_gname(void *private_data, gid_t gid)
  164. {
  165. (void)private_data; /* UNUSED */
  166. (void)gid; /* UNUSED */
  167. return (NULL);
  168. }
  169. static const char *
  170. trivial_lookup_uname(void *private_data, uid_t uid)
  171. {
  172. (void)private_data; /* UNUSED */
  173. (void)uid; /* UNUSED */
  174. return (NULL);
  175. }