You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

263 lines
7.2 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "log.h"
  30. #include "configuration.h"
  31. #include "fileio.h"
  32. struct fileio_internal {
  33. const char *url;
  34. ssize_t size;
  35. enum fileio_type type;
  36. enum fileio_access access;
  37. FILE *file;
  38. };
  39. static inline int fileio_close_local(struct fileio_internal *fileio);
  40. static inline int fileio_open_local(struct fileio_internal *fileio)
  41. {
  42. char file_access[4];
  43. switch (fileio->access)
  44. {
  45. case FILEIO_READ:
  46. strcpy(file_access, "r");
  47. break;
  48. case FILEIO_WRITE:
  49. strcpy(file_access, "w");
  50. break;
  51. case FILEIO_READWRITE:
  52. strcpy(file_access, "w+");
  53. break;
  54. case FILEIO_APPEND:
  55. strcpy(file_access, "a");
  56. break;
  57. case FILEIO_APPENDREAD:
  58. strcpy(file_access, "a+");
  59. break;
  60. default:
  61. LOG_ERROR("BUG: access neither read, write nor readwrite");
  62. return ERROR_INVALID_ARGUMENTS;
  63. }
  64. /* win32 always opens in binary mode */
  65. #ifndef _WIN32
  66. if (fileio->type == FILEIO_BINARY)
  67. #endif
  68. {
  69. strcat(file_access, "b");
  70. }
  71. if (!(fileio->file = open_file_from_path (fileio->url, file_access)))
  72. {
  73. LOG_ERROR("couldn't open %s", fileio->url);
  74. return ERROR_FILEIO_OPERATION_FAILED;
  75. }
  76. if ((fileio->access != FILEIO_WRITE) || (fileio->access == FILEIO_READWRITE))
  77. {
  78. /* NB! Here we use fseek() instead of stat(), since stat is a
  79. * more advanced operation that might not apply to e.g. a disk path
  80. * that refers to e.g. a tftp client */
  81. int result, result2;
  82. result = fseek(fileio->file, 0, SEEK_END);
  83. fileio->size = ftell(fileio->file);
  84. result2 = fseek(fileio->file, 0, SEEK_SET);
  85. if ((fileio->size < 0)||(result < 0)||(result2 < 0))
  86. {
  87. fileio_close_local(fileio);
  88. return ERROR_FILEIO_OPERATION_FAILED;
  89. }
  90. }
  91. else
  92. {
  93. fileio->size = 0x0;
  94. }
  95. return ERROR_OK;
  96. }
  97. int fileio_open(struct fileio *fileio_p, const char *url, enum fileio_access access_type, enum fileio_type type)
  98. {
  99. int retval = ERROR_OK;
  100. struct fileio_internal *fileio = malloc(sizeof(struct fileio_internal));
  101. fileio_p->fp = fileio;
  102. fileio->type = type;
  103. fileio->access = access_type;
  104. fileio->url = strdup(url);
  105. retval = fileio_open_local(fileio);
  106. return retval;
  107. }
  108. static inline int fileio_close_local(struct fileio_internal *fileio)
  109. {
  110. int retval;
  111. if ((retval = fclose(fileio->file)) != 0)
  112. {
  113. if (retval == EBADF)
  114. {
  115. LOG_ERROR("BUG: fileio_local->file not a valid file descriptor");
  116. }
  117. else
  118. {
  119. LOG_ERROR("couldn't close %s: %s", fileio->url, strerror(errno));
  120. }
  121. return ERROR_FILEIO_OPERATION_FAILED;
  122. }
  123. return ERROR_OK;
  124. }
  125. int fileio_close(struct fileio *fileio_p)
  126. {
  127. int retval;
  128. struct fileio_internal *fileio = fileio_p->fp;
  129. retval = fileio_close_local(fileio);
  130. free((void*)fileio->url);
  131. fileio->url = NULL;
  132. free(fileio);
  133. fileio_p->fp = NULL;
  134. return retval;
  135. }
  136. int fileio_seek(struct fileio *fileio_p, size_t position)
  137. {
  138. int retval;
  139. struct fileio_internal *fileio = fileio_p->fp;
  140. if ((retval = fseek(fileio->file, position, SEEK_SET)) != 0)
  141. {
  142. LOG_ERROR("couldn't seek file %s: %s", fileio->url, strerror(errno));
  143. return ERROR_FILEIO_OPERATION_FAILED;
  144. }
  145. return ERROR_OK;
  146. }
  147. static int fileio_local_read(struct fileio_internal *fileio,
  148. size_t size, void *buffer, size_t *size_read)
  149. {
  150. ssize_t retval = fread(buffer, 1, size, fileio->file);
  151. *size_read = (retval >= 0) ? retval : 0;
  152. return (retval < 0) ? retval : ERROR_OK;
  153. }
  154. int fileio_read(struct fileio *fileio_p, size_t size, void *buffer,
  155. size_t *size_read)
  156. {
  157. struct fileio_internal *fileio = fileio_p->fp;
  158. return fileio_local_read(fileio, size, buffer, size_read);
  159. }
  160. int fileio_read_u32(struct fileio *fileio_p, uint32_t *data)
  161. {
  162. uint8_t buf[4];
  163. size_t size_read;
  164. struct fileio_internal *fileio = fileio_p->fp;
  165. int retval = fileio_local_read(fileio, sizeof(uint32_t), buf, &size_read);
  166. if (ERROR_OK == retval && sizeof(uint32_t) != size_read)
  167. retval = -EIO;
  168. if (ERROR_OK == retval)
  169. *data = be_to_h_u32(buf);
  170. return retval;
  171. }
  172. static int fileio_local_fgets(struct fileio_internal *fileio,
  173. size_t size, void *buffer)
  174. {
  175. if (fgets(buffer, size, fileio->file) == NULL)
  176. return ERROR_FILEIO_OPERATION_FAILED;
  177. return ERROR_OK;
  178. }
  179. int fileio_fgets(struct fileio *fileio_p, size_t size, void *buffer)
  180. {
  181. struct fileio_internal *fileio = fileio_p->fp;
  182. return fileio_local_fgets(fileio, size, buffer);
  183. }
  184. static int fileio_local_write(struct fileio_internal *fileio,
  185. size_t size, const void *buffer, size_t *size_written)
  186. {
  187. ssize_t retval = fwrite(buffer, 1, size, fileio->file);
  188. *size_written = (retval >= 0) ? retval : 0;
  189. return (retval < 0) ? retval : ERROR_OK;
  190. }
  191. int fileio_write(struct fileio *fileio_p,
  192. size_t size, const void *buffer, size_t *size_written)
  193. {
  194. struct fileio_internal *fileio = fileio_p->fp;
  195. int retval = fileio_local_write(fileio, size, buffer, size_written);
  196. if (retval == ERROR_OK)
  197. fileio->size += *size_written;
  198. return retval;
  199. }
  200. int fileio_write_u32(struct fileio *fileio_p, uint32_t data)
  201. {
  202. uint8_t buf[4];
  203. h_u32_to_be(buf, data);
  204. size_t size_written;
  205. int retval = fileio_write(fileio_p, 4, buf, &size_written);
  206. if (ERROR_OK == retval && size_written != sizeof(uint32_t))
  207. retval = -EIO;
  208. return retval;
  209. }
  210. /**
  211. * FIX!!!!
  212. *
  213. * For now this can not fail, but that's because a seek was executed
  214. * on startup.
  215. *
  216. * Avoiding the seek on startup opens up for using streams.
  217. *
  218. */
  219. int fileio_size(struct fileio *fileio_p, int *size)
  220. {
  221. struct fileio_internal *fileio = fileio_p->fp;
  222. *size = fileio->size;
  223. return ERROR_OK;
  224. }