ft_pack.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* $MirOS: contrib/hosted/fwcf/ft_pack.c,v 1.7 2006/09/23 23:21:04 tg Exp $ */
  2. /*-
  3. * Copyright (c) 2006
  4. * Thorsten Glaser <tg@mirbsd.de>
  5. *
  6. * Licensee is hereby permitted to deal in this work without restric-
  7. * tion, including unlimited rights to use, publicly perform, modify,
  8. * merge, distribute, sell, give away or sublicence, provided all co-
  9. * pyright notices above, these terms and the disclaimer are retained
  10. * in all redistributions or reproduced in accompanying documentation
  11. * or other materials provided with binary redistributions.
  12. *
  13. * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
  14. * express, or implied, to the maximum extent permitted by applicable
  15. * law, without malicious intent or gross negligence; in no event may
  16. * licensor, an author or contributor be held liable for any indirect
  17. * or other damage, or direct damage except proven a consequence of a
  18. * direct error of said person and intended use of this work, loss or
  19. * other issues arising in any way out of its use, even if advised of
  20. * the possibility of such damage or existence of a defect.
  21. */
  22. #include <sys/param.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include "defs.h"
  30. #include "fts_subs.h"
  31. #include "pack.h"
  32. __RCSID("$MirOS: contrib/hosted/fwcf/ft_pack.c,v 1.7 2006/09/23 23:21:04 tg Exp $");
  33. char *
  34. ft_pack(ftsf_entry *e)
  35. {
  36. char f_header[4096], *hdrptr = f_header;
  37. size_t hdrleft = sizeof (f_header), k;
  38. char *f_data = NULL, *rv;
  39. off_t e_size;
  40. if (e == NULL)
  41. return (NULL);
  42. if ((e->etype != FTSF_FILE) && (e->etype != FTSF_SYMLINK) &&
  43. (e->etype != FTSF_DIR))
  44. return (NULL);
  45. e_size = e->statp->st_size;
  46. if ((k = strlcpy(hdrptr, e->pathname, hdrleft)) >= hdrleft)
  47. return (NULL);
  48. hdrptr += ++k;
  49. hdrleft -= k;
  50. if (e->etype == FTSF_SYMLINK) {
  51. STOREB(0x03);
  52. e->statp->st_mtime = 0;
  53. e->statp->st_mode = 0;
  54. } else if (e->etype == FTSF_DIR) {
  55. STOREB(0x05);
  56. e_size = 0;
  57. }
  58. if (e->statp->st_mtime) {
  59. STOREB(0x10);
  60. STORED(e->statp->st_mtime);
  61. }
  62. if (e->statp->st_gid > 0xFF) {
  63. STOREB('G');
  64. STORED(e->statp->st_gid);
  65. } else if (e->statp->st_gid) {
  66. STOREB('g');
  67. STOREB(e->statp->st_gid);
  68. }
  69. if (e->statp->st_mode > 0xFFFF) {
  70. STOREB('M');
  71. STORED(e->statp->st_mode);
  72. } else if (e->statp->st_mode) {
  73. STOREB('m');
  74. STOREW(e->statp->st_mode);
  75. }
  76. if (e->statp->st_uid > 0xFF) {
  77. STOREB('U');
  78. STORED(e->statp->st_uid);
  79. } else if (e->statp->st_uid) {
  80. STOREB('u');
  81. STOREB(e->statp->st_uid);
  82. }
  83. /* e_size is zero for everything except files and symlinks */
  84. if (e_size > 0xFF) {
  85. STOREB('S');
  86. STORET(e_size);
  87. } else if (e_size) {
  88. STOREB('s');
  89. STOREB(e_size);
  90. }
  91. STOREB(0);
  92. if (e_size) {
  93. if ((f_data = malloc(e_size)) == NULL)
  94. return (NULL);
  95. if (asprintf(&rv, "%s/%s", ftsf_prefix, e->pathname) == -1)
  96. return (NULL);
  97. if (e->etype == FTSF_SYMLINK) {
  98. if (readlink(rv, f_data, e_size) != e_size)
  99. return (NULL);
  100. } else {
  101. int fd;
  102. if ((fd = open(rv, O_RDONLY, 0)) < 0)
  103. return (NULL);
  104. if (read(fd, f_data, e_size) != e_size)
  105. return (NULL);
  106. close(fd);
  107. }
  108. free(rv);
  109. }
  110. k = sizeof (size_t) + (hdrptr - f_header) + e_size;
  111. if ((rv = malloc(k)) == NULL)
  112. return (NULL);
  113. *(size_t *)rv = k;
  114. memcpy(rv + sizeof (size_t), f_header, hdrptr - f_header);
  115. memcpy(rv + sizeof (size_t) + (hdrptr - f_header), f_data, e_size);
  116. return (rv);
  117. }