1
1
//
2
2
// fs.c - wrapper for vfs.c for use by httpd
3
3
//
4
- // v0.1 / 2021-09-08 / Io Engineering / Terje
4
+ // v0.2 / 2021-10-04 / Io Engineering / Terje
5
5
//
6
6
7
7
/*
@@ -45,8 +45,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
45
#if HTTP_ENABLE
46
46
47
47
#include "vfs.h"
48
- #include "lwip/apps/fs .h"
48
+ #include "httpd .h"
49
49
50
+ static const embedded_file_t * * ro_files = NULL ;
50
51
static stream_write_ptr wrptr ;
51
52
static stream_block_tx_buffer_t txbuf = {0 };
52
53
@@ -92,7 +93,7 @@ static void fs_write (const char *s)
92
93
93
94
while (length > txbuf .max_length ) {
94
95
txbuf .length = txbuf .max_length ;
95
- memcpy (txbuf .data , s , txbuf .length );
96
+ memcpy (txbuf .s , s , txbuf .length );
96
97
if (!vf_write ())
97
98
return ;
98
99
length -= txbuf .max_length ;
@@ -131,8 +132,26 @@ struct fs_file *fs_create (void)
131
132
return & v_file ;
132
133
}
133
134
135
+ static const embedded_file_t * file_is_embedded (const char * name )
136
+ {
137
+ uint_fast8_t idx = 0 ;
138
+ const embedded_file_t * file = NULL ;
139
+
140
+ if (* name == '/' )
141
+ name ++ ;
142
+
143
+ if (ro_files ) do {
144
+ if (!strcmp (ro_files [idx ]-> name , name ))
145
+ file = ro_files [idx ];
146
+ } while (file == NULL && ro_files [++ idx ] != NULL );
147
+
148
+ return file ;
149
+ }
150
+
134
151
err_t fs_open (struct fs_file * file , const char * name )
135
152
{
153
+ const embedded_file_t * ro_file ;
154
+
136
155
if (name == NULL )
137
156
return ERR_ARG ;
138
157
@@ -154,6 +173,11 @@ err_t fs_open (struct fs_file *file, const char *name)
154
173
if ((file -> pextension = vfs_open (NULL , fname , "r" ))) {
155
174
file -> len = vfs_size ((vfs_file_t * )file -> pextension );
156
175
file -> is_custom_file = 0 ;
176
+ } else if ((ro_file = file_is_embedded (name ))) {
177
+ file -> len = ro_file -> size ;
178
+ file -> data = (char * )ro_file -> data ;
179
+ file -> is_custom_file = true;
180
+ file -> pextension = (void * )ro_file ;
157
181
} else
158
182
file -> pextension = NULL ;
159
183
}
@@ -197,11 +221,8 @@ int fs_read (struct fs_file *file, char *buffer, int count)
197
221
count = count > file -> len ? file -> len : count ;
198
222
memcpy (buffer , file -> data , count );
199
223
file -> data += count ;
200
- } else {
224
+ } else
201
225
file -> len = count = 0 ;
202
-
203
- }
204
-
205
226
}
206
227
207
228
file -> len -= count ;
@@ -227,4 +248,9 @@ void fs_reset (void)
227
248
}
228
249
}
229
250
251
+ void fs_register_embedded_files (const embedded_file_t * * files )
252
+ {
253
+ ro_files = files ;
254
+ }
255
+
230
256
#endif
0 commit comments