fs.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Adam Dunkels <adam@sics.se>
  30. *
  31. **/
  32. #include "lwip/opt.h"
  33. #include "lwip/def.h"
  34. #include "fs.h"
  35. #include "fsdata.h"
  36. #include <string.h>
  37. /** Set this to 1 to include "fsdata_custom.c" instead of "fsdata.c" for the
  38. * file system (to prevent changing the file included in CVS) */
  39. #ifndef HTTPD_USE_CUSTUM_FSDATA
  40. #define HTTPD_USE_CUSTUM_FSDATA 0
  41. #endif
  42. #if HTTPD_USE_CUSTUM_FSDATA
  43. #include "fsdata_custom.c"
  44. #else /* HTTPD_USE_CUSTUM_FSDATA */
  45. #include "fsdata.c"
  46. #endif /* HTTPD_USE_CUSTUM_FSDATA */
  47. /*-----------------------------------------------------------------------------------*/
  48. /* Define the number of open files that we can support. */
  49. #ifndef LWIP_MAX_OPEN_FILES
  50. #define LWIP_MAX_OPEN_FILES 10
  51. #endif
  52. /* Define the file system memory allocation structure. */
  53. struct fs_table {
  54. struct fs_file file;
  55. u8_t inuse;
  56. };
  57. /* Allocate file system memory */
  58. struct fs_table fs_memory[LWIP_MAX_OPEN_FILES];
  59. #if LWIP_HTTPD_CUSTOM_FILES
  60. int fs_open_custom(struct fs_file *file, const char *name);
  61. void fs_close_custom(struct fs_file *file);
  62. #endif /* LWIP_HTTPD_CUSTOM_FILES */
  63. /*-----------------------------------------------------------------------------------*/
  64. static struct fs_file *
  65. fs_malloc(void)
  66. {
  67. int i;
  68. for(i = 0; i < LWIP_MAX_OPEN_FILES; i++) {
  69. if(fs_memory[i].inuse == 0) {
  70. fs_memory[i].inuse = 1;
  71. return(&fs_memory[i].file);
  72. }
  73. }
  74. return(NULL);
  75. }
  76. /*-----------------------------------------------------------------------------------*/
  77. static void
  78. fs_free(struct fs_file *file)
  79. {
  80. int i;
  81. for(i = 0; i < LWIP_MAX_OPEN_FILES; i++) {
  82. if(&fs_memory[i].file == file) {
  83. fs_memory[i].inuse = 0;
  84. break;
  85. }
  86. }
  87. return;
  88. }
  89. /*-----------------------------------------------------------------------------------*/
  90. struct fs_file *
  91. fs_open(const char *name)
  92. {
  93. struct fs_file *file;
  94. const struct fsdata_file *f;
  95. file = fs_malloc();
  96. if(file == NULL) {
  97. return NULL;
  98. }
  99. #if LWIP_HTTPD_CUSTOM_FILES
  100. if(fs_open_custom(file, name)) {
  101. file->is_custom_file = 1;
  102. return file;
  103. }
  104. file->is_custom_file = 0;
  105. #endif /* LWIP_HTTPD_CUSTOM_FILES */
  106. for(f = FS_ROOT; f != NULL; f = f->next) {
  107. if (!strcmp(name, (char *)f->name)) {
  108. file->data = (const char *)f->data;
  109. file->len = f->len;
  110. file->index = f->len;
  111. file->pextension = NULL;
  112. file->http_header_included = f->http_header_included;
  113. #if HTTPD_PRECALCULATED_CHECKSUM
  114. file->chksum_count = f->chksum_count;
  115. file->chksum = f->chksum;
  116. #endif /* HTTPD_PRECALCULATED_CHECKSUM */
  117. #if LWIP_HTTPD_FILE_STATE
  118. file->state = fs_state_init(file, name);
  119. #endif /* #if LWIP_HTTPD_FILE_STATE */
  120. return file;
  121. }
  122. }
  123. fs_free(file);
  124. return NULL;
  125. }
  126. /*-----------------------------------------------------------------------------------*/
  127. void
  128. fs_close(struct fs_file *file)
  129. {
  130. #if LWIP_HTTPD_CUSTOM_FILES
  131. if (file->is_custom_file) {
  132. fs_close_custom(file);
  133. }
  134. #endif /* LWIP_HTTPD_CUSTOM_FILES */
  135. #if LWIP_HTTPD_FILE_STATE
  136. fs_state_free(file, file->state);
  137. #endif /* #if LWIP_HTTPD_FILE_STATE */
  138. fs_free(file);
  139. }
  140. /*-----------------------------------------------------------------------------------*/
  141. int
  142. fs_read(struct fs_file *file, char *buffer, int count)
  143. {
  144. int read;
  145. if(file->index == file->len) {
  146. return -1;
  147. }
  148. read = file->len - file->index;
  149. if(read > count) {
  150. read = count;
  151. }
  152. MEMCPY(buffer, (file->data + file->index), read);
  153. file->index += read;
  154. return(read);
  155. }
  156. /*-----------------------------------------------------------------------------------*/
  157. int fs_bytes_left(struct fs_file *file)
  158. {
  159. return file->len - file->index;
  160. }