handle.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. if ((oflags & O_ACCMODE) == O_RDWR)
  49. {
  50. errno = EINVAL;
  51. return -1;
  52. }
  53. *t = (TAR *)calloc(1, sizeof(TAR));
  54. if (*t == NULL)
  55. return -1;
  56. (*t)->pathname = pathname;
  57. (*t)->options = options;
  58. (*t)->type = (type ? type : &default_type);
  59. (*t)->oflags = oflags;
  60. if ((oflags & O_ACCMODE) == O_RDONLY)
  61. (*t)->h = libtar_hash_new(256,
  62. (libtar_hashfunc_t)path_hashfunc);
  63. else
  64. (*t)->h = libtar_hash_new(16, (libtar_hashfunc_t)dev_hash);
  65. if ((*t)->h == NULL)
  66. {
  67. free(*t);
  68. return -1;
  69. }
  70. return 0;
  71. }
  72. /* open a new tarfile handle */
  73. int
  74. tar_open(TAR **t, char *pathname, tartype_t *type,
  75. int oflags, int mode, int options)
  76. {
  77. if (tar_init(t, pathname, type, oflags, mode, options) == -1)
  78. return -1;
  79. if ((options & TAR_NOOVERWRITE) && (oflags & O_CREAT))
  80. oflags |= O_EXCL;
  81. #ifdef O_BINARY
  82. oflags |= O_BINARY;
  83. #endif
  84. (*t)->fd = (*((*t)->type->openfunc))(pathname, oflags, mode);
  85. if ((*t)->fd == -1)
  86. {
  87. free(*t);
  88. return -1;
  89. }
  90. return 0;
  91. }
  92. int
  93. tar_fdopen(TAR **t, int fd, char *pathname, tartype_t *type,
  94. int oflags, int mode, int options)
  95. {
  96. if (tar_init(t, pathname, type, oflags, mode, options) == -1)
  97. return -1;
  98. (*t)->fd = fd;
  99. return 0;
  100. }
  101. int
  102. tar_fd(TAR *t)
  103. {
  104. return t->fd;
  105. }
  106. /* close tarfile handle */
  107. int
  108. tar_close(TAR *t)
  109. {
  110. int i;
  111. i = (*(t->type->closefunc))(t->fd);
  112. if (t->h != NULL)
  113. libtar_hash_free(t->h, ((t->oflags & O_ACCMODE) == O_RDONLY
  114. ? free
  115. : (libtar_freefunc_t)tar_dev_free));
  116. free(t);
  117. return i;
  118. }