ar.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. void
  65. dpkg_ar_put_magic(const char *ar_name, int ar_fd)
  66. {
  67. if (fd_write(ar_fd, DPKG_AR_MAGIC, strlen(DPKG_AR_MAGIC)) < 0)
  68. ohshite(_("unable to write file '%s'"), ar_name);
  69. }
  70. void
  71. dpkg_ar_member_put_header(const char *ar_name, int ar_fd,
  72. const char *name, off_t size)
  73. {
  74. char header[sizeof(struct ar_hdr) + 1];
  75. int n;
  76. n = sprintf(header, "%-16s%-12lu0 0 100644 %-10jd`\n",
  77. name, time(NULL), (intmax_t)size);
  78. if (n != sizeof(struct ar_hdr))
  79. ohshit(_("generated corrupt ar header for '%s'"), ar_name);
  80. if (fd_write(ar_fd, header, n) < 0)
  81. ohshite(_("unable to write file '%s'"), ar_name);
  82. }
  83. void
  84. dpkg_ar_member_put_mem(const char *ar_name, int ar_fd,
  85. const char *name, const void *data, size_t size)
  86. {
  87. dpkg_ar_member_put_header(ar_name, ar_fd, name, size);
  88. /* Copy data contents. */
  89. if (fd_write(ar_fd, data, size) < 0)
  90. ohshite(_("unable to write file '%s'"), ar_name);
  91. if (size & 1)
  92. if (fd_write(ar_fd, "\n", 1) < 0)
  93. ohshite(_("unable to write file '%s'"), ar_name);
  94. }
  95. void
  96. dpkg_ar_member_put_file(const char *ar_name, int ar_fd,
  97. const char *name, int fd, off_t size)
  98. {
  99. if (size <= 0) {
  100. struct stat st;
  101. if (fstat(fd, &st))
  102. ohshite(_("failed to fstat ar member file (%s)"), name);
  103. size = st.st_size;
  104. }
  105. dpkg_ar_member_put_header(ar_name, ar_fd, name, size);
  106. /* Copy data contents. */
  107. fd_fd_copy(fd, ar_fd, size, _("ar member file (%s)"), name);
  108. if (size & 1)
  109. if (fd_write(ar_fd, "\n", 1) < 0)
  110. ohshite(_("unable to write file '%s'"), ar_name);
  111. }