handle.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. ** Copyright 1998-2003 University of Illinois Board of Trustees
  3. ** Copyright 1998-2003 Mark D. Roth
  4. ** All rights reserved.
  5. **
  6. ** handle.c - libtar code for initializing a TAR handle
  7. **
  8. ** Mark D. Roth <[email protected]>
  9. ** Campus Information Technologies and Educational Services
  10. ** University of Illinois at Urbana-Champaign
  11. */
  12. #include <libtarint/internal.h>
  13. #include <stdio.h>
  14. #include <fcntl.h>
  15. #include <errno.h>
  16. #ifdef HAVE_UNISTD_H
  17. # include <unistd.h>
  18. #endif
  19. #ifdef STDC_HEADERS
  20. # include <stdlib.h>
  21. #endif
  22. #ifdef HAVE_IO_H
  23. #include <io.h>
  24. //Yogi: hack. this should work on windows where there is no O_ACCMODE defined
  25. #ifndef O_ACCMODE
  26. # define O_ACCMODE 0x0003
  27. #endif
  28. #endif
  29. const char libtar_version[] = PACKAGE_VERSION;
  30. struct libtar_fd_file
  31. {
  32. int fd;
  33. };
  34. static struct libtar_fd_file libtar_fd_file_pointer;
  35. static int libtar_open(void* call_data, const char* pathname, int flags, mode_t mode)
  36. {
  37. struct libtar_fd_file* lf = (struct libtar_fd_file*)call_data;
  38. lf->fd = open(pathname, flags, mode);
  39. return lf->fd;
  40. }
  41. static int libtar_close(void* call_data)
  42. {
  43. struct libtar_fd_file* lf = (struct libtar_fd_file*)call_data;
  44. return close(lf->fd);
  45. }
  46. static ssize_t libtar_read(void* call_data, void* buf, size_t count)
  47. {
  48. struct libtar_fd_file* lf = (struct libtar_fd_file*)call_data;
  49. return (ssize_t)read(lf->fd, buf, (unsigned int)count);
  50. }
  51. static ssize_t libtar_write(void* call_data, const void* buf, size_t count)
  52. {
  53. struct libtar_fd_file* lf = (struct libtar_fd_file*)call_data;
  54. return (ssize_t) write(lf->fd, buf, (unsigned int)count);
  55. }
  56. static tartype_t default_type = { libtar_open, libtar_close, libtar_read, libtar_write, &libtar_fd_file_pointer };
  57. static int
  58. tar_init(TAR **t, char *pathname, tartype_t *type,
  59. int oflags, int mode, int options)
  60. {
  61. (void)mode;
  62. if ((oflags & O_ACCMODE) == O_RDWR)
  63. {
  64. errno = EINVAL;
  65. return -1;
  66. }
  67. *t = (TAR *)calloc(1, sizeof(TAR));
  68. if (*t == NULL)
  69. return -1;
  70. (*t)->pathname = pathname;
  71. (*t)->options = options;
  72. (*t)->type = (type ? type : &default_type);
  73. (*t)->oflags = oflags;
  74. if ((oflags & O_ACCMODE) == O_RDONLY)
  75. (*t)->h = libtar_hash_new(256,
  76. (libtar_hashfunc_t)path_hashfunc);
  77. else
  78. (*t)->h = libtar_hash_new(16, (libtar_hashfunc_t)dev_hash);
  79. if ((*t)->h == NULL)
  80. {
  81. free(*t);
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. /* open a new tarfile handle */
  87. int
  88. tar_open(TAR **t, char *pathname, tartype_t *type,
  89. int oflags, int mode, int options)
  90. {
  91. int res;
  92. if (tar_init(t, pathname, type, oflags, mode, options) == -1)
  93. return -1;
  94. if ((options & TAR_NOOVERWRITE) && (oflags & O_CREAT))
  95. oflags |= O_EXCL;
  96. #ifdef O_BINARY
  97. oflags |= O_BINARY;
  98. #endif
  99. res = (*((*t)->type->openfunc))((*t)->type->call_data, pathname, oflags, mode);
  100. if (res == -1)
  101. {
  102. free(*t);
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. int
  108. tar_fdopen(TAR **t, int fd, char *pathname, tartype_t *type,
  109. int oflags, int mode, int options)
  110. {
  111. if (tar_init(t, pathname, type, oflags, mode, options) == -1)
  112. return -1;
  113. if ( !type )
  114. {
  115. struct libtar_fd_file* lf = (struct libtar_fd_file*)((*t)->type->call_data);
  116. lf->fd = fd;
  117. }
  118. return 0;
  119. }
  120. /* close tarfile handle */
  121. int
  122. tar_close(TAR *t)
  123. {
  124. int i;
  125. i = (*(t->type->closefunc))(t->type->call_data);
  126. if (t->h != NULL)
  127. libtar_hash_free(t->h, ((t->oflags & O_ACCMODE) == O_RDONLY
  128. ? free
  129. : (libtar_freefunc_t)tar_dev_free));
  130. free(t);
  131. return i;
  132. }