@@ -49,16 +49,28 @@ static s32_t vfs_spiffs_map_error_code(s32_t err) {
49
49
return VFS_ERR_FAIL ;
50
50
}
51
51
52
- static int32_t vfs_check_file_path (const char * file_path ){
53
- if (strlen (file_path ) >= VFS_MAX_PATH_LEN ){
52
+ static int32_t vfs_check_file_path (const char * file_path ) {
53
+ if (strlen (file_path ) >= VFS_MAX_PATH_LEN ) {
54
54
return VFS_ERR_MAXNM ;
55
55
}
56
- const char * basename ;
56
+ const char * basename ;
57
57
size_t length ;
58
58
cwk_path_get_basename (file_path , & basename , & length );
59
- if (length <= 0 || length >= VFS_MAX_NAME_LEN ){
59
+ if (length <= 0 || length >= VFS_MAX_NAME_LEN ) {
60
+ return VFS_ERR_MAXNM ;
61
+ }
62
+ return VFS_OK ;
63
+ }
64
+
65
+ static int32_t vfs_check_folder_path (const char * file_path ) {
66
+ int32_t ret = vfs_check_file_path (file_path );
67
+ if (ret != VFS_OK ) {
68
+ return ret ;
69
+ }
70
+ if (strlen (file_path ) + strlen (VFS_SPIFFS_FOLDER_NAME ) + 1 >= VFS_MAX_PATH_LEN ) {
60
71
return VFS_ERR_MAXNM ;
61
72
}
73
+
62
74
return VFS_OK ;
63
75
}
64
76
@@ -160,6 +172,11 @@ int32_t vfs_spiffs_stat_file(const char *file, vfs_obj_t *obj) {
160
172
char path [SPIFFS_OBJ_NAME_LEN ];
161
173
memset (obj , 0 , sizeof (vfs_obj_t ));
162
174
175
+ int res = vfs_check_file_path (file );
176
+ if (res != VFS_OK ) {
177
+ return res ;
178
+ }
179
+
163
180
if (SPIFFS_stat (& fs , file , & s ) == SPIFFS_OK ) {
164
181
cwk_path_get_basename (s .name , & basename , & length );
165
182
@@ -289,12 +306,15 @@ int32_t vfs_spiffs_close_dir(vfs_dir_t *fd) {
289
306
int32_t vfs_spiffs_create_dir (const char * dir ) {
290
307
char path [VFS_MAX_PATH_LEN ];
291
308
292
- NRF_LOG_INFO ("create dir %s\n" , nrf_log_push (dir ));
293
- int res = vfs_check_file_path (dir );
294
- if (res != VFS_OK ){
309
+ NRF_LOG_INFO ("create dir %s, %d\n" , nrf_log_push (dir ), strlen (dir ));
310
+ int res = vfs_check_folder_path (dir );
311
+ if (res != VFS_OK ) {
312
+ NRF_LOG_INFO ("folder path check failed: %d" , res );
295
313
return res ;
296
314
}
315
+
297
316
snprintf (path , sizeof (path ), "%s/%s" , dir , VFS_SPIFFS_FOLDER_NAME );
317
+
298
318
res = SPIFFS_creat (& fs , path , 0 );
299
319
return vfs_spiffs_map_error_code (res );
300
320
}
@@ -335,13 +355,13 @@ int32_t vfs_spiffs_rename_dir_internal(const char *dir_name, const char *new_dir
335
355
vfs_spiffs_dir_t * p_dir = & dir ;
336
356
int32_t err_code = VFS_OK ;
337
357
338
- int ret = vfs_check_file_path (dir_name );
339
- if ( ret != VFS_OK ){
358
+ int ret = vfs_check_folder_path (dir_name );
359
+ if ( ret != VFS_OK ) {
340
360
return ret ;
341
361
}
342
362
343
- int ret2 = vfs_check_file_path (new_dir_name );
344
- if ( ret2 != VFS_OK ){
363
+ int ret2 = vfs_check_folder_path (new_dir_name );
364
+ if ( ret2 != VFS_OK ) {
345
365
return ret ;
346
366
}
347
367
@@ -396,7 +416,7 @@ int32_t vfs_spiffs_rename_dir(const char *dir_name, const char *new_dir_name) {
396
416
/**file operations*/
397
417
int32_t vfs_spiffs_open_file (const char * file , vfs_file_t * fd , uint32_t flags ) {
398
418
int ret = vfs_check_file_path (file );
399
- if ( ret != VFS_OK ){
419
+ if ( ret != VFS_OK ) {
400
420
return ret ;
401
421
}
402
422
fd -> handle = SPIFFS_open (& fs , file , flags , 0 );
@@ -464,7 +484,7 @@ int32_t vfs_spiffs_write_file_data(const char *file, void *buff, size_t buff_siz
464
484
NRF_LOG_INFO ("write file data %s\n" , nrf_log_push (file ));
465
485
466
486
int ret = vfs_check_file_path (file );
467
- if ( ret != VFS_OK ){
487
+ if ( ret != VFS_OK ) {
468
488
return ret ;
469
489
}
470
490
@@ -499,10 +519,16 @@ int32_t vfs_spiffs_read_file_data(const char *file, void *buff, size_t buff_size
499
519
}
500
520
501
521
int32_t vfs_spiffs_rename_file (const char * file , const char * new_file ) {
502
- if ( strlen ( new_file ) >= SPIFFS_OBJ_NAME_LEN ) {
503
- NRF_LOG_INFO ( "rename file error, file %s new file %s is too long" );
504
- return VFS_ERR_MAXNM ;
522
+ int32_t ret = vfs_check_file_path ( file );
523
+ if ( ret != VFS_OK ) {
524
+ return ret ;
505
525
}
526
+
527
+ ret = vfs_check_file_path (new_file );
528
+ if (ret != VFS_OK ) {
529
+ return ret ;
530
+ }
531
+
506
532
NRF_LOG_INFO ("rename file %s => %s\n" , nrf_log_push (file ), nrf_log_push (new_file ));
507
533
int res = SPIFFS_rename (& fs , file , new_file );
508
534
return vfs_spiffs_map_error_code (res );
0 commit comments