1
0

file-serializer.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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: origin = SEEK_SET; break;
  29. case SERIALIZE_SEEK_CURRENT: origin = SEEK_CUR; break;
  30. case SERIALIZE_SEEK_END: origin = SEEK_END; break;
  31. }
  32. if (os_fseeki64(file, offset, origin) == -1)
  33. return -1;
  34. return os_ftelli64(file);
  35. }
  36. static int64_t file_input_get_pos(void *file)
  37. {
  38. return os_ftelli64(file);
  39. }
  40. bool file_input_serializer_init(struct serializer *s, const char *path)
  41. {
  42. s->data = os_fopen(path, "rb");
  43. if (!s->data)
  44. return false;
  45. s->read = file_input_read;
  46. s->write = NULL;
  47. s->seek = file_input_seek;
  48. s->get_pos = file_input_get_pos;
  49. return true;
  50. }
  51. void file_input_serializer_free(struct serializer *s)
  52. {
  53. if (s->data)
  54. fclose(s->data);
  55. }
  56. /* ------------------------------------------------------------------------- */
  57. struct file_output_data {
  58. FILE *file;
  59. char *temp_name;
  60. char *file_name;
  61. };
  62. static size_t file_output_write(void *sdata, const void *data, size_t size)
  63. {
  64. struct file_output_data *out = sdata;
  65. return fwrite(data, 1, size, out->file);
  66. }
  67. static int64_t file_output_seek(void *sdata, int64_t offset,
  68. enum serialize_seek_type seek_type)
  69. {
  70. struct file_output_data *out = sdata;
  71. int origin = SEEK_SET;
  72. switch (seek_type) {
  73. case SERIALIZE_SEEK_START: origin = SEEK_SET; break;
  74. case SERIALIZE_SEEK_CURRENT: origin = SEEK_CUR; break;
  75. case SERIALIZE_SEEK_END: origin = SEEK_END; break;
  76. }
  77. if (os_fseeki64(out->file, offset, origin) == -1)
  78. return -1;
  79. return os_ftelli64(out->file);
  80. }
  81. static int64_t file_output_get_pos(void *sdata)
  82. {
  83. struct file_output_data *out = sdata;
  84. return os_ftelli64(out->file);
  85. }
  86. bool file_output_serializer_init(struct serializer *s, const char *path)
  87. {
  88. FILE *file = os_fopen(path, "wb");
  89. struct file_output_data *out;
  90. if (!file)
  91. return false;
  92. out = bzalloc(sizeof(*out));
  93. out->file = file;
  94. s->data = out;
  95. s->read = NULL;
  96. s->write = file_output_write;
  97. s->seek = file_output_seek;
  98. s->get_pos = file_output_get_pos;
  99. return true;
  100. }
  101. bool file_output_serializer_init_safe(struct serializer *s,
  102. const char *path, const char *temp_ext)
  103. {
  104. struct dstr temp_name = {0};
  105. struct file_output_data *out;
  106. FILE *file;
  107. if (!temp_ext || !*temp_ext)
  108. return false;
  109. dstr_copy(&temp_name, path);
  110. if (*temp_ext != '.')
  111. dstr_cat_ch(&temp_name, '.');
  112. dstr_cat(&temp_name, temp_ext);
  113. file = os_fopen(temp_name.array, "wb");
  114. if (!file) {
  115. dstr_free(&temp_name);
  116. return false;
  117. }
  118. out = bzalloc(sizeof(*out));
  119. out->file_name = bstrdup(path);
  120. out->temp_name = temp_name.array;
  121. out->file = file;
  122. s->data = out;
  123. s->read = NULL;
  124. s->write = file_output_write;
  125. s->seek = file_output_seek;
  126. s->get_pos = file_output_get_pos;
  127. return true;
  128. }
  129. void file_output_serializer_free(struct serializer *s)
  130. {
  131. struct file_output_data *out = s->data;
  132. if (out) {
  133. fclose(out->file);
  134. if (out->temp_name) {
  135. os_unlink(out->file_name);
  136. os_rename(out->temp_name, out->file_name);
  137. }
  138. bfree(out->file_name);
  139. bfree(out->temp_name);
  140. bfree(out);
  141. }
  142. }