ar.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * ar.c - primitives for ar handling
  4. *
  5. * Copyright © 2010 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <time.h>
  25. #include <stdint.h>
  26. #include <unistd.h>
  27. #include <dpkg/i18n.h>
  28. #include <dpkg/dpkg.h>
  29. #include <dpkg/fdio.h>
  30. #include <dpkg/buffer.h>
  31. #include <dpkg/ar.h>
  32. void
  33. dpkg_ar_normalize_name(struct ar_hdr *arh)
  34. {
  35. char *name = arh->ar_name;
  36. int i;
  37. /* Remove trailing spaces from the member name. */
  38. for (i = sizeof(arh->ar_name) - 1; i >= 0 && name[i] == ' '; i--)
  39. name[i] = '\0';
  40. /* Remove optional slash terminator (on GNU-style archives). */
  41. if (name[i] == '/')
  42. name[i] = '\0';
  43. }
  44. off_t
  45. dpkg_ar_member_get_size(const char *ar_name, struct ar_hdr *arh)
  46. {
  47. const char *str = arh->ar_size;
  48. int len = sizeof(arh->ar_size);
  49. off_t size = 0;
  50. while (len && *str == ' ')
  51. str++, len--;
  52. while (len--) {
  53. if (*str == ' ')
  54. break;
  55. if (*str < '0' || *str > '9')
  56. ohshit(_("invalid character '%c' in archive '%.250s' "
  57. "member '%.16s' size"),
  58. *str, arh->ar_name, ar_name);
  59. size *= 10;
  60. size += *str++ - '0';
  61. }
  62. return size;
  63. }
  64. bool
  65. dpkg_ar_member_is_illegal(struct ar_hdr *arh)
  66. {
  67. return memcmp(arh->ar_fmag, ARFMAG, sizeof(arh->ar_fmag)) != 0;
  68. }
  69. void
  70. dpkg_ar_put_magic(const char *ar_name, int ar_fd)
  71. {
  72. if (fd_write(ar_fd, DPKG_AR_MAGIC, strlen(DPKG_AR_MAGIC)) < 0)
  73. ohshite(_("unable to write file '%s'"), ar_name);
  74. }
  75. void
  76. dpkg_ar_member_put_header(const char *ar_name, int ar_fd,
  77. const char *name, off_t size)
  78. {
  79. char header[sizeof(struct ar_hdr) + 1];
  80. int n;
  81. n = sprintf(header, "%-16s%-12lu0 0 100644 %-10jd`\n",
  82. name, time(NULL), (intmax_t)size);
  83. if (n != sizeof(struct ar_hdr))
  84. ohshit(_("generated corrupt ar header for '%s'"), ar_name);
  85. if (fd_write(ar_fd, header, n) < 0)
  86. ohshite(_("unable to write file '%s'"), ar_name);
  87. }
  88. void
  89. dpkg_ar_member_put_mem(const char *ar_name, int ar_fd,
  90. const char *name, const void *data, size_t size)
  91. {
  92. dpkg_ar_member_put_header(ar_name, ar_fd, name, size);
  93. /* Copy data contents. */
  94. if (fd_write(ar_fd, data, size) < 0)
  95. ohshite(_("unable to write file '%s'"), ar_name);
  96. if (size & 1)
  97. if (fd_write(ar_fd, "\n", 1) < 0)
  98. ohshite(_("unable to write file '%s'"), ar_name);
  99. }
  100. void
  101. dpkg_ar_member_put_file(const char *ar_name, int ar_fd,
  102. const char *name, int fd, off_t size)
  103. {
  104. if (size <= 0) {
  105. struct stat st;
  106. if (fstat(fd, &st))
  107. ohshite(_("failed to fstat ar member file (%s)"), name);
  108. size = st.st_size;
  109. }
  110. dpkg_ar_member_put_header(ar_name, ar_fd, name, size);
  111. /* Copy data contents. */
  112. fd_fd_copy(fd, ar_fd, size, _("ar member file (%s)"), name);
  113. if (size & 1)
  114. if (fd_write(ar_fd, "\n", 1) < 0)
  115. ohshite(_("unable to write file '%s'"), ar_name);
  116. }