archive_read_disk.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. inited = 1;
  46. }
  47. return (&av);
  48. }
  49. const char *
  50. archive_read_disk_gname(struct archive *_a, gid_t gid)
  51. {
  52. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  53. if (a->lookup_gname != NULL)
  54. return ((*a->lookup_gname)(a->lookup_gname_data, gid));
  55. return (NULL);
  56. }
  57. const char *
  58. archive_read_disk_uname(struct archive *_a, uid_t uid)
  59. {
  60. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  61. if (a->lookup_uname != NULL)
  62. return ((*a->lookup_uname)(a->lookup_uname_data, uid));
  63. return (NULL);
  64. }
  65. int
  66. archive_read_disk_set_gname_lookup(struct archive *_a,
  67. void *private_data,
  68. const char * (*lookup_gname)(void *private, gid_t gid),
  69. void (*cleanup_gname)(void *private))
  70. {
  71. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  72. __archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
  73. ARCHIVE_STATE_ANY, "archive_read_disk_set_gname_lookup");
  74. if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
  75. (a->cleanup_gname)(a->lookup_gname_data);
  76. a->lookup_gname = lookup_gname;
  77. a->cleanup_gname = cleanup_gname;
  78. a->lookup_gname_data = private_data;
  79. return (ARCHIVE_OK);
  80. }
  81. int
  82. archive_read_disk_set_uname_lookup(struct archive *_a,
  83. void *private_data,
  84. const char * (*lookup_uname)(void *private, uid_t uid),
  85. void (*cleanup_uname)(void *private))
  86. {
  87. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  88. __archive_check_magic(&a->archive, ARCHIVE_READ_DISK_MAGIC,
  89. ARCHIVE_STATE_ANY, "archive_read_disk_set_uname_lookup");
  90. if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
  91. (a->cleanup_uname)(a->lookup_uname_data);
  92. a->lookup_uname = lookup_uname;
  93. a->cleanup_uname = cleanup_uname;
  94. a->lookup_uname_data = private_data;
  95. return (ARCHIVE_OK);
  96. }
  97. /*
  98. * Create a new archive_read_disk object and initialize it with global state.
  99. */
  100. struct archive *
  101. archive_read_disk_new(void)
  102. {
  103. struct archive_read_disk *a;
  104. a = (struct archive_read_disk *)malloc(sizeof(*a));
  105. if (a == NULL)
  106. return (NULL);
  107. memset(a, 0, sizeof(*a));
  108. a->archive.magic = ARCHIVE_READ_DISK_MAGIC;
  109. /* We're ready to write a header immediately. */
  110. a->archive.state = ARCHIVE_STATE_HEADER;
  111. a->archive.vtable = archive_read_disk_vtable();
  112. a->lookup_uname = trivial_lookup_uname;
  113. a->lookup_gname = trivial_lookup_gname;
  114. return (&a->archive);
  115. }
  116. static int
  117. _archive_read_finish(struct archive *_a)
  118. {
  119. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  120. if (a->cleanup_gname != NULL && a->lookup_gname_data != NULL)
  121. (a->cleanup_gname)(a->lookup_gname_data);
  122. if (a->cleanup_uname != NULL && a->lookup_uname_data != NULL)
  123. (a->cleanup_uname)(a->lookup_uname_data);
  124. archive_string_free(&a->archive.error_string);
  125. free(a);
  126. return (ARCHIVE_OK);
  127. }
  128. static int
  129. _archive_read_close(struct archive *_a)
  130. {
  131. (void)_a; /* UNUSED */
  132. return (ARCHIVE_OK);
  133. }
  134. int
  135. archive_read_disk_set_symlink_logical(struct archive *_a)
  136. {
  137. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  138. a->symlink_mode = 'L';
  139. a->follow_symlinks = 1;
  140. return (ARCHIVE_OK);
  141. }
  142. int
  143. archive_read_disk_set_symlink_physical(struct archive *_a)
  144. {
  145. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  146. a->symlink_mode = 'P';
  147. a->follow_symlinks = 0;
  148. return (ARCHIVE_OK);
  149. }
  150. int
  151. archive_read_disk_set_symlink_hybrid(struct archive *_a)
  152. {
  153. struct archive_read_disk *a = (struct archive_read_disk *)_a;
  154. a->symlink_mode = 'H';
  155. a->follow_symlinks = 1; /* Follow symlinks initially. */
  156. return (ARCHIVE_OK);
  157. }
  158. /*
  159. * Trivial implementations of gname/uname lookup functions.
  160. * These are normally overridden by the client, but these stub
  161. * versions ensure that we always have something that works.
  162. */
  163. static const char *
  164. trivial_lookup_gname(void *private_data, gid_t gid)
  165. {
  166. (void)private_data; /* UNUSED */
  167. (void)gid; /* UNUSED */
  168. return (NULL);
  169. }
  170. static const char *
  171. trivial_lookup_uname(void *private_data, uid_t uid)
  172. {
  173. (void)private_data; /* UNUSED */
  174. (void)uid; /* UNUSED */
  175. return (NULL);
  176. }