1
0

handle.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 _MSC_VER
  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. #define libtar_symbol(name, ret, args, callargs) \
  31. static ret libtar_##name args \
  32. { \
  33. return name callargs; \
  34. }
  35. #if defined(__BORLANDC__)
  36. libtar_symbol(open, int, (const char* pathname, int flags, mode_t mode), (pathname, flags, mode));
  37. libtar_symbol(close, int, (int fd), (fd));
  38. libtar_symbol(read, ssize_t, (int fd, void* buf, size_t count), (fd, buf, count));
  39. libtar_symbol(write, ssize_t, (int fd, void* buf, size_t count), (fd, buf, count));
  40. static tartype_t default_type = { libtar_open, libtar_close, libtar_read, libtar_write };
  41. #else
  42. static tartype_t default_type = { open, close, read, write };
  43. #endif
  44. static int
  45. tar_init(TAR **t, char *pathname, tartype_t *type,
  46. int oflags, int mode, int options)
  47. {
  48. (void)mode;
  49. if ((oflags & O_ACCMODE) == O_RDWR)
  50. {
  51. errno = EINVAL;
  52. return -1;
  53. }
  54. *t = (TAR *)calloc(1, sizeof(TAR));
  55. if (*t == NULL)
  56. return -1;
  57. (*t)->pathname = pathname;
  58. (*t)->options = options;
  59. (*t)->type = (type ? type : &default_type);
  60. (*t)->oflags = oflags;
  61. if ((oflags & O_ACCMODE) == O_RDONLY)
  62. (*t)->h = libtar_hash_new(256,
  63. (libtar_hashfunc_t)path_hashfunc);
  64. else
  65. (*t)->h = libtar_hash_new(16, (libtar_hashfunc_t)dev_hash);
  66. if ((*t)->h == NULL)
  67. {
  68. free(*t);
  69. return -1;
  70. }
  71. return 0;
  72. }
  73. /* open a new tarfile handle */
  74. int
  75. tar_open(TAR **t, char *pathname, tartype_t *type,
  76. int oflags, int mode, int options)
  77. {
  78. if (tar_init(t, pathname, type, oflags, mode, options) == -1)
  79. return -1;
  80. if ((options & TAR_NOOVERWRITE) && (oflags & O_CREAT))
  81. oflags |= O_EXCL;
  82. #ifdef O_BINARY
  83. oflags |= O_BINARY;
  84. #endif
  85. (*t)->fd = (*((*t)->type->openfunc))(pathname, oflags, mode);
  86. if ((*t)->fd == -1)
  87. {
  88. free(*t);
  89. return -1;
  90. }
  91. return 0;
  92. }
  93. int
  94. tar_fdopen(TAR **t, int fd, char *pathname, tartype_t *type,
  95. int oflags, int mode, int options)
  96. {
  97. if (tar_init(t, pathname, type, oflags, mode, options) == -1)
  98. return -1;
  99. (*t)->fd = fd;
  100. return 0;
  101. }
  102. int
  103. tar_fd(TAR *t)
  104. {
  105. return t->fd;
  106. }
  107. /* close tarfile handle */
  108. int
  109. tar_close(TAR *t)
  110. {
  111. int i;
  112. i = (*(t->type->closefunc))(t->fd);
  113. if (t->h != NULL)
  114. libtar_hash_free(t->h, ((t->oflags & O_ACCMODE) == O_RDONLY
  115. ? free
  116. : (libtar_freefunc_t)tar_dev_free));
  117. free(t);
  118. return i;
  119. }