file-serializer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2015 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "dstr.h"
  17. #include "file-serializer.h"
  18. #include "platform.h"
  19. static size_t file_input_read(void *file, void *data, size_t size)
  20. {
  21. return fread(data, 1, size, file);
  22. }
  23. static int64_t file_input_seek(void *file, int64_t offset,
  24. enum serialize_seek_type seek_type)
  25. {
  26. int origin = SEEK_SET;
  27. switch (seek_type) {
  28. case SERIALIZE_SEEK_START:
  29. origin = SEEK_SET;
  30. break;
  31. case SERIALIZE_SEEK_CURRENT:
  32. origin = SEEK_CUR;
  33. break;
  34. case SERIALIZE_SEEK_END:
  35. origin = SEEK_END;
  36. break;
  37. }
  38. if (os_fseeki64(file, offset, origin) == -1)
  39. return -1;
  40. return os_ftelli64(file);
  41. }
  42. static int64_t file_input_get_pos(void *file)
  43. {
  44. return os_ftelli64(file);
  45. }
  46. bool file_input_serializer_init(struct serializer *s, const char *path)
  47. {
  48. s->data = os_fopen(path, "rb");
  49. if (!s->data)
  50. return false;
  51. s->read = file_input_read;
  52. s->write = NULL;
  53. s->seek = file_input_seek;
  54. s->get_pos = file_input_get_pos;
  55. return true;
  56. }
  57. void file_input_serializer_free(struct serializer *s)
  58. {
  59. if (s->data)
  60. fclose(s->data);
  61. }
  62. /* ------------------------------------------------------------------------- */
  63. struct file_output_data {
  64. FILE *file;
  65. char *temp_name;
  66. char *file_name;
  67. };
  68. static size_t file_output_write(void *sdata, const void *data, size_t size)
  69. {
  70. struct file_output_data *out = sdata;
  71. return fwrite(data, 1, size, out->file);
  72. }
  73. static int64_t file_output_seek(void *sdata, int64_t offset,
  74. enum serialize_seek_type seek_type)
  75. {
  76. struct file_output_data *out = sdata;
  77. int origin = SEEK_SET;
  78. switch (seek_type) {
  79. case SERIALIZE_SEEK_START:
  80. origin = SEEK_SET;
  81. break;
  82. case SERIALIZE_SEEK_CURRENT:
  83. origin = SEEK_CUR;
  84. break;
  85. case SERIALIZE_SEEK_END:
  86. origin = SEEK_END;
  87. break;
  88. }
  89. if (os_fseeki64(out->file, offset, origin) == -1)
  90. return -1;
  91. return os_ftelli64(out->file);
  92. }
  93. static int64_t file_output_get_pos(void *sdata)
  94. {
  95. struct file_output_data *out = sdata;
  96. return os_ftelli64(out->file);
  97. }
  98. bool file_output_serializer_init(struct serializer *s, const char *path)
  99. {
  100. FILE *file = os_fopen(path, "wb");
  101. struct file_output_data *out;
  102. if (!file)
  103. return false;
  104. out = bzalloc(sizeof(*out));
  105. out->file = file;
  106. s->data = out;
  107. s->read = NULL;
  108. s->write = file_output_write;
  109. s->seek = file_output_seek;
  110. s->get_pos = file_output_get_pos;
  111. return true;
  112. }
  113. bool file_output_serializer_init_safe(struct serializer *s, const char *path,
  114. const char *temp_ext)
  115. {
  116. struct dstr temp_name = {0};
  117. struct file_output_data *out;
  118. FILE *file;
  119. if (!temp_ext || !*temp_ext)
  120. return false;
  121. dstr_copy(&temp_name, path);
  122. if (*temp_ext != '.')
  123. dstr_cat_ch(&temp_name, '.');
  124. dstr_cat(&temp_name, temp_ext);
  125. file = os_fopen(temp_name.array, "wb");
  126. if (!file) {
  127. dstr_free(&temp_name);
  128. return false;
  129. }
  130. out = bzalloc(sizeof(*out));
  131. out->file_name = bstrdup(path);
  132. out->temp_name = temp_name.array;
  133. out->file = file;
  134. s->data = out;
  135. s->read = NULL;
  136. s->write = file_output_write;
  137. s->seek = file_output_seek;
  138. s->get_pos = file_output_get_pos;
  139. return true;
  140. }
  141. void file_output_serializer_free(struct serializer *s)
  142. {
  143. struct file_output_data *out = s->data;
  144. if (out) {
  145. fclose(out->file);
  146. if (out->temp_name) {
  147. os_unlink(out->file_name);
  148. os_rename(out->temp_name, out->file_name);
  149. }
  150. bfree(out->file_name);
  151. bfree(out->temp_name);
  152. bfree(out);
  153. }
  154. }