archive_write_set_format_raw.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*-
  2. * Copyright (c) 2013 Marek Kubica
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. #ifdef HAVE_ERRNO_H
  27. #include <errno.h>
  28. #endif
  29. #include "archive_entry.h"
  30. #include "archive_write_private.h"
  31. static ssize_t archive_write_raw_data(struct archive_write *,
  32. const void *buff, size_t s);
  33. static int archive_write_raw_free(struct archive_write *);
  34. static int archive_write_raw_header(struct archive_write *,
  35. struct archive_entry *);
  36. struct raw {
  37. int entries_written;
  38. };
  39. /*
  40. * Set output format to 'raw' format.
  41. */
  42. int
  43. archive_write_set_format_raw(struct archive *_a)
  44. {
  45. struct archive_write *a = (struct archive_write *)_a;
  46. struct raw *raw;
  47. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  48. ARCHIVE_STATE_NEW, "archive_write_set_format_raw");
  49. /* If someone else was already registered, unregister them. */
  50. if (a->format_free != NULL)
  51. (a->format_free)(a);
  52. raw = (struct raw *)calloc(1, sizeof(*raw));
  53. if (raw == NULL) {
  54. archive_set_error(&a->archive, ENOMEM, "Can't allocate raw data");
  55. return (ARCHIVE_FATAL);
  56. }
  57. raw->entries_written = 0;
  58. a->format_data = raw;
  59. a->format_name = "raw";
  60. /* no options exist for this format */
  61. a->format_options = NULL;
  62. a->format_write_header = archive_write_raw_header;
  63. a->format_write_data = archive_write_raw_data;
  64. a->format_finish_entry = NULL;
  65. /* nothing needs to be done on closing */
  66. a->format_close = NULL;
  67. a->format_free = archive_write_raw_free;
  68. a->archive.archive_format = ARCHIVE_FORMAT_RAW;
  69. a->archive.archive_format_name = "RAW";
  70. return (ARCHIVE_OK);
  71. }
  72. static int
  73. archive_write_raw_header(struct archive_write *a, struct archive_entry *entry)
  74. {
  75. struct raw *raw = (struct raw *)a->format_data;
  76. if (archive_entry_filetype(entry) != AE_IFREG) {
  77. archive_set_error(&a->archive, ERANGE,
  78. "Raw format only supports filetype AE_IFREG");
  79. return (ARCHIVE_FATAL);
  80. }
  81. if (raw->entries_written > 0) {
  82. archive_set_error(&a->archive, ERANGE,
  83. "Raw format only supports one entry per archive");
  84. return (ARCHIVE_FATAL);
  85. }
  86. raw->entries_written++;
  87. return (ARCHIVE_OK);
  88. }
  89. static ssize_t
  90. archive_write_raw_data(struct archive_write *a, const void *buff, size_t s)
  91. {
  92. int ret;
  93. ret = __archive_write_output(a, buff, s);
  94. if (ret >= 0)
  95. return (s);
  96. else
  97. return (ret);
  98. }
  99. static int
  100. archive_write_raw_free(struct archive_write *a)
  101. {
  102. struct raw *raw;
  103. raw = (struct raw *)a->format_data;
  104. free(raw);
  105. a->format_data = NULL;
  106. return (ARCHIVE_OK);
  107. }